/[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 96 by torben, Mon Jun 16 14:04:19 2008 UTC
# Line 3  Line 3 
3    
4  #include "common.h"  #include "common.h"
5    
6    #include <time.h>
7    
8  TaskManager::TaskManager()  TaskManager::TaskManager()
9     : _lastExecuted(0)
10  {  {
11  }  }
12                    
13  TaskManager::~TaskManager()  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    
# Line 19  void TaskManager::LoadTasks() Line 26  void TaskManager::LoadTasks()
26    
27  }  }
28    
29  Task* TaskManager::GetTask(const std::string& taskname)  
30    void TaskManager::AddTask(Task* task)
31  {  {
32          return _tasks[ taskname ];          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  std::vector<Task*> TaskManager::GetTaskList()  void TaskManager::ExecuteTasks(IGsmModem& modem)
56  {  {
57          typedef std::map<std::string, Task*>::iterator MapIterator;          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions
58          std::vector<Task*> task_list;          int now = time(0);
59    
60            if (now < (_lastExecuted + SLEEP_TIME) )
61                    return;
62    
63            _lastExecuted = now;
64    
65            //execute real tasks
66    
67          for (MapIterator it = _tasks.begin(); it != _tasks.end(); it++)          std::map<std::string, Task*>::iterator m_it;
68    
69            for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it)
70          {          {
71                  Task* pl = (*it).second;                  Task* tsk = (*m_it).second;
72                  task_list.push_back(pl);                  tsk->ExecuteTask(modem);
73          }          }
74    
75            //execute temporary tasks
76    
77          return task_list;          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    }
102    
103    

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

  ViewVC Help
Powered by ViewVC 1.1.20