/[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 104 by torben, Tue Jun 17 08:14:12 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    
24  void TaskManager::LoadTasks()  void TaskManager::LoadTasks()
25  {  {
26    
27            //print the loaded tasks
28            std::map<std::string, Task*>::iterator it;      
29            for(it = _tasks.begin(); it != _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  Task* TaskManager::GetTask(const std::string& taskname)  
38    void TaskManager::AddTask(Task* task)
39  {  {
40          return _tasks[ taskname ];          Common* cmn = Common::instance();
41    
42            if (task != 0)
43            {
44                    std::string name = task->GetName();
45    
46                    if ( _tasks[ name ] == 0)
47                            _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  std::vector<Task*> TaskManager::GetTaskList()  void TaskManager::ExecuteTasks(IGsmModem& modem)
64  {  {
65          typedef std::map<std::string, Task*>::iterator MapIterator;          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions
66          std::vector<Task*> task_list;          int now = time(0);
67    
68            if (now < (_lastExecuted + SLEEP_TIME) )
69                    return;
70    
71            _lastExecuted = now;
72    
73          for (MapIterator it = _tasks.begin(); it != _tasks.end(); it++)          //execute real tasks
74    
75            std::map<std::string, Task*>::iterator m_it;
76    
77            for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it)
78          {          {
79                  Task* pl = (*it).second;                  Task* tsk = (*m_it).second;
80                  task_list.push_back(pl);                  tsk->ExecuteTask(modem);
81          }          }
82    
83            //execute temporary tasks
84    
85          return task_list;          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::GetTask(const std::string& taskname)
107    {
108            return _tasks[ taskname ];
109    }
110    
111    

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

  ViewVC Help
Powered by ViewVC 1.1.20