-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmqService.cpp
434 lines (383 loc) · 13.2 KB
/
zmqService.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <curl/curl.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <zmq.hpp>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/time.h>
#include <fstream>
#include <unistd.h>
#include <chrono> // timeout
int NUM = 1000;
namespace zmq
{
class Monitor : public monitor_t
{
public:
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_connected;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_connect_delayed;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_connect_retried;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_listening;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_bind_failed;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_accepted;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_accept_failed;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_closed;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_close_failed;
std::function<void(const zmq_event_t &event_, const char *addr_)> C_on_event_disconnected;
public:
virtual void on_monitor_started()
{
}
virtual void on_event_connected(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_connected)
{
C_on_event_connected(event_, addr_);
}
}
virtual void on_event_connect_delayed(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_connect_delayed)
{
C_on_event_connect_delayed(event_, addr_);
}
}
virtual void on_event_connect_retried(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_connect_retried)
{
C_on_event_connect_retried(event_, addr_);
}
}
virtual void on_event_listening(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_listening)
{
C_on_event_listening(event_, addr_);
}
}
virtual void on_event_bind_failed(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_bind_failed)
{
C_on_event_bind_failed(event_, addr_);
}
}
virtual void on_event_accepted(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_accepted)
{
C_on_event_accepted(event_, addr_);
}
}
virtual void on_event_accept_failed(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_accept_failed)
{
C_on_event_accept_failed(event_, addr_);
}
}
virtual void on_event_closed(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_closed)
{
C_on_event_closed(event_, addr_);
}
}
virtual void on_event_close_failed(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_close_failed)
{
C_on_event_close_failed(event_, addr_);
}
}
virtual void on_event_disconnected(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
if (C_on_event_disconnected)
{
C_on_event_disconnected(event_, addr_);
}
}
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
virtual void on_event_handshake_failed_no_detail(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
virtual void on_event_handshake_failed_protocol(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
virtual void on_event_handshake_failed_auth(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
virtual void on_event_handshake_succeeded(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
#elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
virtual void on_event_handshake_failed(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
virtual void on_event_handshake_succeed(const zmq_event_t &event_,
const char *addr_)
{
(void)event_;
(void)addr_;
}
#endif
virtual void on_event_unknown(const zmq_event_t &event_, const char *addr_)
{
(void)event_;
(void)addr_;
}
};
}; // namespace zmq
struct ChronoScope
{
std::chrono::system_clock::time_point start;
std::string strTag;
ChronoScope(const std::string &strFunctionName)
{
start = std::chrono::system_clock::now();
strTag = strFunctionName;
}
~ChronoScope()
{
std::stringstream threadID;
threadID << std::this_thread::get_id();
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::cout << "[" << threadID.str() << "]"
<< "[" << strTag << "]"
<< "ChronoScope: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
<< "us." << std::endl;
std::cout << "timestamp is : " << std::chrono::duration_cast<std::chrono::milliseconds>(end.time_since_epoch()).count() << std::endl;
}
};
std::string strEnvironmenttr;
using namespace zmq;
void ReadCommand(
const int argc,
char **argv)
{
int32_t ch;
while ((ch = getopt(argc, argv, "e:n:")) != -1)
{
switch (ch)
{
case 'e':
strEnvironmenttr = optarg;
break;
case 'n':
NUM = atoi(optarg);
break;
default:
break;
}
}
}
static void C_on_event_connected(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_connect_delayed(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_connect_retried(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_listening(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_bind_failed(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_accepted(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_accept_failed(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_closed(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_close_failed(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
static void C_on_event_disconnected(const zmq_event_t &event_, const char *addr_)
{
std::cout << __FUNCTION__ << addr_ <<std::endl;
}
std::string strJson = R"({})";
static void monitor(zmq::socket_t& sockethandle)
{
zmq::Monitor CSMonitor;
CSMonitor.C_on_event_connected = C_on_event_connected;
CSMonitor.C_on_event_connect_delayed = C_on_event_connect_delayed;
CSMonitor.C_on_event_connect_retried = C_on_event_connect_retried;
CSMonitor.C_on_event_listening = C_on_event_listening;
CSMonitor.C_on_event_bind_failed = C_on_event_bind_failed;
CSMonitor.C_on_event_accepted = C_on_event_accepted;
CSMonitor.C_on_event_accept_failed = C_on_event_accept_failed;
CSMonitor.C_on_event_closed = C_on_event_closed;
CSMonitor.C_on_event_close_failed = C_on_event_close_failed;
CSMonitor.C_on_event_disconnected = C_on_event_disconnected;
CSMonitor.monitor(sockethandle,"inproc://monitor-pub");
}
void pub()
{
try
{
/* code */
int send_hwm_ = NUM;
zmq::context_t context;
zmq::socket_t backend(context, socket_type::push);
//std::thread monitorfunc(monitor,std::ref(backend));
backend.bind("tcp://10.254.0.42:10001");
backend.set(zmq::sockopt::sndhwm, send_hwm_);
backend.set(zmq::sockopt::connect_timeout, 1000);
zmq::socket_t syncbackend(context, socket_type::req);
syncbackend.bind("tcp://10.254.0.42:10002");
ChronoScope(__FUNCTION__);
const_buffer syncsub("ack", 3);
syncbackend.send(syncsub);
zmq::message_t syncmessage;
recv_result_t result = syncbackend.recv(syncmessage);
if (result.has_value())
{
std::cout << "sync message : " << syncmessage.to_string() << std::endl;
}
for (uint32_t i = 0; i < NUM; i++)
{
std::string SendMsg = std::string(strJson) + std::to_string(i);
zmq::message_t message(SendMsg.begin(), SendMsg.end());
send_result_t resut = backend.send(message,send_flags::none);
if(resut.has_value())
{
std::cout << "resut: " << resut.value() << std::endl;
}
}
//monitorfunc.join();
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}
void sub()
{
int recv_hwm_ = NUM;
zmq::context_t context;
zmq::socket_t frontend(context, socket_type::pull);
//std::thread monitorfunc(monitor,std::ref(frontend));
frontend.connect("tcp://10.254.0.42:10001");
frontend.set(zmq::sockopt::rcvhwm, recv_hwm_);
frontend.set(zmq::sockopt::connect_timeout, 1000);
uint32_t count = 0;
ChronoScope(__FUNCTION__);
zmq::socket_t syncfrontend(context, socket_type::rep);
syncfrontend.connect("tcp://10.254.0.42:10002");
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
bool sendFlag = false;
if (sendFlag == false)
{
zmq::message_t syncmessage;
recv_result_t syncresult = syncfrontend.recv(syncmessage);
if (syncresult.has_value())
{
std::cout << "syncfrontend result : " << syncmessage.to_string() << std::endl;
zmq::message_t ackmessage("ack", 3);
syncfrontend.send(ackmessage, send_flags::none);
sendFlag = true;
std::cout << "sendFlag is :" << sendFlag << std::endl;
}
}
for (;;)
{
//if(count % 1000 == 0 && sendFlag == false)
zmq::message_t message;
recv_result_t result = frontend.recv(message);
if (result.has_value())
{
//std::cout << message.to_string() << std::endl;
count++;
std::cout << "count is: " << count << std::endl;
if(count == 1)
{
start = std::chrono::system_clock::now();
}
if (count == NUM)
{
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::cout << "[" << __FUNCTION__ << "]"
<< "ChronoScope: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< "ms." << std::endl;
break;
}
}
else
{
continue;
}
}
//monitorfunc.join();
}
int main(int argc, char **argv)
{
ReadCommand(argc, argv);
if (strEnvironmenttr == "pub")
{
pub();
}
else if (strEnvironmenttr == "sub")
{
sub();
}
return 0;
}