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