Back to index

d-push  2.0
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions
TopCollector Class Reference
Inheritance diagram for TopCollector:
Inheritance graph
[legend]
Collaboration diagram for TopCollector:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TopCollector ()
 Constructor.
 __destruct ()
 Destructor indicates that the process is shutting down.
 CollectData ($stop=false)
 Advices all other processes that they should start/stop collecting data.
 AnnounceInformation ($addinfo, $preserve=false, $terminating=false)
 Announces a string to the TopCollector.
 ReadLatest ()
 Returns all available top data.
 ClearLatest ($all=false)
 Cleans up data collected so far.
 SetUserAgent ($agent)
 Sets a different UserAgent for this connection.
 SetAsPushConnection ()
 Marks this process as push connection.
 InterProcessData ()
 Constructor.
 InitializeParams ()
 Initializes internal parameters.
 ReInitSharedMem ()
 Reinitializes shared memory by removing, detaching and re-allocating it.
 Clean ()
 Cleans up the shared memory block.
 IsActive ()
 Indicates if the shared memory is active.

Public Attributes

const ENABLEDAT = 2
const TOPDATA = 3
const CLEANUPTIME = 1

Protected Member Functions

 blockMutex ()
 Blocks the class mutex Method blocks until mutex is available! ATTENTION: make sure that you always release a blocked mutex!
 releaseMutex ()
 Releases the class mutex After the release other processes are able to block the mutex themselfs.
 hasData ($id=2)
 Indicates if the requested variable is available in shared memory.
 getData ($id=2)
 Returns the requested variable from shared memory.
 setData ($data, $id=2)
 Writes the transmitted variable to shared memory Subclasses may never use an id < 2!

Protected Attributes

 $preserved
 $latest
 $type
 $allocate

Static Protected Attributes

static $devid
static $pid
static $user
static $start

Private Member Functions

 isEnabled ()
 Indicates if top data should be saved or not Returns true for 10 seconds after the latest CollectData() SHOULD only be called with locked mutex!
 checkArrayStructure (&$topdata)
 Builds an array structure for the top data.

Detailed Description

Definition at line 47 of file topcollector.php.


Constructor & Destructor Documentation

Destructor indicates that the process is shutting down.

public

Definition at line 91 of file topcollector.php.

                                 {
        $this->AnnounceInformation("terminated", false, true);
    }

Here is the call graph for this function:


Member Function Documentation

TopCollector::AnnounceInformation ( addinfo,
preserve = false,
terminating = false 
)

Announces a string to the TopCollector.

Parameters:
string$info
boolean$preserveinfo should be displayed when process terminates
boolean$terminatingindicates if the process is terminating

public

Returns:
boolean

Definition at line 134 of file topcollector.php.

                                                                                           {
        $this->latest["addinfo"] = $addinfo;
        $this->latest["update"] = time();

        if ($terminating) {
            $this->latest["ended"] = time();
            foreach ($this->preserved as $p)
                $this->latest["addinfo"] .= " : ".$p;
        }

        if ($preserve)
            $this->preserved[] = $addinfo;

        // exclusive block
        if ($this->blockMutex()) {

            if ($this->isEnabled()) {
                $topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA): array();

                $this->checkArrayStructure($topdata);

                // update
                $topdata[self::$devid][self::$user][self::$pid] = $this->latest;
                $ok = $this->setData($topdata, self::TOPDATA);
            }
            $this->releaseMutex();
        }
        // end exclusive block

        if ($this->isEnabled() === true && !$ok) {
            ZLog::Write(LOGLEVEL_WARN, "TopCollector::AnnounceInformation(): could not write to shared memory. Z-Push top will not display this data.");
            return false;
        }

        return true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

InterProcessData::blockMutex ( ) [protected, inherited]

Blocks the class mutex Method blocks until mutex is available! ATTENTION: make sure that you always release a blocked mutex!

protected

Returns:
boolean

Definition at line 200 of file interprocessdata.php.

                                    {
        if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
            return @sem_acquire($this->mutexid);

        return false;
    }

Here is the caller graph for this function:

TopCollector::checkArrayStructure ( &$  topdata) [private]

Builds an array structure for the top data.

Parameters:
array$topdatareference to the topdata array

private

Returns:

