From: Roland Häder Date: Sun, 16 Aug 2009 01:52:10 +0000 (+0000) Subject: Exceptions/interfaces/classes added: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ebb079776a7a148d22964dd140cf4d541fdf3826;p=hub.git 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 --- 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/exceptions/tasks/.htaccess b/application/hub/exceptions/tasks/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/exceptions/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/exceptions/tasks/class_InvalidTaskException.php b/application/hub/exceptions/tasks/class_InvalidTaskException.php new file mode 100644 index 000000000..e1b1c3e57 --- /dev/null +++ b/application/hub/exceptions/tasks/class_InvalidTaskException.php @@ -0,0 +1,44 @@ + + * @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 InvalidTaskException extends FrameworkException { + /** + * The super constructor for all exceptions + * + * @param $messageArray Error message array + * @param $code Error code + * @return void + */ + public function __construct (array $messageArray, $code) { + // Construct the message + $message = sprintf("[%s] Current task is invalid which should not happen.", + $messageArray[0]->__toString() + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> 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/shutdown/.htaccess b/application/hub/main/visitor/shutdown/.htaccess deleted file mode 100755 index 3a4288278..000000000 --- a/application/hub/main/visitor/shutdown/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/main/visitor/shutdown/class_Shutdown b/application/hub/main/visitor/shutdown/class_Shutdown deleted file mode 100755 index 59511aa7b..000000000 --- a/application/hub/main/visitor/shutdown/class_Shutdown +++ /dev/null @@ -1,50 +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 Shutdown???Visitor extends BaseVisitor implements PoolVisitor { - /** - * Protected constructor - * - * @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(); - - // Return the prepared instance - return $visitorInstance; - } -} - -// [EOF] -?> diff --git a/application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php b/application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php deleted file mode 100755 index d4ca881d8..000000000 --- a/application/hub/main/visitor/shutdown/class_ShutdownListenerPoolVisitor.php +++ /dev/null @@ -1,67 +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 ShutdownListenerPoolVisitor extends BaseVisitor implements PoolVisitor { - /** - * Protected constructor - * - * @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 createShutdownListenerPoolVisitor () { - // Get new instance - $visitorInstance = new ShutdownListenerPoolVisitor(); - - // Return the prepared instance - return $visitorInstance; - } - - /** - * Visits the given pool instance - * - * @param $poolInstance A Poolable instance - * @return void - */ - public function visitPool (Poolable $poolInstance) { - // Debug message - $this->debugOutput('VISITOR: Visit of ' . $poolInstance->__toString() . ' - START'); - - // Pre-shutdown the pool - $poolInstance->preShutdown(); - - // Debug message - $this->debugOutput('VISITOR: Visit of ' . $poolInstance->__toString() . ' - FINISHED'); - } -} - -// -?> 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/tasks/class_ActiveTaskVisitor.php b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php new file mode 100644 index 000000000..5794ae6bc --- /dev/null +++ b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php @@ -0,0 +1,86 @@ + + * @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 ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryVisitor, PoolVisitor { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set visitor mode + $this->setVisitorMode('task'); + } + + /** + * Creates an instance of this class + * + * @return $visitorInstance An instance a Visitorable class + */ + public final static function createActiveTaskVisitor () { + // Get new instance + $visitorInstance = new ActiveTaskVisitor(); + + // Return the prepared instance + return $visitorInstance; + } + + /** + * Visits the given pool instance + * + * @param $poolInstance A Taskable instance + * @return void + */ + public function visitTask (Taskable $taskInstance) { + // Execute the task from this visitor + $taskInstance->execute(); + } + + /** + * Query visitor method for active tasks + * + * @param $queryInstance A Queryable instance + * @return void + */ + public function visitQuery (Queryable $queryInstance) { + // Handle all queries + $queryInstance->handleQueries(); + } + + /** + * 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] +?>