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

Diff of /smsdaemon/TaskManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 95 by torben, Mon Jun 16 11:30:20 2008 UTC revision 96 by torben, Mon Jun 16 14:04:19 2008 UTC
# Line 11  TaskManager::TaskManager() Line 11  TaskManager::TaskManager()
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 21  void TaskManager::LoadTasks() Line 27  void TaskManager::LoadTasks()
27  }  }
28    
29    
30  void TaskManager::ExecuteTasks(IGsmModem& modem)  void TaskManager::AddTask(Task* task)
31  {  {
32          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          Common* cmn = Common::instance();
         int now = time(0);  
33    
34          if (now > (_lastExecuted + SLEEP_TIME) )          if (task != 0)
35          {          {
36                  _lastExecuted = now;                  std::string name = task->GetName();
   
                 std::map<std::string, Task*>::iterator it;  
37    
38                  for (it = _tasks.begin(); it != _tasks.end(); ++it)                  if ( _tasks[ name ] == 0)
39                  {                          _tasks[ name ] = task;
40                          Task* tsk = (*it).second;                  else
41                          tsk->ExecuteTask(modem);                          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)
 Task* TaskManager::GetTask(const std::string& taskname)  
50  {  {
51          return _tasks[ taskname ];          _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          for (MapIterator it = _tasks.begin(); it != _tasks.end(); ++it)          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* 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.95  
changed lines
  Added in v.96

  ViewVC Help
Powered by ViewVC 1.1.20