Definition at line 282 of file topcollector.php.

                                                    {
        if (!isset($topdata) || !is_array($topdata))
            $topdata = array();

        if (!isset($topdata[self::$devid]))
            $topdata[self::$devid] = array();

        if (!isset($topdata[self::$devid][self::$user]))
            $topdata[self::$devid][self::$user] = array();

        if (!isset($topdata[self::$devid][self::$user][self::$pid]))
            $topdata[self::$devid][self::$user][self::$pid] = array();
    }

Here is the caller graph for this function:

InterProcessData::Clean ( ) [inherited]

Cleans up the shared memory block.

public

Returns:
boolean

Definition at line 166 of file interprocessdata.php.

                            {
        $stat = false;

        // exclusive block
        if ($this->blockMutex()) {
            $cleanuptime = ($this->hasData(1)) ? $this->getData(1) : false;

            // TODO implement Shared Memory cleanup

            $this->releaseMutex();
        }
        // end exclusive block

        return $stat;
    }

Here is the call graph for this function:

TopCollector::ClearLatest ( all = false)

Cleans up data collected so far.

Parameters:
boolean$all(optional) if set all data independently from the age is removed

public

Returns:
boolean status

Definition at line 198 of file topcollector.php.

                                              {
        // it's ok when doing this every 10 sec
        if ($all == false && time() % 10 != 0 )
            return true;

        $stat = false;

        // exclusive block
        if ($this->blockMutex()) {
            if ($all == true) {
                $topdata = array();
            }
            else {
                $topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA) : array();

                $toClear = array();
                foreach ($topdata as $devid=>$users) {
                    foreach ($users as $user=>$pids) {
                        foreach ($pids as $pid=>$line) {
                            // remove everything which terminated for 20 secs or is not updated for more than 120 secs
                            if (($line["ended"] != 0 && time() - $line["ended"] > 20) ||
                                time() - $line["update"] > 120) {
                                $toClear[] = array($devid, $user, $pid);
                            }
                        }
                    }
                }
                foreach ($toClear as $tc)
                    unset($topdata[$tc[0]][$tc[1]][$tc[2]]);
            }

            $stat = $this->setData($topdata, self::TOPDATA);
            $this->releaseMutex();
        }
        // end exclusive block

        return $stat;
    }

Here is the call graph for this function:

TopCollector::CollectData ( stop = false)

Advices all other processes that they should start/stop collecting data.

The data saved is a timestamp. It has to be reactivated every couple of seconds

Parameters:
boolean$stop(opt) default false (do collect)

public

Returns:
boolean indicating if it was set to collect before

Definition at line 105 of file topcollector.php.

                                               {
        $wasEnabled = false;

        // exclusive block
        if ($this->blockMutex()) {
            $wasEnabled = ($this->hasData(self::ENABLEDAT)) ? $this->getData(self::ENABLEDAT) : false;

            $time = time();
            if ($stop === true) $time = 0;

            if (! $this->setData($time, self::ENABLEDAT))
                return false;
            $this->releaseMutex();
        }
        // end exclusive block

        return $wasEnabled;
    }

Here is the call graph for this function:

InterProcessData::getData ( id = 2) [protected, inherited]

Returns the requested variable from shared memory.

Parameters:
int$idint indicating the variable

protected

Returns:
mixed

Definition at line 249 of file interprocessdata.php.

                                        {
        if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
            return @shm_get_var($this->memid, $id);

        return ;
    }

Here is the caller graph for this function:

InterProcessData::hasData ( id = 2) [protected, inherited]

Indicates if the requested variable is available in shared memory.

Parameters:
int$idint indicating the variable

protected

Returns:
boolean

Definition at line 229 of file interprocessdata.php.

                                        {
        if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
            if (function_exists("shm_has_var"))
                return @shm_has_var($this->memid, $id);
            else {
                $some = $this->getData($id);
                return isset($some);
            }
        }
        return false;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Initializes internal parameters.

public

Returns:
boolean

Definition at line 76 of file interprocessdata.php.

                                       {
        if (!isset(self::$devid)) {
            self::$devid = Request::GetDeviceID();
            self::$pid = @getmypid();
            self::$user = Request::GetAuthUser();
            self::$start = time();
        }
        return true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Constructor.

public

Definition at line 62 of file interprocessdata.php.

                                       {
        if (!isset($this->type) || !isset($this->allocate))
            throw new FatalNotImplementedException(sprintf("Class InterProcessData can not be initialized. Subclass %s did not initialize type and allocable memory.", get_class($this)));

        if ($this->InitSharedMem())
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("%s(): Initialized mutexid %s and memid %s.", get_class($this), $this->mutexid, $this->memid));
    }

