-
Notifications
You must be signed in to change notification settings - Fork 1
/
test1.cc
119 lines (90 loc) · 3.22 KB
/
test1.cc
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
#include <iostream>
#include "src/event_loop.h"
#include "src/timer.h"
#include "src/subscriber.h"
#include "src/publisher.h"
#include "src/replier.h"
#include "src/requester.h"
#include "src/udp_source.h"
using namespace std::placeholders;
class TestNode : public EventLoop {
public:
TestNode() :
timer(this),
sub(this),
pub("ipc:///tmp/testsocket"),
replier(this),
requester(this, "ipc:///tmp/repliersocket"),
udp_source(this),
testsub(this)
{
}
~TestNode()
{
}
void on_timeout() {
// return;
std::cout << "timeout callback" << std::endl;
std::string message = "Publishing on timer";
pub.publish(message.c_str(), message.length());
std::string request = "Requestion on timer";
requester.request(request);
std::cout << "Send heartbeat" << std::endl;
std::string hb = "HB";
udp_source.send_packet(hb);
}
void sub_callback(const std::vector<char> message) {
std::string received_string(message.data(), message.size());
std::cout << "Subscriber received: " << received_string << std::endl;
}
void replier_callback(const std::vector<char> message, std::vector<char> &reply) {
std::string received_string(message.data(), message.size());
std::cout << "Replier received: " << received_string << std::endl;
std::string reply_string = "request was received!";
reply.assign(reply_string.begin(), reply_string.end());
}
void requester_callback(const std::vector<char> reply) {
std::string received_string(reply.data(), reply.size());
std::cout << "Requester received: " << received_string << std::endl;
}
void udp_callback(const std::vector<char> message) {
std::cout << "UDP callback" << std::endl;
}
void testsub_callback(const std::vector<char> message) {
std::string received_string(message.data(), message.size());
std::cout << received_string << std::endl;
}
int init()
{
std::cout << "Node init" << std::endl;
// bind timer to callback
timer.set_timeout_callback(std::bind(&TestNode::on_timeout, this));
timer.start_periodic(1500);
sub.set_receive_callback(std::bind(&TestNode::sub_callback, this, _1));
sub.subscribe("ipc:///tmp/testsocket");
replier.set_receive_callback(std::bind(&TestNode::replier_callback, this, _1, _2));
replier.listen("ipc:///tmp/repliersocket");
requester.set_receive_callback(std::bind(&TestNode::requester_callback, this, _1));
udp_source.set_receive_callback(std::bind(&TestNode::udp_callback, this, _1));
udp_source.connect("127.0.0.1", 14557, 14551);
testsub.set_receive_callback(std::bind(&TestNode::testsub_callback, this, _1));
testsub.subscribe("tcp://127.0.0.1:12345");
return 0;
}
private:
Timer timer;
Subscriber sub;
Publisher pub;
Replier replier;
Requester requester;
UdpSource udp_source;
Subscriber testsub;
};
int main(int argc, char* argv[])
{
std::cout << "Start test program" << std::endl;
TestNode node;
node.init();
node.run();
return 0;
}