Repository navigation
Expand file tree
/
Copy pathDispatchMain.cpp
More file actions
340 lines (300 loc) · 7.47 KB
/
Copy pathDispatchMain.cpp
File metadata and controls
340 lines (300 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "../config/config.h"
#include "EventQueue.h"
#include <string>
#include <vector>
#include "../modules/DispatchModule.h"
#include "../modules/modules.h"
#include "DispatchMain.h"
#include "../util/Functor.h"
#include "../util/FDMonitor.h"
using namespace std;
using namespace dispatch;
using namespace dispatch::module;
using namespace dispatch::util;
namespace dispatch { namespace core {
DispatchMain::DispatchMain(int argc, char* argv[]) :
// NameTimeTaggingOutputSet(cout.rdbuf(), cerr.rdbuf(), clog.rdbuf(), cerr.rdbuf()) ,
queue(0),
dont_detach(false)
{
for(int i = 0; i < argc; i++) {
string str = argv[i];
ARGV.push_back(str); //ing(argv[i]));
}
}
DispatchMain::~DispatchMain() {
if (queue) {
delete queue;
}
}
string DispatchMain::getName() {
return "DispatchMain";
}
EventQueue* DispatchMain::getEventQueue() {
if (!queue) {
queue = new ThreadedEventQueue();
}
return queue;
}
void DispatchMain::initialize() {
SigHandler::instance()->registerListener(SIGTERM, this);
SigHandler::instance()->registerListener(SIGINT, this);
}
void DispatchMain::handleSignal(int sig) {
err() << "DISPATCH: Received signal " << sig << " " << SigHandler::signalName(sig) << endl;
switch(sig) {
case SIGINT:
case SIGTERM:
break;
}
}
void DispatchMain::cleanup() {
}
int DispatchMain::start() {
initialize();
int ret = run();
cleanup();
return ret;
}
/*
class A {
public:
void doThing(bool (*isRunning)()) {
int cnt = 0;
while(isRunning()) {
Thread* tptr = Thread::currentThread();
cerr << "Thread magic: "<< cnt++ << endl;
if (tptr == NULL) {
cerr << "Current thread is not available" << endl;
} else {
cerr << "Is in a different thread. Running:" << tptr->isRunning() << endl;
}
sleep(1);
}
cerr << "Thread har avsluttet" << endl;
}
};
*/
string DispatchMain::getConfigFile() {
return config_file;
}
string DispatchMain::getConfigDir() {
return config_dir;
}
void DispatchMain::cmdLineHelp() {
cout << "usage:" << endl;
cout << " dispatch [options]" << endl;
cout << "options: " << endl;
cout << " -h"<< endl;
cout << " --help" << endl;
cout << " Show this help-screen" << endl;
cout << " -c <base_config_file>" << endl;
cout << " --config <base_config_file>" << endl;
cout << " Load <base_config_file> as det initial configuration" << endl;
cout << " -d <base_config_dir>" << endl;
cout << " --config-dir <base_config_dir>" << endl;
cout << " Set <base_config_dir> as the relative base-path for file-references in config-files" << endl;
cout << " -t" << endl;
cout << " --terminal" << endl;
cout << " Don't detach Dispatch from the terminal" << endl;
cout << "" << endl;
}
void DispatchMain::loadDefaultArguments() {
config_file = "dispatch.conf";
config_dir = "../etc";
}
/**
* @TODO the cmdline argument parser is not a pretty sight.
* there should be a library capable of doing this?
*/
int DispatchMain::parseCommandLineArguments() {
vector<string>::iterator iter;
out() << "Scanning arguments " << endl;
int state = 10; // Firt argument should be the program name, after that we turn to state 0
for(iter = ARGV.begin(); iter != ARGV.end(); iter++) {
string arg = *iter;
dbg() << "State: " << state << ": " << arg << endl;
switch(state) {
case 0: // Not in any multi-arg sequence
if ((arg == "-c") || (arg == "--config-file")) {
state = 1;
continue;
} else if ((arg == "-d") || (arg == "--config-dir")) {
state = 2;
continue;
} else if((arg == "-t") || (arg == "--terminal")) {
dont_detach = true;
continue;
} else if ((arg == "-p") || (arg == "--pidfile")) {
state = 3;
continue;
} if ((arg == "-h") || (arg == "--help")) {
cmdLineHelp();
return 2; // Ikke ugyldig men ikk estart opp
}
break;
case 1: // Excpecting file-name
config_file = arg;
state = 0;
continue;
case 2: // Excepting directory
config_dir = arg;
state = 0;
continue;
case 3: // Pidfile
pid_file = arg;
state = 0;
continue;
case 10: // Program name
program_name = arg;
state = 0;
continue;
} // End switch
return false;
} // end foreach
if (state) {
/**
* Hvis vi har state her, så betyr det at det ble fritt for argumenter mitt i en multisekvens
*/
return 0; // Ugyldig
}
return 1; // Gyldig
}
int DispatchMain::run() {
loadDefaultArguments();
int parse_res = parseCommandLineArguments();
if (!parse_res) {
err() << "Illegal arguments" << endl;
return 2;
}
if (parse_res != 1) {
// Noe annet en 1 skal vi avslutte her. Sannsynligvis --help
return 0;
}
config::parseDriver drv(getConfigFile(), getConfigDir()); //"dispatch.conf", "../etc");
// config::parseDriver drv("dispatch.conf", "../etc");
if (drv.parse()) {
err() << "Bad config" << endl;
return 1;
}
// drv.dump();
initLogging();
out() << "Done parsing" << endl;
// ModuleManager& m = ModuleManager::instance();
ModuleManager::instance().loadModules(drv, modules);
err() << modules.size() << " moduler lastet" << endl;
/**
* Attach to main process-object
*/
for(uint i = 0; i < modules.size(); i++) {
MOD(i)->setQueueProvider(this);
}
/**
* Pre-initialize modules
*/
for(uint i = 0; i < modules.size(); i++) {
if (!MOD(i)->preInitialize()) {
err() << "module "<<MOD(i)->getModuleName() << " failed to pre-initialize" << endl;
} else {
err() << "Pre-initialized module " << MOD(i)->getModuleName() << endl;
}
}
/**
* Load config
*/
for(uint i = 0; i < modules.size(); i++) {
GConfigNodeVisitor* visitor = MOD(i)->getConfigVisitor();
if (visitor != NULL) {
drv.runVisitor(visitor);
delete visitor;
}
}
drv.postCheckConfig();
/**
* Create the eventQueue
*/
// EventQueue::instance();
getEventQueue();
/**
* Start'em'up
*/
for(uint i = 0; i < modules.size(); i++) {
if (!MOD(i)->startup()) {
err() << "module " << MOD(i)->getModuleName() << "failed to start up properly, shutting it down" << endl;
MOD(i)->shutdown();
MOD(i)->started = false;
} else {
MOD(i)->started = true;
err() << "Startet modul " << MOD(i)->getModuleName() << endl;
}
}
if (dont_detach) {
service();
return 0;
}
int pid = fork();
if(pid < 0) {
err() << "Unable to fork process" << endl;
return -1;
}
if (pid) {
err() << "in parent process. Child has " << pid << endl;
return 0;
}
service();
return 0;
}
int DispatchMain::service() {
/**
* Start opp socket-handler-tråd
*/
FDMonitor::instance().start();
/**
* Kickstart everything by fireing up the eventqueue
*/
ThreadFunctor<EventQueue, void*> queue_functor(getEventQueue(), &EventQueue::startListener, &EventQueue::stopListener);
queue_functor.setName("EventQueue");
queue_functor.start();
/**
* Vent på eventuelle signaler
*/
err() << "Idling main thread" << endl;
pause();
err() << "Shutting down main thread" << endl;
shutdown(queue_functor);
return 0;
}
int DispatchMain::shutdown(ThreadFunctor<EventQueue,void*>& queue_functor) {
FDMonitor::instance().stop();
/**
* Steng ned main kø
*/
queue_functor.stop();
/**
* Close up
*/
err() << "Dispatch: " << "Stenger ned" << endl;
for(uint i = 0; i < modules.size(); i++) {
if (!MOD(i)) {
continue;
}
if (MOD(i)->started) {
MOD(i)->shutdown();
}
}
sleep(1);
// for(int i = 0; i < modules.size(); i++) {
// }
for(uint i = 0; i < modules.size(); i++) {
modules[i].destroy(MOD(i));
MOD(i) = NULL;
}
for(uint i = 0; i < modules.size(); i++) {
ModuleManager::instance().unloadModule(modules[i]);
}
// Thread::
return 0;
}
void DispatchMain::initLogging() {
}
}} // end namespace