summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Process/Async.php
blob: 78d5959144cf976e04a77f0024b52b4ee1d59664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 * Phergie
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

/**
 * Connection data processor which polls to handle input in an
 * asynchronous manner. Will also cause the application tick at
 * the user-defined wait time.
 *
 * @category Phergie
 * @package  Phergie
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
class Phergie_Process_Async extends Phergie_Process_Abstract
{
    /**
     * Length of time to poll for stream activity (seconds)
     *
     * @var int
     */
    protected $sec = 0;

    /**
     * Length of time to poll for stream activity (microseconds)
     *
     * @var int
     */
    protected $usec = 200000;

    /**
     * Length of time to wait between ticks.
     *
     * @var int
     */
    protected $wait = 0;

    /**
     * Records when the application last performed a tick
     *
     * @var int
     */
    protected $lastTick = 0;

    /**
     * Overrides the parent class to set the poll time.
     *
     * @param Phergie_Bot $bot     Main bot class
     * @param array       $options Processor arguments
     *
     * @return void
     */
    public function __construct(Phergie_Bot $bot, array $options)
    {
        if (!$bot->getDriver() instanceof Phergie_Driver_Streams) {
            throw new Phergie_Process_Exception(
                'The Async event processor requires the Streams driver'
            );
        }

        foreach (array('sec', 'usec') as $var) {
            if (isset($options[$var])) {
                if (!is_int($options[$var])) {
                     throw new Phergie_Process_Exception(
                        'Processor option "' . $var . '" must be an integer'
                     );
                }
                $this->$var = $options[$var];
            }
        }

        if (!isset($this->sec) && !isset($this->usec)) {
            throw new Phergie_Process_Exception(
                'One of the processor options "sec" or "usec" must be specified'
            );
        }

        parent::__construct($bot, $options);
    }

    /**
     * Waits for stream activity and performs event processing on
     * connections with data to read.
     *
     * @return void
     */
    protected function handleEventsAsync()
    {
        $hostmasks = $this->driver->getActiveReadSockets($this->sec, $this->usec);
        if (!$hostmasks) {
            return;
        }
        $connections = $this->connections->getConnections($hostmasks);
        foreach ($connections as $connection) {
            $this->driver->setConnection($connection);
            $this->plugins->setConnection($connection);
            $this->plugins->onTick();

            if ($event = $this->driver->getEvent()) {
                $this->ui->onEvent($event, $connection);
                $this->plugins->setEvent($event);
                $this->plugins->preEvent();
                $this->plugins->{'on' . ucfirst($event->getType())}();
            }

            $this->processEvents($connection);
        }
    }

    /**
     * Perform application tick event on all plugins and connections.
     *
     * @return void
     */
    protected function doTick()
    {
        foreach ($this->connections as $connection) {
            $this->plugins->setConnection($connection);
            $this->plugins->onTick();
            $this->processEvents($connection);
        }
    }

    /**
     * Obtains and processes incoming events, then sends resulting outgoing
     * events.
     *
     * @return void
     */
    public function handleEvents()
    {
        $time = time();
        if ($this->lastTick == 0 || ($this->lastTick + $this->wait <= $time)) {
            $this->doTick();
            $this->lastTick = $time;
        }
        $this->handleEventsAsync();
    }
}