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

Diff of /smsdaemon/TaskManager.cpp

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

revision 104 by torben, Tue Jun 17 08:14:12 2008 UTC revision 196 by torben, Thu Dec 18 06:53:29 2008 UTC
# Line 1  Line 1 
1            
2  #include "TaskManager.h"  #include "TaskManager.h"
3    
4  #include "common.h"  #include "Logger.h"
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          //delete any temporary tasks still in the list
19          std::list<Task*>::iterator it;          std::list<Task*>::iterator it;
20          for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)          for (it = _temp_tasks.begin(); it != _temp_tasks.end(); ++it)
# Line 23  TaskManager::~TaskManager() Line 26  TaskManager::~TaskManager()
26    
27  void TaskManager::LoadTasks()  void TaskManager::LoadTasks()
28  {  {
29            static SpoolTask task;
30    
31          //print the loaded tasks          //print the loaded tasks
32          std::map<std::string, Task*>::iterator it;                std::map<std::string, Task*>::iterator it;
33          for(it = _tasks.begin(); it != _tasks.end(); ++it)          for (it = _persistant_tasks.begin(); it != _persistant_tasks.end(); ++it)
34          {          {
35                  Task* tsk = (*it).second;                  Task* tsk = (*it).second;
36                  if (tsk != 0)                  if (tsk != 0)
37                          Common::instance()->logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );                          Logger::logMessage( std::string("Loaded task \"") + tsk->GetName() + "\"" );
38          }          }
39  }  }
40    
41    
42  void TaskManager::AddTask(Task* task)  void TaskManager::AddPersistantTask(Task* task)
43  {  {
         Common* cmn = Common::instance();  
44    
45          if (task != 0)          if (task != 0)
46          {          {
47                  std::string name = task->GetName();                  std::string name = task->GetName();
48    
49                  if ( _tasks[ name ] == 0)                  if ( _persistant_tasks[ name ] == 0)
50                          _tasks[ name ] = task;                          _persistant_tasks[ name ] = task;
51                  else                  else
52                          cmn->logMessage( std::string("AddTask() -- already have a task called ") + name);                          Logger::logMessage( std::string("AddTask() -- already have a task called ") + name);
53          }          }
54          else          else
55          {          {
56                  cmn->logMessage("AddTask() -- cannot register a null pointer");                  Logger::logMessage("AddTask() -- cannot register a null pointer");
57          }          }
58  }  }
59    
# Line 60  void TaskManager::AddTemporaryTask(Task* Line 63  void TaskManager::AddTemporaryTask(Task*
63  }  }
64    
65    
66  void TaskManager::ExecuteTasks(IGsmModem& modem)  void TaskManager::ExecuteTasks(ISmsTransceiver& modem)
67  {  {
68          const int SLEEP_TIME = 10; //wait at least 10 seconds between executions          const int SLEEP_TIME = 2; //wait this long between task executions
69          int now = time(0);          int now = time(0);
70    
71          if (now < (_lastExecuted + SLEEP_TIME) )          if (now < (_lastExecuted + SLEEP_TIME) )
72                  return;                  return;
73    
74          _lastExecuted = now;          _lastExecuted = now;
# Line 74  void TaskManager::ExecuteTasks(IGsmModem Line 77  void TaskManager::ExecuteTasks(IGsmModem
77    
78          std::map<std::string, Task*>::iterator m_it;          std::map<std::string, Task*>::iterator m_it;
79    
80          for (m_it = _tasks.begin(); m_it != _tasks.end(); ++m_it)          for (m_it = _persistant_tasks.begin(); m_it != _persistant_tasks.end(); ++m_it)
81          {          {
82                  Task* tsk = (*m_it).second;                  Task* tsk = (*m_it).second;
83                  tsk->ExecuteTask(modem);                  tsk->ExecuteTask(modem);
# Line 91  void TaskManager::ExecuteTasks(IGsmModem Line 94  void TaskManager::ExecuteTasks(IGsmModem
94                  if ( tsk->IsFinished() )                  if ( tsk->IsFinished() )
95                  {                  {
96                          delete tsk;                          delete tsk;
97                          l_it = _temp_tasks.erase(l_it);                          l_it = _temp_tasks.erase(l_it);
98                            
99                          //now l_it points to the next element in the list,                          //now l_it points to the next element in the list,
100                          //but since the for() loop will increment it before next iteration we will decrease it here                          //but since the for() loop will increment it before next iteration we will decrease it here
101                          --l_it;                          --l_it;
102                  }                  }
103          }          }
104    
# Line 103  void TaskManager::ExecuteTasks(IGsmModem Line 106  void TaskManager::ExecuteTasks(IGsmModem
106  }  }
107    
108    
109  Task* TaskManager::GetTask(const std::string& taskname)  Task* TaskManager::GetPersistantTask(const std::string& taskname)
110  {  {
111          return _tasks[ taskname ];          return _persistant_tasks[ taskname ];
112  }  }
113    
114    

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

  ViewVC Help
Powered by ViewVC 1.1.20