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

Contents of /smsdaemon/TaskManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (show annotations) (download)
Sat Dec 6 19:30:20 2008 UTC (15 years, 5 months ago) by torben
File size: 2118 byte(s)
Normal task is now Persistant task

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

  ViewVC Help
Powered by ViewVC 1.1.20