]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Process/Async.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Process / Async.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie
20  */
21
22 /**
23  * Connection data processor which polls to handle input in an
24  * asynchronous manner. Will also cause the application tick at
25  * the user-defined wait time.
26  *
27  * @category Phergie
28  * @package  Phergie
29  * @author   Phergie Development Team <team@phergie.org>
30  * @license  http://phergie.org/license New BSD License
31  * @link     http://pear.phergie.org/package/Phergie
32  */
33 class Phergie_Process_Async extends Phergie_Process_Abstract
34 {
35     /**
36      * Length of time to poll for stream activity (seconds)
37      *
38      * @var int
39      */
40     protected $sec = 0;
41
42     /**
43      * Length of time to poll for stream activity (microseconds)
44      *
45      * @var int
46      */
47     protected $usec = 200000;
48
49     /**
50      * Length of time to wait between ticks.
51      *
52      * @var int
53      */
54     protected $wait = 0;
55
56     /**
57      * Records when the application last performed a tick
58      *
59      * @var int
60      */
61     protected $lastTick = 0;
62
63     /**
64      * Overrides the parent class to set the poll time.
65      *
66      * @param Phergie_Bot $bot     Main bot class
67      * @param array       $options Processor arguments
68      *
69      * @return void
70      */
71     public function __construct(Phergie_Bot $bot, array $options)
72     {
73         if (!$bot->getDriver() instanceof Phergie_Driver_Streams) {
74             throw new Phergie_Process_Exception(
75                 'The Async event processor requires the Streams driver'
76             );
77         }
78
79         foreach (array('sec', 'usec') as $var) {
80             if (isset($options[$var])) {
81                 if (!is_int($options[$var])) {
82                      throw new Phergie_Process_Exception(
83                         'Processor option "' . $var . '" must be an integer'
84                      );
85                 }
86                 $this->$var = $options[$var];
87             }
88         }
89
90         if (!isset($this->sec) && !isset($this->usec)) {
91             throw new Phergie_Process_Exception(
92                 'One of the processor options "sec" or "usec" must be specified'
93             );
94         }
95
96         parent::__construct($bot, $options);
97     }
98
99     /**
100      * Waits for stream activity and performs event processing on
101      * connections with data to read.
102      *
103      * @return void
104      */
105     protected function handleEventsAsync()
106     {
107         $hostmasks = $this->driver->getActiveReadSockets($this->sec, $this->usec);
108         if (!$hostmasks) {
109             return;
110         }
111         $connections = $this->connections->getConnections($hostmasks);
112         foreach ($connections as $connection) {
113             $this->driver->setConnection($connection);
114             $this->plugins->setConnection($connection);
115             $this->plugins->onTick();
116
117             if ($event = $this->driver->getEvent()) {
118                 $this->ui->onEvent($event, $connection);
119                 $this->plugins->setEvent($event);
120                 $this->plugins->preEvent();
121                 $this->plugins->{'on' . ucfirst($event->getType())}();
122             }
123
124             $this->processEvents($connection);
125         }
126     }
127
128     /**
129      * Perform application tick event on all plugins and connections.
130      *
131      * @return void
132      */
133     protected function doTick()
134     {
135         foreach ($this->connections as $connection) {
136             $this->plugins->setConnection($connection);
137             $this->plugins->onTick();
138             $this->processEvents($connection);
139         }
140     }
141
142     /**
143      * Obtains and processes incoming events, then sends resulting outgoing
144      * events.
145      *
146      * @return void
147      */
148     public function handleEvents()
149     {
150         $time = time();
151         if ($this->lastTick == 0 || ($this->lastTick + $this->wait <= $time)) {
152             $this->doTick();
153             $this->lastTick = $time;
154         }
155         $this->handleEventsAsync();
156     }
157 }