/**
* A Task handler
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
- * @link http://www.ship-simu.org
+ * @link http://www.shipsimu.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
// Exception constants
const EXCEPTION_TASK_IS_INVALID = 0xb00;
- /**
- * Visitor instance for all tasks while they are active
- */
- private $visitorInstance = NULL;
-
/**
* Protected constructor
*
// Set handler name
$this->setHandlerName('task');
-
- // Init the task list
- $this->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
-
- // Get default instance
- $this->setIteratorInstance($this->getListInstance()->getIterator());
-
- // Init visitor instance for faster loop
- $this->visitorInstance = ObjectFactory::createObjectByConfiguredName('active_task_visitor_class');
}
/**
$handlerInstance = new TaskHandler();
// Output debug message
- $handlerInstance->debugOutput('TASK-HANDLER: Task handler initialized.');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing task handler.');
+
+ // Init the task list
+ $handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
+
+ // Get default instance
+ $handlerInstance->setIteratorInstance($handlerInstance->getListInstance()->getIterator());
+
+ // Init visitor instance for faster loop
+ $handlerInstance->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'));
// Register the first (and generic) idle-loop task
$taskInstance = ObjectFactory::createObjectByConfiguredName('idle_task_class');
$handlerInstance->registerTask('idle_loop', $taskInstance);
+ // Output debug message
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task handler initialized.');
+
// Return the prepared instance
return $handlerInstance;
}
*/
private function executeCurrentTask () {
// Update no task by default
- $updateTask = false;
+ $updateTask = FALSE;
// Is the current task valid?
- if (!$this->getIteratorInstance()->valid()) {
+ if (!$this->getListInstance()->getIterator()->valid()) {
// Not valid!
throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID);
} // END - if
// Get current task
- $currentTask = $this->getIteratorInstance()->current();
+ $currentTask = $this->getListInstance()->getIterator()->current();
// Is the task not yet started?
- if ($currentTask['task_started'] === false) {
+ 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;
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' not started: diff=' . $diff . ',task_startup_delay=' . $currentTask['task_startup_delay']);
+ return;
} // END - if
// Launch the task and mark it as updated
- $currentTask['task_started'] = true;
- $updateTask = true;
+ $currentTask['task_started'] = TRUE;
+ $updateTask = TRUE;
// Debug message
- $this->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
} // END - if
// Get time difference from interval delay
// Is the interval delay reached?
if ((($diff < $currentTask['task_interval_delay']) && ($currentTask['task_max_runs'] == 0)) || (($currentTask['task_total_runs'] == $currentTask['task_max_runs']) && ($currentTask['task_max_runs'] > 0))) {
// Should we update the task from startup?
- if ($updateTask === true) {
+ if ($updateTask === TRUE) {
// Update the task before leaving
$this->updateTask($currentTask);
} // END - if
// Skip this silently
- return false;
+ return;
} // END - if
// Set last activity
// And visit/run it
// @TODO Messurement can be added around this call
- $currentTask['task_instance']->accept($this->visitorInstance);
+ $currentTask['task_instance']->accept($this->getVisitorInstance());
}
/**
*/
private function updateTask (array $taskEntry) {
// Get the key from current iteration
- $key = $this->getIteratorInstance()->key();
+ $key = $this->getListInstance()->getIterator()->key();
// Get the hash from key
$hash = $this->getListInstance()->getHash($key);
*/
private function unregisterTask (array $taskData) {
// Debug output
- $this->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - START');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
// Remove the entry
$this->getListInstance()->removeEntry('tasks', $taskData);
// Debug output
- $this->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - FINISHED');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
}
+ /**
+ * Searches a task by given instance
+ *
+ * @param $taskInstanc An instanceof a Taskable class
+ * @return $taskName Name of the task as used while registration
+ */
+ public function searchTask (Taskable $taskInstance) {
+ // Default is an empty (not found) task name
+ $taskName = '';
+
+ // Get whole list
+ $taskList = $this->getListInstance()->getArrayFromList('tasks');
+
+ // Search all instances
+ foreach ($taskList as $currentTask) {
+ // Does it match given task instance?
+ if ($currentTask['task_instance']->equals($taskInstance)) {
+ // Found it
+ $taskName = $currentTask['id'];
+
+ // Abort here
+ break;
+ } // END - if
+ } // END - foreach
+
+ // Return found name
+ return $taskName;
+ }
+
/**
* Registers a task with a task handler.
*
$taskEntry = array(
// Identifier for the generateHash() method
'id' => $taskName,
- // 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,
+ // Whether the task is started
+ 'task_started' => FALSE,
+ // Whether the task is paused (not yet implemented)
+ 'task_paused' => FALSE,
+ // Whether the task can be paused (not yet implemented)
+ 'task_pauseable' => TRUE,
// Timestamp of registration
'task_registered' => $this->getMilliTime(),
// Last activity timestamp
$this->getListInstance()->addEntry('tasks', $taskEntry);
// Debug message
- $this->debugOutput('TASK-HANDLER: Task registered: taskName=' . $taskName .
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task registered: taskName=' . $taskName .
' (taskInstance=' . $taskInstance->__toString() . ')' .
', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
}
/**
- * Checks wether tasks are left including idle task
+ * Checks whether tasks are left including idle task
*
- * @return $tasksLeft Wether there are tasks left to handle
+ * @return $tasksLeft Whether there are tasks left to handle
*/
public function hasTasksLeft () {
// Do we have tasks there?
*/
public function handleTasks () {
// Should we rewind?
- if (!$this->getIteratorInstance()->valid()) {
+ if (!$this->getListInstance()->getIterator()->valid()) {
// Rewind to the beginning for next loop
- $this->getIteratorInstance()->rewind();
+ $this->getListInstance()->getIterator()->rewind();
} // END - if
// Try to execute the task
$this->executeCurrentTask();
// Go to next entry
- $this->getIteratorInstance()->next();
+ $this->getListInstance()->getIterator()->next();
}
/**
*/
public function doShutdown () {
// Always rewind to the beginning for next loop
- $this->getIteratorInstance()->rewind();
+ $this->getListInstance()->getIterator()->rewind();
// Debug message
- $this->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
// Remember all tasks that has been shutdown for removal
$tasks = array();
// Instance a visitor
- $this->visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class');
+ $this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
// Shutdown all tasks in once go
- while ($this->getIteratorInstance()->valid()) {
+ while ($this->getListInstance()->getIterator()->valid()) {
// Get current entry
- $currentTask = $this->getIteratorInstance()->current();
+ $currentTask = $this->getListInstance()->getIterator()->current();
// Output debug message
- $this->debugOutput('TASK-HANDLER: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
// Shutdown the task
- $currentTask['task_instance']->accept($this->visitorInstance);
+ $currentTask['task_instance']->accept($this->getVisitorInstance());
// Remember this task
- $tasks[] = $currentTask;
+ array_push($tasks, $currentTask);
// Advance to next one
- $this->getIteratorInstance()->next();
+ $this->getListInstance()->getIterator()->next();
} // END - while
// Debug message
- $this->debugOutput('TASK-HANDLER: Shutdown of all tasks completed.');
+ self::createDebugInstance(__CLASS__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutdown of all tasks completed.');
// Remove all tasks
foreach ($tasks as $entry) {