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

Diff of /smsdaemon/TaskManager.cpp

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

revision 157 by torben, Mon Dec 8 21:28:40 2008 UTC revision 520 by torben, Sat Dec 26 23:01:01 2009 UTC
# Line 1  Line 1 
1            
2  #include "TaskManager.h"  #include "TaskManager.h"
3    
4  #include "Logger.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            std::map<std::string, Task*>::iterator it;
40            for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
41            {
42                    delete it->second;
43            }
44            _persistant_tasks.clear();
45    }
46    
47  void TaskManager::LoadTasks()  void TaskManager::LoadTasks()
48  {  {
49          static SpoolTask task;  
50            Logger::logMessage("--------  TaskList  --------");
51            std::vector<Value> tasklist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "task");
52    
53            for (unsigned i=0; i<tasklist.size(); i++)
54            {
55                    std::string current = tasklist[i];
56    
57                    std::string name;
58                    unsigned pos = current.find(' ');
59                    std::map<std::string,std::string> args;
60    
61                    std::string argstr;
62    
63                    if (pos == std::string::npos)
64                    {
65                            name = current;
66                    }
67                    else
68                    {
69                            name = Util::str_trim(current.substr(0,pos));
70                            argstr = Util::str_trim(current.substr(pos+1,1024));
71                            args = ConfigHelper::ParseArguments(argstr);
72                    }
73    
74                    Task* task = 0;
75                    try
76                    {
77                            task = CreateTask(name, args );
78                    }
79                    catch (std::exception& e)
80                    {
81                            Logger::logMessage(std::string("Failed to load task ") + name + " with args: " + argstr);
82                            Logger::logMessage(std::string("Reason: ") + e.what());
83                            continue;
84                    }
85    
86                    if (task)
87                            AddPersistantTask(task);
88                    else
89                            Logger::logMessage( std::string("Unknown task: ")+name);
90    
91            }
92    
93          //print the loaded tasks          //print the loaded tasks
94          std::map<std::string, Task*>::iterator it;                std::map<std::string, Task*>::iterator it;
95          for(it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)          for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
96          {          {
97                  Task* tsk = (*it).second;                  Task* tsk = (*it).second;
98                  if (tsk != 0)                  if (tsk != 0)
# Line 65  void TaskManager::AddTemporaryTask(Task* Line 127  void TaskManager::AddTemporaryTask(Task*
127    
128  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
129  {  {
130          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          const int SLEEP_TIME = 2; //wait this long between task executions
131          int now = time(0);          int now = time(0);
132    
133          if (now < (_lastExecuted + SLEEP_TIME) )          if (now < (_lastExecuted + SLEEP_TIME) )
134                  return;                  return;
135    
136          _lastExecuted = now;          _lastExecuted = now;
# Line 86  void TaskManager::ExecuteTasks(ISmsTrans Line 148  void TaskManager::ExecuteTasks(ISmsTrans
148          //execute temporary tasks          //execute temporary tasks
149    
150          std::list<Task*>::iterator l_it;          std::list<Task*>::iterator l_it;
151          for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it)          for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); )
152          {          {
153                  Task* tsk = (*l_it);                  Task* tsk = (*l_it);
154    
155                  tsk->ExecuteTask(modem);                  tsk->ExecuteTask(modem);
156    
157                  if ( tsk->IsFinished() )                  if ( tsk->IsFinished() )
158                  {                  {
159                          delete tsk;                          delete tsk;
160                          l_it = _temp_tasks.erase(l_it);                          l_it = _temp_tasks.erase(l_it);
161                            
162                          //now l_it points to the next element in the list,                          //now l_it points to the next element in the list,
163                          //but since the for() loop will increment it before next iteration we will decrease it here                  } else {
164                          --l_it;                          ++l_it; //advance pointer as normal
165                  }                  }      
166          }          }
167    
168    
# Line 108  void TaskManager::ExecuteTasks(ISmsTrans Line 171  void TaskManager::ExecuteTasks(ISmsTrans
171    
172  Task* TaskManager::GetPersistantTask(const std::string& taskname)  Task* TaskManager::GetPersistantTask(const std::string& taskname)
173  {  {
174          return _persistant_tasks[ taskname ];          std::map<std::string, Task*>::iterator it;
175            it = _persistant_tasks.find(taskname);
176            if (it != _persistant_tasks.end())
177                    return it->second;
178            else
179                    return 0;
180  }  }
181    
182    

Legend:
Removed from v.157  
changed lines
  Added in v.520

  ViewVC Help
Powered by ViewVC 1.1.20