1 |
|
|
2 |
#include "TaskManager.h" |
#include "TaskManager.h" |
3 |
|
|
4 |
#include "common.h" |
#include "Logger.h" |
5 |
|
|
6 |
#include <time.h> |
#include <time.h> |
7 |
|
|
8 |
|
#include "tasks/SpoolTask.h" |
9 |
|
|
10 |
|
|
11 |
TaskManager::TaskManager() |
TaskManager::TaskManager() |
12 |
: _lastExecuted(0) |
: _lastExecuted(0) |
13 |
{ |
{ |
14 |
} |
} |
15 |
|
|
16 |
TaskManager::~TaskManager() |
TaskManager::~TaskManager() |
17 |
{ |
{ |
18 |
|
//delete any temporary tasks still in the list |
19 |
|
std::list<Task*>::iterator it; |
20 |
|
for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it) |
21 |
|
{ |
22 |
|
delete (*it); |
23 |
|
} |
24 |
} |
} |
25 |
|
|
26 |
|
|
27 |
void TaskManager::LoadTasks() |
void TaskManager::LoadTasks() |
28 |
{ |
{ |
29 |
|
static SpoolTask task; |
30 |
|
|
31 |
|
//print the loaded tasks |
32 |
|
std::map<std::string, Task*>::iterator it; |
33 |
|
for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it) |
34 |
|
{ |
35 |
|
Task* tsk = (*it).second; |
36 |
|
if (tsk != 0) |
37 |
|
Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" ); |
38 |
|
} |
39 |
} |
} |
40 |
|
|
41 |
|
|
42 |
void TaskManager::ExecuteTasks() |
void TaskManager::AddPersistantTask(Task* task) |
43 |
{ |
{ |
|
int now = time(0); |
|
44 |
|
|
45 |
if (now > _lastExecuted) // only execute the tasks once every second |
if (task != 0) |
46 |
{ |
{ |
47 |
_lastExecuted = now; |
std::string name = task->GetName(); |
|
|
|
|
std::map<std::string, Task*>::iterator it; |
|
48 |
|
|
49 |
for (it = _tasks.begin(); it != _tasks.end(); ++it) |
if ( _persistant_tasks[ name ] == 0) |
50 |
{ |
_persistant_tasks[ name ] = task; |
51 |
Task* tsk = (*it).second; |
else |
52 |
tsk->ExecuteTask(); |
Logger::logMessage( std::string("AddTask() -- already have a task called ") + name); |
53 |
} |
} |
54 |
|
else |
55 |
|
{ |
56 |
|
Logger::logMessage("AddTask() -- cannot register a null pointer"); |
57 |
} |
} |
58 |
} |
} |
59 |
|
|
60 |
|
void TaskManager::AddTemporaryTask(Task* task) |
|
Task* TaskManager::GetTask(const std::string& taskname) |
|
61 |
{ |
{ |
62 |
return _tasks[ taskname ]; |
_temp_tasks.push_back(task); |
63 |
} |
} |
64 |
|
|
65 |
|
|
66 |
std::vector<Task*> TaskManager::GetTaskList() |
void TaskManager::ExecuteTasks(ISmsTransceiver& modem) |
67 |
{ |
{ |
68 |
typedef std::map<std::string, Task*>::iterator MapIterator; |
const int SLEEP_TIME = 2; //wait this long between task executions |
69 |
std::vector<Task*> task_list; |
int now = time(0); |
70 |
|
|
71 |
for (MapIterator it = _tasks.begin(); it != _tasks.end(); ++it) |
if (now < (_lastExecuted + SLEEP_TIME) ) |
72 |
|
return; |
73 |
|
|
74 |
|
_lastExecuted = now; |
75 |
|
|
76 |
|
//execute real tasks |
77 |
|
|
78 |
|
std::map<std::string, Task*>::iterator m_it; |
79 |
|
|
80 |
|
for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it) |
81 |
{ |
{ |
82 |
Task* pl = (*it).second; |
Task* tsk = (*m_it).second; |
83 |
task_list.push_back(pl); |
tsk->ExecuteTask(modem); |
84 |
} |
} |
85 |
|
|
86 |
|
//execute temporary tasks |
87 |
|
|
88 |
return task_list; |
std::list<Task*>::iterator l_it; |
89 |
} |
for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it) |
90 |
|
{ |
91 |
|
Task* tsk = (*l_it); |
92 |
|
tsk->ExecuteTask(modem); |
93 |
|
|
94 |
|
if ( tsk->IsFinished() ) |
95 |
|
{ |
96 |
|
delete tsk; |
97 |
|
l_it = _temp_tasks.erase(l_it); |
98 |
|
|
99 |
|
//now l_it points to the next element in the list, |
100 |
|
//but since the for() loop will increment it before next iteration we will decrease it here |
101 |
|
--l_it; |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
Task* TaskManager::GetPersistantTask(const std::string& taskname) |
110 |
|
{ |
111 |
|
return _persistant_tasks[ taskname ]; |
112 |
|
} |
113 |
|
|
114 |
|
|