-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand-listener.cpp
56 lines (43 loc) · 1.15 KB
/
command-listener.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
#include "interface.h"
#include <iostream>
#include <unistd.h>
#include <string>
#include <json/json.h>
using namespace Chaos;
CommandListener::CommandListener() {
const std::string endpoint = "tcp://*:5555";
// initialize the 0MQ context
//zmqpp::context context;
// generate a pull socket
zmqpp::socket_type type = zmqpp::socket_type::reply;
socket = new zmqpp::socket(context, type);
// bind to the socket
socket->bind(endpoint);
}
CommandListener::~CommandListener() {
if(socket != NULL) {
delete socket;
}
}
void CommandListener::addObserver( CommandListenerObserver* observer ) {
this->observer = observer;
}
void CommandListener::doAction() {
//std::cout << "in doAction()" << std::endl;
// receive the message
zmqpp::message message;
// decompose the message
socket->receive(message); // Blocking
std::string text;
message >> text;
//Do some 'work'
//std::this_thread::sleep_for(std::chrono::seconds(1));
//std::cout << "Received: " << text << std::endl;
socket->send( reply.c_str() );
if (observer != NULL) {
observer->newCommand(text);
}
}
void CommandListener::setReply(const std::string& reply) {
this->reply = reply;
}