]> git.mxchange.org Git - hub.git/commitdiff
Tasks extended, iterator added:
authorRoland Häder <roland@mxchange.org>
Sat, 15 Aug 2009 21:34:07 +0000 (21:34 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 15 Aug 2009 21:34:07 +0000 (21:34 +0000)
- 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
application/hub/config.php
application/hub/interfaces/handler/task/class_HandleableTask.php
application/hub/main/commands/console/class_HubConsoleMainCommand.php
application/hub/main/handler/tasks/class_TaskHandler.php
application/hub/main/iterator/pool/class_DefaultIterator.php [new file with mode: 0755]
application/hub/main/iterator/pool/class_ShutdownPoolIterator.php [deleted file]
application/hub/main/listener/tcp/class_TcpListener.php
application/hub/main/lists/class_BaseList.php
application/hub/main/lists/tasks/.htaccess [new file with mode: 0644]
application/hub/main/lists/tasks/class_TaskList.php [new file with mode: 0644]

index 82f2b2aafad82e22515640fdc42ef12e5a19e151..089e3d130160a547e049cd7ce306a4437fafdde4 100644 (file)
@@ -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
index 76bbc6b7881288fe0e2c6b7038f8530a56c88c34..e47156876b6f054836f806b95f9ac6794f4c6333 100644 (file)
@@ -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]
 ?>
index f805475ca4cf9e234b48db604314b396e6ff4b35..45781cc44622ac8bbd6171deb291cb80e04c21d9 100644 (file)
@@ -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 ();
 }
 
 //
index 4be290c5269ec99e0526d384cb2d5e427c091667..f2b33a06840230e32a2e2759dcb75e5826660b40 100644 (file)
@@ -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
index c4e920555b9326a5b78137eb2fe9b83e504a893f..ccaf3da47acfc3e7ee01e574b624af3f01d3819e 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 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 (executable)
index 0000000..2c73694
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+/**
+ * A Default iterator
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (executable)
index 1fbc0f1..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-<?php
-/**
- * A ShutdownPool iterator
- *
- * @author             Roland Haeder <webmaster@ship-simu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-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]
-?>
index 50e6c068554439352e3e6e54f07305f6d23e7618..8002d4a09334de6efc4d306c19abc15029b5e3e9 100644 (file)
@@ -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) {
index 99306aca654d5252e7019735de4a79a59ce426c9..5f14f47be4b36c62168d0abea27212ef32b44c56 100644 (file)
@@ -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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..8074649
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * A Task list
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>