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

Diff of /smsdaemon/main.cpp

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

revision 26 by torben, Mon Jun 9 18:15:53 2008 UTC revision 211 by torben, Sun Dec 21 22:08:20 2008 UTC
# Line 1  Line 1 
 #include <iostream>  
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"
 #include "GsmModem.h"  
 #include "SerialPort.h"  
   
 #include "Plugin.h"  
 #include "kbhit.h"  
8    
9  #include "util.h"  #include "ModemTransceiver.h"
10    #include "DebugTransceiver.h"
11    #include "SmsToolTransceiver.h"
12    #include "ProxyTransceiver.h"
13    
14    #include "serialport/SerialPort.h"
15    #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;  
   
         mainContinue = true;  
36    
37          while (mainContinue)          port = new SerialPort ( config->GetValue("gsmmodem","serialport") );
38            try
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!");  
                         }  
   
                         modem.DeleteSms(sms[i].sms_index);  
                 }  
   
                 if (sms.size() == 0)  
                         Util::Sleep(10);  
51    
52                  if (cmn->isDebug && kbhit())          transceiver = new ModemTransceiver(*port);
                         break;  
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()
65    {
66            port->Close();
67            delete port;
68            delete transceiver;
69    }
70    
71  int main(int argc, char* argv[])  int main(int argc, char* argv[])
72  {  {
# Line 80  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 = 500;  
         cmn->gid = 500;  
81    
82          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
83            cmn->daemonStart = time(0);
84    
85            ConfigFile* config = cmn->GetConfigfile();
86    
87            bool res = config->Open( cmn->configFilePath ) ;
88            if (!res)
89            {
90                    cout << "Could not open config file:" << cmn->configFilePath << endl;
91                    return 1;
92            }
93    
94            Logger::initLog();
95    
96    
           
97          /////////////////////          /////////////////////
98                    string transconf = config->GetValue("smsdaemon", "transceiver");
99          //Write a delimiter line in the logfile to seperate sessions  
100    
101            if (transconf == "internal")
102            {
103                    openModemPort();
104                    closeModemPort();
105            }
106            else if ( transconf == "debug" || transconf == "smstools")
107            {
108                    //do nothing
109            }
110            else
111            {
112                    Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
113                    exit(1);
114            }
115    
116    
117    
118          if (Common::instance()->isDaemon)          if (Common::instance()->isDaemon)
119            {
120                    lookup_uid_values();
121                  daemonize();                  daemonize();
122                    cmn->daemonized = true;
123            }
124    
         cmn->logMessage("--------------------------------");  
   
         cmn->pluginManager.LoadPlugins();  
           
125    
126          SerialPort port("/dev/ttyS1" );          if (transconf == "internal")
127          port.Open( SerialPort::BAUD_9600 );          {
128          GsmModem modem(port);                  openModemPort();
129            }
130            else if (transconf =="smstools")
131            {
132                    transceiver = new SmsToolTransceiver();
133            }
134            else if (transconf == "debug")
135            {
136                    transceiver = new DebugTransceiver();
137            }
138    
139            ProxyTransceiver proxy(*transceiver);
140    
141          //////////////////////////////////          //////////////////////////////////
142    
143          Common::instance()->logMessage("SMS daemon started");          SmsDaemon daemon(proxy);
144    
145          modem.DeleteAllSms();          daemon.Start();
146            //returns here when main-loop exits
147    
148          main_loop(modem);          if (cmn->isDaemon)
149                    daemonCleanup();
150    
151            if (transconf == "builtin")
152            {
153                    closeModemPort();
154            }
155    
156            return 0;
157  }  }
158    

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

  ViewVC Help
Powered by ViewVC 1.1.20