-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmq_test.cpp
95 lines (73 loc) · 2.47 KB
/
mmq_test.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
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
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "mmq.h"
#include "stdio.h"
void sleep(int ms) {
#ifdef WIN32
Sleep(ms);
#else
usleep(ms*1000);
#endif
}
int main(int argc, char* argv[]) {
//In this example we create 4 peers
// 1 host peer, 1 sending peer and 2 receiving peers
mmq_peer host, sender, receiver1, receiver2;
host = mmq_peer_create();
sender = mmq_peer_create();
receiver1 = mmq_peer_create();
receiver2 = mmq_peer_create();
mmq_peer_connect(host, "tcp://127.0.0.1:8888", MMQ_PEER_FLAG_HOST);
mmq_peer_connect(receiver1, "tcp://127.0.0.1:8888", 0);
mmq_peer_connect(receiver2, "tcp://127.0.0.1:8888", 0);
mmq_peer_connect(sender, "tcp://127.0.0.1:8888", 0);
//subscribe receivers to topic "foo"
mmq_peer_subscribe(receiver1, "foo");
mmq_peer_subscribe(receiver2, "foo");
//wait for the host and receiver peer connection to establish
while (mmq_peer_get_status(receiver1) != MMQ_PEER_STATUS_CONNECTED
|| mmq_peer_get_status(receiver2) != MMQ_PEER_STATUS_CONNECTED) {
mmq_peer_run(host);
mmq_peer_run(receiver1);
mmq_peer_run(receiver2);
printf("connecting...\n");
sleep(100);
}
printf("connected\n");
//connect the sender and publish a message to the "foo" topic
mmq_msg msg1 = mmq_msg_create("hello world!", 26);
mmq_peer_publish(sender, "foo", msg1);
int message_count = 0;
while (message_count <= 1) {
//message loop
mmq_msg msg = mmq_peer_get_msg(receiver1);
while (msg) {
message_count++;
printf("receiver1 got message: %s\n", mmq_msg_get_data(msg));
mmq_msg_close(&msg);
msg = mmq_peer_get_msg(receiver1);
}
msg = mmq_peer_get_msg(receiver2);
while (msg) {
message_count++;
printf("receiver2 got message: %s\n", mmq_msg_get_data(msg));
mmq_msg_close(&msg);
msg = mmq_peer_get_msg(receiver2);
}
mmq_peer_run(host);
mmq_peer_run(sender);
mmq_peer_run(receiver1);
mmq_peer_run(receiver2);
sleep(100);
printf("loop...\n");
}
//cleanup and close peer connections
mmq_peer_close(&host);
mmq_peer_close(&sender);
mmq_peer_close(&receiver1);
mmq_peer_close(&receiver2);
return 0;
}