-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLirLogger.cpp
70 lines (55 loc) · 1.71 KB
/
LirLogger.cpp
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
/*
* LirLogger
*
* A daemon that grabs, encodes, and logs frames in relation
* to environmental sensors.
*
*/
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include "LirCommand.hpp"
volatile sig_atomic_t daemonrunning=1;
void catch_term(int sig){
daemonrunning=0;
return;
}
int main(){
printf("Starting Lir Logger. All subsequent messages will be appended to system log.\n");
openlog("LIRLOGGER",LOG_PID,0);
if(daemon(1,0)){
syslog(LOG_DAEMON|LOG_ERR,"Error becoming daemon. Error: %d Exiting.",errno);
exit(EXIT_FAILURE);
}
syslog(LOG_DAEMON|LOG_INFO,"Daemon running");
// Initialize Lir Command
syslog(LOG_DAEMON|LOG_INFO,"Creating command instance.");
LirCommand* com = LirCommand::Instance();
// Setup SIGTERM Handler
signal(SIGTERM,catch_term);
// Setup ZMQ RESP Server
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://*:5555");
while(daemonrunning){
zmq::message_t requestMessage;
// Receive Request
socket.recv(&requestMessage);
std::string request((char *)requestMessage.data(), requestMessage.size());
std::string response = com->parseCommand(request);
// Send Response
zmq::message_t responseMessage(response.size());
memcpy((void*)responseMessage.data(), response.data(), response.size());
socket.send(responseMessage);
}
// Housekeeping
com->stopLogger(); // Just in case it was left running
syslog(LOG_DAEMON|LOG_INFO,"Daemon Exited Successfully.");
closelog();
exit(EXIT_SUCCESS);
}