1 |
torben |
158 |
#include "Common.h" |
2 |
torben |
26 |
|
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 <sstream> |
11 |
|
|
#include <time.h> |
12 |
|
|
|
13 |
torben |
92 |
#include "TaskManager.h" |
14 |
|
|
#include "PluginManager.h" |
15 |
torben |
146 |
#include "ConfigFile.h" |
16 |
torben |
216 |
#include "Util.h" |
17 |
|
|
#include "Logger.h" |
18 |
torben |
92 |
|
19 |
torben |
26 |
using namespace std; |
20 |
|
|
|
21 |
|
|
Common* Common::instance() |
22 |
|
|
{ |
23 |
|
|
static Common store; |
24 |
|
|
return &store; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
|
28 |
torben |
92 |
Common::Common() |
29 |
torben |
196 |
: _pluginManager(0), _taskManager(0) |
30 |
torben |
92 |
{ |
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 |
208 |
reloadConfig = false; |
38 |
torben |
92 |
} |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
Common::~Common() |
42 |
|
|
{ |
43 |
|
|
if (_pluginManager) |
44 |
|
|
delete _pluginManager; |
45 |
|
|
|
46 |
|
|
if (_taskManager) |
47 |
|
|
delete _taskManager; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
PluginManager* Common::GetPluginManager() |
51 |
|
|
{ |
52 |
|
|
return _pluginManager; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
TaskManager* Common::GetTaskManager() |
56 |
|
|
{ |
57 |
|
|
return _taskManager; |
58 |
|
|
} |
59 |
|
|
|
60 |
torben |
146 |
ConfigFile* Common::GetConfigfile() |
61 |
|
|
{ |
62 |
|
|
return _configFile; |
63 |
|
|
} |
64 |
|
|
|
65 |
torben |
26 |
|
66 |
|
|
void Common::printUsage() |
67 |
|
|
{ |
68 |
torben |
106 |
cout << VERSION << endl; |
69 |
|
|
cout << SVNVERSION << endl << endl; |
70 |
torben |
26 |
cout << "Usage --daemon|--debug [arguments]" << endl; |
71 |
|
|
cout << "Commandline arguments :" << endl; |
72 |
torben |
146 |
cout << " --daemon : Run in daemon (background) mode" << endl; |
73 |
|
|
cout << " --debug : Run as normal (frontground) process," << endl; |
74 |
|
|
cout << " with all messages written to std out" << endl; |
75 |
|
|
cout << " --config <file>: Specify which config file to use " << endl; |
76 |
torben |
196 |
cout << " default" << configFilePath << endl; |
77 |
torben |
146 |
cout << " --help : Show this help screen" << endl; |
78 |
torben |
26 |
|
79 |
|
|
exit(0); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
void Common::loadConfig(int argc, char* argv[]) |
83 |
|
|
{ |
84 |
|
|
isDaemon = false; |
85 |
|
|
isDebug = false; |
86 |
|
|
|
87 |
|
|
for (int i = 1; i< argc; i++) |
88 |
|
|
{ |
89 |
|
|
string current(argv[i]); |
90 |
torben |
196 |
if (current == "--daemon") |
91 |
torben |
26 |
{ |
92 |
torben |
196 |
isDaemon = true; |
93 |
torben |
26 |
isDebug = false; |
94 |
torben |
196 |
|
95 |
torben |
26 |
} |
96 |
|
|
else if (current == "--debug") |
97 |
|
|
{ |
98 |
|
|
isDaemon = false; |
99 |
|
|
isDebug = true; |
100 |
|
|
} |
101 |
torben |
146 |
else if (current == "--config") |
102 |
|
|
{ |
103 |
torben |
175 |
i++; |
104 |
|
|
if ( i<argc) |
105 |
torben |
196 |
{ |
106 |
torben |
175 |
configFilePath = argv[i]; |
107 |
|
|
} |
108 |
|
|
else |
109 |
|
|
{ |
110 |
|
|
printUsage(); |
111 |
|
|
exit(1); |
112 |
|
|
} |
113 |
torben |
196 |
|
114 |
torben |
146 |
} |
115 |
torben |
26 |
else if (current == "-h" || current == "--help") |
116 |
|
|
{ |
117 |
|
|
printUsage(); |
118 |
|
|
} |
119 |
|
|
else |
120 |
|
|
{ |
121 |
torben |
106 |
cout << "Unknown argument : " << current << endl << endl; |
122 |
torben |
26 |
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
if (! ( isDaemon || isDebug)) |
126 |
|
|
printUsage(); |
127 |
|
|
} |
128 |
torben |
36 |
|
129 |
|
|
|
130 |
|
|
std::string Common::getStatusMessage() |
131 |
|
|
{ |
132 |
|
|
long int now = time(0); |
133 |
|
|
|
134 |
|
|
long int diff = now - this->daemonStart; |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
std::ostringstream out; |
138 |
|
|
out << "Uptime " << diff << " seconds. "; |
139 |
|
|
out << "Messages received:" << this->smsCounter.incomming << ". "; |
140 |
|
|
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
141 |
torben |
196 |
|
142 |
torben |
36 |
return out.str(); |
143 |
|
|
} |