From ebb079776a7a148d22964dd140cf4d541fdf3826 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 16 Aug 2009 01:52:10 +0000 Subject: [PATCH] Exceptions/interfaces/classes added: - InvalidTaskException added which will be thrown when ->valid() returns false - TaskVisitor and QueryVisitor interfaces added - Interface Taskable added which should be implemented by tasks - TaskPoolIterator added (currently it is like DefaultIterator class) - ActiveTaskVisitor (unfinished) added and some visitXyz() methods added - Dublicate shutdown visitor directory removed - TaskHandler now is able to hanle tasks basicly (some unimplemented features) - Missing configuration entries added --- .gitattributes | 15 ++- application/hub/config.php | 24 ++-- .../shutdown => exceptions/tasks}/.htaccess | 0 .../tasks/class_InvalidTaskException.php} | 32 ++--- .../hub/interfaces/lists/class_Listable.php | 10 ++ application/hub/interfaces/tasks/.htaccess | 1 + .../hub/interfaces/tasks/class_Taskable.php | 34 +++++ .../hub/interfaces/visitor/query/.htaccess | 1 + .../visitor/query/class_QueryVisitor.php | 35 +++++ .../hub/interfaces/visitor/tasks/.htaccess | 1 + .../visitor/tasks/class_TaskVisitor.php | 35 +++++ .../console/class_HubConsoleMainCommand.php | 3 - .../query/local/class_LocalQueryConnector.php | 3 +- .../main/handler/tasks/class_TaskHandler.php | 119 +++++++++++++++-- .../hub/main/iterator/pool/tasks/.htaccess | 1 + .../pool/tasks/class_TaskPoolIterator.php | 124 ++++++++++++++++++ application/hub/main/lists/class_BaseList.php | 37 +++++- application/hub/main/tasks/class_ | 9 ++ .../main/tasks/idle/class_IdleLoopTask.php | 16 ++- application/hub/main/visitor/tasks/.htaccess | 1 + .../class_ActiveTaskVisitor.php} | 45 +++++-- 21 files changed, 483 insertions(+), 63 deletions(-) rename application/hub/{main/visitor/shutdown => exceptions/tasks}/.htaccess (100%) mode change 100755 => 100644 rename application/hub/{main/visitor/shutdown/class_Shutdown => exceptions/tasks/class_InvalidTaskException.php} (59%) mode change 100755 => 100644 create mode 100644 application/hub/interfaces/tasks/.htaccess create mode 100644 application/hub/interfaces/tasks/class_Taskable.php create mode 100644 application/hub/interfaces/visitor/query/.htaccess create mode 100644 application/hub/interfaces/visitor/query/class_QueryVisitor.php create mode 100644 application/hub/interfaces/visitor/tasks/.htaccess create mode 100644 application/hub/interfaces/visitor/tasks/class_TaskVisitor.php create mode 100644 application/hub/main/iterator/pool/tasks/.htaccess create mode 100644 application/hub/main/iterator/pool/tasks/class_TaskPoolIterator.php create mode 100644 application/hub/main/visitor/tasks/.htaccess rename application/hub/main/visitor/{shutdown/class_ShutdownListenerPoolVisitor.php => tasks/class_ActiveTaskVisitor.php} (62%) mode change 100755 => 100644 diff --git a/.gitattributes b/.gitattributes index 089e3d130..a7f2b027c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,6 +12,8 @@ application/hub/exceptions/lists/.htaccess -text application/hub/exceptions/lists/class_InvalidListHashException.php -text application/hub/exceptions/lists/class_ListGroupAlreadyAddedException.php -text application/hub/exceptions/lists/class_NoListGroupException.php -text +application/hub/exceptions/tasks/.htaccess -text +application/hub/exceptions/tasks/class_InvalidTaskException.php -text application/hub/init.php -text application/hub/interfaces/.htaccess -text application/hub/interfaces/connectors/.htaccess -text @@ -45,6 +47,8 @@ application/hub/interfaces/states/client/.htaccess -text application/hub/interfaces/states/client/class_ClientStateable.php -text application/hub/interfaces/states/hub/.htaccess -text application/hub/interfaces/states/hub/class_HubStateable.php -text +application/hub/interfaces/tasks/.htaccess -text +application/hub/interfaces/tasks/class_Taskable.php -text application/hub/interfaces/visitor/.htaccess -text application/hub/interfaces/visitor/class_Visitable.php -text application/hub/interfaces/visitor/class_Visitor.php -text @@ -52,6 +56,10 @@ application/hub/interfaces/visitor/pool/.htaccess -text application/hub/interfaces/visitor/pool/class_PoolVisitor.php -text application/hub/interfaces/visitor/pool/listener/.htaccess -text application/hub/interfaces/visitor/pool/listener/class_ListenerPoolVisitor.php -text +application/hub/interfaces/visitor/query/.htaccess -text +application/hub/interfaces/visitor/query/class_QueryVisitor.php -text +application/hub/interfaces/visitor/tasks/.htaccess -text +application/hub/interfaces/visitor/tasks/class_TaskVisitor.php -text application/hub/loader.php -text application/hub/main/.htaccess -text application/hub/main/class_ -text @@ -133,6 +141,8 @@ application/hub/main/iterator/pool/handler/class_HandlerPoolIterator.php -text application/hub/main/iterator/pool/shutdown/.htaccess -text application/hub/main/iterator/pool/shutdown/class_Shutdown -text application/hub/main/iterator/pool/shutdown/class_ShutdownPoolIterator.php -text +application/hub/main/iterator/pool/tasks/.htaccess -text +application/hub/main/iterator/pool/tasks/class_TaskPoolIterator.php -text application/hub/main/listener/.htaccess -text application/hub/main/listener/class_ -text application/hub/main/listener/class_BaseListener.php -text @@ -217,9 +227,8 @@ application/hub/main/visitor/pool/handler/class_HandlerListenerPoolVisitor.php - application/hub/main/visitor/pool/shutdown/.htaccess -text application/hub/main/visitor/pool/shutdown/class_Shutdown -text application/hub/main/visitor/pool/shutdown/class_ShutdownListenerPoolVisitor.php -text -application/hub/main/visitor/shutdown/.htaccess -text -application/hub/main/visitor/shutdown/class_Shutdown -text -application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php -text +application/hub/main/visitor/tasks/.htaccess -text +application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php -text application/hub/starter.php -text /clear-cache.sh -text db/.htaccess -text diff --git a/application/hub/config.php b/application/hub/config.php index e47156876..319f83868 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -87,15 +87,9 @@ $cfg->setConfigEntry('client_pool_class', 'DefaultClientPool'); // CFG: POOL-ENTRIES-LIST-CLASS $cfg->setConfigEntry('pool_entries_list_class', 'PoolEntriesList'); -// CFG: SHUTDOWN-LISTENER-POOL-VISITOR-CLASS -$cfg->setConfigEntry('shutdown_listener_pool_visitor_class', 'ShutdownListenerPoolVisitor'); - // CFG: SHUTDOWN-POOL-ITERATOR-CLASS $cfg->setConfigEntry('shutdown_pool_iterator_class', 'ShutdownPoolIterator'); -// CFG: HANDLER-LISTENER-POOL-VISITOR-CLASS -$cfg->setConfigEntry('handler_listener_pool_visitor_class', 'HandlerListenerPoolVisitor'); - // CFG: HANDLER-POOL-ITERATOR-CLASS $cfg->setConfigEntry('handler_pool_iterator_class', 'HandlerPoolIterator'); @@ -105,6 +99,15 @@ $cfg->setConfigEntry('network_listen_iterator_class', 'NetworkListenIterator'); // CFG: NETWORK-PACKAGE-HANDLER-CLASS $cfg->setConfigEntry('tcp_network_package_handler_class', 'TcpNetworkPackageHandler'); +// CFG: SHUTDOWN-LISTENER-POOL-VISITOR-CLASS +$cfg->setConfigEntry('shutdown_listener_pool_visitor_class', 'ShutdownListenerPoolVisitor'); + +// CFG: HANDLER-LISTENER-POOL-VISITOR-CLASS +$cfg->setConfigEntry('handler_listener_pool_visitor_class', 'HandlerListenerPoolVisitor'); + +// CFG: ACTIVE-TASK-VISITOR-CLASS +$cfg->setConfigEntry('active_task_visitor_class', 'ActiveTaskVisitor'); + // CFG: LIST-GROUP-CLASS $cfg->setConfigEntry('list_group_class', 'ListGroupList'); @@ -175,13 +178,13 @@ $cfg->setConfigEntry('news_main_limit', 5); $cfg->setConfigEntry('task_handler_class', 'TaskHandler'); // CFG: TASK-NETWORK-PACKAGE-READER-STARTUP-DELAY -$cfg->setConfigEntry('task_network_package_reader_startup_delay', 5000); +$cfg->setConfigEntry('task_network_package_reader_startup_delay', 2000); // CFG: TASK-NETWORK-PACKAGE-READER-INTERVAL-DELAY $cfg->setConfigEntry('task_network_package_reader_interval_delay', 10); // CFG: TASK-QUERY-HANDLER-STARTUP-DELAY -$cfg->setConfigEntry('task_query_handler_startup_delay', 3000); +$cfg->setConfigEntry('task_query_handler_startup_delay', 1000); // CFG: TASK-QUERY-HANDLER-INTERVAL-DELAY $cfg->setConfigEntry('task_query_handler_interval_delay', 10); @@ -190,7 +193,7 @@ $cfg->setConfigEntry('task_query_handler_interval_delay', 10); $cfg->setConfigEntry('task_idle_loop_startup_delay', 0); // CFG: TASK-IDLE-LOOP-INTERVAL-DELAY -$cfg->setConfigEntry('task_idle_loop_interval_delay', 10); +$cfg->setConfigEntry('task_idle_loop_interval_delay', 0); // CFG: IDLE-TASK-CLASS $cfg->setConfigEntry('idle_task_class', 'IdleLoopTask'); @@ -198,6 +201,9 @@ $cfg->setConfigEntry('idle_task_class', 'IdleLoopTask'); // CFG: TASK-LIST-CLASS $cfg->setConfigEntry('task_list_class', 'TaskList'); +// CFG: TASK-POOL-ITERATOR-CLASS +$cfg->setConfigEntry('task_pool_iterator_class', 'TaskPoolIterator'); + // CFG: DEFAULT-ITERATOR-CLASS $cfg->setConfigEntry('default_iterator_class', 'DefaultIterator'); diff --git a/application/hub/main/visitor/shutdown/.htaccess b/application/hub/exceptions/tasks/.htaccess old mode 100755 new mode 100644 similarity index 100% rename from application/hub/main/visitor/shutdown/.htaccess rename to application/hub/exceptions/tasks/.htaccess diff --git a/application/hub/main/visitor/shutdown/class_Shutdown b/application/hub/exceptions/tasks/class_InvalidTaskException.php old mode 100755 new mode 100644 similarity index 59% rename from application/hub/main/visitor/shutdown/class_Shutdown rename to application/hub/exceptions/tasks/class_InvalidTaskException.php index 59511aa7b..e1b1c3e57 --- a/application/hub/main/visitor/shutdown/class_Shutdown +++ b/application/hub/exceptions/tasks/class_InvalidTaskException.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -19,30 +19,24 @@ * 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 . + * along with this program. If not, see . */ -class Shutdown???Visitor extends BaseVisitor implements PoolVisitor { +class InvalidTaskException extends FrameworkException { /** - * Protected constructor + * The super constructor for all exceptions * + * @param $messageArray Error message array + * @param $code Error code * @return void */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this class - * - * @return $visitorInstance An instance a Visitorable class - */ - public final static function createShutdown???Visitor () { - // Get new instance - $visitorInstance = new Shutdown???Visitor(); + public function __construct (array $messageArray, $code) { + // Construct the message + $message = sprintf("[%s] Current task is invalid which should not happen.", + $messageArray[0]->__toString() + ); - // Return the prepared instance - return $visitorInstance; + // Call parent exception constructor + parent::__construct($message, $code); } } diff --git a/application/hub/interfaces/lists/class_Listable.php b/application/hub/interfaces/lists/class_Listable.php index d2b26b911..60712ed18 100644 --- a/application/hub/interfaces/lists/class_Listable.php +++ b/application/hub/interfaces/lists/class_Listable.php @@ -59,6 +59,16 @@ interface Listable extends FrameworkInterface, IteratorAggregate { * @throws NoListGroupException If the given group is not found */ function addEntry ($groupName, $entry); + + /** + * Updates the given entry by hash with given array + * + * @param $hash Hash for this entry + * @param $entryArray Array with entry we should update + * @return void + * @throws InvalidListHashException If the solved hash index is invalid + */ + function updateCurrentEntryByHash ($hash, array $entryArray); } // diff --git a/application/hub/interfaces/tasks/.htaccess b/application/hub/interfaces/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/tasks/class_Taskable.php b/application/hub/interfaces/tasks/class_Taskable.php new file mode 100644 index 000000000..a32e0af5e --- /dev/null +++ b/application/hub/interfaces/tasks/class_Taskable.php @@ -0,0 +1,34 @@ + + * @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 . + */ +interface Taskable extends FrameworkInterface { + /** + * Executes the task + * + * @return void + */ + function execute (); +} + +// +?> diff --git a/application/hub/interfaces/visitor/query/.htaccess b/application/hub/interfaces/visitor/query/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/visitor/query/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/visitor/query/class_QueryVisitor.php b/application/hub/interfaces/visitor/query/class_QueryVisitor.php new file mode 100644 index 000000000..4e59d8fe7 --- /dev/null +++ b/application/hub/interfaces/visitor/query/class_QueryVisitor.php @@ -0,0 +1,35 @@ + + * @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 . + */ +interface QueryVisitor extends Visitor { + /** + * Query visitor method for active queries + * + * @param $queryInstance A Queryable instance + * @return void + */ + function visitQuery (Queryable $queryInstance); +} + +// [EOF] +?> diff --git a/application/hub/interfaces/visitor/tasks/.htaccess b/application/hub/interfaces/visitor/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/visitor/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/visitor/tasks/class_TaskVisitor.php b/application/hub/interfaces/visitor/tasks/class_TaskVisitor.php new file mode 100644 index 000000000..40093324e --- /dev/null +++ b/application/hub/interfaces/visitor/tasks/class_TaskVisitor.php @@ -0,0 +1,35 @@ + + * @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 . + */ +interface TaskVisitor extends Visitor { + /** + * Visits the given task instance + * + * @param $taskInstance A Taskable instance + * @return void + */ + function visitTask (Taskable $taskInstance); +} + +// [EOF] +?> diff --git a/application/hub/main/commands/console/class_HubConsoleMainCommand.php b/application/hub/main/commands/console/class_HubConsoleMainCommand.php index f2b33a068..d57140cb1 100644 --- a/application/hub/main/commands/console/class_HubConsoleMainCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleMainCommand.php @@ -93,9 +93,6 @@ class HubConsoleMainCommand extends BaseCommand implements Commandable { // Handle the listeners // @TODO We may have to catch some exceptions here //$nodeInstance->getListenerPoolInstance()->handleListenerPool(); - - // Handle the qeues - //$nodeInstance->getQueryInstance()->handleQueues(); } // END - while // Debug message diff --git a/application/hub/main/connectors/query/local/class_LocalQueryConnector.php b/application/hub/main/connectors/query/local/class_LocalQueryConnector.php index c410ca156..22f052135 100644 --- a/application/hub/main/connectors/query/local/class_LocalQueryConnector.php +++ b/application/hub/main/connectors/query/local/class_LocalQueryConnector.php @@ -56,7 +56,8 @@ class LocalQueryConnector extends BaseQueryConnector implements Connectable, Que * @return void */ public function accept (Visitor $visitorInstance) { - $this->partialStub('visitor='.$visitorInstance->__toString()); + // Visit the query connector + $visitorInstance->visitQuery($this); } } diff --git a/application/hub/main/handler/tasks/class_TaskHandler.php b/application/hub/main/handler/tasks/class_TaskHandler.php index ccaf3da47..919bc896b 100644 --- a/application/hub/main/handler/tasks/class_TaskHandler.php +++ b/application/hub/main/handler/tasks/class_TaskHandler.php @@ -22,6 +22,9 @@ * along with this program. If not, see . */ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { + // Exception constants + const EXCEPTION_TASK_IS_INVALID = 0xb00; + /** * A task list instance */ @@ -32,6 +35,11 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { */ private $iteratorInstance = null; + /** + * Visitor instance for all tasks while they are active + */ + private $visitorInstance = null; + /** * Protected constructor * @@ -46,6 +54,9 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { // Get default instance $this->iteratorInstance = $this->listInstance->getIterator(); + + // Init visitor instance for faster loop + $this->visitorInstance = ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'); } /** @@ -65,8 +76,87 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { } /** - * Registers a task with a task handler. This method throws a - * TaskAlreadyRegisteredException if the task has already been registered + * Tries to execute the given task. If as task should not be started (yet) + * or the interval time (see task_interval_delay) is not yet reached the + * task is quietly skipped. + * + * @return void + * @throws InvalidTaskException If the current task is invalid + */ + private function executeCurrentTask () { + // Update no task by default + $updateTask = false; + + // Is the current task valid? + if (!$this->iteratorInstance->valid()) { + // Not valid! + throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID); + } // END - if + + // Get current task + $currentTask = $this->iteratorInstance->current(); + + // Is the task not yet started? + if ($currentTask['task_started'] === false) { + // Determine difference between current time and registration + $diff = ($this->getMilliTime() - $currentTask['task_registered']) * 1000; + + // Should we start now? + if ($diff < $currentTask['task_startup_delay']) { + // Skip this silently + return false; + } // END - if + + // Launch the task and mark it as updated + $currentTask['task_started'] = true; + $updateTask = true; + } // END - if + + // Get time difference from interval delay + $diff = ($this->getMilliTime() - $currentTask['task_last_activity']) * 1000; + + // Is the interval delay reached? + if ($diff < $currentTask['task_interval_delay']) { + // Should we update the task from startup? + if ($updateTask === true) { + // Update the task before leaving + $this->updateTask($currentTask); + } // END - if + + // Skip this silently + return false; + } // END - if + + // Set last activity + $currentTask['task_last_activity'] = $this->getMilliTime(); + + // Update the task + $this->updateTask($currentTask); + + // And visit/run it + // @TODO Messurement can be added around this call + $currentTask['task_instance']->accept($this->visitorInstance); + } + + /** + * Updates given task by updating the underlaying list + * + * @param $taskEntry An array with a task + * @return void + */ + private function updateTask (array $taskEntry) { + // Get the key from current iteration + $key = $this->iteratorInstance->key(); + + // Get the hash from key + $hash = $this->listInstance->getHash($key); + + // Update the entry + $this->listInstance->updateCurrentEntryByHash($hash, $taskEntry); + } + + /** + * Registers a task with a task handler. * * @param $taskName A task name to register the task on * @param $taskInstance The instance we should register as a task @@ -75,11 +165,23 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { public function registerTask ($taskName, Visitable $taskInstance) { // Create the entry $taskEntry = array( + // Identifier for the generateHash() method 'id' => $taskName, - 'task_registered' => time(), - 'task_last_active' => 0, + // Wether the task is started + 'task_started' => false, + // Wether the task is paused (not yet implemented) + 'task_paused' => false, + // Wether the task can be paused (not yet implemented) + 'task_pauseable' => true, + // Timestamp of registration + 'task_registered' => $this->getMilliTime(), + // Last activity timestamp + 'task_last_activity' => 0, + // Task instance itself 'task_instance' => $taskInstance, + // Startup delay in milliseconds 'task_startup_delay' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_startup_delay'), + // Interval time (delay) in milliseconds before this task is executed again 'task_interval_delay' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_interval_delay'), ); @@ -99,7 +201,7 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { * * @return $tasksLeft Wether there are tasks left to handle */ - public final function hasTasksLeft () { + public function hasTasksLeft () { // Do we have tasks there? $tasksLeft = (($this->listInstance instanceof Listable) && ($this->listInstance->count() > 0)); @@ -122,11 +224,8 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { $this->iteratorInstance->rewind(); } // END - if - // Get current entry - $taskEntry = $this->iteratorInstance->current(); - - // Debug message - $this->debugOutput('Handling task ' . $taskEntry['id']); + // Try to execute the task + $this->executeCurrentTask(); // Go to next entry $this->iteratorInstance->next(); diff --git a/application/hub/main/iterator/pool/tasks/.htaccess b/application/hub/main/iterator/pool/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/iterator/pool/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/iterator/pool/tasks/class_TaskPoolIterator.php b/application/hub/main/iterator/pool/tasks/class_TaskPoolIterator.php new file mode 100644 index 000000000..dff213860 --- /dev/null +++ b/application/hub/main/iterator/pool/tasks/class_TaskPoolIterator.php @@ -0,0 +1,124 @@ + + * @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 + * @todo This current implementation is not recommended, use a + * @todo latency-based iteration or similar approaches + * + * 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 TaskPoolIterator 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 createTaskPoolIterator (IteratorAggregate $listInstance) { + // Get new instance + $iteratorInstance = new TaskPoolIterator(); + + // 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/lists/class_BaseList.php b/application/hub/main/lists/class_BaseList.php index 5f14f47be..fe416e104 100644 --- a/application/hub/main/lists/class_BaseList.php +++ b/application/hub/main/lists/class_BaseList.php @@ -270,16 +270,30 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { return $isValid; } + /** + * Getter for hash from given hash index + * + * @param $hashIndex Index holding the hash + * @return $hash The hash + */ + public final function getHash ($hashIndex) { + // Get it ... + $hash = $this->listIndex[$hashIndex]; + + // ... and return it + return $hash; + } + /** * Gets an entry from given hash index * * @param $hashIndex The hash index to resolve the mapped entry * @return $entry Solved entry from list - * @throws InvalidListHashException If the solved hash index is invalid + * @throws InvalidListHashException If the solved hash index is invalid */ public function getEntry ($hashIndex) { // Get the hash value - $hash = $this->listIndex[$hashIndex]; + $hash = $this->getHash($hashIndex); // Is the hash valid? if (!$this->isHashValid($hash)) { @@ -293,6 +307,25 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { // Return it return $entry; } + + /** + * Updates the given entry by hash with given array + * + * @param $hash Hash for this entry + * @param $entryArray Array with entry we should update + * @return void + * @throws InvalidListHashException If the solved hash index is invalid + */ + public function updateCurrentEntryByHash ($hash, array $entryArray) { + // Is the hash valid? + if (!$this->isHashValid($hash)) { + // Throw an exception here, hashIndex is unknown at this point + throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH); + } + + // Set the entry + $this->listEntries[$hash] = $entryArray; + } } // diff --git a/application/hub/main/tasks/class_ b/application/hub/main/tasks/class_ index 8a8e537cf..2760f7799 100644 --- a/application/hub/main/tasks/class_ +++ b/application/hub/main/tasks/class_ @@ -55,6 +55,15 @@ class ???Task extends BaseTask implements Visitable { public function accept (Visitor $visitorInstance) { $this->partialStub('Please implement this method. visitor='.$visitorInstance->__toString()); } + + /** + * Executes the task + * + * @return void + */ + public function execute () { + $this->partialStub('Unimplemented task.'); + } } // [EOF] diff --git a/application/hub/main/tasks/idle/class_IdleLoopTask.php b/application/hub/main/tasks/idle/class_IdleLoopTask.php index 29da9dba1..f8299a6c0 100644 --- a/application/hub/main/tasks/idle/class_IdleLoopTask.php +++ b/application/hub/main/tasks/idle/class_IdleLoopTask.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class IdleLoopTask extends BaseTask implements Visitable { +class IdleLoopTask extends BaseTask implements Visitable, Taskable { /** * Protected constructor * @@ -50,10 +50,20 @@ class IdleLoopTask extends BaseTask implements Visitable { * * @param $visitorInstance An instance of a Visitor class * @return void - * @todo 0% */ public function accept (Visitor $visitorInstance) { - $this->partialStub('Please implement this method. visitor='.$visitorInstance->__toString()); + // Visit this task + $visitorInstance->visitTask($this); + } + + /** + * Executes the task + * + * @return void + */ + public function execute () { + // Idle here a little (2 milliseconds) + $this->idle(2); } } diff --git a/application/hub/main/visitor/tasks/.htaccess b/application/hub/main/visitor/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/visitor/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php old mode 100755 new mode 100644 similarity index 62% rename from application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php rename to application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php index d4ca881d8..5794ae6bc --- a/application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php +++ b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.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 ShutdownListenerPoolVisitor extends BaseVisitor implements PoolVisitor { +class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryVisitor, PoolVisitor { /** * Protected constructor * @@ -30,6 +30,9 @@ class ShutdownListenerPoolVisitor extends BaseVisitor implements PoolVisitor { protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); + + // Set visitor mode + $this->setVisitorMode('task'); } /** @@ -37,9 +40,9 @@ class ShutdownListenerPoolVisitor extends BaseVisitor implements PoolVisitor { * * @return $visitorInstance An instance a Visitorable class */ - public final static function createShutdownListenerPoolVisitor () { + public final static function createActiveTaskVisitor () { // Get new instance - $visitorInstance = new ShutdownListenerPoolVisitor(); + $visitorInstance = new ActiveTaskVisitor(); // Return the prepared instance return $visitorInstance; @@ -48,20 +51,36 @@ class ShutdownListenerPoolVisitor extends BaseVisitor implements PoolVisitor { /** * Visits the given pool instance * - * @param $poolInstance A Poolable instance + * @param $poolInstance A Taskable instance * @return void */ - public function visitPool (Poolable $poolInstance) { - // Debug message - $this->debugOutput('VISITOR: Visit of ' . $poolInstance->__toString() . ' - START'); + public function visitTask (Taskable $taskInstance) { + // Execute the task from this visitor + $taskInstance->execute(); + } - // Pre-shutdown the pool - $poolInstance->preShutdown(); + /** + * Query visitor method for active tasks + * + * @param $queryInstance A Queryable instance + * @return void + */ + public function visitQuery (Queryable $queryInstance) { + // Handle all queries + $queryInstance->handleQueries(); + } - // Debug message - $this->debugOutput('VISITOR: Visit of ' . $poolInstance->__toString() . ' - FINISHED'); + /** + * Pool visitor method for active tasks + * + * @param $poolInstance A Poolable instance + * @return void + */ + public function visitPool (Poolable $poolInstance) { + // We don't need to visit a pool as an active task because a pool can + // never become a task. } } -// +// [EOF] ?> -- 2.39.2