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

Diff of /smsdaemon/TaskManager.cpp

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

revision 96 by torben, Mon Jun 16 14:04:19 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    
11    #include "tasks/SpoolTask.h"
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 20  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    
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
88            std::map<std::string, Task*>::iterator it;
89            for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
90            {
91                    Task* tsk = (*it).second;
92                    if (tsk != 0)
93                            Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );
94            }
95  }  }
96    
97    
98  void TaskManager::AddTask(Task* task)  void TaskManager::AddPersistantTask(Task* task)
99  {  {
         Common* cmn = Common::instance();  
100    
101          if (task != 0)          if (task != 0)
102          {          {
103                  std::string name = task->GetName();                  std::string name = task->GetName();
104    
105                  if ( _tasks[ name ] == 0)                  if ( _persistant_tasks[ name ] == 0)
106                          _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 52  void TaskManager::AddTemporaryTask(Task* Line 119  void TaskManager::AddTemporaryTask(Task*
119  }  }
120    
121    
122  void TaskManager::ExecuteTasks(IGsmModem& 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 66  void TaskManager::ExecuteTasks(IGsmModem Line 133  void TaskManager::ExecuteTasks(IGsmModem
133    
134          std::map<std::string, Task*>::iterator m_it;          std::map<std::string, Task*>::iterator m_it;
135    
136          for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it)          for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it)
137          {          {
138                  Task* tsk = (*m_it).second;                  Task* tsk = (*m_it).second;
139                  tsk->ExecuteTask(modem);                  tsk->ExecuteTask(modem);
# Line 83  void TaskManager::ExecuteTasks(IGsmModem Line 150  void TaskManager::ExecuteTasks(IGsmModem
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    
# Line 95  void TaskManager::ExecuteTasks(IGsmModem Line 162  void TaskManager::ExecuteTasks(IGsmModem
162  }  }
163    
164    
165  Task* TaskManager::GetTask(const std::string& taskname)  Task* TaskManager::GetPersistantTask(const std::string& taskname)
166  {  {
167          return _tasks[ taskname ];          return _persistant_tasks[ taskname ];
168  }  }
169    
170    

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

  ViewVC Help
Powered by ViewVC 1.1.20