/[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 211 by torben, Sun Dec 21 22:08:20 2008 UTC
# Line 1  Line 1 
1  #include <string>  #include <string>
2    #include <stdlib.h>
3    #include <iostream>
4    
 #include <cctype>  
 #include <sstream>  
5  #include "daemon.h"  #include "daemon.h"
6  #include "common.h"  #include "Common.h"
7    #include "Logger.h"
8    
9  #include "GsmModem.h"  #include "ModemTransceiver.h"
10  #include "SerialPort.h"  #include "DebugTransceiver.h"
11    #include "SmsToolTransceiver.h"
12  #include "Plugin.h"  #include "ProxyTransceiver.h"
13  #include "kbhit.h"  
14    #include "serialport/SerialPort.h"
15  #include "util.h"  #include "SmsDaemon.h"
16    #include "ConfigFile.h"
17    
18  using namespace std;  using namespace std;
 using namespace Util;  
19    
20    
21  void create_log_message(SMS& sms,bool hasPlugin)  bool sms_exit(int exitcode)
22  {  {
23          ostringstream os;          if (Common::instance()->isDaemon)
24          os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);                  daemonCleanup();
         if (!hasPlugin)  
                 os << " -- PLUGIN NOT FOUND";  
25    
26          Common::instance()->logMessage(os.str());          exit(exitcode);
27  }  }
28    
29    SerialPort* port = 0;
30    ISmsTransceiver* transceiver = 0;
31    
32  void main_loop(GsmModem& modem)  void openModemPort()
33  {  {
34          Common* cmn = Common::instance();          Common* cmn = Common::instance();
35          volatile bool& mainContinue = cmn->mainContinue;          ConfigFile* config = cmn->GetConfigfile();
   
         PluginManager& manager = cmn->pluginManager;  
36    
37          mainContinue = true;          port = new SerialPort ( config->GetValue("gsmmodem","serialport") );
38            try
         while (mainContinue)  
39          {          {
40                  vector<SMS> sms = modem.ReadSms();                  port->Open( SerialPort::BAUD_9600,
41                                SerialPort::CHAR_SIZE_8,
42                  for (unsigned int i=0; i<sms.size(); ++i)                              SerialPort::PARITY_NONE,
43                  {                              SerialPort::STOP_BITS_1,
44                          string cmd = GetSmsCommand(sms[i]);                              SerialPort::FLOW_CONTROL_HARD );
45                                    }
46                          cmd = Util::str_tolower(cmd);          catch (std::exception &e)
47            {
48                          Plugin* pl = manager.GetPlugin(cmd);                  Logger::logMessage( string("PortOpen Exception: ") + e.what() );
49                    sms_exit(1);
50                          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++;  
                 }  
   
                 cmn->taskManager.ExecuteTasks();  
   
   
                 if (cmn->isDebug && kbhit())  
                         break;  
51    
52                  Util::Sleep(10);          transceiver = new ModemTransceiver(*port);
53    
54            try
55            {
56                    ((ModemTransceiver*)transceiver)->Init();
57            }
58            catch (std::exception& e)
59            {
60                    Logger::logMessage( string("ModemTransceiver Exception: ") + e.what() );
61                    sms_exit(2);
62          }          }
63  }  }
64    void closeModemPort()
 bool sms_exit(int exitcode)  
65  {  {
66      if (Common::instance()->isDaemon)          port->Close();
67          daemonCleanup();          delete port;
68            delete transceiver;
         exit(exitcode);  
69  }  }
70    
71  int main(int argc, char* argv[])  int main(int argc, char* argv[])
# Line 89  int main(int argc, char* argv[]) Line 74  int main(int argc, char* argv[])
74    
75          //Set default values          //Set default values
76    
77          cmn->setLogfile( "/var/log/smsdaemon.log");          Logger::setLogfile( "/var/log/smsdaemon.log");
78          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
79          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
80    
         cmn->uid = 1000;  
         cmn->gid = 1000;  
81    
82          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
83          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
84    
85                    ConfigFile* config = cmn->GetConfigfile();
         /////////////////////  
           
         //Write a delimiter line in the logfile to seperate sessions  
86    
87          if (Common::instance()->isDaemon)          bool res = config->Open( cmn->configFilePath ) ;
88                  daemonize();          if (!res)
   
         cmn->logMessage("--------------------------------");  
   
         cmn->taskManager.LoadTasks();  
         cmn->pluginManager.LoadPlugins();  
           
         SerialPort port("/dev/ttyS1" );  
         try  
89          {          {
90                  port.Open( SerialPort::BAUD_9600,                  cout << "Could not open config file:" << cmn->configFilePath << endl;
91                                     SerialPort::CHAR_SIZE_8,                  return 1;
                                    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);  
92          }          }
93    
94          GsmModem modem(port);          Logger::initLog();
95    
96          try  
97            /////////////////////
98            string transconf = config->GetValue("smsdaemon", "transceiver");
99    
100    
101            if (transconf == "internal")
102          {          {
103                  modem.Init();                  openModemPort();
104                    closeModemPort();
105          }          }
106          catch (std::exception& e)          else if ( transconf == "debug" || transconf == "smstools")
107          {          {
108                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  //do nothing
109                  sms_exit(2);          }
110            else
111            {
112                    Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
113                    exit(1);
114          }          }
115    
116    
         //////////////////////////////////  
117    
118          cmn->logMessage("SMS daemon started");          if (Common::instance()->isDaemon)
119            {
120                    lookup_uid_values();
121                    daemonize();
122                    cmn->daemonized = true;
123            }
124    
125    
126          modem.DeleteAllSms();          if (transconf == "internal")
           
         try  
127          {          {
128                  main_loop(modem);                  openModemPort();
129          }          }
130          catch (std::exception& e)          else if (transconf =="smstools")
131          {          {
132                  cmn->logMessage( e.what() );                  transceiver = new SmsToolTransceiver();
133          }          }
134          catch (...)          else if (transconf == "debug")
135          {          {
136                  cmn->logMessage( "Caught unknown exception" );                  transceiver = new DebugTransceiver();
137          }          }
138    
139          cmn->logMessage( cmn->getStatusMessage() );          ProxyTransceiver proxy(*transceiver);
140    
141      if (cmn->isDaemon)          //////////////////////////////////
142          daemonCleanup();  
143            SmsDaemon daemon(proxy);
144    
145            daemon.Start();
146            //returns here when main-loop exits
147    
148            if (cmn->isDaemon)
149                    daemonCleanup();
150    
151            if (transconf == "builtin")
152            {
153                    closeModemPort();
154            }
155    
156          return 0;          return 0;
157  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20