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

Diff of /smsdaemon/TaskManager.cpp

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

revision 92 by torben, Mon Jun 16 11:30:20 2008 UTC revision 129 by torben, Sat Dec 6 19:30:20 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    
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 = _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::ExecuteTasks(IGsmModem& modem)  void TaskManager::AddPersistantTask(Task* task)
39  {  {
40          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          Common* cmn = Common::instance();
         int now = time(0);  
41    
42          if (now > (_lastExecuted + SLEEP_TIME) )          if (task != 0)
43          {          {
44                  _lastExecuted = now;                  std::string name = task->GetName();
45    
46                  std::map<std::string, Task*>::iterator it;                  if ( _persistant_tasks[ name ] == 0)
47                            _persistant_tasks[ name ] = task;
48                  for (it = _tasks.begin(); it != _tasks.end(); ++it)                  else
49                  {                          cmn->logMessage( std::string("AddTask() -- already have a task called ") + name);
50                          Task* tsk = (*it).second;          }
51                          tsk->ExecuteTask(modem);          else
52                  }          {
53                    cmn->logMessage("AddTask() -- cannot register a null pointer");
54          }          }
55  }  }
56    
57    void TaskManager::AddTemporaryTask(Task* task)
 Task* TaskManager::GetTask(const std::string& taskname)  
58  {  {
59          return _tasks[ taskname ];          _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            //execute real tasks
74    
75          for (MapIterator it = _tasks.begin(); it != _tasks.end(); ++it)          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* 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::GetPersistantTask(const std::string& taskname)
107    {
108            return _persistant_tasks[ taskname ];
109    }
110    
111    

Legend:
Removed from v.92  
changed lines
  Added in v.129

  ViewVC Help
Powered by ViewVC 1.1.20