--- smsdaemon/TaskManager.cpp 2008/12/07 20:58:41 149 +++ smsdaemon/TaskManager.cpp 2009/04/10 20:11:21 223 @@ -1,7 +1,10 @@ - + #include "TaskManager.h" -#include "common.h" +#include "Logger.h" +#include "Common.h" +#include "Util.h" +#include "ConfigFile.h" #include @@ -9,12 +12,12 @@ TaskManager::TaskManager() - : _lastExecuted(0) + : _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) @@ -23,25 +26,83 @@ } } +Task* TaskManager::CreateTask(const std::string& taskName, const std::map& arguments) +{ + if (taskName =="spool") + return new SpoolTask(); + + return 0; +} + +void TaskManager::DestroyTasks() +{ + std::map::iterator it; + for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it) + { + delete it->second; + } + _persistant_tasks.clear(); +} void TaskManager::LoadTasks() { - static SpoolTask task; + + Logger::logMessage("-------- TaskList --------"); + std::vector tasklist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "task"); + + for (unsigned i=0; i args; + + std::string argstr; + + if (pos == std::string::npos) + { + name = current; + } + else + { + name = Util::str_trim(current.substr(0,pos)); + argstr = Util::str_trim(current.substr(pos+1,1024)); + args = ConfigHelper::ParseArguments(argstr); + } + + Task* task = 0; + try + { + task = CreateTask(name, args ); + } + catch (std::exception& e) + { + Logger::logMessage(std::string("Failed to load task ") + name + " with args: " + argstr); + Logger::logMessage(std::string("Reason: ") + e.what()); + continue; + } + + if (task) + AddPersistantTask(task); + else + Logger::logMessage( std::string("Unknown task: ")+name); + + } //print the loaded tasks - std::map::iterator it; - for(it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it) + std::map::iterator it; + for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it) { Task* tsk = (*it).second; if (tsk != 0) - Common::instance()->logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" ); + Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" ); } } void TaskManager::AddPersistantTask(Task* task) { - Common* cmn = Common::instance(); if (task != 0) { @@ -50,11 +111,11 @@ if ( _persistant_tasks[ name ] == 0) _persistant_tasks[ name ] = task; else - cmn->logMessage( std::string("AddTask() -- already have a task called ") + name); + Logger::logMessage( std::string("AddTask() -- already have a task called ") + name); } else { - cmn->logMessage("AddTask() -- cannot register a null pointer"); + Logger::logMessage("AddTask() -- cannot register a null pointer"); } } @@ -66,10 +127,10 @@ void TaskManager::ExecuteTasks(ISmsTransceiver& modem) { - const int SLEEP_TIME = 10; //wait at least 10 seconds between executions + const int SLEEP_TIME = 2; //wait this long between task executions int now = time(0); - if (now < (_lastExecuted + SLEEP_TIME) ) + if (now < (_lastExecuted + SLEEP_TIME) ) return; _lastExecuted = now; @@ -95,11 +156,11 @@ if ( tsk->IsFinished() ) { delete tsk; - l_it = _temp_tasks.erase(l_it); - + 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; + --l_it; } } @@ -109,7 +170,12 @@ Task* TaskManager::GetPersistantTask(const std::string& taskname) { - return _persistant_tasks[ taskname ]; + std::map::iterator it; + it = _persistant_tasks.find(taskname); + if (it != _persistant_tasks.end()) + return it->second; + else + return 0; }