1 |
torben |
26 |
#include "common.h" |
2 |
|
|
|
3 |
torben |
94 |
|
4 |
|
|
#include "version.h" |
5 |
|
|
|
6 |
torben |
114 |
#include <stdlib.h> |
7 |
torben |
26 |
#include <string> |
8 |
|
|
#include <iostream> |
9 |
|
|
#include <iomanip> |
10 |
|
|
#include <fstream> |
11 |
|
|
#include <sstream> |
12 |
|
|
#include <time.h> |
13 |
|
|
|
14 |
torben |
92 |
#include "TaskManager.h" |
15 |
|
|
#include "PluginManager.h" |
16 |
torben |
146 |
#include "ConfigFile.h" |
17 |
torben |
92 |
|
18 |
torben |
26 |
using namespace std; |
19 |
torben |
109 |
const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
20 |
torben |
26 |
|
21 |
|
|
Common* Common::instance() |
22 |
|
|
{ |
23 |
|
|
static Common store; |
24 |
|
|
return &store; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
|
28 |
torben |
92 |
Common::Common() |
29 |
|
|
: _pluginManager(0), _taskManager(0) |
30 |
|
|
{ |
31 |
|
|
_pluginManager = new PluginManager(); |
32 |
|
|
_taskManager = new TaskManager(); |
33 |
torben |
146 |
configFilePath = "/etc/smsdaemon.conf"; |
34 |
|
|
_configFile = new ConfigFile(); |
35 |
torben |
150 |
|
36 |
|
|
daemonized = false; |
37 |
torben |
92 |
} |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
Common::~Common() |
41 |
|
|
{ |
42 |
|
|
if (_pluginManager) |
43 |
|
|
delete _pluginManager; |
44 |
|
|
|
45 |
|
|
if (_taskManager) |
46 |
|
|
delete _taskManager; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
PluginManager* Common::GetPluginManager() |
50 |
|
|
{ |
51 |
|
|
return _pluginManager; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
TaskManager* Common::GetTaskManager() |
55 |
|
|
{ |
56 |
|
|
return _taskManager; |
57 |
|
|
} |
58 |
|
|
|
59 |
torben |
146 |
ConfigFile* Common::GetConfigfile() |
60 |
|
|
{ |
61 |
|
|
return _configFile; |
62 |
|
|
} |
63 |
|
|
|
64 |
torben |
26 |
void Common::logMessage(string msg) |
65 |
|
|
{ |
66 |
|
|
time_t t = time(0); |
67 |
|
|
tm now; |
68 |
|
|
localtime_r(&t, &now); |
69 |
|
|
|
70 |
|
|
ostringstream out; |
71 |
|
|
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
72 |
|
|
out << " " << setw(2) << setfill('0') << now.tm_hour; |
73 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_min; |
74 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
75 |
|
|
out << " " << msg << endl; |
76 |
|
|
|
77 |
torben |
150 |
if (this->isDaemon && _logFile != "" && daemonized) |
78 |
torben |
26 |
{ |
79 |
|
|
seteuid(0); |
80 |
|
|
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
81 |
|
|
if (of) |
82 |
|
|
{ |
83 |
|
|
of << out.str(); |
84 |
|
|
of.flush(); |
85 |
|
|
of.close(); |
86 |
|
|
} |
87 |
|
|
seteuid( this->uid); |
88 |
|
|
} |
89 |
|
|
else |
90 |
|
|
{ |
91 |
|
|
cout << out.str(); |
92 |
|
|
cout.flush(); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
void Common::printUsage() |
97 |
|
|
{ |
98 |
torben |
106 |
cout << VERSION << endl; |
99 |
|
|
cout << SVNVERSION << endl << endl; |
100 |
torben |
26 |
cout << "Usage --daemon|--debug [arguments]" << endl; |
101 |
|
|
cout << "Commandline arguments :" << endl; |
102 |
torben |
146 |
cout << " --daemon : Run in daemon (background) mode" << endl; |
103 |
|
|
cout << " --debug : Run as normal (frontground) process," << endl; |
104 |
|
|
cout << " with all messages written to std out" << endl; |
105 |
|
|
cout << " --config <file>: Specify which config file to use " << endl; |
106 |
|
|
cout << " default" << configFilePath << endl; |
107 |
|
|
cout << " --help : Show this help screen" << endl; |
108 |
torben |
26 |
|
109 |
|
|
exit(0); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
void Common::loadConfig(int argc, char* argv[]) |
113 |
|
|
{ |
114 |
|
|
isDaemon = false; |
115 |
|
|
isDebug = false; |
116 |
|
|
|
117 |
|
|
for (int i = 1; i< argc; i++) |
118 |
|
|
{ |
119 |
|
|
string current(argv[i]); |
120 |
|
|
if (current == "--daemon") |
121 |
|
|
{ |
122 |
|
|
isDaemon = true; |
123 |
|
|
isDebug = false; |
124 |
|
|
|
125 |
|
|
} |
126 |
|
|
else if (current == "--debug") |
127 |
|
|
{ |
128 |
|
|
isDaemon = false; |
129 |
|
|
isDebug = true; |
130 |
|
|
} |
131 |
torben |
146 |
else if (current == "--config") |
132 |
|
|
{ |
133 |
|
|
} |
134 |
torben |
26 |
else if (current == "-h" || current == "--help") |
135 |
|
|
{ |
136 |
|
|
printUsage(); |
137 |
|
|
} |
138 |
|
|
else |
139 |
|
|
{ |
140 |
torben |
106 |
cout << "Unknown argument : " << current << endl << endl; |
141 |
torben |
26 |
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
if (! ( isDaemon || isDebug)) |
145 |
|
|
printUsage(); |
146 |
|
|
} |
147 |
torben |
36 |
|
148 |
|
|
|
149 |
|
|
std::string Common::getStatusMessage() |
150 |
|
|
{ |
151 |
|
|
long int now = time(0); |
152 |
|
|
|
153 |
|
|
long int diff = now - this->daemonStart; |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
std::ostringstream out; |
157 |
|
|
out << "Uptime " << diff << " seconds. "; |
158 |
|
|
out << "Messages received:" << this->smsCounter.incomming << ". "; |
159 |
|
|
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
160 |
|
|
|
161 |
|
|
return out.str(); |
162 |
|
|
} |