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

Contents of /smsdaemon/TaskManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (show annotations) (download)
Mon Jun 16 14:04:19 2008 UTC (15 years, 11 months ago) by torben
File size: 1756 byte(s)
Finish the task interface

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 }
28
29
30 void TaskManager::AddTask(Task* task)
31 {
32 Common* cmn = Common::instance();
33
34 if (task != 0)
35 {
36 std::string name = task->GetName();
37
38 if ( _tasks[ name ] == 0)
39 _tasks[ name ] = task;
40 else
41 cmn->logMessage( std::string("AddTask() -- already have a task called ") + name);
42 }
43 else
44 {
45 cmn->logMessage("AddTask() -- cannot register a null pointer");
46 }
47 }
48
49 void TaskManager::AddTemporaryTask(Task* task)
50 {
51 _temp_tasks.push_back(task);
52 }
53
54
55 void TaskManager::ExecuteTasks(IGsmModem& modem)
56 {
57 const int SLEEP_TIME = 10; //wait at least 10 seconds between executions
58 int now = time(0);
59
60 if (now < (_lastExecuted + SLEEP_TIME) )
61 return;
62
63 _lastExecuted = now;
64
65 //execute real tasks
66
67 std::map<std::string, Task*>::iterator m_it;
68
69 for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it)
70 {
71 Task* tsk = (*m_it).second;
72 tsk->ExecuteTask(modem);
73 }
74
75 //execute temporary tasks
76
77 std::list<Task*>::iterator l_it;
78 for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it)
79 {
80 Task* tsk = (*l_it);
81 tsk->ExecuteTask(modem);
82
83 if ( tsk->IsFinished() )
84 {
85 delete tsk;
86 l_it = _temp_tasks.erase(l_it);
87
88 //now l_it points to the next element in the list,
89 //but since the for() loop will increment it before next iteration we will decrease it here
90 --l_it;
91 }
92 }
93
94
95 }
96
97
98 Task* TaskManager::GetTask(const std::string& taskname)
99 {
100 return _tasks[ taskname ];
101 }

  ViewVC Help
Powered by ViewVC 1.1.20