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

Diff of /smsdaemon/main.cpp

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

revision 149 by torben, Sun Dec 7 20:58:41 2008 UTC revision 157 by torben, Mon Dec 8 21:28:40 2008 UTC
# Line 3  Line 3 
3    
4  #include "daemon.h"  #include "daemon.h"
5  #include "common.h"  #include "common.h"
6    #include "Logger.h"
7    
8  #include "ModemTransceiver.h"  #include "ModemTransceiver.h"
9    #include "DebugTransceiver.h"
10    #include "SmsToolTransceiver.h"
11    
12  #include "serialport/SerialPort.h"  #include "serialport/SerialPort.h"
13  #include "SmsDaemon.h"  #include "SmsDaemon.h"
14  #include "ConfigFile.h"  #include "ConfigFile.h"
# Line 20  bool sms_exit(int exitcode) Line 24  bool sms_exit(int exitcode)
24          exit(exitcode);          exit(exitcode);
25  }  }
26    
27    SerialPort* port = 0;
28    ISmsTransceiver* transceiver = 0;
29    
30    void openModemPort()
31    {
32            Common* cmn = Common::instance();
33            ConfigFile* config = cmn->GetConfigfile();
34    
35            port = new SerialPort ( config->GetValue("smsdaemon","serialport") );
36            try
37            {
38                    port->Open( SerialPort::BAUD_9600,
39                                       SerialPort::CHAR_SIZE_8,
40                                       SerialPort::PARITY_NONE,
41                                       SerialPort::STOP_BITS_1,
42                                       SerialPort::FLOW_CONTROL_HARD );
43            }
44            catch(std::exception &e)
45            {
46                    Logger::logMessage( string("PortOpen Exception: ") + e.what() );
47                    sms_exit(1);
48            }
49    
50            transceiver = new ModemTransceiver(*port);
51    
52            try
53            {
54                    ((ModemTransceiver*)transceiver)->Init();
55            }
56            catch (std::exception& e)
57            {
58                    Logger::logMessage( string("ModemTransceiver Exception: ") + e.what() );
59                    sms_exit(2);
60            }
61    }
62    void closeModemPort()
63    {
64            port->Close();
65            delete port;
66            delete transceiver;
67    }
68    
69  int main(int argc, char* argv[])  int main(int argc, char* argv[])
70  {  {
# Line 27  int main(int argc, char* argv[]) Line 72  int main(int argc, char* argv[])
72    
73          //Set default values          //Set default values
74    
75          cmn->setLogfile( "/var/log/smsdaemon.log");          Logger::setLogfile( "/var/log/smsdaemon.log");
76          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
77          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
78    
# Line 39  int main(int argc, char* argv[]) Line 84  int main(int argc, char* argv[])
84                    
85          bool res = config->Open( cmn->configFilePath ) ;          bool res = config->Open( cmn->configFilePath ) ;
86          if (!res) {          if (!res) {
87                  cmn->logMessage(string("Could not open config file:") + cmn->configFilePath);                  Logger::logMessage(string("Could not open config file:") + cmn->configFilePath);
88                  return 1;                  return 1;
89          }          }
90                    
91    
92          /////////////////////          /////////////////////
93            string transconf = config->GetValue("smsdaemon", "transceiver");
94                    
         //Write a delimiter line in the logfile to seperate sessions  
95    
96          if (Common::instance()->isDaemon)          if (transconf == "internal")
97          {          {
98                  lookup_uid_values();                  openModemPort();
99                  daemonize();                  closeModemPort();
100            }
101            else if ( transconf == "debug" || transconf == "smstools")
102            {
103                    //do nothing
104          }          }
105            else
           
         SerialPort port( config->GetValue("smsdaemon","serialport") );  
         try  
106          {          {
107                  port.Open( SerialPort::BAUD_9600,                  Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
108                                     SerialPort::CHAR_SIZE_8,                  exit(1);
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
109          }          }
110          catch(std::exception &e)  
111            
112    
113            if (Common::instance()->isDaemon)
114          {          {
115                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  lookup_uid_values();
116                  sms_exit(1);                  daemonize();
117                    cmn->daemonized = true;
118          }          }
119    
         ModemTransceiver modem(port);  
120    
121          try          if (transconf == "internal")
122          {          {
123                  modem.Init();                  openModemPort();
124          }          }
125          catch (std::exception& e)          else if (transconf =="smstools")
126          {          {
127                  cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() );                  transceiver = new SmsToolTransceiver();
128                  sms_exit(2);          }
129            else if (transconf == "debug")
130            {
131                    transceiver = new DebugTransceiver();
132          }          }
133    
   
134          //////////////////////////////////          //////////////////////////////////
135    
136          SmsDaemon daemon(modem);          SmsDaemon daemon(*transceiver);
137    
138          daemon.Start();          daemon.Start();
139          //returns here when main-loop exits          //returns here when main-loop exits
# Line 93  int main(int argc, char* argv[]) Line 141  int main(int argc, char* argv[])
141      if (cmn->isDaemon)      if (cmn->isDaemon)
142          daemonCleanup();          daemonCleanup();
143    
144            if (transconf == "builtin")
145            {
146                    closeModemPort();
147            }
148    
149          return 0;          return 0;
150  }  }
151    

Legend:
Removed from v.149  
changed lines
  Added in v.157

  ViewVC Help
Powered by ViewVC 1.1.20