-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbitreader.cpp
More file actions
461 lines (358 loc) · 13.4 KB
/
rabbitreader.cpp
File metadata and controls
461 lines (358 loc) · 13.4 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* File: rabbitreader.cpp
* Author: max
*
* Created on December 14, 2014, 1:28 AM
*/
#include <string>
#include <amqp_ssl_socket.h>
#include <amqp_framing.h>
#include "reader.hpp"
#include <mongo/bson/bson.h>
#include <mongo/client/dbclient.h>
#include "mongowriter.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/thread.hpp>
extern "C" {
#include <signal.h>
#include <libdaemon/dfork.h>
#include <libdaemon/dlog.h>
#include <libdaemon/dpid.h>
#include <libdaemon/dexec.h>
}
#define FORKDEBUG 1
#ifdef FORKDEBUG
extern "C" const char* pidFileStub(void) {
return "/tmp/reader.pid";
}
#endif
class printAcceptor : public messageAcceptor {
virtual void accept(char *message, int len);
};
void printAcceptor::accept(char *message, int len) {
std::cout << "Message:" << message << std::endl;
}
boost::condition_variable condSignal;
boost::mutex mutSignal;
volatile bool exitFlag=false;
extern "C" void signalHandler(int) {
boost::unique_lock<boost::mutex> lock(mutSignal);
exitFlag=true;
condSignal.notify_all();
}
void waitHandler() {
boost::unique_lock<boost::mutex> lock(mutSignal);
if(exitFlag) return;
condSignal.wait(lock);
}
void installHandler(int signal) {
struct sigaction action;
struct sigaction saveaction;
memset(&action, 0, sizeof(action));
action.sa_handler=signalHandler;
int rcaction=sigaction(signal, &action, &saveaction);
}
struct theDaemonRunner {
AMQPConnector &connector;
volatile bool quit;
theDaemonRunner(
AMQPConnector &connector );
void operator()();
};
theDaemonRunner::theDaemonRunner(
AMQPConnector &amq) : connector(amq), quit(false) {
}
void theDaemonRunner::operator()() {
{ // start processing
daemon_log(LOG_INFO, "Starting connector");
try {
connector.run();
} catch (std::string str) {
daemon_log(LOG_ERR, "Unknown shit in AMQP.");
} catch (...) {
daemon_log(LOG_ERR, "Unknown shit while init.");
}
daemon_log(LOG_INFO, "Exiting connector");
connector.close();
}
quit=true;
}
int consoleRun(AMPQParam &amq, mongoConfig &m) {
{
mongowriter writer(m);
if (writer.ready) {
AMQPConnector connector(&writer, amq);
try {
connector.init();
connector.run();
} catch (std::string str) {
std::cerr << "Error in AMQP connector:" << str << std::endl;
return 5;
} catch (...) {
std::cerr << "Unknown shit while run!" << std::endl;
return 5;
}
//finally
{
connector.close();
}
}
}
return 0;
}
int daemonRun(AMPQParam &amq, mongoConfig &m) {
installHandler(SIGINT);
installHandler(SIGTERM);
installHandler(SIGQUIT);
installHandler(SIGHUP);
{
mongowriter writer(m);
if (!writer.ready) {
daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno));
daemon_retval_send(3);
goto finish;
}
{
AMQPConnector connector(&writer, amq);
try {
connector.init();
#ifndef FORKDEBUG
// Send OK to parent process
daemon_retval_send(0);
#endif
} catch (std::string str) {
daemon_log(LOG_ERR, "Error in AMQP connector: (%s).", str.c_str());
daemon_retval_send(3);
goto finish;
} catch (...) {
daemon_retval_send(3);
daemon_log(LOG_ERR, "Unknown shit while init.");
goto finish;
}
{
theDaemonRunner monitor(connector);
boost::thread monitorTHread(monitor);
waitHandler();
connector.inter();
//
//
//
monitorTHread.join();
}}}
finish: daemon_log(LOG_INFO, "Stopping monitor");
return 0;
}
namespace po = boost::program_options;
void printHelp(po::options_description &desc, const char *argv0) {
std::cout << "Exec: " << argv0
//<< "\n" << descConf
<< "\n" << desc << std::endl;
}
int main(int argc, char **argv) {
#ifdef FORKDEBUG
daemon_pid_file_proc=pidFileStub;
#endif
AMPQParam amq;
//printAcceptor acceptor;
mongoConfig m;
// std::string rexch, rexchtype, rexchkey;
std::string config;
bool daemon = false;
bool fKill = false;
int esetuid = -2;
po::options_description descConf("Config options");
descConf.add_options()
("config,c", po::value<std::string>(&config)->default_value("")->implicit_value("/etc/reader"), "Config file")
("kill,k", po::value<bool>(&fKill)->default_value(false)->implicit_value(true), "Kill instance of daemon process stop if no fork")
("fork,f", po::value<bool>(&daemon)->default_value(false)->implicit_value(true), "Fork new instance of daemon process");
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("qhost,H", po::value<std::string>(&amq.hostname)->default_value("127.0.0.0"), "Rabbit MQ host name")
("qvhost,V", po::value<std::string>(&amq.vhost)->default_value("/"), "Rabbit MQ vhost")
("quser,U", po::value<std::string>(&amq.vhostuser)->default_value("guest"), "Rabbit MQ user")
("qpass,P", po::value<std::string>(&amq.vhostpass)->default_value("guest"), "Rabbit MQ password")
("exch,X", po::value<std::string>(&amq.exchange)->required(), "Rabbit MQ exchange")
("expassive", po::value<bool>(&amq.passive)->default_value(false), "Rabbit MQ exchange is passive")
("exdurable", po::value<bool>(&amq.durable)->default_value(true), "Rabbit MQ exchange is durable")
("port", po::value<int>(&amq.port)->default_value(5672), "Rabbit MQ port to connect")
("exchtype,T", po::value<std::string>(&amq.extype)->default_value("fanout"), "Rabbit MQ exchange type")
("exchkey,K", po::value<std::string>(&amq.bindingkey)->default_value(""), "Rabbit MQ routing key")
("mhost,m", po::value<std::string>(&m.host)->default_value("127.0.0.1"), "MongoDB host")
("mcollection,C", po::value<std::string>(&m.maincol)->required(), "MongoDB collection")
("mdb,D", po::value<std::string>(&m.db)->required(), "MongoDB database")
("muser,u", po::value<std::string>(&m.name)->default_value(""), "MongoDB user")
("mpass,p", po::value<std::string>(&m.pass)->default_value(""), "MongoDB password")
("merr,E", po::value<std::string>(&m.dumpcol)->default_value("errors"), "MongoDB collection for malformed messages")
("fork,f", po::value<bool>(&daemon)->default_value(false)->implicit_value(true), "Fork new instance of daemon process")
("kill,k", po::value<bool>(&fKill)->default_value(false)->implicit_value(true), "Kill running instance of daemon process")
("setuid,s", po::value<int>(&esetuid)->default_value(0)->implicit_value(-2), "Set UID jail to nobody or to particular UID")
("config,c", po::value<std::string>(&config)->default_value("")->implicit_value("/etc/reader"), "Write config file");
const po::positional_options_description p;
if (argc < 2) {
printHelp(desc, argv[0]);
return 1;
}
bool invalidConfigCommanline = true;
try {
po::variables_map vm;
po::variables_map vmc;
try {
po::parsed_options parsedConfig = po::command_line_parser(argc, argv).options(descConf).positional(p).run();
po::store(parsedConfig, vmc);
po::notify(vmc);
if (argc >= 0) // sort of useless as allways true bu here to make "too smart" clion happy
invalidConfigCommanline = false;
} catch (std::exception &e) {
// invalidConfigCommanline=true;
}
// std::vector<std::string> v = po::collect_unrecognized(parsedConfig.options, po::include_positional);
//
if (invalidConfigCommanline) {
po::parsed_options parsed = po::command_line_parser(argc, argv).options(desc).positional(p).run();
po::store(parsed, vm);
if (vm.count("help")) {
printHelp(desc, argv[0]);
return 1;
}
po::notify(vm);
if (!config.empty()) {
std::ofstream fconf(config.c_str());
bool ferr = (fconf.bad() || !fconf.good());
if (ferr) {
std::cout << "Error writing to " << config << std::endl;
std::cout << "----- Please copy data below manualy -----" << std::endl;
} else {
std::cout << "Writing configuration to " << config << std::endl;
}
for (std::vector<po::option>::const_iterator i = parsed.options.begin(); i != parsed.options.end(); ++i) {
const po::option &o = *i;
//if (vm.find(o.string_key) != vm.end()) {
// an unknown option
for (std::vector<std::string>::const_iterator ivalue = o.value.begin(); ivalue != o.value.end(); ++ivalue) {
if (o.string_key != "config") {
std::cout << o.string_key << "=" << *ivalue << std::endl; // o.string_key << "=" << o.value;
if (!ferr) fconf << o.string_key << "=" << *ivalue << "\n";
}
}
// }
}
fconf.close();
return 0;
}
} else {
bool saveDaemon = daemon;
bool saveKill = fKill;
std::ifstream file;
file.open(config.c_str());
po::store(po::parse_config_file(file, desc, true), vm);
po::notify(vm);
daemon |= saveDaemon;
fKill |= saveKill;
}
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
std::cout
//<< descConf << "\n"
<< desc << std::endl;
return 3;
} catch (...) {
std::cerr << "Unknown error!" << "\n";
return 3;
}
if (!daemon && !fKill)
return consoleRun(amq, m);
pid_t pid = 0;
/* Reset signal handlers */
if (daemon_reset_sigs(-1) < 0) {
daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno));
return 1;
}
/* Unblock signals */
if (daemon_unblock_sigs(-1) < 0) {
daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno));
return 1;
}
daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
if (fKill) {
int ret;
// Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it.
if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0) {
daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
}
if (!daemon) return (ret < 0) ? 1 : 0;
}
if ((pid = daemon_pid_file_is_running()) >= 0) {
daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid);
return 1;
}
#ifndef FORKDEBUG
if (daemon_retval_init() < 0) {
daemon_log(LOG_ERR, "Failed to create pipe.");
return 1;
}
pid = daemon_fork();
#else
pid=0;
#endif
if (pid < 0) { // can't fork
daemon_retval_done();
return 1;
} else if (pid) { // parent
int ret;
// Wait for 20 seconds for the return value passed from the daemon process
if ((ret = daemon_retval_wait(20)) < 0) {
daemon_log(LOG_ERR, "Could not recieve return value from daemon process: %s", strerror(errno));
return 255;
}
daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret);
return ret;
} else {
// The daemon
// Close FDs
if (daemon_close_all(-1) < 0) {
daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno));
// Send the error condition to the parent process
daemon_retval_send(1);
goto finish;
}
// Create the PID file
if (daemon_pid_file_create() < 0) {
daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno));
daemon_retval_send(2);
goto finish;
}
// Initialize signal handling
if (esetuid && setuid(esetuid)) {
daemon_log(LOG_ERR, "Could not suid (%s).", strerror(errno));
daemon_retval_send(4);
goto finish;
}
daemonRun(amq, m);
// Do a cleanup
finish:
daemon_log(LOG_INFO, "Exiting...");
daemon_retval_send(255);
daemon_pid_file_remove();
return 0;
}
//Wait-free ring buffer
/*
pthread_mutex_lock(&mymutex);
boost::atomic<message *> exchangePoint;
std::cout << "--- ==== ---" << std::endl;
int rc = pthread_create(&th, NULL, WorkerReader, NULL);
if (rc){
std::cout << "ERROR; return code from pthread_create() is " << rc << std::endl;
exit(-1);
}
pthread_cond_wait(&count_threshold_cv, &mymutex);
pthread_mutex_unlock(&mymutex);
std::cout << "--- World " << std::endl;
return 0;
*
* */
}