forked from mannkind/rf24node_msgproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMQPWrapper.cpp
49 lines (38 loc) · 1.22 KB
/
AMQPWrapper.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
#include "AMQPWrapper.h"
AMQPWrapper *cb_obj_wrapper;
AMQPWrapper::AMQPWrapper(std::string _connstr) : amqp(_connstr), exchange(this->amqp.createExchange("RF24NodeEx")), queue(this->amqp.createQueue("RF24Node")) {
cb_obj_wrapper = this;
}
void AMQPWrapper::begin(void) {
printf("Connecting to AMQP\n");
this->exchange->Declare("RF24NodeEx", "topic");
this->queue->Declare();
this->queue->Bind("RF24NodeEx", ".sensornet.in.#");
this->queue->addEvent(AMQP_MESSAGE, AMQPWrapper::amqp_on_message);
}
void AMQPWrapper::end(void) {
}
void AMQPWrapper::loop(void) {
}
void AMQPWrapper::send_message(std::string subject, std::string body) {
this->exchange->Publish(body, subject);
}
void AMQPWrapper::set_on_message_callback(on_msg_cb cb) {
this->cb = cb;
}
void AMQPWrapper::on_disconnect(int mid) {
printf("Disconnecting from AMQP\n");
this->end();
sleep(15);
this->begin();
}
void AMQPWrapper::on_message(AMQPMessage *message) {
uint32_t j = 0;
auto subject = message->getRoutingKey();
auto body = std::string(message->getMessage(&j), j);
this->cb(subject, body);
}
int AMQPWrapper::amqp_on_message(AMQPMessage *message) {
cb_obj_wrapper->on_message(message);
return 0;
}