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

Contents of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (show annotations) (download)
Sun Dec 7 21:42:15 2008 UTC (15 years, 5 months ago) by torben
File size: 2053 byte(s)
Make daemon mode a chance to dump error messages to console before daemonizing

1 #include <string>
2 #include <stdlib.h>
3
4 #include "daemon.h"
5 #include "common.h"
6
7 #include "ModemTransceiver.h"
8 #include "serialport/SerialPort.h"
9 #include "SmsDaemon.h"
10 #include "ConfigFile.h"
11
12 using namespace std;
13
14
15 bool sms_exit(int exitcode)
16 {
17 if (Common::instance()->isDaemon)
18 daemonCleanup();
19
20 exit(exitcode);
21 }
22
23 SerialPort* port = 0;
24 ISmsTransceiver* transceiver = 0;
25
26 void openModemPort()
27 {
28 Common* cmn = Common::instance();
29 ConfigFile* config = cmn->GetConfigfile();
30
31 port = new SerialPort ( config->GetValue("smsdaemon","serialport") );
32 try
33 {
34 port->Open( SerialPort::BAUD_9600,
35 SerialPort::CHAR_SIZE_8,
36 SerialPort::PARITY_NONE,
37 SerialPort::STOP_BITS_1,
38 SerialPort::FLOW_CONTROL_HARD );
39 }
40 catch(std::exception &e)
41 {
42 cmn->logMessage( string("PortOpen Exception: ") + e.what() );
43 sms_exit(1);
44 }
45
46 transceiver = new ModemTransceiver(*port);
47
48 try
49 {
50 ((ModemTransceiver*)transceiver)->Init();
51 }
52 catch (std::exception& e)
53 {
54 cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() );
55 sms_exit(2);
56 }
57 }
58 void closeModemPort()
59 {
60 port->Close();
61 delete port;
62 delete transceiver;
63 }
64
65 int main(int argc, char* argv[])
66 {
67 Common* cmn = Common::instance();
68
69 //Set default values
70
71 cmn->setLogfile( "/var/log/smsdaemon.log");
72 cmn->pidfile = "/var/run/smsdaemon.pid";
73 cmn->spooldir = "/var/spool/smsdaemon";
74
75
76 cmn->loadConfig(argc,argv);
77 cmn->daemonStart = time(0);
78
79 ConfigFile* config = cmn->GetConfigfile();
80
81 bool res = config->Open( cmn->configFilePath ) ;
82 if (!res) {
83 cmn->logMessage(string("Could not open config file:") + cmn->configFilePath);
84 return 1;
85 }
86
87
88 /////////////////////
89
90
91 openModemPort();
92 closeModemPort();
93
94
95 if (Common::instance()->isDaemon)
96 {
97 lookup_uid_values();
98 daemonize();
99 cmn->daemonized = true;
100 }
101
102 openModemPort();
103
104 //////////////////////////////////
105
106 SmsDaemon daemon(*transceiver);
107
108 daemon.Start();
109 //returns here when main-loop exits
110
111 if (cmn->isDaemon)
112 daemonCleanup();
113
114 closeModemPort();
115
116 return 0;
117 }

  ViewVC Help
Powered by ViewVC 1.1.20