ffi-nats-core 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ffi-nats-core.gemspec +8 -0
- data/lib/ffi/nats/core/version.rb +1 -1
- data/vendor/cnats/CMakeLists.txt +137 -0
- data/vendor/cnats/adapters/libevent.h +220 -0
- data/vendor/cnats/adapters/libuv.h +472 -0
- data/vendor/cnats/examples/CMakeLists.txt +56 -0
- data/vendor/cnats/examples/asynctimeout.c +83 -0
- data/vendor/cnats/examples/examples.h +322 -0
- data/vendor/cnats/examples/libevent-pub.c +136 -0
- data/vendor/cnats/examples/libevent-sub.c +104 -0
- data/vendor/cnats/examples/libuv-pub.c +120 -0
- data/vendor/cnats/examples/libuv-sub.c +114 -0
- data/vendor/cnats/examples/publisher.c +62 -0
- data/vendor/cnats/examples/queuegroup.c +132 -0
- data/vendor/cnats/examples/replier.c +149 -0
- data/vendor/cnats/examples/requestor.c +75 -0
- data/vendor/cnats/examples/subscriber.c +133 -0
- data/vendor/cnats/src/CMakeLists.txt +31 -0
- data/vendor/cnats/src/asynccb.c +66 -0
- data/vendor/cnats/src/asynccb.h +42 -0
- data/vendor/cnats/src/buf.c +246 -0
- data/vendor/cnats/src/buf.h +116 -0
- data/vendor/cnats/src/comsock.c +474 -0
- data/vendor/cnats/src/comsock.h +81 -0
- data/vendor/cnats/src/conn.c +2725 -0
- data/vendor/cnats/src/conn.h +75 -0
- data/vendor/cnats/src/err.h +31 -0
- data/vendor/cnats/src/gc.h +27 -0
- data/vendor/cnats/src/hash.c +725 -0
- data/vendor/cnats/src/hash.h +141 -0
- data/vendor/cnats/src/include/n-unix.h +56 -0
- data/vendor/cnats/src/include/n-win.h +59 -0
- data/vendor/cnats/src/mem.h +20 -0
- data/vendor/cnats/src/msg.c +155 -0
- data/vendor/cnats/src/msg.h +43 -0
- data/vendor/cnats/src/nats.c +1734 -0
- data/vendor/cnats/src/nats.h +2024 -0
- data/vendor/cnats/src/natsp.h +518 -0
- data/vendor/cnats/src/natstime.c +79 -0
- data/vendor/cnats/src/natstime.h +27 -0
- data/vendor/cnats/src/nuid.c +265 -0
- data/vendor/cnats/src/nuid.h +21 -0
- data/vendor/cnats/src/opts.c +1030 -0
- data/vendor/cnats/src/opts.h +19 -0
- data/vendor/cnats/src/parser.c +869 -0
- data/vendor/cnats/src/parser.h +87 -0
- data/vendor/cnats/src/pub.c +293 -0
- data/vendor/cnats/src/srvpool.c +380 -0
- data/vendor/cnats/src/srvpool.h +71 -0
- data/vendor/cnats/src/stats.c +54 -0
- data/vendor/cnats/src/stats.h +21 -0
- data/vendor/cnats/src/status.c +60 -0
- data/vendor/cnats/src/status.h +95 -0
- data/vendor/cnats/src/sub.c +956 -0
- data/vendor/cnats/src/sub.h +34 -0
- data/vendor/cnats/src/timer.c +86 -0
- data/vendor/cnats/src/timer.h +57 -0
- data/vendor/cnats/src/unix/cond.c +103 -0
- data/vendor/cnats/src/unix/mutex.c +107 -0
- data/vendor/cnats/src/unix/sock.c +105 -0
- data/vendor/cnats/src/unix/thread.c +162 -0
- data/vendor/cnats/src/url.c +134 -0
- data/vendor/cnats/src/url.h +24 -0
- data/vendor/cnats/src/util.c +823 -0
- data/vendor/cnats/src/util.h +75 -0
- data/vendor/cnats/src/version.h +29 -0
- data/vendor/cnats/src/version.h.in +29 -0
- data/vendor/cnats/src/win/cond.c +86 -0
- data/vendor/cnats/src/win/mutex.c +54 -0
- data/vendor/cnats/src/win/sock.c +158 -0
- data/vendor/cnats/src/win/strings.c +108 -0
- data/vendor/cnats/src/win/thread.c +180 -0
- data/vendor/cnats/test/CMakeLists.txt +35 -0
- data/vendor/cnats/test/certs/ca.pem +38 -0
- data/vendor/cnats/test/certs/client-cert.pem +30 -0
- data/vendor/cnats/test/certs/client-key.pem +51 -0
- data/vendor/cnats/test/certs/server-cert.pem +31 -0
- data/vendor/cnats/test/certs/server-key.pem +51 -0
- data/vendor/cnats/test/dylib/CMakeLists.txt +10 -0
- data/vendor/cnats/test/dylib/nonats.c +13 -0
- data/vendor/cnats/test/list.txt +125 -0
- data/vendor/cnats/test/test.c +11655 -0
- data/vendor/cnats/test/tls.conf +15 -0
- data/vendor/cnats/test/tlsverify.conf +19 -0
- metadata +83 -1
@@ -0,0 +1,62 @@
|
|
1
|
+
// Copyright 2015 Apcera Inc. All rights reserved.
|
2
|
+
|
3
|
+
#include "examples.h"
|
4
|
+
|
5
|
+
static const char *usage = ""\
|
6
|
+
"-txt text to send (default is 'hello')\n" \
|
7
|
+
"-count number of messages to send\n";
|
8
|
+
|
9
|
+
int main(int argc, char **argv)
|
10
|
+
{
|
11
|
+
natsConnection *conn = NULL;
|
12
|
+
natsStatistics *stats = NULL;
|
13
|
+
natsOptions *opts = NULL;
|
14
|
+
int64_t last = 0;
|
15
|
+
natsStatus s;
|
16
|
+
|
17
|
+
opts = parseArgs(argc, argv, usage);
|
18
|
+
|
19
|
+
printf("Sending %" PRId64 " messages to subject '%s'\n", total, subj);
|
20
|
+
|
21
|
+
s = natsConnection_Connect(&conn, opts);
|
22
|
+
|
23
|
+
if (s == NATS_OK)
|
24
|
+
s = natsStatistics_Create(&stats);
|
25
|
+
|
26
|
+
if (s == NATS_OK)
|
27
|
+
start = nats_Now();
|
28
|
+
|
29
|
+
for (count = 0; (s == NATS_OK) && (count < total); count++)
|
30
|
+
{
|
31
|
+
s = natsConnection_PublishString(conn, subj, txt);
|
32
|
+
|
33
|
+
if (nats_Now() - last >= 1000)
|
34
|
+
{
|
35
|
+
s = printStats(STATS_OUT, conn, NULL, stats);
|
36
|
+
last = nats_Now();
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
if (s == NATS_OK)
|
41
|
+
s = natsConnection_FlushTimeout(conn, 1000);
|
42
|
+
|
43
|
+
if (s == NATS_OK)
|
44
|
+
{
|
45
|
+
printPerf("Sent", total, start, elapsed);
|
46
|
+
}
|
47
|
+
else
|
48
|
+
{
|
49
|
+
printf("Error: %d - %s\n", s, natsStatus_GetText(s));
|
50
|
+
nats_PrintLastErrorStack(stderr);
|
51
|
+
}
|
52
|
+
|
53
|
+
// Destroy all our objects to avoid report of memory leak
|
54
|
+
natsStatistics_Destroy(stats);
|
55
|
+
natsConnection_Destroy(conn);
|
56
|
+
natsOptions_Destroy(opts);
|
57
|
+
|
58
|
+
// To silence reports of memory still in used with valgrind
|
59
|
+
nats_Close();
|
60
|
+
|
61
|
+
return 0;
|
62
|
+
}
|
@@ -0,0 +1,132 @@
|
|
1
|
+
// Copyright 2015-2016 Apcera Inc. All rights reserved.
|
2
|
+
|
3
|
+
#include "examples.h"
|
4
|
+
|
5
|
+
static const char *usage = "" \
|
6
|
+
"-gd use global message delivery thread pool\n" \
|
7
|
+
"-name queue name (default is 'worker')\n" \
|
8
|
+
"-count number of expected messages\n";
|
9
|
+
|
10
|
+
static void
|
11
|
+
onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
12
|
+
{
|
13
|
+
// If 'print' is on, the server is likely to break the connection
|
14
|
+
// since the client library will become a slow consumer.
|
15
|
+
if (print)
|
16
|
+
printf("Received msg: %s - %.*s\n",
|
17
|
+
natsMsg_GetSubject(msg),
|
18
|
+
natsMsg_GetDataLength(msg),
|
19
|
+
natsMsg_GetData(msg));
|
20
|
+
|
21
|
+
if (start == 0)
|
22
|
+
start = nats_Now();
|
23
|
+
|
24
|
+
// We should be using a mutex to protect those variables since
|
25
|
+
// they are used from the subscription's delivery and the main
|
26
|
+
// threads. For demo purposes, this is fine.
|
27
|
+
if (++count == total)
|
28
|
+
elapsed = nats_Now() - start;
|
29
|
+
|
30
|
+
natsMsg_Destroy(msg);
|
31
|
+
}
|
32
|
+
|
33
|
+
static void
|
34
|
+
asyncCb(natsConnection *nc, natsSubscription *sub, natsStatus err, void *closure)
|
35
|
+
{
|
36
|
+
printf("Async error: %d - %s\n", err, natsStatus_GetText(err));
|
37
|
+
}
|
38
|
+
|
39
|
+
int main(int argc, char **argv)
|
40
|
+
{
|
41
|
+
natsConnection *conn = NULL;
|
42
|
+
natsOptions *opts = NULL;
|
43
|
+
natsSubscription *sub = NULL;
|
44
|
+
natsStatistics *stats = NULL;
|
45
|
+
natsMsg *msg = NULL;
|
46
|
+
natsStatus s;
|
47
|
+
|
48
|
+
opts = parseArgs(argc, argv, usage);
|
49
|
+
|
50
|
+
printf("Listening %ssynchronously on '%s' with name '%s'.\n",
|
51
|
+
(async ? "a" : ""), subj, name);
|
52
|
+
|
53
|
+
s = natsOptions_SetErrorHandler(opts, asyncCb, NULL);
|
54
|
+
|
55
|
+
if (s == NATS_OK)
|
56
|
+
s = natsConnection_Connect(&conn, opts);
|
57
|
+
|
58
|
+
if (s == NATS_OK)
|
59
|
+
{
|
60
|
+
if (async)
|
61
|
+
s = natsConnection_QueueSubscribe(&sub, conn, subj, name, onMsg, NULL);
|
62
|
+
else
|
63
|
+
s = natsConnection_QueueSubscribeSync(&sub, conn, subj, name);
|
64
|
+
}
|
65
|
+
|
66
|
+
// For maximum performance, set no limit on the number of pending messages.
|
67
|
+
if (s == NATS_OK)
|
68
|
+
s = natsSubscription_SetPendingLimits(sub, -1, -1);
|
69
|
+
|
70
|
+
if (s == NATS_OK)
|
71
|
+
s = natsSubscription_AutoUnsubscribe(sub, (int) total);
|
72
|
+
|
73
|
+
if (s == NATS_OK)
|
74
|
+
s = natsStatistics_Create(&stats);
|
75
|
+
|
76
|
+
if ((s == NATS_OK) && async)
|
77
|
+
{
|
78
|
+
while (s == NATS_OK)
|
79
|
+
{
|
80
|
+
s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats);
|
81
|
+
|
82
|
+
if (count == total)
|
83
|
+
break;
|
84
|
+
|
85
|
+
if (s == NATS_OK)
|
86
|
+
nats_Sleep(1000);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
else if (s == NATS_OK)
|
90
|
+
{
|
91
|
+
int64_t last = 0;
|
92
|
+
|
93
|
+
for (count = 0; (s == NATS_OK) && (count < total); count++)
|
94
|
+
{
|
95
|
+
s = natsSubscription_NextMsg(&msg, sub, 10000);
|
96
|
+
if (s != NATS_OK)
|
97
|
+
break;
|
98
|
+
|
99
|
+
if (start == 0)
|
100
|
+
start = nats_Now();
|
101
|
+
|
102
|
+
if (nats_Now() - last >= 1000)
|
103
|
+
{
|
104
|
+
s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats);
|
105
|
+
last = nats_Now();
|
106
|
+
}
|
107
|
+
|
108
|
+
natsMsg_Destroy(msg);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
if (s == NATS_OK)
|
113
|
+
{
|
114
|
+
printPerf("Received", total, start, elapsed);
|
115
|
+
}
|
116
|
+
else
|
117
|
+
{
|
118
|
+
printf("Error: %d - %s\n", s, natsStatus_GetText(s));
|
119
|
+
nats_PrintLastErrorStack(stderr);
|
120
|
+
}
|
121
|
+
|
122
|
+
// Destroy all our objects to avoid report of memory leak
|
123
|
+
natsStatistics_Destroy(stats);
|
124
|
+
natsSubscription_Destroy(sub);
|
125
|
+
natsConnection_Destroy(conn);
|
126
|
+
natsOptions_Destroy(opts);
|
127
|
+
|
128
|
+
// To silence reports of memory still in used with valgrind
|
129
|
+
nats_Close();
|
130
|
+
|
131
|
+
return 0;
|
132
|
+
}
|
@@ -0,0 +1,149 @@
|
|
1
|
+
// Copyright 2015-2016 Apcera Inc. All rights reserved.
|
2
|
+
|
3
|
+
#include "examples.h"
|
4
|
+
|
5
|
+
static const char *usage = "" \
|
6
|
+
"-gd use global message delivery thread pool\n" \
|
7
|
+
"-sync receive synchronously (default is asynchronous)\n" \
|
8
|
+
"-count number of expected requests\n";
|
9
|
+
|
10
|
+
static void
|
11
|
+
onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
12
|
+
{
|
13
|
+
natsStatus s;
|
14
|
+
|
15
|
+
if (print)
|
16
|
+
printf("Received msg: %s - %.*s\n",
|
17
|
+
natsMsg_GetSubject(msg),
|
18
|
+
natsMsg_GetDataLength(msg),
|
19
|
+
natsMsg_GetData(msg));
|
20
|
+
|
21
|
+
if (start == 0)
|
22
|
+
start = nats_Now();
|
23
|
+
|
24
|
+
s = natsConnection_PublishString(nc, natsMsg_GetReply(msg),
|
25
|
+
"here's some help");
|
26
|
+
if (s == NATS_OK)
|
27
|
+
s = natsConnection_Flush(nc);
|
28
|
+
|
29
|
+
// We should be using a mutex to protect those variables since
|
30
|
+
// they are used from the subscription's delivery and the main
|
31
|
+
// threads. For demo purposes, this is fine.
|
32
|
+
if (++count == total)
|
33
|
+
elapsed = nats_Now() - start;
|
34
|
+
|
35
|
+
natsMsg_Destroy(msg);
|
36
|
+
}
|
37
|
+
|
38
|
+
static void
|
39
|
+
asyncCb(natsConnection *nc, natsSubscription *sub, natsStatus err, void *closure)
|
40
|
+
{
|
41
|
+
if (print)
|
42
|
+
printf("Async error: %d - %s\n", err, natsStatus_GetText(err));
|
43
|
+
|
44
|
+
natsSubscription_GetDropped(sub, (int64_t*) &dropped);
|
45
|
+
}
|
46
|
+
|
47
|
+
int main(int argc, char **argv)
|
48
|
+
{
|
49
|
+
natsConnection *conn = NULL;
|
50
|
+
natsOptions *opts = NULL;
|
51
|
+
natsSubscription *sub = NULL;
|
52
|
+
natsStatistics *stats = NULL;
|
53
|
+
natsMsg *msg = NULL;
|
54
|
+
natsStatus s;
|
55
|
+
|
56
|
+
opts = parseArgs(argc, argv, usage);
|
57
|
+
|
58
|
+
printf("Listening %ssynchronously for requests on '%s'\n",
|
59
|
+
(async ? "a" : ""), subj);
|
60
|
+
|
61
|
+
s = natsOptions_SetErrorHandler(opts, asyncCb, NULL);
|
62
|
+
|
63
|
+
if (s == NATS_OK)
|
64
|
+
s = natsConnection_Connect(&conn, opts);
|
65
|
+
|
66
|
+
if (s == NATS_OK)
|
67
|
+
{
|
68
|
+
if (async)
|
69
|
+
s = natsConnection_Subscribe(&sub, conn, subj, onMsg, NULL);
|
70
|
+
else
|
71
|
+
s = natsConnection_SubscribeSync(&sub, conn, subj);
|
72
|
+
}
|
73
|
+
|
74
|
+
// For maximum performance, set no limit on the number of pending messages.
|
75
|
+
if (s == NATS_OK)
|
76
|
+
s = natsSubscription_SetPendingLimits(sub, -1, -1);
|
77
|
+
|
78
|
+
if (s == NATS_OK)
|
79
|
+
s = natsSubscription_AutoUnsubscribe(sub, (int) total);
|
80
|
+
|
81
|
+
if (s == NATS_OK)
|
82
|
+
s = natsStatistics_Create(&stats);
|
83
|
+
|
84
|
+
if ((s == NATS_OK) && async)
|
85
|
+
{
|
86
|
+
while (s == NATS_OK)
|
87
|
+
{
|
88
|
+
s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats);
|
89
|
+
|
90
|
+
if (count + dropped == total)
|
91
|
+
break;
|
92
|
+
|
93
|
+
if (s == NATS_OK)
|
94
|
+
nats_Sleep(1000);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
else if (s == NATS_OK)
|
98
|
+
{
|
99
|
+
int64_t last = 0;
|
100
|
+
|
101
|
+
for (count = 0; (s == NATS_OK) && (count < total); count++)
|
102
|
+
{
|
103
|
+
s = natsSubscription_NextMsg(&msg, sub, 10000);
|
104
|
+
if (s == NATS_OK)
|
105
|
+
s = natsConnection_PublishString(conn,
|
106
|
+
natsMsg_GetReply(msg),
|
107
|
+
"here's some help");
|
108
|
+
if (s == NATS_OK)
|
109
|
+
s = natsConnection_Flush(conn);
|
110
|
+
if (s == NATS_OK)
|
111
|
+
{
|
112
|
+
if (start == 0)
|
113
|
+
start = nats_Now();
|
114
|
+
|
115
|
+
if (nats_Now() - last >= 1000)
|
116
|
+
{
|
117
|
+
s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats);
|
118
|
+
last = nats_Now();
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
natsMsg_Destroy(msg);
|
123
|
+
}
|
124
|
+
|
125
|
+
if (s == NATS_OK)
|
126
|
+
s = natsConnection_FlushTimeout(conn, 1000);
|
127
|
+
}
|
128
|
+
|
129
|
+
if (s == NATS_OK)
|
130
|
+
{
|
131
|
+
printPerf("Received", count, start, elapsed);
|
132
|
+
}
|
133
|
+
else
|
134
|
+
{
|
135
|
+
printf("Error: %d - %s\n", s, natsStatus_GetText(s));
|
136
|
+
nats_PrintLastErrorStack(stderr);
|
137
|
+
}
|
138
|
+
|
139
|
+
// Destroy all our objects to avoid report of memory leak
|
140
|
+
natsStatistics_Destroy(stats);
|
141
|
+
natsSubscription_Destroy(sub);
|
142
|
+
natsConnection_Destroy(conn);
|
143
|
+
natsOptions_Destroy(opts);
|
144
|
+
|
145
|
+
// To silence reports of memory still in used with valgrind
|
146
|
+
nats_Close();
|
147
|
+
|
148
|
+
return 0;
|
149
|
+
}
|
@@ -0,0 +1,75 @@
|
|
1
|
+
// Copyright 2015 Apcera Inc. All rights reserved.
|
2
|
+
|
3
|
+
#include "examples.h"
|
4
|
+
|
5
|
+
static const char *usage = "" \
|
6
|
+
"-txt text to send (default is 'hello')\n" \
|
7
|
+
"-count number of requests to send\n";
|
8
|
+
|
9
|
+
int main(int argc, char **argv)
|
10
|
+
{
|
11
|
+
natsConnection *conn = NULL;
|
12
|
+
natsStatistics *stats = NULL;
|
13
|
+
natsOptions *opts = NULL;
|
14
|
+
natsMsg *reply = NULL;
|
15
|
+
int64_t last = 0;
|
16
|
+
natsStatus s;
|
17
|
+
|
18
|
+
opts = parseArgs(argc, argv, usage);
|
19
|
+
|
20
|
+
printf("Sending %" PRId64 " requests to subject '%s'\n", total, subj);
|
21
|
+
|
22
|
+
s = natsConnection_Connect(&conn, opts);
|
23
|
+
|
24
|
+
if (s == NATS_OK)
|
25
|
+
s = natsStatistics_Create(&stats);
|
26
|
+
|
27
|
+
if (s == NATS_OK)
|
28
|
+
start = nats_Now();
|
29
|
+
|
30
|
+
for (count = 0; (s == NATS_OK) && (count < total); count++)
|
31
|
+
{
|
32
|
+
s = natsConnection_RequestString(&reply, conn, subj, txt, 1000);
|
33
|
+
if (s != NATS_OK)
|
34
|
+
break;
|
35
|
+
|
36
|
+
if (print)
|
37
|
+
{
|
38
|
+
printf("Received reply: %s - %.*s\n",
|
39
|
+
natsMsg_GetSubject(reply),
|
40
|
+
natsMsg_GetDataLength(reply),
|
41
|
+
natsMsg_GetData(reply));
|
42
|
+
}
|
43
|
+
|
44
|
+
if (nats_Now() - last >= 1000)
|
45
|
+
{
|
46
|
+
s = printStats(STATS_OUT,conn, NULL, stats);
|
47
|
+
last = nats_Now();
|
48
|
+
}
|
49
|
+
|
50
|
+
natsMsg_Destroy(reply);
|
51
|
+
}
|
52
|
+
|
53
|
+
if (s == NATS_OK)
|
54
|
+
s = natsConnection_FlushTimeout(conn, 1000);
|
55
|
+
|
56
|
+
if (s == NATS_OK)
|
57
|
+
{
|
58
|
+
printPerf("Sent", total, start, elapsed);
|
59
|
+
}
|
60
|
+
else
|
61
|
+
{
|
62
|
+
printf("Error: %d - %s\n", s, natsStatus_GetText(s));
|
63
|
+
nats_PrintLastErrorStack(stderr);
|
64
|
+
}
|
65
|
+
|
66
|
+
// Destroy all our objects to avoid report of memory leak
|
67
|
+
natsStatistics_Destroy(stats);
|
68
|
+
natsConnection_Destroy(conn);
|
69
|
+
natsOptions_Destroy(opts);
|
70
|
+
|
71
|
+
// To silence reports of memory still in used with valgrind
|
72
|
+
nats_Close();
|
73
|
+
|
74
|
+
return 0;
|
75
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
// Copyright 2015-2016 Apcera Inc. All rights reserved.
|
2
|
+
|
3
|
+
#include "examples.h"
|
4
|
+
|
5
|
+
static const char *usage = ""\
|
6
|
+
"-gd use global message delivery thread pool\n" \
|
7
|
+
"-sync receive synchronously (default is asynchronous)\n" \
|
8
|
+
"-count number of expected messages\n";
|
9
|
+
|
10
|
+
static void
|
11
|
+
onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
12
|
+
{
|
13
|
+
if (print)
|
14
|
+
printf("Received msg: %s - %.*s\n",
|
15
|
+
natsMsg_GetSubject(msg),
|
16
|
+
natsMsg_GetDataLength(msg),
|
17
|
+
natsMsg_GetData(msg));
|
18
|
+
|
19
|
+
if (start == 0)
|
20
|
+
start = nats_Now();
|
21
|
+
|
22
|
+
// We should be using a mutex to protect those variables since
|
23
|
+
// they are used from the subscription's delivery and the main
|
24
|
+
// threads. For demo purposes, this is fine.
|
25
|
+
if (++count == total)
|
26
|
+
elapsed = nats_Now() - start;
|
27
|
+
|
28
|
+
natsMsg_Destroy(msg);
|
29
|
+
}
|
30
|
+
|
31
|
+
static void
|
32
|
+
asyncCb(natsConnection *nc, natsSubscription *sub, natsStatus err, void *closure)
|
33
|
+
{
|
34
|
+
if (print)
|
35
|
+
printf("Async error: %d - %s\n", err, natsStatus_GetText(err));
|
36
|
+
|
37
|
+
natsSubscription_GetDropped(sub, (int64_t*) &dropped);
|
38
|
+
}
|
39
|
+
|
40
|
+
int main(int argc, char **argv)
|
41
|
+
{
|
42
|
+
natsConnection *conn = NULL;
|
43
|
+
natsOptions *opts = NULL;
|
44
|
+
natsSubscription *sub = NULL;
|
45
|
+
natsStatistics *stats = NULL;
|
46
|
+
natsMsg *msg = NULL;
|
47
|
+
natsStatus s;
|
48
|
+
|
49
|
+
opts = parseArgs(argc, argv, usage);
|
50
|
+
|
51
|
+
printf("Listening %ssynchronously on '%s'.\n",
|
52
|
+
(async ? "a" : ""), subj);
|
53
|
+
|
54
|
+
s = natsOptions_SetErrorHandler(opts, asyncCb, NULL);
|
55
|
+
|
56
|
+
if (s == NATS_OK)
|
57
|
+
s = natsConnection_Connect(&conn, opts);
|
58
|
+
|
59
|
+
if (s == NATS_OK)
|
60
|
+
{
|
61
|
+
if (async)
|
62
|
+
s = natsConnection_Subscribe(&sub, conn, subj, onMsg, NULL);
|
63
|
+
else
|
64
|
+
s = natsConnection_SubscribeSync(&sub, conn, subj);
|
65
|
+
}
|
66
|
+
|
67
|
+
// For maximum performance, set no limit on the number of pending messages.
|
68
|
+
if (s == NATS_OK)
|
69
|
+
s = natsSubscription_SetPendingLimits(sub, -1, -1);
|
70
|
+
|
71
|
+
if (s == NATS_OK)
|
72
|
+
s = natsSubscription_AutoUnsubscribe(sub, (int) total);
|
73
|
+
|
74
|
+
if (s == NATS_OK)
|
75
|
+
s = natsStatistics_Create(&stats);
|
76
|
+
|
77
|
+
if ((s == NATS_OK) && async)
|
78
|
+
{
|
79
|
+
while (s == NATS_OK)
|
80
|
+
{
|
81
|
+
s = printStats(STATS_IN|STATS_COUNT, conn, sub, stats);
|
82
|
+
|
83
|
+
if (count + dropped == total)
|
84
|
+
break;
|
85
|
+
|
86
|
+
if (s == NATS_OK)
|
87
|
+
nats_Sleep(1000);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
else if (s == NATS_OK)
|
91
|
+
{
|
92
|
+
int64_t last = 0;
|
93
|
+
|
94
|
+
for (count = 0; (s == NATS_OK) && (count < total); count++)
|
95
|
+
{
|
96
|
+
s = natsSubscription_NextMsg(&msg, sub, 10000);
|
97
|
+
if (s != NATS_OK)
|
98
|
+
break;
|
99
|
+
|
100
|
+
if (start == 0)
|
101
|
+
start = nats_Now();
|
102
|
+
|
103
|
+
if (nats_Now() - last >= 1000)
|
104
|
+
{
|
105
|
+
s = printStats(STATS_IN|STATS_COUNT, conn, sub, stats);
|
106
|
+
last = nats_Now();
|
107
|
+
}
|
108
|
+
|
109
|
+
natsMsg_Destroy(msg);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
if (s == NATS_OK)
|
114
|
+
{
|
115
|
+
printPerf("Received", count, start, elapsed);
|
116
|
+
}
|
117
|
+
else
|
118
|
+
{
|
119
|
+
printf("Error: %d - %s\n", s, natsStatus_GetText(s));
|
120
|
+
nats_PrintLastErrorStack(stderr);
|
121
|
+
}
|
122
|
+
|
123
|
+
// Destroy all our objects to avoid report of memory leak
|
124
|
+
natsStatistics_Destroy(stats);
|
125
|
+
natsSubscription_Destroy(sub);
|
126
|
+
natsConnection_Destroy(conn);
|
127
|
+
natsOptions_Destroy(opts);
|
128
|
+
|
129
|
+
// To silence reports of memory still in used with valgrind
|
130
|
+
nats_Close();
|
131
|
+
|
132
|
+
return 0;
|
133
|
+
}
|