]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Process/Async.php
Merged in Phergie change (Allow sec/usec = 0)
[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;
41
42     /**
43      * Length of time to poll for stream activity (microseconds)
44      *
45      * @var int
46      */
47     protected $usec;
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
121                 if (!$this->plugins->preEvent()) {
122                     continue;
123                 }
124
125                 $this->plugins->{'on' . ucfirst($event->getType())}();
126             }
127
128             $this->processEvents($connection);
129         }
130     }
131
132     /**
133      * Perform application tick event on all plugins and connections.
134      *
135      * @return void
136      */
137     protected function doTick()
138     {
139         foreach ($this->connections as $connection) {
140             $this->plugins->setConnection($connection);
141             $this->plugins->onTick();
142             $this->processEvents($connection);
143         }
144     }
145
146     /**
147      * Obtains and processes incoming events, then sends resulting outgoing
148      * events.
149      *
150      * @return void
151      */
152     public function handleEvents()
153     {
154         $time = time();
155         if ($this->lastTick == 0 || ($this->lastTick + $this->wait <= $time)) {
156             $this->doTick();
157             $this->lastTick = $time;
158         }
159         $this->handleEventsAsync();
160     }
161 }