/[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 149 by torben, Sun Dec 7 20:58:41 2008 UTC
# Line 5  Line 5 
5    
6  #include <time.h>  #include <time.h>
7    
8    #include "tasks/SpoolTask.h"
9    
10    
11  TaskManager::TaskManager()  TaskManager::TaskManager()
12   : _lastExecuted(0)   : _lastExecuted(0)
13  {  {
14  }  }
15                    
16  TaskManager::~TaskManager()  TaskManager::~TaskManager()
17  {  {      
18            //delete any temporary tasks still in the list
19            std::list<Task*>::iterator it;
20            for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)
21            {
22                    delete (*it);
23            }
24  }  }
25    
26    
27  void TaskManager::LoadTasks()  void TaskManager::LoadTasks()
28  {  {
29            static SpoolTask task;
30    
31            //print the loaded tasks
32            std::map<std::string, Task*>::iterator it;      
33            for(it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
34            {
35                    Task* tsk = (*it).second;
36                    if (tsk != 0)
37                            Common::instance()->logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );
38            }
39  }  }
40    
41    
42  void TaskManager::ExecuteTasks(IGsmModem& modem)  void TaskManager::AddPersistantTask(Task* task)
43  {  {
44          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          Common* cmn = Common::instance();
         int now = time(0);  
45    
46          if (now > (_lastExecuted + SLEEP_TIME) )          if (task != 0)
47          {          {
48                  _lastExecuted = now;                  std::string name = task->GetName();
   
                 std::map<std::string, Task*>::iterator it;  
49    
50                  for (it = _tasks.begin(); it != _tasks.end(); ++it)                  if ( _persistant_tasks[ name ] == 0)
51                  {                          _persistant_tasks[ name ] = task;
52                          Task* tsk = (*it).second;                  else
53                          tsk->ExecuteTask(modem);                          cmn->logMessage( std::string("AddTask() -- already have a task called ") + name);
54                  }          }
55            else
56            {
57                    cmn->logMessage("AddTask() -- cannot register a null pointer");
58          }          }
59  }  }
60    
61    void TaskManager::AddTemporaryTask(Task* task)
 Task* TaskManager::GetTask(const std::string& taskname)  
62  {  {
63          return _tasks[ taskname ];          _temp_tasks.push_back(task);
64  }  }
65    
66    
67  std::vector<Task*> TaskManager::GetTaskList()  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
68  {  {
69          typedef std::map<std::string, Task*>::iterator MapIterator;          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions
70          std::vector<Task*> task_list;          int now = time(0);
71    
72            if (now < (_lastExecuted + SLEEP_TIME) )
73                    return;
74    
75            _lastExecuted = now;
76    
77            //execute real tasks
78    
79          for (MapIterator it = _tasks.begin(); it != _tasks.end(); ++it)          std::map<std::string, Task*>::iterator m_it;
80    
81            for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it)
82          {          {
83                  Task* pl = (*it).second;                  Task* tsk = (*m_it).second;
84                  task_list.push_back(pl);                  tsk->ExecuteTask(modem);
85          }          }
86    
87            //execute temporary tasks
88    
89          return task_list;          std::list<Task*>::iterator l_it;
90  }          for (l_it = _temp_tasks.begin(); l_it != _temp_tasks.end(); ++l_it)
91            {
92                    Task* tsk = (*l_it);
93                    tsk->ExecuteTask(modem);
94    
95                    if ( tsk->IsFinished() )
96                    {
97                            delete tsk;
98                            l_it = _temp_tasks.erase(l_it);
99                            
100                            //now l_it points to the next element in the list,
101                            //but since the for() loop will increment it before next iteration we will decrease it here
102                            --l_it;
103                    }
104            }
105    
106    
107    }
108    
109    
110    Task* TaskManager::GetPersistantTask(const std::string& taskname)
111    {
112            return _persistant_tasks[ taskname ];
113    }
114    
115    

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

  ViewVC Help
Powered by ViewVC 1.1.20