ffi-nats-core 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/ffi-nats-core.gemspec +8 -0
  3. data/lib/ffi/nats/core/version.rb +1 -1
  4. data/vendor/cnats/CMakeLists.txt +137 -0
  5. data/vendor/cnats/adapters/libevent.h +220 -0
  6. data/vendor/cnats/adapters/libuv.h +472 -0
  7. data/vendor/cnats/examples/CMakeLists.txt +56 -0
  8. data/vendor/cnats/examples/asynctimeout.c +83 -0
  9. data/vendor/cnats/examples/examples.h +322 -0
  10. data/vendor/cnats/examples/libevent-pub.c +136 -0
  11. data/vendor/cnats/examples/libevent-sub.c +104 -0
  12. data/vendor/cnats/examples/libuv-pub.c +120 -0
  13. data/vendor/cnats/examples/libuv-sub.c +114 -0
  14. data/vendor/cnats/examples/publisher.c +62 -0
  15. data/vendor/cnats/examples/queuegroup.c +132 -0
  16. data/vendor/cnats/examples/replier.c +149 -0
  17. data/vendor/cnats/examples/requestor.c +75 -0
  18. data/vendor/cnats/examples/subscriber.c +133 -0
  19. data/vendor/cnats/src/CMakeLists.txt +31 -0
  20. data/vendor/cnats/src/asynccb.c +66 -0
  21. data/vendor/cnats/src/asynccb.h +42 -0
  22. data/vendor/cnats/src/buf.c +246 -0
  23. data/vendor/cnats/src/buf.h +116 -0
  24. data/vendor/cnats/src/comsock.c +474 -0
  25. data/vendor/cnats/src/comsock.h +81 -0
  26. data/vendor/cnats/src/conn.c +2725 -0
  27. data/vendor/cnats/src/conn.h +75 -0
  28. data/vendor/cnats/src/err.h +31 -0
  29. data/vendor/cnats/src/gc.h +27 -0
  30. data/vendor/cnats/src/hash.c +725 -0
  31. data/vendor/cnats/src/hash.h +141 -0
  32. data/vendor/cnats/src/include/n-unix.h +56 -0
  33. data/vendor/cnats/src/include/n-win.h +59 -0
  34. data/vendor/cnats/src/mem.h +20 -0
  35. data/vendor/cnats/src/msg.c +155 -0
  36. data/vendor/cnats/src/msg.h +43 -0
  37. data/vendor/cnats/src/nats.c +1734 -0
  38. data/vendor/cnats/src/nats.h +2024 -0
  39. data/vendor/cnats/src/natsp.h +518 -0
  40. data/vendor/cnats/src/natstime.c +79 -0
  41. data/vendor/cnats/src/natstime.h +27 -0
  42. data/vendor/cnats/src/nuid.c +265 -0
  43. data/vendor/cnats/src/nuid.h +21 -0
  44. data/vendor/cnats/src/opts.c +1030 -0
  45. data/vendor/cnats/src/opts.h +19 -0
  46. data/vendor/cnats/src/parser.c +869 -0
  47. data/vendor/cnats/src/parser.h +87 -0
  48. data/vendor/cnats/src/pub.c +293 -0
  49. data/vendor/cnats/src/srvpool.c +380 -0
  50. data/vendor/cnats/src/srvpool.h +71 -0
  51. data/vendor/cnats/src/stats.c +54 -0
  52. data/vendor/cnats/src/stats.h +21 -0
  53. data/vendor/cnats/src/status.c +60 -0
  54. data/vendor/cnats/src/status.h +95 -0
  55. data/vendor/cnats/src/sub.c +956 -0
  56. data/vendor/cnats/src/sub.h +34 -0
  57. data/vendor/cnats/src/timer.c +86 -0
  58. data/vendor/cnats/src/timer.h +57 -0
  59. data/vendor/cnats/src/unix/cond.c +103 -0
  60. data/vendor/cnats/src/unix/mutex.c +107 -0
  61. data/vendor/cnats/src/unix/sock.c +105 -0
  62. data/vendor/cnats/src/unix/thread.c +162 -0
  63. data/vendor/cnats/src/url.c +134 -0
  64. data/vendor/cnats/src/url.h +24 -0
  65. data/vendor/cnats/src/util.c +823 -0
  66. data/vendor/cnats/src/util.h +75 -0
  67. data/vendor/cnats/src/version.h +29 -0
  68. data/vendor/cnats/src/version.h.in +29 -0
  69. data/vendor/cnats/src/win/cond.c +86 -0
  70. data/vendor/cnats/src/win/mutex.c +54 -0
  71. data/vendor/cnats/src/win/sock.c +158 -0
  72. data/vendor/cnats/src/win/strings.c +108 -0
  73. data/vendor/cnats/src/win/thread.c +180 -0
  74. data/vendor/cnats/test/CMakeLists.txt +35 -0
  75. data/vendor/cnats/test/certs/ca.pem +38 -0
  76. data/vendor/cnats/test/certs/client-cert.pem +30 -0
  77. data/vendor/cnats/test/certs/client-key.pem +51 -0
  78. data/vendor/cnats/test/certs/server-cert.pem +31 -0
  79. data/vendor/cnats/test/certs/server-key.pem +51 -0
  80. data/vendor/cnats/test/dylib/CMakeLists.txt +10 -0
  81. data/vendor/cnats/test/dylib/nonats.c +13 -0
  82. data/vendor/cnats/test/list.txt +125 -0
  83. data/vendor/cnats/test/test.c +11655 -0
  84. data/vendor/cnats/test/tls.conf +15 -0
  85. data/vendor/cnats/test/tlsverify.conf +19 -0
  86. metadata +83 -1
