2010-06-16 01:55:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2010-07-24 19:26:20 +01:00
|
|
|
* Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2010-07-24 19:26:20 +01:00
|
|
|
* @category Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-24 19:26:20 +01:00
|
|
|
* Connection data processor which polls to handle input in an
|
2010-06-16 01:55:39 +01:00
|
|
|
* asynchronous manner. Will also cause the application tick at
|
|
|
|
* the user-defined wait time.
|
|
|
|
*
|
2010-07-24 19:26:20 +01:00
|
|
|
* @category Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2010-07-26 19:13:00 +01:00
|
|
|
protected $sec = 0;
|
2010-06-16 01:55:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of time to poll for stream activity (microseconds)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2010-07-26 19:13:00 +01:00
|
|
|
protected $usec = 200000;
|
2010-06-16 01:55:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
2010-07-24 19:26:20 +01:00
|
|
|
* Overrides the parent class to set the poll time.
|
2010-06-16 01:55:39 +01:00
|
|
|
*
|
|
|
|
* @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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-26 19:13:00 +01:00
|
|
|
if (!isset($this->sec) && !isset($this->usec)) {
|
2010-06-16 01:55:39 +01:00
|
|
|
throw new Phergie_Process_Exception(
|
|
|
|
'One of the processor options "sec" or "usec" must be specified'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($bot, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-24 19:26:20 +01:00
|
|
|
* Waits for stream activity and performs event processing on
|
2010-06-16 01:55:39 +01:00
|
|
|
* 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);
|
|
|
|
|
|
|
|
if (!$this->plugins->preEvent()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-24 19:26:20 +01:00
|
|
|
* Obtains and processes incoming events, then sends resulting outgoing
|
2010-06-16 01:55:39 +01:00
|
|
|
* events.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleEvents()
|
|
|
|
{
|
|
|
|
$time = time();
|
|
|
|
if ($this->lastTick == 0 || ($this->lastTick + $this->wait <= $time)) {
|
|
|
|
$this->doTick();
|
|
|
|
$this->lastTick = $time;
|
|
|
|
}
|
|
|
|
$this->handleEventsAsync();
|
|
|
|
}
|
|
|
|
}
|