/[projects]/smsdaemon/TaskManager.cpp
ViewVC logotype

Diff of /smsdaemon/TaskManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 149 by torben, Sun Dec 7 20:58:41 2008 UTC revision 207 by torben, Sun Dec 21 17:42:30 2008 UTC
# Line 1  Line 1 
1            
2  #include "TaskManager.h"  #include "TaskManager.h"
3    
4  #include "common.h"  #include "Logger.h"
5    #include "Common.h"
6    #include "Util.h"
7    #include "ConfigFile.h"
8    
9  #include <time.h>  #include <time.h>
10    
# Line 9  Line 12 
12    
13    
14  TaskManager::TaskManager()  TaskManager::TaskManager()
15   : _lastExecuted(0)                  : _lastExecuted(0)
16  {  {
17  }  }
18            
19  TaskManager::~TaskManager()  TaskManager::~TaskManager()
20  {        {
21          //delete any temporary tasks still in the list          //delete any temporary tasks still in the list
22          std::list<Task*>::iterator it;          std::list<Task*>::iterator it;
23          for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)          for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)
# Line 23  TaskManager::~TaskManager() Line 26  TaskManager::~TaskManager()
26          }          }
27  }  }
28    
29    Task* TaskManager::CreateTask(const std::string& taskName, const std::map<std::string, std::string>& arguments)
30    {
31            if (taskName =="spool")
32                    return new SpoolTask();
33    
34            return 0;
35    }
36    
37    void TaskManager::DestroyTasks()
38    {
39    }
40    
41  void TaskManager::LoadTasks()  void TaskManager::LoadTasks()
42  {  {
43          static SpoolTask task;  
44            Logger::logMessage("--------  TaskList  --------");
45            std::vector<Value> tasklist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "task");
46    
47            for (unsigned i=0; i<tasklist.size(); i++)
48            {
49                    std::string current = tasklist[i];
50    
51                    std::string name;
52                    unsigned pos = current.find(' ');
53                    std::map<std::string,std::string> args;
54    
55                    std::string argstr;
56    
57                    if (pos == std::string::npos)
58                    {
59                            name = current;
60                    }
61                    else
62                    {
63                            name = Util::str_trim(current.substr(0,pos));
64                            argstr = Util::str_trim(current.substr(pos+1,1024));
65                            args = ConfigHelper::ParseArguments(argstr);
66                    }
67    
68                    Task* task = 0;
69                    try
70                    {
71                            task = CreateTask(name, args );
72                    }
73                    catch (std::exception& e)
74                    {
75                            Logger::logMessage(std::string("Failed to load task ") + name + " with args: " + argstr);
76                            Logger::logMessage(std::string("Reason: ") + e.what());
77                            continue;
78                    }
79    
80                    if (task)
81                            AddPersistantTask(task);
82                    else
83                            Logger::logMessage( std::string("Unknown task: ")+name);
84    
85            }
86    
87          //print the loaded tasks          //print the loaded tasks
88          std::map<std::string, Task*>::iterator it;                std::map<std::string, Task*>::iterator it;
89          for(it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)          for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
90          {          {
91                  Task* tsk = (*it).second;                  Task* tsk = (*it).second;
92                  if (tsk != 0)                  if (tsk != 0)
93                          Common::instance()->logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );                          Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );
94          }          }
95  }  }
96    
97    
98  void TaskManager::AddPersistantTask(Task* task)  void TaskManager::AddPersistantTask(Task* task)
99  {  {
         Common* cmn = Common::instance();  
100    
101          if (task != 0)          if (task != 0)
102          {          {
# Line 50  void TaskManager::AddPersistantTask(Task Line 105  void TaskManager::AddPersistantTask(Task
105                  if ( _persistant_tasks[ name ] == 0)                  if ( _persistant_tasks[ name ] == 0)
106                          _persistant_tasks[ name ] = task;                          _persistant_tasks[ name ] = task;
107                  else                  else
108                          cmn->logMessage( std::string("AddTask() -- already have a task called ") + name);                          Logger::logMessage( std::string("AddTask() -- already have a task called ") + name);
109          }          }
110          else          else
111          {          {
112                  cmn->logMessage("AddTask() -- cannot register a null pointer");                  Logger::logMessage("AddTask() -- cannot register a null pointer");
113          }          }
114  }  }
115    
# Line 66  void TaskManager::AddTemporaryTask(Task* Line 121  void TaskManager::AddTemporaryTask(Task*
121    
122  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
123  {  {
124          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          const int SLEEP_TIME = 2; //wait this long between task executions
125          int now = time(0);          int now = time(0);
126    
127          if (now < (_lastExecuted + SLEEP_TIME) )          if (now < (_lastExecuted + SLEEP_TIME) )
128                  return;                  return;
129    
130          _lastExecuted = now;          _lastExecuted = now;
# Line 95  void TaskManager::ExecuteTasks(ISmsTrans Line 150  void TaskManager::ExecuteTasks(ISmsTrans
150                  if ( tsk->IsFinished() )                  if ( tsk->IsFinished() )
151                  {                  {
152                          delete tsk;                          delete tsk;
153                          l_it = _temp_tasks.erase(l_it);                          l_it = _temp_tasks.erase(l_it);
154                            
155                          //now l_it points to the next element in the list,                          //now l_it points to the next element in the list,
156                          //but since the for() loop will increment it before next iteration we will decrease it here                          //but since the for() loop will increment it before next iteration we will decrease it here
157                          --l_it;                          --l_it;
158                  }                  }
159          }          }
160    

Legend:
Removed from v.149  
changed lines
  Added in v.207

  ViewVC Help
Powered by ViewVC 1.1.20