-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanomsg_estp.c
801 lines (703 loc) · 22.9 KB
/
nanomsg_estp.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/**
* collectd - src/nanomsg_estp.c
* Copyright (C) 2005-2010 Florian octo Forster
* Copyright (C) 2009 Aman Gupta
* Copyright (C) 2010 Julien Ammous
* Copyright (C) 2012 Paul Colomiets
* Copyright (C) 2013 Insollo Entertainment, LLC
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Florian octo Forster <octo at verplant.org>
* Aman Gupta <aman at tmm1.net>
* Julien Ammous
**/
#define _GNU_SOURCE
#include "config.h"
#include "collectd.h"
#include "common.h" /* auxiliary functions */
#include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
#include "utils_avltree.h"
#include "network.h"
/* for htons() */
#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include <pthread.h>
#include <alloca.h>
#include <time.h>
#include <stdio.h>
#include <nanomsg/nn.h>
#include <nanomsg/pubsub.h>
#include <nanomsg/pipeline.h>
#ifndef TIME_T_TO_CDTIME_T
/* Older versions with do not have CDTIME_T just use time_t */
#define TIME_T_TO_CDTIME_T(tm) (tm)
#define CDTIME_T_TO_TIME_T(tm) (tm)
#endif
struct cmq_socket_s {
int socket;
};
typedef struct cmq_socket_s cmq_socket_t;
struct cmq_cache_entry_s {
char *fullname;
char *timestamp;
char *type;
char *items;
int nitems;
unsigned long mask;
value_t values[];
};
typedef struct cmq_cache_entry_s cmq_cache_entry_t;
static c_avl_tree_t *staging = NULL;
static pthread_t *receive_thread_ids = NULL;
static size_t receive_thread_num = 0;
static int sending_sockets_num = 0;
static pthread_mutex_t staging_mutex;
// private data
static int thread_running = 0;
static void cmq_close_callback (void *value) /* {{{ */
{
cmq_socket_t *val = value;
if (val->socket >= 0)
(void) nn_close (val->socket);
free(value);
} /* }}} void cmq_close_callback */
static void dispatch_entry(char *fullname, char *timestamp, time_t interval,
char *vtype, int values_len, value_t *values) {
struct tm timest;
value_list_t vl;
strcpy(vl.type, vtype);
if (!strptime (timestamp, "%Y-%m-%dT%H:%M:%SZ", ×t)) {
WARNING("Nanomsg-ESTP: can't parse timestamp");
return;
}
// Hostname
char *cpos = fullname;
char *end = strchr(cpos, ':');
if(!end) {
WARNING("Nanomsg-ESTP: No delimiter after hostname");
return;
}
if(end - cpos > 63) {
WARNING("Nanomsg-ESTP: Too long hostname");
return;
}
memcpy(vl.host, cpos, end-cpos);
vl.host[end-cpos] = 0;
// Plugin name
cpos = end+1;
end = strchr(cpos, ':');
if(!end) {
WARNING("Nanomsg-ESTP: No delimiter after application/subsystem name");
return;
}
if(end - cpos > 63) {
WARNING("Nanomsg-ESTP: Too long application/subsystem name");
return;
}
memcpy(vl.plugin, cpos, end-cpos);
vl.plugin[end-cpos] = 0;
// Plugin instance
cpos = end+1;
end = strchr(cpos, ':');
if(!end) {
WARNING("Nanomsg-ESTP: No delimiter after resource name");
return;
}
if(end - cpos > 63) {
WARNING("Nanomsg-ESTP: Too long resource name");
return;
}
memcpy(vl.plugin_instance, cpos, end-cpos);
vl.plugin_instance[end-cpos] = 0;
// Type instance
cpos = end+1;
end = strchr(cpos, ':');
if(!end) {
WARNING("Nanomsg-ESTP: No delimiter after metric name");
return;
}
if(end - cpos > 63) {
WARNING("Nanomsg-ESTP: Too long metric name");
return;
}
memcpy(vl.type_instance, cpos, end-cpos);
vl.type_instance[end-cpos] = 0;
vl.time = TIME_T_TO_CDTIME_T(timegm(×t));
vl.interval = TIME_T_TO_CDTIME_T(interval);
vl.values = values;
vl.values_len = values_len;
vl.meta = NULL;
plugin_dispatch_values (&vl);
}
static char *find_suffix(char *fullname) {
char *endsuffix = fullname + strlen(fullname)-1;
if(*endsuffix != ':') {
WARNING("Nanomsg-ESTP: Metric full name not ends with ':'");
return NULL;
}
char *suffix = endsuffix-1;
while(suffix > fullname) {
if(*suffix == '.' || *suffix == ':') {
return suffix+1;
}
--suffix;
}
return NULL;
}
static int find_field(char *suffix, char *items) {
int suffixlen = strlen(suffix) - 1 /* colon at the end */;
int idx = 0;
if(!strncmp(suffix, items, suffixlen)
&& (items[suffixlen] == 0 || items[suffixlen] == ',')) {
return 0;
}
char *c;
for(c = items; *c; ++c) {
if(*c == ',') {
c += 1;
idx += 1;
if(!strncmp(suffix, c, suffixlen)
&& (c[suffixlen] == 0 || c[suffixlen] == ',')) {
return idx;
}
}
}
WARNING("Nanomsg-ESTP: Can't find suffix in list of suffixes");
return -1;
}
static void dispatch_multi_entry (char *fullname, char *timestamp,
time_t interval,
char *type, char *items,
value_t value) {
char *suffix = find_suffix(fullname);
if(!suffix)
return;
int myindex = find_field(suffix, items);
if(myindex < 0)
return;
int clen = suffix-fullname+2;
char cutname[clen];
memcpy(cutname, fullname, clen-2);
cutname[clen-2] = ':';
cutname[clen-1] = 0;
cmq_cache_entry_t *entry;
int rc = c_avl_get (staging, cutname, (void *)&entry);
if(rc == 0) {
if(!strcmp(entry->timestamp, timestamp)
&& !strcmp(entry->type, type)
&& !strcmp(entry->items, items)) {
entry->values[myindex] = value;
entry->mask |= 1 << myindex;
if(entry->mask == (1 << entry->nitems)-1) {
dispatch_entry(cutname, timestamp, interval, type,
entry->nitems, entry->values);
c_avl_remove(staging, cutname, NULL, NULL);
free(entry);
}
return;
} else {
INFO("Nanomsg-ESTP: Clearing cache entry as it is stale\n");
c_avl_remove(staging, cutname, NULL, NULL);
free(entry);
}
}
// Ok, let's create new entry
int tlen = strlen(timestamp) + 1;
int ylen = strlen(type) + 1;
int ilen = strlen(items) + 1;
int nitems = 1;
char *c;
for(c = items; *c; ++c)
if(*c == ',')
nitems += 1;
entry = malloc(sizeof(cmq_cache_entry_t)
+ nitems*sizeof(value_t)
+ clen + tlen + ylen + ilen);
if(!entry) {
WARNING("Not enough memory for new entry");
return;
}
char *curbuf = ((char *)entry) + sizeof(cmq_cache_entry_t)
+ nitems*sizeof(value_t);
memcpy(curbuf, cutname, clen);
entry->fullname = curbuf;
curbuf += clen;
memcpy(curbuf, timestamp, tlen);
entry->timestamp = curbuf;
curbuf += tlen;
memcpy(curbuf, type, ylen);
entry->type = curbuf;
curbuf += ylen;
memcpy(curbuf, items, ilen);
entry->items = curbuf;
curbuf += ilen;
entry->nitems = nitems;
entry->mask = 1 << myindex;
entry->values[myindex] = value;
c_avl_insert(staging, entry->fullname, entry);
}
static void parse_message (char *data, int dlen)
{
char fullname[300];
char timestamp[32];
unsigned long interval;
char value[64];
char *endline = memchr (data, '\n', dlen);
int headlen = dlen;
if(endline)
headlen = endline - data;
char *hdata = alloca(headlen+1);
memcpy(hdata, data, headlen);
hdata[headlen] = 0;
int rc = sscanf (hdata, "ESTP:%300s %31s %lu %63s",
fullname, timestamp, &interval, value);
if (rc != 4) {
WARNING("Nanomsg-ESTP: message has wrong format");
return;
}
int vdouble = 0;
double dvalue;
char *end;
long lvalue = strtol (value, &end, 10);
if(*end == '.') {
dvalue = strtod(value, &end);
vdouble = 1;
}
if(end == hdata) {
WARNING("Nanomsg-ESTP: wrong value");
return;
}
char vtype[64];
value_t val;
// Parse type
if(*end == ':') {
++end;
if(*end == 'd') {
strcpy(vtype, "derive"); // FIXME: derive can be negative
if(vdouble) {
val.derive = dvalue;
} else {
val.derive = lvalue;
}
} else if(*end == 'c') {
strcpy(vtype, "derive"); // non-wrapping counter
if(vdouble) {
val.counter = dvalue;
} else {
val.counter = lvalue;
}
} else if(*end == 'a') {
strcpy(vtype, "absolute");
if(vdouble) {
val.absolute = dvalue;
} else {
val.absolute = lvalue;
}
} else {
WARNING("Nanomsg-ESTP: Unknown type");
return;
}
} else {
strcpy(vtype, "gauge");
if(vdouble) {
val.gauge = dvalue;
} else {
val.gauge = lvalue;
}
}
// Let's find extension
if(endline) { // there is extension data
char *ext = memmem(endline, dlen - headlen, "\n :collectd:", 12);
if(ext) { // collectd extension found
ext += 12;
char *endext = memchr(ext, '\n', data + dlen - ext);
int elen;
if(!endext)
elen = data + dlen - ext;
else
elen = endext - ext;
char *fullext = alloca(elen+1);
memcpy(fullext, ext, elen);
fullext[elen] = 0;
while (42) {
char ekey[24];
char evalue[elen];
int chars;
if(sscanf(fullext, " %23[^=]=%s%n", ekey, evalue, &chars) < 2)
break;
fullext += chars;
if(!strcmp(ekey, "type")) {
strncpy(vtype, evalue, 64);
} else if(!strcmp(ekey, "items")) {
pthread_mutex_lock(&staging_mutex);
dispatch_multi_entry(fullname, timestamp, interval,
vtype, evalue, val);
pthread_mutex_unlock(&staging_mutex);
return;
}
}
}
}
dispatch_entry(fullname, timestamp, interval, vtype, 1, &val);
}
static void *receive_thread (void *sock) /* {{{ */
{
int rc;
char *buf;
int cmq_socket = (long)sock;
assert (cmq_socket >= 0);
thread_running = 1;
while (thread_running) {
rc = nn_recv (cmq_socket, &buf, NN_MSG, /* flags = */ 0);
if (rc < 0) {
if ((errno == EAGAIN) || (errno == EINTR))
continue;
ERROR ("nanomsg plugin: nn_recv failed: %s", nn_strerror (errno));
break;
}
parse_message (buf, rc);
DEBUG("nanomsg plugin: received data, parse returned %d", rc);
(void) nn_freemsg (buf);
} /* while (thread_running) */
DEBUG ("nanomsg plugin: Receive thread is terminating.");
(void) nn_close (cmq_socket);
return (NULL);
} /* }}} void *receive_thread */
#define PACKET_SIZE 512
static int put_single_value (cmq_socket_t *sockstr,
const char *name, value_t value,
const value_list_t *vl, const data_source_t *ds,
char *extdata)
{
int datalen;
char data[640];
char tstring[32];
time_t timeval = CDTIME_T_TO_TIME_T(vl->time);
unsigned interval = CDTIME_T_TO_TIME_T(vl->interval);
struct tm tstruct;
gmtime_r(&timeval, &tstruct);
strftime(tstring, 32, "%Y-%m-%dT%H:%M:%SZ", &tstruct);
if(ds->type == DS_TYPE_COUNTER) {
datalen = snprintf(data, 640, "ESTP:%s:%s:%s:%s: %s %d %llu:c%s",
vl->host, vl->plugin, vl->plugin_instance, name,
tstring, interval, value.counter, extdata);
} else if(ds->type == DS_TYPE_GAUGE) {
datalen = snprintf(data, 640, "ESTP:%s:%s:%s:%s: %s %d %lf%s",
vl->host, vl->plugin, vl->plugin_instance, name,
tstring, interval, value.gauge, extdata);
} else if(ds->type == DS_TYPE_DERIVE) {
datalen = snprintf(data, 640, "ESTP:%s:%s:%s:%s: %s %d %ld:d%s",
vl->host, vl->plugin, vl->plugin_instance, name,
tstring, interval, value.derive, extdata);
} else if(ds->type == DS_TYPE_ABSOLUTE) {
datalen = snprintf(data, 640, "ESTP:%s:%s:%s:%s: %s %d %lu:a%s",
vl->host, vl->plugin, vl->plugin_instance, name,
tstring, interval, value.absolute, extdata);
} else {
WARNING("nanomsg: Unknown type");
return -1;
}
int rc = nn_send(sockstr->socket, data, datalen, NN_DONTWAIT);
if(rc < 0) {
if(errno == EAGAIN) {
WARNING("nanomsg: Unable to queue message, queue may be full");
return -1;
} else {
ERROR("nanomsg: nn_send failed: %s", nn_strerror(errno));
return -1;
}
}
DEBUG("nanomsg: data sent");
return 0;
}
static int write_value (const data_set_t *ds, /* {{{ */
const value_list_t *vl,
user_data_t *user_data)
{
assert (vl->values_len == ds->ds_num);
if(ds->ds_num > 1) {
int i;
int elen = 100; // prefix and type
for(i = 0; i < ds->ds_num; ++i)
elen += strlen(ds->ds[i].name) + 1;
char *extdata = alloca(elen);
char *cur = extdata + sprintf(extdata,
"\n :collectd: type=%s items=%s",
ds->type, ds->ds[0].name);
for(i = 1; i < ds->ds_num; ++i) {
*cur++ = ',';
int len = strlen(ds->ds[i].name);
memcpy(cur, ds->ds[i].name, len);
cur += len;
}
*cur = 0;
for(i = 0; i < ds->ds_num; ++i) {
if(*vl->type_instance) {
char name[64];
int len;
len = snprintf(name, 63, "%s.%s",
vl->type_instance, ds->ds[i].name);
name[len] = 0;
put_single_value((cmq_socket_t *)user_data->data,
name, vl->values[i], vl, &ds->ds[i], extdata);
} else {
put_single_value((cmq_socket_t *)user_data->data,
ds->ds[i].name, vl->values[i], vl, &ds->ds[i], extdata);
}
}
} else {
char extdata[100] = "";
if(strcmp(ds->type, "gauge")
&& strcmp(ds->type, "counter")
&& strcmp(ds->type, "derive")
&& strcmp(ds->type, "absolute")) {
sprintf(extdata, "\n :collectd: type=%s", ds->type);
}
put_single_value((cmq_socket_t *)user_data->data,
vl->type_instance, vl->values[0], vl, &ds->ds[0], extdata);
}
return 0;
}
static int cmq_config_mode (oconfig_item_t *ci) /* {{{ */
{
char buffer[64] = "";
int status;
status = cf_util_get_string_buffer (ci, buffer, sizeof (buffer));
if (status != 0)
return (-1);
if (strcasecmp ("Publish", buffer) == 0)
return (NN_PUB);
else if (strcasecmp ("Subscribe", buffer) == 0)
return (NN_SUB);
else if (strcasecmp ("Push", buffer) == 0)
return (NN_PUSH);
else if (strcasecmp ("Pull", buffer) == 0)
return (NN_PULL);
ERROR ("nanomsg plugin: Unrecognized communication pattern: \"%s\"",
buffer);
return (-1);
} /* }}} int cmq_config_mode */
static int cmq_config_socket (oconfig_item_t *ci) /* {{{ */
{
int type;
int status;
int i;
int endpoints_num;
int cmq_socket;
type = cmq_config_mode (ci);
if (type < 0)
return (-1);
/* Create a new socket */
cmq_socket = nn_socket (AF_SP, type);
if (cmq_socket < 0) {
ERROR ("nanomsg plugin: nn_socket failed: %s",
nn_strerror (errno));
return (-1);
}
if (type == NN_SUB) {
/* Subscribe to all messages */
/* TODO(tailhook) implement subscription configuration */
status = nn_setsockopt (cmq_socket, NN_SUB, NN_SUB_SUBSCRIBE,
/* prefix = */ "", /* prefix length = */ 0);
if (status != 0) {
ERROR ("nanomsg plugin: nn_setsockopt (NN_SUB_SUBSCRIBE) failed: %s",
nn_strerror (errno));
(void) nn_close (cmq_socket);
return (-1);
}
}
/* Iterate over all children and do all the binds and connects requested. */
endpoints_num = 0;
for (i = 0; i < ci->children_num; i++) {
oconfig_item_t *child = ci->children + i;
if (strcasecmp ("Bind", child->key) == 0) {
char *value = NULL;
status = cf_util_get_string (child, &value);
if (status != 0)
continue;
DEBUG("Binding to %s", value);
status = nn_bind (cmq_socket, value);
if (status < 0) {
ERROR ("nanomsg plugin: nn_bind (\"%s\") failed: %s",
value, nn_strerror (errno));
sfree (value);
continue;
}
endpoints_num++;
continue;
} /* Bind */
else if (strcasecmp ("Connect", child->key) == 0) {
char *value = NULL;
status = cf_util_get_string (child, &value);
if (status != 0)
continue;
DEBUG("Connecting to %s", value);
status = nn_connect (cmq_socket, value);
if (status < 0) {
ERROR ("nanomsg plugin: nn_connect (\"%s\") failed: %s",
value, nn_strerror (errno));
sfree (value);
continue;
}
sfree (value);
endpoints_num++;
continue;
} /* Connect */
else {
ERROR ("nanomsg plugin: The \"%s\" config option is now allowed here.",
child->key);
}
} /* for (i = 0; i < ci->children_num; i++) */
if (endpoints_num == 0) {
ERROR ("nanomsg plugin: No (valid) \"Bind\"/\"Connect\" "
"option was found in this \"Socket\" block.");
(void) nn_close (cmq_socket);
return (-1);
}
/* If this is a receiving socket, create a new receive thread */
if ((type == NN_SUB) || (type == NN_PULL)) {
pthread_t *thread_ptr;
thread_ptr = realloc (receive_thread_ids,
sizeof (*receive_thread_ids) * (receive_thread_num + 1));
if (thread_ptr == NULL) {
ERROR ("nanomsg plugin: realloc failed.");
return (-1);
}
receive_thread_ids = thread_ptr;
thread_ptr = receive_thread_ids + receive_thread_num;
status = pthread_create (thread_ptr,
/* attr = */ NULL,
/* func = */ receive_thread,
/* args = */ (void *)(long)cmq_socket);
if (status != 0) {
char errbuf[1024];
ERROR ("nanomsg plugin: pthread_create failed: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
(void) nn_close (cmq_socket);
return (-1);
}
receive_thread_num++;
}
/* If this is a sending socket, register a new write function */
else if ((type == NN_PUB) || (type == NN_PUSH)) {
user_data_t ud = { NULL, NULL };
char name[20];
cmq_socket_t *sockstr = malloc(sizeof(cmq_socket_t));
if(!sockstr) {
char errbuf[1024];
ERROR ("nanomsg plugin: malloc failed: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
(void) nn_close (cmq_socket);
return (-1);
}
sockstr->socket = cmq_socket;
ud.data = sockstr;
ud.free_func = cmq_close_callback;
ssnprintf (name, sizeof (name), "nanomsg/%i", sending_sockets_num);
sending_sockets_num++;
plugin_register_write (name, write_value, &ud);
}
return (0);
} /* }}} int cmq_config_socket */
/*
* Config schema:
*
* <Plugin "nanomsg_estp">
* <Socket Publish>
* Connect "tcp://localhost:6666"
* </Socket>
* <Socket Subscribe>
* Bind "tcp://eth0:6666"
* Bind "tcp://collectd.example.com:6666"
* </Socket>
* </Plugin>
*/
static int cmq_config (oconfig_item_t *ci) /* {{{ */
{
int status;
int i;
for (i = 0; i < ci->children_num; i++) {
oconfig_item_t *child = ci->children + i;
if (strcasecmp ("Socket", child->key) == 0) {
status = cmq_config_socket (child);
if(status < 0) {
return status;
}
} else {
WARNING ("nanomsg plugin: "
"The \"%s\" config option is not allowed here.", child->key);
}
}
return (0);
} /* }}} int cmq_config */
static int plugin_init (void)
{
int i, symval;
const char *symname;
int version, revision, age;
version = 0;
revision = 0;
age = 0;
for(i = 0;;++i) {
symname = nn_symbol(i, &symval);
if(!symname)
break;
if(!strcmp(symname, "NN_VERSION_CURRENT"))
version = symval;
if(!strcmp(symname, "NN_VERSION_REVISION"))
revision = symval;
if(!strcmp(symname, "NN_VERSION_AGE"))
age = symval;
}
INFO("nanomsg plugin loaded (nanomsg v%d.%d.%d).",
version, revision, age);
pthread_mutex_init(&staging_mutex, NULL);
staging = c_avl_create ((void *) strcmp);
if (staging == NULL) {
ERROR("Nanomsg-ESTP : c_avl_create failed: %s", strerror(errno));
return -1;
}
return 0;
}
static int my_shutdown (void)
{
if( thread_running ) {
thread_running = 0;
DEBUG("nanomsg: shutting down");
nn_term();
}
if( staging ) {
while (42) {
char *key = NULL;
cmq_cache_entry_t *value = NULL;
int rc = c_avl_pick (staging, (void *) &key, (void *) &value);
if (rc != 0)
break;
free (key);
free (value);
}
c_avl_destroy (staging);
}
pthread_mutex_destroy(&staging_mutex);
return 0;
}
void module_register (void)
{
plugin_register_complex_config("nanomsg_estp", cmq_config);
plugin_register_init("nanomsg_estp", plugin_init);
plugin_register_shutdown ("nanomsg_estp", my_shutdown);
}