/[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 214 by torben, Mon Dec 22 22:22:42 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;          std::string portstr = config->GetValue("gsmmodem","serialport","");
38            if (portstr == "")
         while (mainContinue)  
39          {          {
40                  vector<SMS> sms = modem.ReadSms();                  Logger::logMessage("'serialport' not defined in config file");
41                    sms_exit(2);
42                  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);  
                         }  
   
                         modem.DeleteSms(sms[i].sms_index);  
                         cmn->smsCounter.incomming++;  
                 }  
   
                 cmn->taskManager.ExecuteTasks();  
   
43    
44                  if (cmn->isDebug && kbhit())          port = new SerialPort ( portstr );
45                          break;          try
46            {
47                    port->Open( SerialPort::BAUD_9600,
48                                SerialPort::CHAR_SIZE_8,
49                                SerialPort::PARITY_NONE,
50                                SerialPort::STOP_BITS_1,
51                                SerialPort::FLOW_CONTROL_HARD );
52            }
53            catch (std::exception &e)
54            {
55                    Logger::logMessage( string("PortOpen Exception: ") + e.what() );
56                    sms_exit(1);
57            }
58    
59                  Util::Sleep(10);          transceiver = new ModemTransceiver(*port);
60    
61            try
62            {
63                    ((ModemTransceiver*)transceiver)->Init();
64            }
65            catch (std::exception& e)
66            {
67                    Logger::logMessage( string("ModemTransceiver Exception: ") + e.what() );
68                    sms_exit(2);
69          }          }
70  }  }
71    void closeModemPort()
 bool sms_exit(int exitcode)  
72  {  {
73      if (Common::instance()->isDaemon)          port->Close();
74          daemonCleanup();          delete port;
75            delete transceiver;
         exit(exitcode);  
76  }  }
77    
78  int main(int argc, char* argv[])  int main(int argc, char* argv[])
# Line 89  int main(int argc, char* argv[]) Line 81  int main(int argc, char* argv[])
81    
82          //Set default values          //Set default values
83    
84          cmn->setLogfile( "/var/log/smsdaemon.log");          Logger::setLogfile( "/var/log/smsdaemon.log");
85          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
86          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
87    
         cmn->uid = 1000;  
         cmn->gid = 1000;  
88    
89          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
90          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
91    
92                    ConfigFile* config = cmn->GetConfigfile();
         /////////////////////  
           
         //Write a delimiter line in the logfile to seperate sessions  
93    
94          if (Common::instance()->isDaemon)          bool res = config->Open( cmn->configFilePath ) ;
95                  daemonize();          if (!res)
96            {
97                    cout << "Could not open config file:" << cmn->configFilePath << endl;
98                    return 1;
99            }
100    
101          cmn->logMessage("--------------------------------");          Logger::initLog();
102    
103          cmn->taskManager.LoadTasks();  
104          cmn->pluginManager.LoadPlugins();          /////////////////////
105                    string transconf = config->GetValue("smsdaemon", "transceiver", "");
106          SerialPort port("/dev/ttyS1" );  
107          try          if (transconf == "")
108          {          {
109                  port.Open( SerialPort::BAUD_9600,                  Logger::logMessage("No transceiver defined in config file");
110                                     SerialPort::CHAR_SIZE_8,                  exit(1);
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
111          }          }
112          catch(std::exception &e)          else if (transconf == "internal")
113          {          {
114                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  openModemPort();
115                  sms_exit(1);                  closeModemPort();
116          }          }
117            else if ( transconf == "debug" || transconf == "smstools")
         GsmModem modem(port);  
   
         try  
118          {          {
119                  modem.Init();                  //do nothing
120          }          }
121          catch (std::exception& e)          else
122          {          {
123                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
124                  sms_exit(2);                  exit(1);
125          }          }
126    
127    
         //////////////////////////////////  
128    
129          cmn->logMessage("SMS daemon started");          if (Common::instance()->isDaemon)
130            {
131                    lookup_uid_values();
132                    daemonize();
133                    cmn->daemonized = true;
134            }
135    
136    
137          modem.DeleteAllSms();          if (transconf == "internal")
           
         try  
138          {          {
139                  main_loop(modem);                  openModemPort();
140          }          }
141          catch (std::exception& e)          else if (transconf =="smstools")
142          {          {
143                  cmn->logMessage( e.what() );                  transceiver = new SmsToolTransceiver();
144          }          }
145          catch (...)          else if (transconf == "debug")
146          {          {
147                  cmn->logMessage( "Caught unknown exception" );                  transceiver = new DebugTransceiver();
148          }          }
149    
150          cmn->logMessage( cmn->getStatusMessage() );          ProxyTransceiver proxy(*transceiver);
151    
152      if (cmn->isDaemon)          //////////////////////////////////
153          daemonCleanup();  
154            SmsDaemon daemon(proxy);
155    
156            daemon.Start();
157            //returns here when main-loop exits
158    
159            if (cmn->isDaemon)
160                    daemonCleanup();
161    
162            if (transconf == "builtin")
163            {
164                    closeModemPort();
165            }
166    
167          return 0;          return 0;
168  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20