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

Diff of /smsdaemon/main.cpp

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

revision 85 by torben, Mon Jun 16 06:46:56 2008 UTC revision 88 by torben, Mon Jun 16 09:04:05 2008 UTC
# Line 1  Line 1 
1  #include <string>  #include <string>
2    
 #include <cctype>  
 #include <sstream>  
3  #include "daemon.h"  #include "daemon.h"
4  #include "common.h"  #include "common.h"
5    
6  #include "GsmModem.h"  #include "GsmModem.h"
7  #include "SerialPort.h"  #include "SerialPort.h"
8    #include "SmsDaemon.h"
9    
 #include "Plugin.h"  
 #include "kbhit.h"  
   
 #include "util.h"  
10    
11  using namespace std;  using namespace std;
 using namespace Util;  
   
   
 void create_log_message(SMS& sms,bool hasPlugin)  
 {  
         ostringstream os;  
         os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);  
         if (!hasPlugin)  
                 os << " -- PLUGIN NOT FOUND";  
   
         Common::instance()->logMessage(os.str());  
 }  
   
   
 void main_loop(GsmModem& modem)  
 {  
         Common* cmn = Common::instance();  
         volatile bool& mainContinue = cmn->mainContinue;  
   
         PluginManager& manager = cmn->pluginManager;  
   
         mainContinue = true;  
   
         while (mainContinue)  
         {  
                 vector<SMS> sms = modem.ReadSms();  
   
                 for (unsigned int i=0; i<sms.size(); ++i)  
                 {  
                         string cmd = GetSmsCommand(sms[i]);  
                           
                         cmd = Util::str_tolower(cmd);  
   
                         Plugin* pl = manager.GetPlugin(cmd);  
   
                         create_log_message(sms[i], pl != 0);  
   
                         if (pl)  
                         {  
                                 pl->Execute(modem, sms[i]);  
                         }  
                         else  
                         {  
                                 modem.SendSms(sms[i].sender, "Unknown command!", false);  
                         }  
12    
                         modem.DeleteSms(sms[i].sms_index);  
                         cmn->smsCounter.incomming++;  
                 }  
   
                 cmn->taskManager.ExecuteTasks();  
   
   
                 if (cmn->isDebug && kbhit())  
                         break;  
   
                 Util::Sleep(10);  
   
         }  
 }  
13    
14  bool sms_exit(int exitcode)  bool sms_exit(int exitcode)
15  {  {
# Line 107  int main(int argc, char* argv[]) Line 43  int main(int argc, char* argv[])
43          if (Common::instance()->isDaemon)          if (Common::instance()->isDaemon)
44                  daemonize();                  daemonize();
45    
         cmn->logMessage("--------------------------------");  
   
         cmn->taskManager.LoadTasks();  
         cmn->pluginManager.LoadPlugins();  
46                    
47          SerialPort port("/dev/ttyS1" );          SerialPort port("/dev/ttyS1" );
48          try          try
# Line 142  int main(int argc, char* argv[]) Line 74  int main(int argc, char* argv[])
74    
75          //////////////////////////////////          //////////////////////////////////
76    
77          cmn->logMessage("SMS daemon started");          SmsDaemon daemon(modem);
   
         modem.DeleteAllSms();  
           
         try  
         {  
                 main_loop(modem);  
         }  
         catch (std::exception& e)  
         {  
                 cmn->logMessage( e.what() );  
         }  
         catch (...)  
         {  
                 cmn->logMessage( "Caught unknown exception" );  
         }  
78    
79          cmn->logMessage( cmn->getStatusMessage() );          daemon.Start();
80            //returns here when main-loop exits
81    
82      if (cmn->isDaemon)      if (cmn->isDaemon)
83          daemonCleanup();          daemonCleanup();

Legend:
Removed from v.85  
changed lines
  Added in v.88

  ViewVC Help
Powered by ViewVC 1.1.20