From 3c317195b3170f917f8c3655c21b92b5c3173b1c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 15 Aug 2009 21:34:07 +0000 Subject: [PATCH] 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) --- .gitattributes | 4 +- application/hub/config.php | 6 ++ .../handler/task/class_HandleableTask.php | 20 ++++- .../console/class_HubConsoleMainCommand.php | 7 +- .../main/handler/tasks/class_TaskHandler.php | 75 ++++++++++++++++++- ...Iterator.php => class_DefaultIterator.php} | 8 +- .../main/listener/tcp/class_TcpListener.php | 10 ++- application/hub/main/lists/class_BaseList.php | 5 +- application/hub/main/lists/tasks/.htaccess | 1 + .../hub/main/lists/tasks/class_TaskList.php | 53 +++++++++++++ 10 files changed, 176 insertions(+), 13 deletions(-) rename application/hub/main/iterator/pool/{class_ShutdownPoolIterator.php => class_DefaultIterator.php} (92%) create mode 100644 application/hub/main/lists/tasks/.htaccess create mode 100644 application/hub/main/lists/tasks/class_TaskList.php 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_ShutdownPoolIterator.php b/application/hub/main/iterator/pool/class_DefaultIterator.php similarity index 92% rename from application/hub/main/iterator/pool/class_ShutdownPoolIterator.php rename to application/hub/main/iterator/pool/class_DefaultIterator.php index 1fbc0f190..2c73694ee 100755 --- a/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php +++ b/application/hub/main/iterator/pool/class_DefaultIterator.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -21,7 +21,7 @@ * 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 { +class DefaultIterator extends BaseIterator implements Iterator { /** * Key for the global list index */ @@ -43,9 +43,9 @@ class ShutdownPoolIterator extends BaseIterator implements Iterator { * @param $listInstance A list of a IteratorAggregate * @return $iteratorInstance An instance a Iterator class */ - public final static function createShutdownPoolIterator (IteratorAggregate $listInstance) { + public final static function createDefaultIterator (IteratorAggregate $listInstance) { // Get new instance - $iteratorInstance = new ShutdownPoolIterator(); + $iteratorInstance = new DefaultIterator(); // Set the list $iteratorInstance->setListInstance($listInstance); 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] +?> -- 2.39.2