-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfeed-reader.cc
494 lines (455 loc) · 17.7 KB
/
feed-reader.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <netinet/in.h>
#include <netdb.h>
#include <zmq.h>
#include "schema.pb.h"
#include <unistd.h>
namespace gpb = google::protobuf;
namespace
{
const char* g_appName = NULL;
const char* const DEFAULT_ZMQ_ENDPOINT = "tcp://localhost:7779";
const char* const DEFAULT_ZMQ_SUB_FILTER = "";
void usage()
{
printf("\n");
printf("Usage: %s [options]\n", g_appName);
printf("Options:\n");
printf(" -e <endpoint> ZMQ endpoint to connect/bind to. Default: %s\n", DEFAULT_ZMQ_ENDPOINT);
printf(" -f <filter> Message filter to apply on a ZMQ_SUB socket. Default: %s\n",
strlen(DEFAULT_ZMQ_SUB_FILTER) ? DEFAULT_ZMQ_SUB_FILTER : "<empty>");
printf(" -b Listen for ZMQ endpoint on port 7779. Default: connect\n");
printf("\n");
exit(1);
}
std::ostream& printIndent(std::ostream& os, int indent)
{
for (int i = 0; i < indent; ++i)
os << " ";
return os;
}
std::ostream& printIpAddress(std::ostream& os, const ce::nbapi::ip_address& msg, int indent)
{
static const size_t IPV6_SIZE = (sizeof(uint8_t) * 16);
static const size_t IPV4_SIZE = sizeof(uint32_t);
if (msg.has_af())
{
ce::nbapi::ip_address::addr_family af = msg.af();
printIndent(os, indent); os << "af: " << msg.addr_family_Name(af) << "\n";
if (msg.has_addr())
{
const std::string& addrTmp = msg.addr();
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
socklen_t salen;
int error = -1;
char nameInfo[80];
if (af == ce::nbapi::ip_address::ADDR_FAMILY_INET6)
{
memset(&sin6, 0, sizeof(struct sockaddr_in6));
sin6.sin6_family = AF_INET6;
memcpy(sin6.sin6_addr.s6_addr, addrTmp.data(), IPV6_SIZE);
salen = sizeof(struct sockaddr_in6);
error = getnameinfo((struct sockaddr *)&sin6, salen, nameInfo, sizeof(nameInfo),
NULL, 0, NI_NUMERICHOST);
}
else
{
memset(&sin, 0, sizeof (struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr.s_addr, addrTmp.data(), IPV4_SIZE);
salen = sizeof(struct sockaddr_in);
error = getnameinfo((struct sockaddr *)&sin, salen, nameInfo, sizeof(nameInfo),
NULL, 0, NI_NUMERICHOST);
}
if (!error)
printIndent(os, indent); os << "addr: " << nameInfo << "\n";
}
}
return os;
}
std::ostream& printMacAddress(std::ostream& os, const ce::nbapi::mac_address& msg, int indent)
{
if (msg.has_addr())
{
const std::string& addrTmp = msg.addr();
if (addrTmp.size() == 6)
{
const unsigned char* addrData = (const unsigned char*)addrTmp.data();
char macAddStr[18];
sprintf(macAddStr, "%02x:%02x:%02x:%02x:%02x:%02x",
addrData[0], addrData[1], addrData[2],
addrData[3], addrData[4], addrData[5]);
printIndent(os, indent); os << "addr: " << macAddStr << "\n";
}
}
return os;
}
std::ostream& printMessage(std::ostream& os, const gpb::Message& msg, int indent)
{
const gpb::Descriptor* desc = msg.GetDescriptor();
try
{
if (desc == ce::nbapi::ip_address::descriptor())
return printIpAddress(os, dynamic_cast<const ce::nbapi::ip_address&>(msg), indent);
else if (desc == ce::nbapi::mac_address::descriptor())
return printMacAddress(os, dynamic_cast<const ce::nbapi::mac_address&>(msg), indent);
}
catch (const std::bad_cast& e)
{
return os;
}
const gpb::Reflection* refl = msg.GetReflection();
std::vector<const gpb::FieldDescriptor*> fieldDescList;
refl->ListFields(msg, &fieldDescList);
for (size_t i = 0; i < fieldDescList.size(); ++i)
{
const gpb::FieldDescriptor* fieldDesc = fieldDescList[i];
int fieldSize = fieldDesc->is_repeated() ? refl->FieldSize(msg, fieldDesc) : -1;
int k;
switch (fieldDesc->cpp_type())
{
case gpb::FieldDescriptor::CPPTYPE_MESSAGE:
{
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
printIndent(os, indent); os << fieldDesc->name();
os << " {\n";
printMessage(os, refl->GetRepeatedMessage(msg, fieldDesc, k), indent+1);
printIndent(os, indent); os << "}\n";
}
}
else
{
printIndent(os, indent); os << fieldDesc->name();
os << " {\n";
printMessage(os, refl->GetMessage(msg, fieldDesc), indent+1);
printIndent(os, indent); os << "}\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_INT32:
{
int32_t tmpInt32;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpInt32 = refl->GetRepeatedInt32(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpInt32 << "\n";
}
}
else
{
tmpInt32 = refl->GetInt32(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpInt32 << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_INT64:
{
int64_t tmpInt64;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpInt64 = refl->GetRepeatedInt64(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpInt64 << "\n";
}
}
else
{
tmpInt64 = refl->GetInt64(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpInt64 << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_UINT32:
{
uint32_t tmpUInt32;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpUInt32 = refl->GetRepeatedUInt32(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpUInt32 << "\n";
}
}
else
{
tmpUInt32 = refl->GetUInt32(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpUInt32 << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_UINT64:
{
uint64_t tmpUInt64;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpUInt64 = refl->GetRepeatedUInt64(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpUInt64 << "\n";
}
}
else
{
tmpUInt64 = refl->GetUInt64(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpUInt64 << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_DOUBLE:
{
double tmpDouble;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpDouble = refl->GetRepeatedDouble(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpDouble << "\n";
}
}
else
{
tmpDouble = refl->GetDouble(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpDouble << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_FLOAT:
{
float tmpFloat;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpFloat = refl->GetRepeatedFloat(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpFloat << "\n";
}
}
else
{
tmpFloat = refl->GetFloat(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << tmpFloat << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_BOOL:
{
bool tmpBool;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpBool = refl->GetRepeatedBool(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << (tmpBool ? "true" : "false") << "\n";
}
}
else
{
tmpBool = refl->GetBool(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << (tmpBool ? "true" : "false") << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_ENUM:
{
const gpb::EnumValueDescriptor* enumDesc;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
enumDesc = refl->GetRepeatedEnum(msg, fieldDesc, k);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << enumDesc->name() << "\n";
}
}
else
{
enumDesc = refl->GetEnum(msg, fieldDesc);
printIndent(os, indent); os << fieldDesc->name();
os << ": " << enumDesc->name() << "\n";
}
}
break;
case gpb::FieldDescriptor::CPPTYPE_STRING:
{
std::string tmpString;
if (fieldDesc->is_repeated())
{
for (k = 0; k < fieldSize; ++k)
{
tmpString = refl->GetRepeatedStringReference(msg, fieldDesc, k, &tmpString);
printIndent(os, indent); os << fieldDesc->name();
if (fieldDesc->type() == gpb::FieldDescriptor::TYPE_STRING)
os << ": " << tmpString << "\n";
else
{
char* tmpSz = new char[(tmpString.size()*2)+1];
const unsigned char* data = (const unsigned char*)tmpString.data();
for (size_t j=0; j < tmpString.size(); ++j)
sprintf(&tmpSz[j*2], "%02X", data[j]);
os << ": " << tmpSz << "\n";
delete[] tmpSz;
}
}
}
else
{
tmpString = refl->GetStringReference(msg, fieldDesc, &tmpString);
printIndent(os, indent); os << fieldDesc->name();
if (fieldDesc->type() == gpb::FieldDescriptor::TYPE_STRING)
os << ": " << tmpString << "\n";
else
{
char* tmpSz = new char[(tmpString.size()*2)+1];
const unsigned char* data = (const unsigned char*)tmpString.data();
for (size_t j=0; j < tmpString.size(); ++j)
sprintf(&tmpSz[j*2], "%02X", data[j]);
os << ": " << tmpSz << "\n";
delete[] tmpSz;
}
}
}
break;
default:
printIndent(os, indent); os << ": ???\n";
break;
}
}
return os;
}
std::ostream& operator<<(std::ostream& os, const ce::nbapi::nb_event& ev)
{
return printMessage(os, ev, 0);
}
}
int main(int argc, char* argv[])
{
g_appName = argv[0];
std::string endpoint(DEFAULT_ZMQ_ENDPOINT);
std::string filter(DEFAULT_ZMQ_SUB_FILTER);
int c;
bool doBind = false;
while((c = getopt(argc, argv, "hf:e:b")) != -1)
{
switch (c)
{
case 'f':
if (optarg && optarg[0])
filter.assign(optarg);
break;
case 'e':
if (optarg && optarg[0])
endpoint.assign(optarg);
break;
case 'b':
doBind = true;
break;
default:
usage();
break;
};
}
void* ctx = zmq_ctx_new();
if (!ctx)
perror("zmq_ctx_new");
assert(ctx);
void* sub = zmq_socket(ctx, ZMQ_SUB);
if (!sub)
perror("zmq_socket");
assert(sub);
if (doBind)
{
endpoint.assign("tcp://*:7779");
printf("Attempting to 'bind' to endpoint: %s\n", endpoint.c_str());
if (zmq_bind(sub, endpoint.c_str()) != 0)
{
perror("zmq_bind");
assert(0);
}
}
else
{
printf("Attempting to 'connect' to endpoint: %s\n", endpoint.c_str());
if (zmq_connect(sub, endpoint.c_str()) != 0)
{
perror("zmq_connect");
assert(0);
}
}
printf("Connected to endpoint: %s\n", endpoint.c_str());
if (zmq_setsockopt(sub, ZMQ_SUBSCRIBE, filter.c_str(), filter.size()) != 0)
{
perror("zmq_setsockopt");
assert(0);
}
printf("Subscribed to topic: \"%s\"\n", filter.c_str());
zmq_msg_t zmsg;
size_t cnt = 1;
std::string strTopic;
ce::nbapi::nb_event ev;
int rc;
while (true)
{
bool more = true;
size_t partNum = 0;
while (more)
{
zmq_msg_init(&zmsg);
rc = zmq_msg_recv(&zmsg, sub, 0);
more = zmq_msg_more(&zmsg) != 0;
if (rc < 0)
{
perror("zmq_msg_recv");
more = false;
}
else
{
if (partNum == 0)
{
strTopic.assign(static_cast<const char*>(zmq_msg_data(&zmsg)), zmq_msg_size(&zmsg));
printf("[%zu] Recv event with topic \"%s\"\n", cnt++, strTopic.c_str());
}
else
{
ev.Clear();
if (ev.ParseFromArray(zmq_msg_data(&zmsg), zmq_msg_size(&zmsg)))
std::cout << ev << std::endl;
else
printf("Protobuf failed to parse event\n");
}
}
zmq_msg_close(&zmsg);
++partNum;
}
}
std::cout << "Cleaning..." << std::endl;
if (zmq_close(sub) != 0)
perror("zmq_close");
if (zmq_ctx_destroy(ctx) != 0)
perror("zmq_close");
return 0;
}