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

Contents of /smsdaemon/TaskManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 520 - (show annotations) (download)
Sat Dec 26 23:01:01 2009 UTC (14 years, 4 months ago) by torben
File size: 3583 byte(s)
Added delayspam plugin+task


1
2 #include "TaskManager.h"
3
4 #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()
15 : _lastExecuted(0)
16 {
17 }
18
19 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 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()
48 {
49
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
94 std::map<std::string, Task*>::iterator it;
95 for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
96 {
97 Task* tsk = (*it).second;
98 if (tsk != 0)
99 Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );
100 }
101 }
102
103
104 void TaskManager::AddPersistantTask(Task* task)
105 {
106
107 if (task != 0)
108 {
109 std::string name = task->GetName();
110
111 if ( _persistant_tasks[ name ] == 0)
112 _persistant_tasks[ name ] = task;
113 else
114 Logger::logMessage( std::string("AddTask() -- already have a task called ") + name);
115 }
116 else
117 {
118 Logger::logMessage("AddTask() -- cannot register a null pointer");
119 }
120 }
121
122 void TaskManager::AddTemporaryTask(Task* task)
123 {
124 _temp_tasks.push_back(task);
125 }
126
127
128 void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
129 {
130 const int SLEEP_TIME = 2; //wait this long between task executions
131 int now = time(0);
132
133 if (now < (_lastExecuted + SLEEP_TIME) )
134 return;
135
136 _lastExecuted = now;
137
138 //execute real tasks
139
140 std::map<std::string, Task*>::iterator m_it;
141
142 for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it)
143 {
144 Task* tsk = (*m_it).second;
145 tsk->ExecuteTask(modem);
146 }
147
148 //execute temporary tasks
149
150 std::list<Task*>::iterator l_it;
151 for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); )
152 {
153 Task* tsk = (*l_it);
154
155 tsk->ExecuteTask(modem);
156
157 if ( tsk->IsFinished() )
158 {
159 delete tsk;
160 l_it = _temp_tasks.erase(l_it);
161
162 //now l_it points to the next element in the list,
163 } else {
164 ++l_it; //advance pointer as normal
165 }
166 }
167
168
169 }
170
171
172 Task* TaskManager::GetPersistantTask(const std::string& taskname)
173 {
174 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 }

  ViewVC Help
Powered by ViewVC 1.1.20