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

Diff of /smsdaemon/main.cpp

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

revision 88 by torben, Mon Jun 16 09:04:05 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    
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 "SmsDaemon.h"  #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;
19    
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            port = new SerialPort ( config->GetValue("gsmmodem","serialport") );
38            try
39            {
40                    port->Open( SerialPort::BAUD_9600,
41                                SerialPort::CHAR_SIZE_8,
42                                SerialPort::PARITY_NONE,
43                                SerialPort::STOP_BITS_1,
44                                SerialPort::FLOW_CONTROL_HARD );
45            }
46            catch (std::exception &e)
47            {
48                    Logger::logMessage( string("PortOpen Exception: ") + e.what() );
49                    sms_exit(1);
50            }
51    
52            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()
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  {  {
73          Common* cmn = Common::instance();          Common* cmn = Common::instance();
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();
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");
         //Write a delimiter line in the logfile to seperate sessions  
99    
         if (Common::instance()->isDaemon)  
                 daemonize();  
100    
101                    if (transconf == "internal")
         SerialPort port("/dev/ttyS1" );  
         try  
102          {          {
103                  port.Open( SerialPort::BAUD_9600,                  openModemPort();
104                                     SerialPort::CHAR_SIZE_8,                  closeModemPort();
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
105          }          }
106          catch(std::exception &e)          else if ( transconf == "debug" || transconf == "smstools")
107          {          {
108                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  //do nothing
109                  sms_exit(1);          }
110            else
111            {
112                    Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
113                    exit(1);
114          }          }
115    
         GsmModem modem(port);  
116    
117          try  
118            if (Common::instance()->isDaemon)
119          {          {
120                  modem.Init();                  lookup_uid_values();
121                    daemonize();
122                    cmn->daemonized = true;
123          }          }
124          catch (std::exception& e)  
125    
126            if (transconf == "internal")
127          {          {
128                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  openModemPort();
129                  sms_exit(2);          }
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          SmsDaemon daemon(modem);          SmsDaemon daemon(proxy);
144    
145          daemon.Start();          daemon.Start();
146          //returns here when main-loop exits          //returns here when main-loop exits
147    
148      if (cmn->isDaemon)          if (cmn->isDaemon)
149          daemonCleanup();                  daemonCleanup();
150    
151            if (transconf == "builtin")
152            {
153                    closeModemPort();
154            }
155    
156          return 0;          return 0;
157  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20