Here is the call graph for this function:

InterProcessData::IsActive ( ) [inherited]

Indicates if the shared memory is active.

public

Returns:
boolean

Definition at line 188 of file interprocessdata.php.

                               {
        return ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false));
    }

Here is the caller graph for this function:

TopCollector::isEnabled ( ) [private]

Indicates if top data should be saved or not Returns true for 10 seconds after the latest CollectData() SHOULD only be called with locked mutex!

private

Returns:
boolean

Definition at line 269 of file topcollector.php.

                                 {
        $isEnabled = ($this->hasData(self::ENABLEDAT)) ? $this->getData(self::ENABLEDAT) : false;
        return ($isEnabled !== false && ($isEnabled +300) > time());
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Returns all available top data.

public

Returns:
array

Definition at line 177 of file topcollector.php.

                                 {
        $topdata = array();

        // exclusive block
        if ($this->blockMutex()) {
            $topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA) : array();
            $this->releaseMutex();
        }
        // end exclusive block

        return $topdata;
    }

Here is the call graph for this function:

Reinitializes shared memory by removing, detaching and re-allocating it.

public

Returns:
boolean

Definition at line 156 of file interprocessdata.php.

                                      {
        return ($this->RemoveSharedMem() && $this->InitSharedMem());
    }

Here is the call graph for this function:

InterProcessData::releaseMutex ( ) [protected, inherited]

Releases the class mutex After the release other processes are able to block the mutex themselfs.

protected

Returns:
boolean

Definition at line 214 of file interprocessdata.php.

                                      {
        if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
            return @sem_release($this->mutexid);

        return false;
    }

Here is the caller graph for this function:

Marks this process as push connection.

Parameters:
string$agentpublic
Returns:
boolean

Definition at line 257 of file topcollector.php.

                                          {
        $this->latest["push"] = true;
    }
InterProcessData::setData ( data,
id = 2 
) [protected, inherited]

Writes the transmitted variable to shared memory Subclasses may never use an id < 2!

Parameters:
mixed$datadata which should be saved into shared memory
int$idint indicating the variable (bigger than 2!)

protected

Returns:
boolean

Definition at line 266 of file interprocessdata.php.

                                               {
        if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
            return @shm_put_var($this->memid, $id, $data);

        return false;
    }

Here is the caller graph for this function:

Sets a different UserAgent for this connection.

Parameters:
string$agentpublic
Returns:
boolean

Definition at line 245 of file topcollector.php.

                                         {
        $this->latest["devagent"] = $agent;
    }

Constructor.

public

Definition at line 59 of file topcollector.php.

                                   {
        // initialize super parameters
        $this->allocate = 2097152; // 2 MB
        $this->type = 20;
        parent::__construct();

        // initialize params
        $this->InitializeParams();

        $this->preserved = array();
        // static vars come from the parent class
        $this->latest = array(  "pid"       => self::$pid,
                                "ip"        => Request::GetRemoteAddr(),
                                "user"      => self::$user,
                                "start"     => self::$start,
                                "devtype"   => Request::GetDeviceType(),
                                "devid"     => self::$devid,
                                "devagent"  => Request::GetUserAgent(),
                                "command"   => Request::GetCommandCode(),
                                "ended"     => 0,
                                "push"      => false,
                        );

        $this->AnnounceInformation("initializing");
    }

Here is the call graph for this function:


Member Data Documentation

InterProcessData::$allocate [protected, inherited]

Definition at line 53 of file interprocessdata.php.

InterProcessData::$devid [static, protected, inherited]

Definition at line 48 of file interprocessdata.php.

TopCollector::$latest [protected]

Definition at line 52 of file topcollector.php.

InterProcessData::$pid [static, protected, inherited]

Definition at line 49 of file interprocessdata.php.

Definition at line 51 of file topcollector.php.

InterProcessData::$start [static, protected, inherited]

Definition at line 51 of file interprocessdata.php.

InterProcessData::$type [protected, inherited]

Definition at line 52 of file interprocessdata.php.

InterProcessData::$user [static, protected, inherited]

Definition at line 50 of file interprocessdata.php.

const InterProcessData::CLEANUPTIME = 1 [inherited]

Definition at line 46 of file interprocessdata.php.

Definition at line 48 of file topcollector.php.

Definition at line 49 of file topcollector.php.


The documentation for this class was generated from the following file: