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

Diff of /smsdaemon/main.cpp

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

revision 75 by torben, Fri Jun 13 10:10:06 2008 UTC revision 150 by torben, Sun Dec 7 21:42:15 2008 UTC
# Line 1  Line 1 
1  #include <string>  #include <string>
2    #include <stdlib.h>
3    
 #include <cctype>  
 #include <sstream>  
4  #include "daemon.h"  #include "daemon.h"
5  #include "common.h"  #include "common.h"
6    
7  #include "GsmModem.h"  #include "ModemTransceiver.h"
8  #include "SerialPort.h"  #include "serialport/SerialPort.h"
9    #include "SmsDaemon.h"
10  #include "Plugin.h"  #include "ConfigFile.h"
 #include "kbhit.h"  
   
 #include "util.h"  
11    
12  using namespace std;  using namespace std;
 using namespace Util;  
13    
14    
15  void create_log_message(SMS& sms,bool hasPlugin)  bool sms_exit(int exitcode)
16  {  {
17          ostringstream os;      if (Common::instance()->isDaemon)
18          os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);          daemonCleanup();
         if (!hasPlugin)  
                 os << " -- PLUGIN NOT FOUND";  
19    
20          Common::instance()->logMessage(os.str());          exit(exitcode);
21  }  }
22    
23    SerialPort* port = 0;
24    ISmsTransceiver* transceiver = 0;
25    
26  void main_loop(GsmModem& modem)  void openModemPort()
27  {  {
28          Common* cmn = Common::instance();          Common* cmn = Common::instance();
29          volatile bool& mainContinue = cmn->mainContinue;          ConfigFile* config = cmn->GetConfigfile();
   
         PluginManager& manager = cmn->pluginManager;  
   
         mainContinue = true;  
30    
31          while (mainContinue)          port = new SerialPort ( config->GetValue("smsdaemon","serialport") );
32            try
33          {          {
34                  vector<SMS> sms = modem.ReadSms();                  port->Open( SerialPort::BAUD_9600,
35                                       SerialPort::CHAR_SIZE_8,
36                  for (unsigned int i=0; i<sms.size(); ++i)                                     SerialPort::PARITY_NONE,
37                  {                                     SerialPort::STOP_BITS_1,
38                          string cmd = GetSmsCommand(sms[i]);                                     SerialPort::FLOW_CONTROL_HARD );
39                                    }
40                          cmd = Util::str_tolower(cmd);          catch(std::exception &e)
41            {
42                          Plugin* pl = manager.GetPlugin(cmd);                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );
43                    sms_exit(1);
44                          create_log_message(sms[i], pl != 0);          }
   
                         if (pl)  
                         {  
                                 pl->Execute(modem, sms[i]);  
                         }  
                         else  
                         {  
                                 modem.SendSms(sms[i].sender, "Unknown command!", false);  
                         }  
   
                         modem.DeleteSms(sms[i].sms_index);  
                         cmn->smsCounter.incomming++;  
                 }  
   
                 if (sms.size() == 0)  
                         Util::Sleep(10);  
45    
46                  if (cmn->isDebug && kbhit())          transceiver = new ModemTransceiver(*port);
                         break;  
47    
48            try
49            {
50                    ((ModemTransceiver*)transceiver)->Init();
51            }
52            catch (std::exception& e)
53            {
54                    cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() );
55                    sms_exit(2);
56          }          }
57  }  }
58    void closeModemPort()
 bool sms_exit(int exitcode)  
59  {  {
60      if (Common::instance()->isDaemon)          port->Close();
61          daemonCleanup();          delete port;
62            delete transceiver;
         exit(exitcode);  
63  }  }
64    
65  int main(int argc, char* argv[])  int main(int argc, char* argv[])
# Line 91  int main(int argc, char* argv[]) Line 72  int main(int argc, char* argv[])
72          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
73          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
74    
         cmn->uid = 1000;  
         cmn->gid = 1000;  
75    
76          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
77          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
78    
79            ConfigFile* config = cmn->GetConfigfile();
80                    
81          /////////////////////          bool res = config->Open( cmn->configFilePath ) ;
82            if (!res) {
83                    cmn->logMessage(string("Could not open config file:") + cmn->configFilePath);
84                    return 1;
85            }
86                    
         //Write a delimiter line in the logfile to seperate sessions  
87    
88          if (Common::instance()->isDaemon)          /////////////////////
                 daemonize();  
   
         cmn->logMessage("--------------------------------");  
   
         cmn->pluginManager.LoadPlugins();  
89                    
         SerialPort port("/dev/ttyS1" );  
         try  
         {  
                 port.Open( SerialPort::BAUD_9600,  
                                    SerialPort::CHAR_SIZE_8,  
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
         }  
         catch(std::exception &e)  
         {  
                 cmn->logMessage( string("PortOpen Exception: ") + e.what() );  
                 sms_exit(1);  
         }  
90    
91          GsmModem modem(port);          openModemPort();
92            closeModemPort();
93            
94    
95          try          if (Common::instance()->isDaemon)
96          {          {
97                  modem.Init();                  lookup_uid_values();
98          }                  daemonize();
99          catch (std::exception& e)                  cmn->daemonized = true;
         {  
                 cmn->logMessage( string("GsmModem Exception: ") + e.what() );  
                 sms_exit(2);  
100          }          }
101    
102            openModemPort();
103    
104          //////////////////////////////////          //////////////////////////////////
105    
106          cmn->logMessage("SMS daemon started");          SmsDaemon daemon(*transceiver);
107    
108          modem.DeleteAllSms();          daemon.Start();
109            //returns here when main-loop exits
         main_loop(modem);  
   
         cmn->logMessage( cmn->getStatusMessage() );  
110    
111      if (cmn->isDaemon)      if (cmn->isDaemon)
112          daemonCleanup();          daemonCleanup();
113    
114            closeModemPort();
115    
116          return 0;          return 0;
117  }  }
118    

Legend:
Removed from v.75  
changed lines
  Added in v.150

  ViewVC Help
Powered by ViewVC 1.1.20