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

Diff of /smsdaemon/TaskManager.cpp

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

revision 26 by torben, Mon Jun 9 18:15:53 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>
10    
11    #include "tasks/SpoolTask.h"
12    
13    
14  TaskManager::TaskManager()  TaskManager::TaskManager()
15                    : _lastExecuted(0)
16  {  {
17  }  }
18            
19  TaskManager::~TaskManager()  TaskManager::~TaskManager()
20  {  {
21            //delete any temporary tasks still in the list
22            std::list<Task*>::iterator it;
23            for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)
24            {
25                    delete (*it);
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  Task* TaskManager::GetTask(const std::string& taskname)  
98    void TaskManager::AddPersistantTask(Task* task)
99  {  {
100          return _tasks[ taskname ];  
101            if (task != 0)
102            {
103                    std::string name = task->GetName();
104    
105                    if ( _persistant_tasks[ name ] == 0)
106                            _persistant_tasks[ name ] = task;
107                    else
108                            Logger::logMessage( std::string("AddTask() -- already have a task called ") + name);
109            }
110            else
111            {
112                    Logger::logMessage("AddTask() -- cannot register a null pointer");
113            }
114    }
115    
116    void TaskManager::AddTemporaryTask(Task* task)
117    {
118            _temp_tasks.push_back(task);
119  }  }
120    
121    
122  std::vector<Task*> TaskManager::GetTaskList()  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
123  {  {
124          typedef std::map<std::string, Task*>::iterator MapIterator;          const int SLEEP_TIME = 2; //wait this long between task executions
125          std::vector<Task*> task_list;          int now = time(0);
126    
127            if (now < (_lastExecuted + SLEEP_TIME) )
128                    return;
129    
130            _lastExecuted = now;
131    
132          for (MapIterator it = _tasks.begin(); it != _tasks.end(); it++)          //execute real tasks
133    
134            std::map<std::string, Task*>::iterator m_it;
135    
136            for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it)
137          {          {
138                  Task* pl = (*it).second;                  Task* tsk = (*m_it).second;
139                  task_list.push_back(pl);                  tsk->ExecuteTask(modem);
140          }          }
141    
142            //execute temporary tasks
143    
144            std::list<Task*>::iterator l_it;
145            for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it)
146            {
147                    Task* tsk = (*l_it);
148                    tsk->ExecuteTask(modem);
149    
150                    if ( tsk->IsFinished() )
151                    {
152                            delete tsk;
153                            l_it = _temp_tasks.erase(l_it);
154    
155                            //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
157                            --l_it;
158                    }
159            }
160    
         return task_list;  
 }  
161    
162    }
163    
164    
165    Task* TaskManager::GetPersistantTask(const std::string& taskname)
166    {
167            return _persistant_tasks[ taskname ];
168    }
169    
170    

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

  ViewVC Help
Powered by ViewVC 1.1.20