@@ -0,0 +1,56 @@
1
+ # We need this directory to build the examples
2
+ include_directories(${PROJECT_SOURCE_DIR}/src)
3
+ include_directories(${PROJECT_SOURCE_DIR}/examples)
4
+
5
+ if(NATS_BUILD_LIBUV_EXAMPLE)
6
+ include_directories(${LIBUV_DIR}/include)
7
+ endif()
8
+
9
+ if(NATS_BUILD_LIBEVENT_EXAMPLE)
10
+ include_directories(${LIBEVENT_DIR}/include)
11
+ endif()
12
+
13
+ if(WIN32)
14
+ set(LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
15
+ else()
16
+ set(LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
17
+ endif()
18
+
19
+ # Get all the .c files in the examples directory
20
+ file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples *.c)
21
+
22
+ add_definitions(-Dnats_IMPORTS)
23
+
24
+ # For each file...
25
+ foreach(examples_src ${EXAMPLES_SOURCES})
26
+
27
+ # Remove the suffix so that it becomes the executable name
28
+ string(REPLACE ".c" "" examplename ${examples_src})
29
+ set(exampleexe "nats-${examplename}")
30
+
31
+ set(buildExample OFF)
32
+ set(NATS_ASYNC_IO_LIB "")
33
+
34
+ if(examplename MATCHES "libuv")
35
+ if(NATS_BUILD_LIBUV_EXAMPLE)
36
+ set(buildExample ON)
37
+ set(NATS_ASYNC_IO_LIB ${LIBUV_DIR}/lib/libuv${LIB_SUFFIX})
38
+ endif()
39
+ elseif(examplename MATCHES "libevent")
40
+ if(NATS_BUILD_LIBEVENT_EXAMPLE)
41
+ set(buildExample ON)
42
+ set(NATS_ASYNC_IO_LIB ${LIBEVENT_DIR}/lib/libevent${LIB_SUFFIX})
43
+ endif()
44
+ else()
45
+ set(buildExample ON)
46
+ endif()
47
+
48
+ if(buildExample)
49
+ # Build the executable
50
+ add_executable(${exampleexe} ${PROJECT_SOURCE_DIR}/examples/${examples_src})
51
+
52
+ # Link dynamically
53
+ target_link_libraries(${exampleexe} nats ${NATS_EXTRA_LIB} ${NATS_ASYNC_IO_LIB})
54
+ endif()
55
+
56
+ endforeach()
@@ -0,0 +1,83 @@
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
+ "-queue use a queue subscriber with this name\n" \
8
+ "-timeout <ms> timeout in milliseconds (default is 10sec)\n" \
9
+ "-count number of expected messages\n";
10
+
11
+ static volatile bool done = false;
12
+
13
+ static void
14
+ onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
15
+ {
16
+ // This callback will be invoked with a NULL message when the
17
+ // subscription times out.
18
+ if (print && (msg != NULL))
19
+ printf("Received msg: %s - %.*s\n",
20
+ natsMsg_GetSubject(msg),
21
+ natsMsg_GetDataLength(msg),
22
+ natsMsg_GetData(msg));
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 ((msg == NULL) || (++count == total))
28
+ {
29
+ printf("%s, destroying subscription\n",
30
+ (msg == NULL ? "Subscription timed-out" : "All messages received"));
31
+
32
+ natsSubscription_Destroy(sub);
33
+ done = true;
34
+ }
35
+
36
+ // It is safe to call natsMsg_Destroy() with a NULL message.
37
+ natsMsg_Destroy(msg);
38
+ }
39
+
40
+ int main(int argc, char **argv)
41
+ {
42
+ natsConnection *conn = NULL;
43
+ natsOptions *opts = NULL;
44
+ natsSubscription *sub = NULL;
45
+ natsStatus s;
46
+
47
+ opts = parseArgs(argc, argv, usage);
48
+
49
+ printf("Listening asynchronously on '%s' with a timeout of %d ms.\n",
50
+ subj, (int) timeout);
51
+
52
+ s = natsConnection_Connect(&conn, opts);
53
+ if (s == NATS_OK)
54
+ {
55
+ if (name != NULL)
56
+ s = natsConnection_QueueSubscribeTimeout(&sub, conn, subj, name,
57
+ timeout, onMsg, NULL);
58
+ else
59
+ s = natsConnection_SubscribeTimeout(&sub, conn, subj,
60
+ timeout, onMsg, NULL);
61
+ }
62
+ // Check every half a second for end of test.
63
+ while ((s == NATS_OK) && !done)
64
+ {
65
+ nats_Sleep(500);
66
+ }
67
+
68
+ if (s != NATS_OK)
69
+ {
70
+ printf("Error: %d - %s\n", s, natsStatus_GetText(s));
71
+ nats_PrintLastErrorStack(stderr);
72
+ }
73
+
74
+ // Destroy all our objects to avoid report of memory leak
75
+ // Do not destroy subscription since it is done in the callback
76
+ natsConnection_Destroy(conn);
77
+ natsOptions_Destroy(opts);
78
+
79
+ // To silence reports of memory still in used with valgrind
80
+ nats_Close();
81
+
82
+ return 0;
83
+ }
@@ -0,0 +1,322 @@
1
+ // Copyright 2015-2016 Apcera Inc. All rights reserved.
2
+
3
+
4
+ #ifndef EXAMPLES_H_
5
+ #define EXAMPLES_H_
6
+
7
+ #include <nats.h>
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+
11
+ #ifdef _WIN32
12
+ #define strcasecmp _stricmp
13
+ #define strdup _strdup
14
+ #else
15
+ #ifndef NATS_LIBUV_INCLUDE
16
+ extern char *strdup(const char *s);
17
+ #endif
18
+ #include <strings.h>
19
+ #endif
20
+
21
+ #define STATS_IN 0x1
22
+ #define STATS_OUT 0x2
23
+ #define STATS_COUNT 0x4
24
+
25
+ #define MAX_SERVERS (10)
26
+
27
+ bool async = true;
28
+ const char *subj = "foo";
29
+ const char *txt = "hello";
30
+ const char *name = "worker";
31
+ int64_t total = 1000000;
32
+
33
+ volatile int64_t count = 0;
34
+ volatile int64_t dropped = 0;
35
+ int64_t start = 0;
36
+ volatile int64_t elapsed = 0;
37
+ bool print = false;
38
+ int64_t timeout = 10000; // 10 seconds.
39
+
40
+ natsOptions *opts = NULL;
41
+
42
+ const char *certFile = NULL;
43
+ const char *keyFile = NULL;
44
+
45
+ static natsStatus
46
+ printStats(int mode, natsConnection *conn, natsSubscription *sub,
47
+ natsStatistics *stats)
48
+ {
49
+ natsStatus s = NATS_OK;
50
+ uint64_t inMsgs, inBytes, outMsgs, outBytes, reconnected;
51
+ int pending = 0;
52
+ int64_t delivered, dropped = 0;
53
+
54
+ s = natsConnection_GetStats(conn, stats);
55
+ if (s == NATS_OK)
56
+ s = natsStatistics_GetCounts(stats, &inMsgs, &inBytes,
57
+ &outMsgs, &outBytes, &reconnected);
58
+ if ((s == NATS_OK) && (sub != NULL))
59
+ {
60
+ s = natsSubscription_GetStats(sub, &pending, NULL, NULL, NULL,
61
+ &delivered, &dropped);
62
+
63
+ // Since we use AutoUnsubscribe(), when the max has been reached,
64
+ // the subscription is automatically closed, so this call would
65
+ // return "Invalid Subscription". Ignore this error.
66
+ if (s == NATS_INVALID_SUBSCRIPTION)
67
+ {
68
+ s = NATS_OK;
69
+ pending = 0;
70
+ }
71
+ }
72
+
73
+ if (s == NATS_OK)
74
+ {
75
+ if (mode & STATS_IN)
76
+ {
77
+ printf("In Msgs: %9" PRIu64 " - "\
78
+ "In Bytes: %9" PRIu64 " - ", inMsgs, inBytes);
79
+ }
80
+ if (mode & STATS_OUT)
81
+ {
82
+ printf("Out Msgs: %9" PRIu64 " - "\
83
+ "Out Bytes: %9" PRIu64 " - ", outMsgs, outBytes);
84
+ }
85
+ if (mode & STATS_COUNT)
86
+ {
87
+ printf("Delivered: %9" PRId64 " - ", delivered);
88
+ printf("Pending: %5d - ", pending);
89
+ printf("Dropped: %5" PRId64 " - ", dropped);
90
+ }
91
+ printf("Reconnected: %3" PRIu64 "\n", reconnected);
92
+ }
93
+
94
+ return s;
95
+ }
96
+
97
+ static void
98
+ printPerf(const char *txt, int64_t count, int64_t start, int64_t elapsed)
99
+ {
100
+ if ((start > 0) && (elapsed == 0))
101
+ elapsed = nats_Now() - start;
102
+
103
+ if (elapsed <= 0)
104
+ printf("\nNot enough messages or too fast to report performance!\n");
105
+ else
106
+ printf("\n%s %" PRId64 " messages in "\
107
+ "%" PRId64 " milliseconds (%d msgs/sec)\n",
108
+ txt, count, elapsed, (int)((count * 1000) / elapsed));
109
+ }
110
+
111
+ static void
112
+ printUsageAndExit(const char *progName, const char *usage)
113
+ {
114
+ printf("\nUsage: %s [options]\n\nThe options are:\n\n"\
115
+ "-h prints the usage\n" \
116
+ "-s server url(s) (list of comma separated nats urls)\n" \
117
+ "-tls use secure (SSL/TLS) connection\n" \
118
+ "-tlscacert trusted certificates file\n" \
119
+ "-tlscert client certificate (PEM format only)\n" \
120
+ "-tlskey client private key file (PEM format only)\n" \
121
+ "-tlsciphers ciphers suite\n"
122
+ "-tlshost server certificate's expected hostname\n" \
123
+ "-tlsskip skip server certificate verification\n" \
124
+ "-subj subject (default is 'foo')\n" \
125
+ "%s\n",
126
+ progName, usage);
127
+
128
+ natsOptions_Destroy(opts);
129
+ nats_Close();
130
+
131
+ exit(1);
132
+ }
133
+
134
+ static natsStatus
135
+ parseUrls(const char *urls, natsOptions *opts)
136
+ {
137
+ char *serverUrls[MAX_SERVERS];
138
+ int count = 0;
139
+ natsStatus s = NATS_OK;
140
+ char *urlsCopy = NULL;
141
+ char *commaPos = NULL;
142
+ char *ptr = NULL;
143
+
144
+ urlsCopy = strdup(urls);
145
+ if (urlsCopy == NULL)
146
+ return NATS_NO_MEMORY;
147
+
148
+ memset(serverUrls, 0, sizeof(serverUrls));
149
+
150
+ ptr = urlsCopy;
151
+
152
+ do
153
+ {
154
+ if (count == MAX_SERVERS)
155
+ {
156
+ s = NATS_INSUFFICIENT_BUFFER;
157
+ break;
158
+ }
159
+
160
+ serverUrls[count++] = ptr;
161
+ commaPos = strchr(ptr, ',');
162
+ if (commaPos != NULL)
163
+ {
164
+ ptr = (char*)(commaPos + 1);
165
+ *(commaPos) = '\0';
166
+ }
167
+ else
168
+ {
169
+ ptr = NULL;
170
+ }
171
+ } while (ptr != NULL);
172
+
173
+ if (s == NATS_OK)
174
+ s = natsOptions_SetServers(opts, (const char**) serverUrls, count);
175
+
176
+ free(urlsCopy);
177
+
178
+ return s;
179
+ }
180
+
181
+ static natsOptions*
182
+ parseArgs(int argc, char **argv, const char *usage)
183
+ {
184
+ natsStatus s = NATS_OK;
185
+ bool urlsSet = false;
186
+ int i;
187
+
188
+ if (natsOptions_Create(&opts) != NATS_OK)
189
+ s = NATS_NO_MEMORY;
190
+
191
+ for (i=1; (i<argc) && (s == NATS_OK); i++)
192
+ {
193
+ if ((strcasecmp(argv[i], "-h") == 0)
194
+ || (strcasecmp(argv[i], "-help") == 0))
195
+ {
196
+ printUsageAndExit(argv[0], usage);
197
+ }
198
+ else if (strcasecmp(argv[i], "-s") == 0)
199
+ {
200
+ if (i + 1 == argc)
201
+ printUsageAndExit(argv[0], usage);
202
+
203
+ s = parseUrls(argv[++i], opts);
204
+ if (s == NATS_OK)
205
+ urlsSet = true;
206
+ }
207
+ else if (strcasecmp(argv[i], "-tls") == 0)
208
+ {
209
+ s = natsOptions_SetSecure(opts, true);
210
+ }
211
+ else if (strcasecmp(argv[i], "-tlscacert") == 0)
212
+ {
213
+ if (i + 1 == argc)
214
+ printUsageAndExit(argv[0], usage);
215
+
216
+ s = natsOptions_LoadCATrustedCertificates(opts, argv[++i]);
217
+ }
218
+ else if (strcasecmp(argv[i], "-tlscert") == 0)
219
+ {
220
+ if (i + 1 == argc)
221
+ printUsageAndExit(argv[0], usage);
222
+
223
+ certFile = argv[++i];
224
+ }
225
+ else if (strcasecmp(argv[i], "-tlskey") == 0)
226
+ {
227
+ if (i + 1 == argc)
228
+ printUsageAndExit(argv[0], usage);
229
+
230
+ keyFile = argv[++i];
231
+ }
232
+ else if (strcasecmp(argv[i], "-tlsciphers") == 0)
233
+ {
234
+ if (i + 1 == argc)
235
+ printUsageAndExit(argv[0], usage);
236
+
237
+ s = natsOptions_SetCiphers(opts, argv[++i]);
238
+ }
239
+ else if (strcasecmp(argv[i], "-tlshost") == 0)
240
+ {
241
+ if (i + 1 == argc)
242
+ printUsageAndExit(argv[0], usage);
243
+
244
+ s = natsOptions_SetExpectedHostname(opts, argv[++i]);
245
+ }
246
+ else if (strcasecmp(argv[i], "-tlsskip") == 0)
247
+ {
248
+ s = natsOptions_SkipServerVerification(opts, true);
249
+ }
250
+ else if (strcasecmp(argv[i], "-sync") == 0)
251
+ {
252
+ async = false;
253
+ }
254
+ else if (strcasecmp(argv[i], "-subj") == 0)
255
+ {
256
+ if (i + 1 == argc)
257
+ printUsageAndExit(argv[0], usage);
258
+
259
+ subj = argv[++i];
260
+ }
261
+ else if ((strcasecmp(argv[i], "-name") == 0) ||
262
+ (strcasecmp(argv[i], "-queue") == 0))
263
+ {
264
+ if (i + 1 == argc)
265
+ printUsageAndExit(argv[0], usage);
266
+
267
+ name = argv[++i];
268
+ }
269
+ else if (strcasecmp(argv[i], "-count") == 0)
270
+ {
271
+ if (i + 1 == argc)
272
+ printUsageAndExit(argv[0], usage);
273
+
274
+ total = atol(argv[++i]);
275
+ }
276
+ else if (strcasecmp(argv[i], "-txt") == 0)
277
+ {
278
+ if (i + 1 == argc)
279
+ printUsageAndExit(argv[0], usage);
280
+
281
+ txt = argv[++i];
282
+ }
283
+ else if (strcasecmp(argv[i], "-timeout") == 0)
284
+ {
285
+ if (i + 1 == argc)
286
+ printUsageAndExit(argv[0], usage);
287
+
288
+ timeout = atol(argv[++i]);
289
+ }
290
+ else if (strcasecmp(argv[i], "-gd") == 0)
291
+ {
292
+ s = natsOptions_UseGlobalMessageDelivery(opts, true);
293
+ }
294
+ else
295
+ {
296
+ printf("Unknown option: '%s'\n", argv[i]);
297
+ printUsageAndExit(argv[0], usage);
298
+ }
299
+ }
300
+
301
+ if ((s == NATS_OK) && ((certFile != NULL) || (keyFile != NULL)))
302
+ s = natsOptions_LoadCertificatesChain(opts, certFile, keyFile);
303
+
304
+ if ((s == NATS_OK) && !urlsSet)
305
+ s = parseUrls(NATS_DEFAULT_URL, opts);
306
+
307
+ if (s != NATS_OK)
308
+ {
309
+ printf("Error parsing arguments: %d - %s\n",
310
+ s, natsStatus_GetText(s));
311
+
312
+ nats_PrintLastErrorStack(stderr);
313
+
314
+ natsOptions_Destroy(opts);
315
+ nats_Close();
316
+ exit(1);
317
+ }
318
+
319
+ return opts;
320
+ }
321
+
322
+ #endif /* EXAMPLES_H_ */