From: Roland Häder Date: Sat, 15 Aug 2009 21:34:07 +0000 (+0000) Subject: Tasks extended, iterator added: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3c317195b3170f917f8c3655c21b92b5c3173b1c;p=hub.git Tasks extended, iterator added: - TaskList class added which encapsulates our task list array - Default iterator with simple incrementing index added - Handling of all tasks (unfinished) added - Adding of tasks basicly finished (with debug output) --- diff --git a/.gitattributes b/.gitattributes index 82f2b2aaf..089e3d130 100644 --- a/.gitattributes +++ b/.gitattributes @@ -126,7 +126,7 @@ application/hub/main/iterator/class_BaseIterator.php -text application/hub/main/iterator/network/.htaccess -text application/hub/main/iterator/network/class_NetworkListenIterator.php -text application/hub/main/iterator/pool/.htaccess -text -application/hub/main/iterator/pool/class_ShutdownPoolIterator.php -text +application/hub/main/iterator/pool/class_DefaultIterator.php -text application/hub/main/iterator/pool/handler/.htaccess -text application/hub/main/iterator/pool/handler/class_Handler -text application/hub/main/iterator/pool/handler/class_HandlerPoolIterator.php -text @@ -157,6 +157,8 @@ application/hub/main/lists/groups/.htaccess -text application/hub/main/lists/groups/class_ListGroupList.php -text application/hub/main/lists/pool/.htaccess -text application/hub/main/lists/pool/class_PoolEntriesList.php -text +application/hub/main/lists/tasks/.htaccess -text +application/hub/main/lists/tasks/class_TaskList.php -text application/hub/main/nodes/.htaccess -text application/hub/main/nodes/boot/.htaccess -text application/hub/main/nodes/boot/class_HubBootNode.php -text diff --git a/application/hub/config.php b/application/hub/config.php index 76bbc6b78..e47156876 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -195,5 +195,11 @@ $cfg->setConfigEntry('task_idle_loop_interval_delay', 10); // CFG: IDLE-TASK-CLASS $cfg->setConfigEntry('idle_task_class', 'IdleLoopTask'); +// CFG: TASK-LIST-CLASS +$cfg->setConfigEntry('task_list_class', 'TaskList'); + +// CFG: DEFAULT-ITERATOR-CLASS +$cfg->setConfigEntry('default_iterator_class', 'DefaultIterator'); + // [EOF] ?> diff --git a/application/hub/interfaces/handler/task/class_HandleableTask.php b/application/hub/interfaces/handler/task/class_HandleableTask.php index f805475ca..45781cc44 100644 --- a/application/hub/interfaces/handler/task/class_HandleableTask.php +++ b/application/hub/interfaces/handler/task/class_HandleableTask.php @@ -26,12 +26,28 @@ interface HandleableTask extends Handleable { * Registers a task with a task handler. This method throws a * TaskAlreadyRegisteredException if the task has already been registered * - * @param $taskName A task name to register the task on + * @param $taskName A task name to register the task on * @param $taskInstance The instance we should register as a task * @return void - * @throws TaskAlreadyRegisteredException If the given task is already registered */ function registerTask ($taskName, Visitable $taskInstance); + + /** + * Checks wether tasks are left including idle task. + * + * @return $tasksLeft Wether there are tasks left to handle + */ + function hasTasksLeft (); + + /** + * Handles all tasks by checking if they should startup or if it is their + * turn to run. You should use this method in a while() loop in conjuntion + * with hasTasksLeft() so you can e.g. shutdown by adding a ShutdownTask + * which will attempt to remove all tasks from the task handler. + * + * @return void + */ + function handleTasks (); } // diff --git a/application/hub/main/commands/console/class_HubConsoleMainCommand.php b/application/hub/main/commands/console/class_HubConsoleMainCommand.php index 4be290c52..f2b33a068 100644 --- a/application/hub/main/commands/console/class_HubConsoleMainCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleMainCommand.php @@ -87,12 +87,15 @@ class HubConsoleMainCommand extends BaseCommand implements Commandable { // so the whole application runs on nice speed. This while-loop goes // until the hub is no longer active. while (($nodeInstance->isHubActive()) && ($handlerInstance->hasTasksLeft())) { + // Handle all tasks here + $handlerInstance->handleTasks(); + // Handle the listeners // @TODO We may have to catch some exceptions here - $nodeInstance->getListenerPoolInstance()->handleListenerPool(); + //$nodeInstance->getListenerPoolInstance()->handleListenerPool(); // Handle the qeues - $nodeInstance->getQueryInstance()->handleQueues(); + //$nodeInstance->getQueryInstance()->handleQueues(); } // END - while // Debug message diff --git a/application/hub/main/handler/tasks/class_TaskHandler.php b/application/hub/main/handler/tasks/class_TaskHandler.php index c4e920555..ccaf3da47 100644 --- a/application/hub/main/handler/tasks/class_TaskHandler.php +++ b/application/hub/main/handler/tasks/class_TaskHandler.php @@ -22,6 +22,16 @@ * along with this program. If not, see . */ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { + /** + * A task list instance + */ + private $listInstance = null; + + /** + * Instance for iterator + */ + private $iteratorInstance = null; + /** * Protected constructor * @@ -30,6 +40,12 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); + + // Init the task list + $this->listInstance = ObjectFactory::createObjectByConfiguredName('task_list_class'); + + // Get default instance + $this->iteratorInstance = $this->listInstance->getIterator(); } /** @@ -55,10 +71,65 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { * @param $taskName A task name to register the task on * @param $taskInstance The instance we should register as a task * @return void - * @throws TaskAlreadyRegisteredException If the task is already registered */ public function registerTask ($taskName, Visitable $taskInstance) { - $this->partialStub('taskName=' . $taskName . '/' . $taskInstance->__toString()); + // Create the entry + $taskEntry = array( + 'id' => $taskName, + 'task_registered' => time(), + 'task_last_active' => 0, + 'task_instance' => $taskInstance, + 'task_startup_delay' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_startup_delay'), + 'task_interval_delay' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_interval_delay'), + ); + + // Add the entry + $this->listInstance->addEntry('tasks', $taskEntry); + + // Debug message + $this->debugOutput('TASK-HANDLER: Task ' . $taskName . + ' (taskInstance=' . $taskInstance->__toString() . ')' . + ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' . + ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms registered.' + ); + } + + /** + * Checks wether tasks are left including idle task + * + * @return $tasksLeft Wether there are tasks left to handle + */ + public final function hasTasksLeft () { + // Do we have tasks there? + $tasksLeft = (($this->listInstance instanceof Listable) && ($this->listInstance->count() > 0)); + + // Return result + return $tasksLeft; + } + + /** + * Handles all tasks by checking if they should startup or if it is their + * turn to run. You should use this method in a while() loop in conjuntion + * with hasTasksLeft() so you can e.g. shutdown by adding a ShutdownTask + * which will attempt to remove all tasks from the task handler. + * + * @return void + */ + public function handleTasks () { + // Should we rewind? + if (!$this->iteratorInstance->valid()) { + // Rewind to the beginning for next loop + $this->iteratorInstance->rewind(); + } // END - if + + // Get current entry + $taskEntry = $this->iteratorInstance->current(); + + // Debug message + $this->debugOutput('Handling task ' . $taskEntry['id']); + + // Go to next entry + $this->iteratorInstance->next(); } } diff --git a/application/hub/main/iterator/pool/class_DefaultIterator.php b/application/hub/main/iterator/pool/class_DefaultIterator.php new file mode 100755 index 000000000..2c73694ee --- /dev/null +++ b/application/hub/main/iterator/pool/class_DefaultIterator.php @@ -0,0 +1,122 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class DefaultIterator extends BaseIterator implements Iterator { + /** + * Key for the global list index + */ + private $indexKey = 0; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @param $listInstance A list of a IteratorAggregate + * @return $iteratorInstance An instance a Iterator class + */ + public final static function createDefaultIterator (IteratorAggregate $listInstance) { + // Get new instance + $iteratorInstance = new DefaultIterator(); + + // Set the list + $iteratorInstance->setListInstance($listInstance); + + // Return the prepared instance + return $iteratorInstance; + } + + /** + * Getter for current value from group or generic + * + * @return $current Current value in iteration + * @throws IndexOutOfBoundsException If $indexKey is out of bounds + */ + public function current () { + // Default is null + $current = null; + + // Is the entry valid? + if (!$this->valid()) { + // Throw an exception here + throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS); + } // END - if + + // Now get the entry + $current = $this->getListInstance()->getEntry($this->key()); + + // Return it + return $current; + } + + /** + * Getter for key from group or generic + * + * @return $indexKey Current key in iteration + */ + public function key () { + return $this->indexKey; + } + + /** + * Advances to the next entry + * + * @return void + */ + public function next () { + $this->indexKey++; + } + + /** + * Rewinds to the beginning of the iteration + * + * @return void + */ + public function rewind () { + $this->indexKey = 0; + } + + /** + * Checks wether the current entry is valid (not at the end of the list) + * + * @return $isValid Wether the current entry is there + */ + public function valid () { + // Check for total active clients and if we are not at the end + $isValid = ($this->key() < $this->getListInstance()->count()); + + // Return result + return $isValid; + } +} + +// [EOF] +?> diff --git a/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php b/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php deleted file mode 100755 index 1fbc0f190..000000000 --- a/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php +++ /dev/null @@ -1,122 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class ShutdownPoolIterator extends BaseIterator implements Iterator { - /** - * Key for the global list index - */ - private $indexKey = 0; - - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this class - * - * @param $listInstance A list of a IteratorAggregate - * @return $iteratorInstance An instance a Iterator class - */ - public final static function createShutdownPoolIterator (IteratorAggregate $listInstance) { - // Get new instance - $iteratorInstance = new ShutdownPoolIterator(); - - // Set the list - $iteratorInstance->setListInstance($listInstance); - - // Return the prepared instance - return $iteratorInstance; - } - - /** - * Getter for current value from group or generic - * - * @return $current Current value in iteration - * @throws IndexOutOfBoundsException If $indexKey is out of bounds - */ - public function current () { - // Default is null - $current = null; - - // Is the entry valid? - if (!$this->valid()) { - // Throw an exception here - throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS); - } // END - if - - // Now get the entry - $current = $this->getListInstance()->getEntry($this->key()); - - // Return it - return $current; - } - - /** - * Getter for key from group or generic - * - * @return $indexKey Current key in iteration - */ - public function key () { - return $this->indexKey; - } - - /** - * Advances to the next entry - * - * @return void - */ - public function next () { - $this->indexKey++; - } - - /** - * Rewinds to the beginning of the iteration - * - * @return void - */ - public function rewind () { - $this->indexKey = 0; - } - - /** - * Checks wether the current entry is valid (not at the end of the list) - * - * @return $isValid Wether the current entry is there - */ - public function valid () { - // Check for total active clients and if we are not at the end - $isValid = ($this->key() < $this->getListInstance()->count()); - - // Return result - return $isValid; - } -} - -// [EOF] -?> diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 50e6c0685..8002d4a09 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -162,9 +162,17 @@ class TcpListener extends BaseListener implements Listenable { public function doListen () { // Get all readers $readers = $this->getPoolInstance()->getAllSockets(); + $writers = array(); + $excepts = array(); // Check if we have some clients left - $left = socket_select($readers, $writers = null, $excepts = null, 0, 150); + $left = socket_select( + $readers, + $writers, + $excepts, + 0, + 150 + ); // Some new clients found? if ($left < 1) { diff --git a/application/hub/main/lists/class_BaseList.php b/application/hub/main/lists/class_BaseList.php index 99306aca6..5f14f47be 100644 --- a/application/hub/main/lists/class_BaseList.php +++ b/application/hub/main/lists/class_BaseList.php @@ -60,7 +60,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { */ public function getIterator () { // Prepare a default iterator - $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class'); + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this)); // And return it return $iteratorInstance; @@ -226,6 +226,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { } elseif (is_string($entry)) { // String found $entry2 = crc32($entry) . ':' . strlen($entry); + } elseif ((is_array($entry)) && (isset($entry['id']))) { + // Supported array found + $entry2 = crc32($entry['id']) . ':' . count($entry); } else { // Unsupported type detected $this->debugOutut(__METHOD__ . ': entry type ' . gettype($entry) . ' is unsupported.'); diff --git a/application/hub/main/lists/tasks/.htaccess b/application/hub/main/lists/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/lists/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/lists/tasks/class_TaskList.php b/application/hub/main/lists/tasks/class_TaskList.php new file mode 100644 index 000000000..80746499c --- /dev/null +++ b/application/hub/main/lists/tasks/class_TaskList.php @@ -0,0 +1,53 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class TaskList extends BaseList implements Listable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @return $listInstance An instance a Listable class + */ + public final static function createTaskList () { + // Get new instance + $listInstance = new TaskList(); + + // Add tasks group + $listInstance->addGroup('tasks'); + + // Return the prepared instance + return $listInstance; + } +} + +// [EOF] +?>