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.
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,136 @@
1
+ // Copyright 2016 Apcera Inc. All rights reserved.
2
+
3
+ #include "../adapters/libevent.h"
4
+ #include "examples.h"
5
+
6
+ #ifndef WIN32
7
+ #include <pthread.h>
8
+ #define THREAD_T pthread_t
9
+ #define THREAD_FN void *
10
+ #define THREAD_RETURN() return (NULL)
11
+ #define THREAD_START(threadvar, fn, arg) \
12
+ pthread_create(&(threadvar), NULL, fn, arg)
13
+ #define THREAD_JOIN(th) pthread_join(th, NULL)
14
+ #else
15
+ #include <process.h>
16
+ #define THREAD_T HANDLE
17
+ #define THREAD_FN unsigned __stdcall
18
+ #define THREAD_RETURN() return (0)
19
+ #define THREAD_START(threadvar, fn, arg) do { \
20
+ uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \
21
+ (threadvar) = (HANDLE) threadhandle; \
22
+ } while (0)
23
+ #define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE)
24
+ #endif
25
+
26
+ static const char *usage = ""\
27
+ "-txt text to send (default is 'hello')\n" \
28
+ "-count number of messages to send\n";
29
+
30
+ typedef struct
31
+ {
32
+ natsConnection *conn;
33
+ natsStatus status;
34
+
35
+ } threadInfo;
36
+
37
+ static THREAD_FN
38
+ pubThread(void *arg)
39
+ {
40
+ threadInfo *info = (threadInfo*) arg;
41
+ natsStatus s = NATS_OK;
42
+
43
+ for (count = 0; (s == NATS_OK) && (count < total); count++)
44
+ s = natsConnection_PublishString(info->conn, subj, txt);
45
+
46
+ if (s == NATS_OK)
47
+ s = natsConnection_Flush(info->conn);
48
+
49
+ natsConnection_Close(info->conn);
50
+
51
+ info->status = s;
52
+
53
+ if (s != NATS_OK)
54
+ nats_PrintLastErrorStack(stderr);
55
+
56
+ THREAD_RETURN();
57
+ }
58
+
59
+ int main(int argc, char **argv)
60
+ {
61
+ natsConnection *conn = NULL;
62
+ natsOptions *opts = NULL;
63
+ natsSubscription *sub = NULL;
64
+ natsStatus s = NATS_OK;
65
+ struct event_base *evLoop= NULL;
66
+ THREAD_T pub;
67
+ threadInfo info;
68
+
69
+ nats_Open(-1);
70
+
71
+ opts = parseArgs(argc, argv, usage);
72
+
73
+ printf("Sending %" PRId64 " messages to subject '%s'\n", total, subj);
74
+
75
+ // One time initialization of things that we need.
76
+ natsLibevent_Init();
77
+
78
+ // Create a loop.
79
+ evLoop = event_base_new();
80
+ if (evLoop == NULL)
81
+ s = NATS_ERR;
82
+
83
+ // Indicate which loop and callbacks to use once connected.
84
+ if (s == NATS_OK)
85
+ s = natsOptions_SetEventLoop(opts, (void*) evLoop,
86
+ natsLibevent_Attach,
87
+ natsLibevent_Read,
88
+ natsLibevent_Write,
89
+ natsLibevent_Detach);
90
+
91
+ if (s == NATS_OK)
92
+ s = natsConnection_Connect(&conn, opts);
93
+
94
+ if (s == NATS_OK)
95
+ start = nats_Now();
96
+
97
+ if (s == NATS_OK)
98
+ {
99
+ info.conn = conn;
100
+ info.status = NATS_OK;
101
+
102
+ THREAD_START(pub, pubThread, (void*) &info);
103
+ }
104
+
105
+ if (s == NATS_OK)
106
+ {
107
+ event_base_dispatch(evLoop);
108
+
109
+ THREAD_JOIN(pub);
110
+ s = info.status;
111
+ }
112
+
113
+ if (s == NATS_OK)
114
+ {
115
+ printPerf("Sent", 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
+ natsSubscription_Destroy(sub);
125
+ natsConnection_Destroy(conn);
126
+ natsOptions_Destroy(opts);
127
+
128
+ if (evLoop != NULL)
129
+ event_base_free(evLoop);
130
+
131
+ // To silence reports of memory still in used with valgrind
132
+ nats_Close();
133
+ libevent_global_shutdown();
134
+
135
+ return 0;
136
+ }
@@ -0,0 +1,104 @@
1
+ // Copyright 2016 Apcera Inc. All rights reserved.
2
+
3
+ #include "../adapters/libevent.h"
4
+ #include "examples.h"
5
+
6
+ static const char *usage = ""\
7
+ "-gd use global message delivery thread pool\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
+ natsMsg_Destroy(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
+ {
29
+ elapsed = nats_Now() - start;
30
+
31
+ natsConnection_Close(nc);
32
+ }
33
+ }
34
+
35
+ int main(int argc, char **argv)
36
+ {
37
+ natsConnection *conn = NULL;
38
+ natsOptions *opts = NULL;
39
+ natsSubscription *sub = NULL;
40
+ natsStatus s = NATS_OK;
41
+ struct event_base *evLoop= NULL;
42
+
43
+ nats_Open(-1);
44
+
45
+ opts = parseArgs(argc, argv, usage);
46
+
47
+ printf("Listening on '%s'.\n", subj);
48
+
49
+ // One time initialization of things that we need.
50
+ natsLibevent_Init();
51
+
52
+ // Create a loop.
53
+ evLoop = event_base_new();
54
+ if (evLoop == NULL)
55
+ s = NATS_ERR;
56
+
57
+ // Indicate which loop and callbacks to use once connected.
58
+ if (s == NATS_OK)
59
+ s = natsOptions_SetEventLoop(opts, (void*) evLoop,
60
+ natsLibevent_Attach,
61
+ natsLibevent_Read,
62
+ natsLibevent_Write,
63
+ natsLibevent_Detach);
64
+
65
+ if (s == NATS_OK)
66
+ s = natsConnection_Connect(&conn, opts);
67
+
68
+ if (s == NATS_OK)
69
+ s = natsConnection_Subscribe(&sub, conn, subj, onMsg, NULL);
70
+
71
+ // For maximum performance, set no limit on the number of pending messages.
72
+ if (s == NATS_OK)
73
+ s = natsSubscription_SetPendingLimits(sub, -1, -1);
74
+
75
+ // Run the event loop.
76
+ // This call will return when the connection is closed (either after
77
+ // receiving all messages, or disconnected and unable to reconnect).
78
+ if (s == NATS_OK)
79
+ event_base_dispatch(evLoop);
80
+
81
+ if (s == NATS_OK)
82
+ {
83
+ printPerf("Received", count, start, elapsed);
84
+ }
85
+ else
86
+ {
87
+ printf("Error: %d - %s\n", s, natsStatus_GetText(s));
88
+ nats_PrintLastErrorStack(stderr);
89
+ }
90
+
91
+ // Destroy all our objects to avoid report of memory leak
92
+ natsSubscription_Destroy(sub);
93
+ natsConnection_Destroy(conn);
94
+ natsOptions_Destroy(opts);
95
+
96
+ if (evLoop != NULL)
97
+ event_base_free(evLoop);
98
+
99
+ // To silence reports of memory still in used with valgrind
100
+ nats_Close();
101
+ libevent_global_shutdown();
102
+
103
+ return 0;
104
+ }
@@ -0,0 +1,120 @@
1
+ // Copyright 2016 Apcera Inc. All rights reserved.
2
+
3
+ #include "../adapters/libuv.h"
4
+ #include "examples.h"
5
+
6
+ static const char *usage = ""\
7
+ "-txt text to send (default is 'hello')\n" \
8
+ "-count number of messages to send\n";
9
+
10
+ typedef struct
11
+ {
12
+ natsConnection *conn;
13
+ natsStatus status;
14
+
15
+ } threadInfo;
16
+
17
+ static void
18
+ pubThread(void *arg)
19
+ {
20
+ threadInfo *info = (threadInfo*) arg;
21
+ natsStatus s = NATS_OK;
22
+
23
+ for (count = 0; (s == NATS_OK) && (count < total); count++)
24
+ s = natsConnection_PublishString(info->conn, subj, txt);
25
+
26
+ if (s == NATS_OK)
27
+ s = natsConnection_Flush(info->conn);
28
+
29
+ natsConnection_Close(info->conn);
30
+
31
+ info->status = s;
32
+ }
33
+
34
+ int main(int argc, char **argv)
35
+ {
36
+ natsConnection *conn = NULL;
37
+ natsOptions *opts = NULL;
38
+ natsSubscription *sub = NULL;
39
+ natsStatus s = NATS_OK;
40
+ uv_loop_t *uvLoop= NULL;
41
+ uv_thread_t pub;
42
+ threadInfo info;
43
+
44
+ opts = parseArgs(argc, argv, usage);
45
+
46
+ printf("Sending %" PRId64 " messages to subject '%s'\n", total, subj);
47
+
48
+ // One time initialization of things that we need.
49
+ natsLibuv_Init();
50
+
51
+ // Create a loop.
52
+ uvLoop = uv_default_loop();
53
+ if (uvLoop != NULL)
54
+ {
55
+ // Libuv is not thread-safe. Almost all calls to libuv need to
56
+ // occur from the thread where the loop is running. NATS library
57
+ // may have to call into the event loop from different threads.
58
+ // This call allows natsLibuv APIs to know if they are executing
59
+ // from the event loop thread or not.
60
+ natsLibuv_SetThreadLocalLoop(uvLoop);
61
+ }
62
+ else
63
+ {
64
+ s = NATS_ERR;
65
+ }
66
+
67
+ // Indicate which loop and callbacks to use once connected.
68
+ if (s == NATS_OK)
69
+ s = natsOptions_SetEventLoop(opts, (void*) uvLoop,
70
+ natsLibuv_Attach,
71
+ natsLibuv_Read,
72
+ natsLibuv_Write,
73
+ natsLibuv_Detach);
74
+
75
+ if (s == NATS_OK)
76
+ s = natsConnection_Connect(&conn, opts);
77
+
78
+ if (s == NATS_OK)
79
+ start = nats_Now();
80
+
81
+ if (s == NATS_OK)
82
+ {
83
+ info.conn = conn;
84
+ info.status = NATS_OK;
85
+
86
+ if (uv_thread_create(&pub, pubThread, (void*) &info) != 0)
87
+ s = NATS_ERR;
88
+ }
89
+
90
+ if (s == NATS_OK)
91
+ {
92
+ uv_run(uvLoop, UV_RUN_DEFAULT);
93
+
94
+ uv_thread_join(&pub);
95
+ s = info.status;
96
+ }
97
+
98
+ if (s == NATS_OK)
99
+ {
100
+ printPerf("Sent", count, start, elapsed);
101
+ }
102
+ else
103
+ {
104
+ printf("Error: %d - %s\n", s, natsStatus_GetText(s));
105
+ nats_PrintLastErrorStack(stderr);
106
+ }
107
+
108
+ // Destroy all our objects to avoid report of memory leak
109
+ natsSubscription_Destroy(sub);
110
+ natsConnection_Destroy(conn);
111
+ natsOptions_Destroy(opts);
112
+
113
+ if (uvLoop != NULL)
114
+ uv_loop_close(uvLoop);
115
+
116
+ // To silence reports of memory still in used with valgrind
117
+ nats_Close();
118
+
119
+ return 0;
120
+ }
@@ -0,0 +1,114 @@
1
+ // Copyright 2016 Apcera Inc. All rights reserved.
2
+
3
+ #include "../adapters/libuv.h"
4
+ #include "examples.h"
5
+
6
+ static const char *usage = ""\
7
+ "-gd use global message delivery thread pool\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
+ natsMsg_Destroy(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
+ {
29
+ elapsed = nats_Now() - start;
30
+
31
+ natsConnection_Close(nc);
32
+ }
33
+ }
34
+
35
+ int main(int argc, char **argv)
36
+ {
37
+ natsConnection *conn = NULL;
38
+ natsOptions *opts = NULL;
39
+ natsSubscription *sub = NULL;
40
+ natsStatus s = NATS_OK;
41
+ uv_loop_t *uvLoop= NULL;
42
+
43
+ opts = parseArgs(argc, argv, usage);
44
+
45
+ printf("Listening on '%s'.\n", subj);
46
+
47
+ // One time initialization of things that we need.
48
+ natsLibuv_Init();
49
+
50
+ // Create a loop.
51
+ uvLoop = uv_default_loop();
52
+ if (uvLoop != NULL)
53
+ {
54
+ // Libuv is not thread-safe. Almost all calls to libuv need to
55
+ // occur from the thread where the loop is running. NATS library
56
+ // may have to call into the event loop from different threads.
57
+ // This call allows natsLibuv APIs to know if they are executing
58
+ // from the event loop thread or not.
59
+ natsLibuv_SetThreadLocalLoop(uvLoop);
60
+ }
61
+ else
62
+ {
63
+ s = NATS_ERR;
64
+ }
65
+
66
+ // Indicate which loop and callbacks to use once connected.
67
+ if (s == NATS_OK)
68
+ s = natsOptions_SetEventLoop(opts, (void*) uvLoop,
69
+ natsLibuv_Attach,
70
+ natsLibuv_Read,
71
+ natsLibuv_Write,
72
+ natsLibuv_Detach);
73
+
74
+ if (s == NATS_OK)
75
+ s = natsConnection_Connect(&conn, opts);
76
+
77
+ if (s == NATS_OK)
78
+ s = natsConnection_Subscribe(&sub, conn, subj, onMsg, NULL);
79
+
80
+ // For maximum performance, set no limit on the number of pending messages.
81
+ if (s == NATS_OK)
82
+ s = natsSubscription_SetPendingLimits(sub, -1, -1);
83
+
84
+ // Run the event loop.
85
+ // This call will return when the connection is closed (either after
86
+ // receiving all messages, or disconnected and unable to reconnect).
87
+ if (s == NATS_OK)
88
+ {
89
+ uv_run(uvLoop, UV_RUN_DEFAULT);
90
+ }
91
+
92
+ if (s == NATS_OK)
93
+ {
94
+ printPerf("Received", count, start, elapsed);
95
+ }
96
+ else
97
+ {
98
+ printf("Error: %d - %s\n", s, natsStatus_GetText(s));
99
+ nats_PrintLastErrorStack(stderr);
100
+ }
101
+
102
+ // Destroy all our objects to avoid report of memory leak
103
+ natsSubscription_Destroy(sub);
104
+ natsConnection_Destroy(conn);
105
+ natsOptions_Destroy(opts);
106
+
107
+ if (uvLoop != NULL)
108
+ uv_loop_close(uvLoop);
109
+
110
+ // To silence reports of memory still in used with valgrind
111
+ nats_Close();
112
+
113
+ return 0;
114
+ }