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

Diff of /smsdaemon/main.cpp

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

revision 146 by torben, Sun Dec 7 20:06:12 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>  #include <stdlib.h>
3    #include <iostream>
4    
5  #include "daemon.h"  #include "daemon.h"
6  #include "common.h"  #include "Common.h"
7    #include "Logger.h"
8    
9    #include "ModemTransceiver.h"
10    #include "DebugTransceiver.h"
11    #include "SmsToolTransceiver.h"
12    #include "ProxyTransceiver.h"
13    
 #include "GsmModem.h"  
14  #include "serialport/SerialPort.h"  #include "serialport/SerialPort.h"
15  #include "SmsDaemon.h"  #include "SmsDaemon.h"
16  #include "ConfigFile.h"  #include "ConfigFile.h"
# Line 14  using namespace std; Line 20  using namespace std;
20    
21  bool sms_exit(int exitcode)  bool sms_exit(int exitcode)
22  {  {
23      if (Common::instance()->isDaemon)          if (Common::instance()->isDaemon)
24          daemonCleanup();                  daemonCleanup();
25    
26          exit(exitcode);          exit(exitcode);
27  }  }
28    
29    SerialPort* port = 0;
30    ISmsTransceiver* transceiver = 0;
31    
32    void openModemPort()
33    {
34            Common* cmn = Common::instance();
35            ConfigFile* config = cmn->GetConfigfile();
36    
37            std::string portstr = config->GetValue("gsmmodem","serialport","");
38            if (portstr == "")
39            {
40                    Logger::logMessage("'serialport' not defined in config file");
41                    sms_exit(2);
42            }
43    
44            port = new SerialPort ( portstr );
45            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            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()
72    {
73            port->Close();
74            delete port;
75            delete transceiver;
76    }
77    
78  int main(int argc, char* argv[])  int main(int argc, char* argv[])
79  {  {
# Line 27  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    
# Line 36  int main(int argc, char* argv[]) Line 90  int main(int argc, char* argv[])
90          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
91    
92          ConfigFile* config = cmn->GetConfigfile();          ConfigFile* config = cmn->GetConfigfile();
93            
94          bool res = config->Open( cmn->configFilePath ) ;          bool res = config->Open( cmn->configFilePath ) ;
95          if (!res) {          if (!res)
96                  cmn->logMessage(string("Could not open config file:") + cmn->configFilePath);          {
97                    cout << "Could not open config file:" << cmn->configFilePath << endl;
98                  return 1;                  return 1;
99          }          }
100            
101            Logger::initLog();
102    
103    
104          /////////////////////          /////////////////////
105                    string transconf = config->GetValue("smsdaemon", "transceiver", "");
         //Write a delimiter line in the logfile to seperate sessions  
106    
107          if (Common::instance()->isDaemon)          if (transconf == "")
108          {          {
109                  lookup_uid_values();                  Logger::logMessage("No transceiver defined in config file");
110                  daemonize();                  exit(1);
111          }          }
112            else if (transconf == "internal")
           
         SerialPort port( config->GetValue("smsdaemon","serialport") );  
         try  
113          {          {
114                  port.Open( SerialPort::BAUD_9600,                  openModemPort();
115                                     SerialPort::CHAR_SIZE_8,                  closeModemPort();
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
116          }          }
117          catch(std::exception &e)          else if ( transconf == "debug" || transconf == "smstools")
118          {          {
119                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  //do nothing
120                  sms_exit(1);          }
121            else
122            {
123                    Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
124                    exit(1);
125          }          }
126    
         GsmModem modem(port);  
127    
128          try  
129            if (Common::instance()->isDaemon)
130          {          {
131                  modem.Init();                  lookup_uid_values();
132                    daemonize();
133                    cmn->daemonized = true;
134          }          }
135          catch (std::exception& e)  
136    
137            if (transconf == "internal")
138          {          {
139                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  openModemPort();
140                  sms_exit(2);          }
141            else if (transconf =="smstools")
142            {
143                    transceiver = new SmsToolTransceiver();
144            }
145            else if (transconf == "debug")
146            {
147                    transceiver = new DebugTransceiver();
148          }          }
149    
150            ProxyTransceiver proxy(*transceiver);
151    
152          //////////////////////////////////          //////////////////////////////////
153    
154          SmsDaemon daemon(modem);          SmsDaemon daemon(proxy);
155    
156          daemon.Start();          daemon.Start();
157          //returns here when main-loop exits          //returns here when main-loop exits
158    
159      if (cmn->isDaemon)          if (cmn->isDaemon)
160          daemonCleanup();                  daemonCleanup();
161    
162            if (transconf == "builtin")
163            {
164                    closeModemPort();
165            }
166    
167          return 0;          return 0;
168  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20