#include "TaskManager.h" #include "common.h" #include TaskManager::TaskManager() : _lastExecuted(0) { } TaskManager::~TaskManager() { //delete any temporary tasks still in the list std::list::iterator it; for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it) { delete (*it); } } void TaskManager::LoadTasks() { //print the loaded tasks std::map::iterator it; for(it = _tasks.begin(); it != _tasks.end(); ++it) { Task* tsk = (*it).second; if (tsk != 0) Common::instance()->logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" ); } } void TaskManager::AddTask(Task* task) { Common* cmn = Common::instance(); if (task != 0) { std::string name = task->GetName(); if ( _tasks[ name ] == 0) _tasks[ name ] = task; else cmn->logMessage( std::string("AddTask() -- already have a task called ") + name); } else { cmn->logMessage("AddTask() -- cannot register a null pointer"); } } void TaskManager::AddTemporaryTask(Task* task) { _temp_tasks.push_back(task); } void TaskManager::ExecuteTasks(IGsmModem& modem) { const int SLEEP_TIME = 10; //wait at least 10 seconds between executions int now = time(0); if (now < (_lastExecuted + SLEEP_TIME) ) return; _lastExecuted = now; //execute real tasks std::map::iterator m_it; for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it) { Task* tsk = (*m_it).second; tsk->ExecuteTask(modem); } //execute temporary tasks std::list::iterator l_it; for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it) { Task* tsk = (*l_it); tsk->ExecuteTask(modem); if ( tsk->IsFinished() ) { delete tsk; l_it = _temp_tasks.erase(l_it); //now l_it points to the next element in the list, //but since the for() loop will increment it before next iteration we will decrease it here --l_it; } } } Task* TaskManager::GetTask(const std::string& taskname) { return _tasks[ taskname ]; }