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

Contents of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 214 - (show annotations) (download)
Mon Dec 22 22:22:42 2008 UTC (15 years, 4 months ago) by torben
File size: 3011 byte(s)
Make smsdaemon less 'crashable' if a config value is not defined in the config file

1 #include <string>
2 #include <stdlib.h>
3 #include <iostream>
4
5 #include "daemon.h"
6 #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
14 #include "serialport/SerialPort.h"
15 #include "SmsDaemon.h"
16 #include "ConfigFile.h"
17
18 using namespace std;
19
20
21 bool sms_exit(int exitcode)
22 {
23 if (Common::instance()->isDaemon)
24 daemonCleanup();
25
26 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[])
79 {
80 Common* cmn = Common::instance();
81
82 //Set default values
83
84 Logger::setLogfile( "/var/log/smsdaemon.log");
85 cmn->pidfile = "/var/run/smsdaemon.pid";
86 cmn->spooldir = "/var/spool/smsdaemon";
87
88
89 cmn->loadConfig(argc,argv);
90 cmn->daemonStart = time(0);
91
92 ConfigFile* config = cmn->GetConfigfile();
93
94 bool res = config->Open( cmn->configFilePath ) ;
95 if (!res)
96 {
97 cout << "Could not open config file:" << cmn->configFilePath << endl;
98 return 1;
99 }
100
101 Logger::initLog();
102
103
104 /////////////////////
105 string transconf = config->GetValue("smsdaemon", "transceiver", "");
106
107 if (transconf == "")
108 {
109 Logger::logMessage("No transceiver defined in config file");
110 exit(1);
111 }
112 else if (transconf == "internal")
113 {
114 openModemPort();
115 closeModemPort();
116 }
117 else if ( transconf == "debug" || transconf == "smstools")
118 {
119 //do nothing
120 }
121 else
122 {
123 Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
124 exit(1);
125 }
126
127
128
129 if (Common::instance()->isDaemon)
130 {
131 lookup_uid_values();
132 daemonize();
133 cmn->daemonized = true;
134 }
135
136
137 if (transconf == "internal")
138 {
139 openModemPort();
140 }
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(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;
168 }

  ViewVC Help
Powered by ViewVC 1.1.20