summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Process
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Irc/extlib/phergie/Phergie/Process')
-rwxr-xr-xplugins/Irc/extlib/phergie/Phergie/Process/Abstract.php130
-rw-r--r--plugins/Irc/extlib/phergie/Phergie/Process/Async.php161
-rwxr-xr-xplugins/Irc/extlib/phergie/Phergie/Process/Exception.php33
-rw-r--r--plugins/Irc/extlib/phergie/Phergie/Process/Standard.php61
4 files changed, 385 insertions, 0 deletions
diff --git a/plugins/Irc/extlib/phergie/Phergie/Process/Abstract.php b/plugins/Irc/extlib/phergie/Phergie/Process/Abstract.php
new file mode 100755
index 000000000..68d45289e
--- /dev/null
+++ b/plugins/Irc/extlib/phergie/Phergie/Process/Abstract.php
@@ -0,0 +1,130 @@
+<?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
+ */
+
+/**
+ * Base class for obtaining and processing incoming events.
+ *
+ * @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
+ */
+abstract class Phergie_Process_Abstract
+{
+ /**
+ * Current driver instance
+ *
+ * @var Phergie_Driver_Abstract
+ */
+ protected $driver;
+
+ /**
+ * Current connection handler instance
+ *
+ * @var Phergie_Connection_Handler
+ */
+ protected $connections;
+
+ /**
+ * Current plugin handler instance
+ *
+ * @var Phergie_Plugin_Handler
+ */
+ protected $plugins;
+
+ /**
+ * Current event handler instance
+ *
+ * @var Phergie_Event_Handler
+ */
+ protected $events;
+
+ /**
+ * Current end-user interface instance
+ *
+ * @var Phergie_Ui_Abstract
+ */
+ protected $ui;
+
+ /**
+ * List of arguments for use within the instance
+ *
+ * @var array
+ */
+ protected $options = array();
+
+ /**
+ * Gets the required class refences from Phergie_Bot.
+ *
+ * @param Phergie_Bot $bot Current bot instance in use
+ * @param array $options Optional processor arguments
+ *
+ * @return void
+ */
+ public function __construct(Phergie_Bot $bot, array $options = array())
+ {
+ $this->driver = $bot->getDriver();
+ $this->plugins = $bot->getPluginHandler();
+ $this->connections = $bot->getConnectionHandler();
+ $this->events = $bot->getEventHandler();
+ $this->ui = $bot->getUi();
+ $this->options = $options;
+ }
+
+ /**
+ * Sends resulting outgoing events from ealier processing in handleEvents.
+ *
+ * @param Phergie_Connection $connection Active connection
+ *
+ * @return void
+ */
+ protected function processEvents(Phergie_Connection $connection)
+ {
+ if (count($this->events)) {
+ $this->plugins->preDispatch();
+ foreach ($this->events as $event) {
+ $this->ui->onCommand($event, $connection);
+
+ $method = 'do' . ucfirst(strtolower($event->getType()));
+ call_user_func_array(
+ array($this->driver, $method),
+ $event->getArguments()
+ );
+ }
+ $this->plugins->postDispatch();
+
+ if ($this->events->hasEventOfType(Phergie_Event_Request::TYPE_QUIT)) {
+ $this->ui->onQuit($connection);
+ $this->connections->removeConnection($connection);
+ }
+
+ $this->events->clearEvents();
+ }
+ }
+
+ /**
+ * Obtains and processes incoming events.
+ *
+ * @return void
+ */
+ public abstract function handleEvents();
+}
diff --git a/plugins/Irc/extlib/phergie/Phergie/Process/Async.php b/plugins/Irc/extlib/phergie/Phergie/Process/Async.php
new file mode 100644
index 000000000..8605bf39f
--- /dev/null
+++ b/plugins/Irc/extlib/phergie/Phergie/Process/Async.php
@@ -0,0 +1,161 @@
+<?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;
+
+ /**
+ * Length of time to poll for stream activity (microseconds)
+ *
+ * @var int
+ */
+ protected $usec;
+
+ /**
+ * 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 (empty($this->sec) && empty($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);
+
+ 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);
+ }
+ }
+
+ /**
+ * 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();
+ }
+}
diff --git a/plugins/Irc/extlib/phergie/Phergie/Process/Exception.php b/plugins/Irc/extlib/phergie/Phergie/Process/Exception.php
new file mode 100755
index 000000000..f964443c6
--- /dev/null
+++ b/plugins/Irc/extlib/phergie/Phergie/Process/Exception.php
@@ -0,0 +1,33 @@
+<?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
+ */
+
+/**
+ * Exception related to event processor operations.
+ *
+ * @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_Exception extends Phergie_Exception
+{
+}
diff --git a/plugins/Irc/extlib/phergie/Phergie/Process/Standard.php b/plugins/Irc/extlib/phergie/Phergie/Process/Standard.php
new file mode 100644
index 000000000..385c65fa2
--- /dev/null
+++ b/plugins/Irc/extlib/phergie/Phergie/Process/Standard.php
@@ -0,0 +1,61 @@
+<?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 reads all connections looking
+ * for a response.
+ *
+ * @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_Standard extends Phergie_Process_Abstract
+{
+ /**
+ * Obtains and processes incoming events, then sends resulting outgoing
+ * events.
+ *
+ * @return void
+ */
+ public function handleEvents()
+ {
+ foreach ($this->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);
+ }
+ }
+}