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

Diff of /smsdaemon/SmsDaemon.cpp

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

revision 126 by torben, Sat Dec 6 14:25:53 2008 UTC revision 217 by torben, Tue Dec 23 14:20:43 2008 UTC
# Line 3  Line 3 
3  #include <string>  #include <string>
4    
5  #include <sstream>  #include <sstream>
6  #include "common.h"  #include <stdlib.h>
7    
8  #include "GsmModem.h"  #include "Common.h"
9    
10    #include "ISmsTransceiver.h"
11    
12  #include "Plugin.h"  #include "Plugin.h"
13  #include "kbhit.h"  #include "kbhit.h"
14    
15  #include "util.h"  #include "Util.h"
16    
17  #include "TaskManager.h"  #include "TaskManager.h"
18  #include "PluginManager.h"  #include "PluginManager.h"
19    #include "ConfigFile.h"
20    #include "Logger.h"
21    #include "AccessManager.h"
22    
23  #include "version.h"  #include "version.h"
24    
# Line 22  using namespace std; Line 27  using namespace std;
27  void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)  void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)
28  {  {
29          ostringstream os;          ostringstream os;
30          os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);          os << "Recieved sms from " << sms.GetSender() << " ; command=" << sms.ExtractCommand();
31          if (!hasPlugin)          if (!hasPlugin)
32                  os << " -- PLUGIN NOT FOUND";                  os << " -- PLUGIN NOT FOUND";
33    
34          Common::instance()->logMessage(os.str());          Logger::logMessage(os.str());
35  }  }
36    
37    
38  void SmsDaemon::CheckSms()  void SmsDaemon::CheckSms()
39  {  {
40          const int INTERVAL = 250; //no of milliseconds between sms checks          const int INTERVAL = 1000; //no of milliseconds between sms checks
41          timeval now = Util::GetTimeOfDay();          timeval now = Util::GetTimeOfDay();
42    
43          if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL)          if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL)
# Line 47  void SmsDaemon::CheckSms() Line 52  void SmsDaemon::CheckSms()
52    
53          for (unsigned int i=0; i<sms.size(); ++i)          for (unsigned int i=0; i<sms.size(); ++i)
54          {          {
55                  string cmd = GetSmsCommand(sms[i]);                  string cmd = sms[i].ExtractCommand();
56                            
57                  cmd = Util::str_tolower(cmd);                  cmd = Util::str_tolower(cmd);
58    
59                  Plugin* pl = manager->GetPlugin(cmd);                  Plugin* pl = manager->GetPlugin(cmd);
# Line 61  void SmsDaemon::CheckSms() Line 66  void SmsDaemon::CheckSms()
66                  }                  }
67                  else                  else
68                  {                  {
69                          _modem.SendSms(sms[i].sender, "Unknown command!", false);                          _modem.SendSms(sms[i].GetSender(), "Unknown command!", false);
70                  }                  }
71    
                 _modem.DeleteSms(sms[i].sms_index);  
72                  cmn->smsCounter.incomming++;                  cmn->smsCounter.incomming++;
73          }          }
74  }  }
# Line 74  void SmsDaemon::MainLoop() Line 78  void SmsDaemon::MainLoop()
78  {  {
79          Common* cmn = Common::instance();          Common* cmn = Common::instance();
80          volatile bool& mainContinue = cmn->mainContinue;          volatile bool& mainContinue = cmn->mainContinue;
81            volatile bool& reloadConfig = cmn->reloadConfig;
82    
83          mainContinue = true;          mainContinue = true;
84    
         int loopcount = 0;  
85          while (mainContinue)          while (mainContinue)
86          {          {
                 if (loopcount > 100 )  
                 {  
                         CheckSms();  
87    
88                          cmn->GetTaskManager()->ExecuteTasks(_modem);                  CheckSms();
89    
90                          loopcount = 0;                  cmn->GetTaskManager()->ExecuteTasks(_modem);
                 }  
91    
92                  if (cmn->isDebug && kbhit())                  if (cmn->isDebug && kbhit())
93                          break;                          break;
94    
95                  loopcount++;                  if (reloadConfig)
96                  Util::Sleep(10);                          ReloadConfig();
97    
98                    Util::Sleep(100);
99    
100          }          }
101  }  }
102    
103    void SmsDaemon::ReloadConfig()
104    {
105            Common* cmn = Common::instance();
106            cmn->reloadConfig = false;
107    
108            cmn->GetConfigfile()->Reload();
109    
110            cmn->GetPluginManager()->DestroyPlugins();
111            cmn->GetPluginManager()->LoadPlugins();
112    
113            cmn->GetTaskManager()->DestroyTasks();
114            cmn->GetTaskManager()->LoadTasks();
115    
116            AccessManager::Load(); //reload blaclist & privileged list
117    }
118    
119  void SmsDaemon::Start()  void SmsDaemon::Start()
120  {  {
121          Common* cmn = Common::instance();          Common* cmn = Common::instance();
122            string transceiver = cmn->GetConfigfile()->GetValue("smsdaemon","transceiver");
123    
124            srand(time(0));
125    
126          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
127          _lastSmsCheck = Util::GetTimeOfDay();          _lastSmsCheck = Util::GetTimeOfDay();
128    
129          cmn->logMessage("--------------------------------");          Logger::logMessage("--------------------------------");
130          cmn->logMessage( VERSION );          Logger::logMessage( VERSION );
131          cmn->logMessage( SVNVERSION );          Logger::logMessage( SVNVERSION );
132            Logger::logMessage( string("Transceiver: ") + transceiver );
133    
134            AccessManager::Load(); //reload blaclist & privileged list
135          cmn->GetTaskManager()->LoadTasks();          cmn->GetTaskManager()->LoadTasks();
136          cmn->GetPluginManager()->LoadPlugins();          cmn->GetPluginManager()->LoadPlugins();
137          cmn->logMessage("SMS daemon started");          Logger::logMessage("SMS daemon started");
138    
139    
         _modem.DeleteAllSms();  
           
140          try          try
141          {          {
142                  MainLoop();                  MainLoop();
143          }          }
144          catch (std::exception& e)          catch (std::exception& e)
145          {          {
146                  cmn->logMessage( e.what() );                  Logger::logMessage( e.what() );
147          }          }
148          catch (...)          catch (...)
149          {          {
150                  cmn->logMessage( "Caught unknown exception" );                  Logger::logMessage( "Caught unknown exception" );
151          }          }
152    
153          cmn->logMessage( cmn->getStatusMessage() );          cmn->GetPluginManager()->DestroyPlugins();
154    
155            Logger::logMessage( cmn->getStatusMessage() );
156  }  }

Legend:
Removed from v.126  
changed lines
  Added in v.217

  ViewVC Help
Powered by ViewVC 1.1.20