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,34 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef SUB_H_
4
+ #define SUB_H_
5
+
6
+ #include "natsp.h"
7
+
8
+ #ifdef DEV_MODE
9
+ // For type safety...
10
+
11
+ void natsSub_Lock(natsSubscription *sub);
12
+ void natsSub_Unlock(natsSubscription *sub);
13
+
14
+ #else
15
+
16
+ #define natsSub_Lock(s) natsMutex_Lock((s)->mu)
17
+ #define natsSub_Unlock(s) natsMutex_Unlock((s)->mu)
18
+
19
+ #endif // DEV_MODE
20
+
21
+ void
22
+ natsSub_retain(natsSubscription *sub);
23
+
24
+ void
25
+ natsSub_release(natsSubscription *sub);
26
+
27
+ natsStatus
28
+ natsSub_create(natsSubscription **newSub, natsConnection *nc, const char *subj,
29
+ const char *queueGroup, int64_t timeout, natsMsgHandler cb, void *cbClosure);
30
+
31
+ void
32
+ natsSub_close(natsSubscription *sub, bool connectionClosed);
33
+
34
+ #endif /* SUB_H_ */
@@ -0,0 +1,86 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include "natsp.h"
4
+ #include "mem.h"
5
+ #include "util.h"
6
+
7
+ static void
8
+ _freeTimer(natsTimer *t)
9
+ {
10
+ if (t == NULL)
11
+ return;
12
+
13
+ natsMutex_Destroy(t->mu);
14
+ NATS_FREE(t);
15
+ }
16
+
17
+ void
18
+ natsTimer_Release(natsTimer *t)
19
+ {
20
+ int refs = 0;
21
+
22
+ natsMutex_Lock(t->mu);
23
+
24
+ refs = --(t->refs);
25
+
26
+ natsMutex_Unlock(t->mu);
27
+
28
+ if (refs == 0)
29
+ _freeTimer(t);
30
+ }
31
+
32
+ natsStatus
33
+ natsTimer_Create(natsTimer **timer, natsTimerCb timerCb, natsTimerStopCb stopCb,
34
+ int64_t interval, void* closure)
35
+ {
36
+ natsStatus s = NATS_OK;
37
+ natsTimer *t = (natsTimer*) NATS_CALLOC(1, sizeof(natsTimer));
38
+
39
+ if (t == NULL)
40
+ return nats_setDefaultError(NATS_NO_MEMORY);
41
+
42
+ t->refs = 1;
43
+ t->cb = timerCb;
44
+ t->stopCb = stopCb;
45
+ t->closure = closure;
46
+
47
+ s = natsMutex_Create(&(t->mu));
48
+ if (s == NATS_OK)
49
+ {
50
+ // Doing so, so that nats_resetTimer() does not try to remove the timer
51
+ // from the list (since it is new it would not be there!).
52
+ t->stopped = true;
53
+
54
+ nats_resetTimer(t, interval);
55
+
56
+ *timer = t;
57
+ }
58
+ else
59
+ _freeTimer(t);
60
+
61
+ return NATS_UPDATE_ERR_STACK(s);
62
+ }
63
+
64
+ void
65
+ natsTimer_Stop(natsTimer *timer)
66
+ {
67
+ // Proxy for this call:
68
+ nats_stopTimer(timer);
69
+ }
70
+
71
+ void
72
+ natsTimer_Reset(natsTimer *timer, int64_t interval)
73
+ {
74
+ // Proxy for this call:
75
+ nats_resetTimer(timer, interval);
76
+ }
77
+
78
+ void
79
+ natsTimer_Destroy(natsTimer *timer)
80
+ {
81
+ if (timer == NULL)
82
+ return;
83
+
84
+ nats_stopTimer(timer);
85
+ natsTimer_Release(timer);
86
+ }
@@ -0,0 +1,57 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef TIMER_H_
4
+ #define TIMER_H_
5
+
6
+ #include <stdint.h>
7
+
8
+ //#include "natsp.h"
9
+ #include "status.h"
10
+
11
+ struct __natsTimer;
12
+
13
+ // Callback signature for timer
14
+ typedef void (*natsTimerCb)(struct __natsTimer *timer, void* closure);
15
+
16
+ // Callback invoked when the timer has been stopped and guaranteed
17
+ // not to be in the timer callback.
18
+ typedef void (*natsTimerStopCb)(struct __natsTimer *timer, void* closure);
19
+
20
+ typedef struct __natsTimer
21
+ {
22
+ struct __natsTimer *prev;
23
+ struct __natsTimer *next;
24
+
25
+ natsMutex *mu;
26
+ int refs;
27
+
28
+ natsTimerCb cb;
29
+ natsTimerStopCb stopCb;
30
+ void* closure;
31
+
32
+ int64_t interval;
33
+ int64_t absoluteTime;
34
+
35
+ bool stopped;
36
+ bool inCallback;
37
+
38
+ } natsTimer;
39
+
40
+ natsStatus
41
+ natsTimer_Create(natsTimer **timer, natsTimerCb timerCb, natsTimerStopCb stopCb,
42
+ int64_t interval, void* closure);
43
+
44
+ void
45
+ natsTimer_Stop(natsTimer *timer);
46
+
47
+ void
48
+ natsTimer_Reset(natsTimer *timer, int64_t interval);
49
+
50
+ void
51
+ natsTimer_Release(natsTimer *t);
52
+
53
+ void
54
+ natsTimer_Destroy(natsTimer *timer);
55
+
56
+
57
+ #endif /* TIMER_H_ */
@@ -0,0 +1,103 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include "../natsp.h"
4
+
5
+ #include <errno.h>
6
+
7
+ #include "../util.h"
8
+ #include "../mem.h"
9
+
10
+ natsStatus
11
+ natsCondition_Create(natsCondition **cond)
12
+ {
13
+ natsCondition *c = (natsCondition*) NATS_CALLOC(1, sizeof(natsCondition));
14
+ natsStatus s = NATS_OK;
15
+
16
+ if (c == NULL)
17
+ return nats_setDefaultError(NATS_NO_MEMORY);
18
+
19
+ if (pthread_cond_init(c, NULL) != 0)
20
+ s = nats_setError(NATS_SYS_ERROR, "pthread_cond_init error: %d", errno);
21
+
22
+ if (s == NATS_OK)
23
+ *cond = c;
24
+ else
25
+ NATS_FREE(c);
26
+
27
+ return s;
28
+ }
29
+
30
+ void
31
+ natsCondition_Wait(natsCondition *cond, natsMutex *mutex)
32
+ {
33
+ if (pthread_cond_wait(cond, mutex) != 0)
34
+ abort();
35
+ }
36
+
37
+ static natsStatus
38
+ _timedWait(natsCondition *cond, natsMutex *mutex, bool isAbsolute, int64_t timeout)
39
+ {
40
+ int r;
41
+ struct timespec ts;
42
+ int64_t target;
43
+
44
+ if (timeout <= 0)
45
+ return NATS_TIMEOUT;
46
+
47
+ target = (isAbsolute ? timeout : (nats_Now() + timeout));
48
+
49
+ ts.tv_sec = target / 1000;
50
+ ts.tv_nsec = (target % 1000) * 1000000;
51
+
52
+ if (ts.tv_nsec >= 1000000000L)
53
+ {
54
+ ts.tv_sec++;
55
+ ts.tv_nsec -= 1000000000L;
56
+ }
57
+
58
+ r = pthread_cond_timedwait(cond, mutex, &ts);
59
+
60
+ if (r == 0)
61
+ return NATS_OK;
62
+
63
+ if (r == ETIMEDOUT)
64
+ return NATS_TIMEOUT;
65
+
66
+ return nats_setError(NATS_SYS_ERROR, "pthread_cond_timedwait error: %d", errno);
67
+ }
68
+
69
+ natsStatus
70
+ natsCondition_TimedWait(natsCondition *cond, natsMutex *mutex, int64_t timeout)
71
+ {
72
+ return _timedWait(cond, mutex, false, timeout);
73
+ }
74
+
75
+ natsStatus
76
+ natsCondition_AbsoluteTimedWait(natsCondition *cond, natsMutex *mutex, int64_t absoluteTime)
77
+ {
78
+ return _timedWait(cond, mutex, true, absoluteTime);
79
+ }
80
+
81
+ void
82
+ natsCondition_Signal(natsCondition *cond)
83
+ {
84
+ if (pthread_cond_signal(cond) != 0)
85
+ abort();
86
+ }
87
+
88
+ void
89
+ natsCondition_Broadcast(natsCondition *cond)
90
+ {
91
+ if (pthread_cond_broadcast(cond) != 0)
92
+ abort();
93
+ }
94
+
95
+ void
96
+ natsCondition_Destroy(natsCondition *cond)
97
+ {
98
+ if (cond == NULL)
99
+ return;
100
+
101
+ pthread_cond_destroy(cond);
102
+ NATS_FREE(cond);
103
+ }
@@ -0,0 +1,107 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include "../natsp.h"
4
+ #include "../mem.h"
5
+
6
+ natsStatus
7
+ natsMutex_Create(natsMutex **newMutex)
8
+ {
9
+ natsStatus s = NATS_OK;
10
+ pthread_mutexattr_t attr;
11
+ natsMutex *m = NATS_CALLOC(1, sizeof(natsMutex));
12
+ bool noAttrDestroy = false;
13
+
14
+ if (m == NULL)
15
+ s = nats_setDefaultError(NATS_NO_MEMORY);
16
+
17
+ if ((s == NATS_OK)
18
+ && (pthread_mutexattr_init(&attr) != 0)
19
+ && (noAttrDestroy = true))
20
+ {
21
+ s = nats_setError(NATS_SYS_ERROR, "pthread_mutexattr_init error: %d",
22
+ errno);
23
+ }
24
+
25
+ if ((s == NATS_OK)
26
+ && (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0))
27
+ {
28
+ s = nats_setError(NATS_SYS_ERROR, "pthread_mutexattr_settype error: %d",
29
+ errno);
30
+ }
31
+
32
+ if ((s == NATS_OK)
33
+ && (pthread_mutex_init(m, &attr) != 0))
34
+ {
35
+ s = nats_setError(NATS_SYS_ERROR, "pthread_mutex_init error: %d",
36
+ errno);
37
+ }
38
+
39
+ if (!noAttrDestroy)
40
+ pthread_mutexattr_destroy(&attr);
41
+
42
+ if (s == NATS_OK)
43
+ *newMutex = m;
44
+ else
45
+ NATS_FREE(m);
46
+
47
+ return s;
48
+ }
49
+
50
+ bool
51
+ natsMutex_TryLock(natsMutex *m)
52
+ {
53
+ if (pthread_mutex_trylock(m) == 0)
54
+ return true;
55
+
56
+ return false;
57
+ }
58
+
59
+ void
60
+ natsMutex_Lock(natsMutex *m)
61
+ {
62
+ // The "rep" instruction used for spinning is not supported on ARM.
63
+ #ifndef __arm__
64
+ if (gLockSpinCount > 0)
65
+ {
66
+ int64_t attempts = 0;
67
+
68
+ while (pthread_mutex_trylock(m) != 0)
69
+ {
70
+ if (++attempts <= gLockSpinCount)
71
+ {
72
+ __asm__ __volatile__ ("rep; nop");
73
+ }
74
+ else
75
+ {
76
+ if (pthread_mutex_lock(m))
77
+ abort();
78
+
79
+ break;
80
+ }
81
+ }
82
+ }
83
+ else
84
+ #endif
85
+ {
86
+ if (pthread_mutex_lock(m))
87
+ abort();
88
+ }
89
+ }
90
+
91
+
92
+ void
93
+ natsMutex_Unlock(natsMutex *m)
94
+ {
95
+ if (pthread_mutex_unlock(m))
96
+ abort();
97
+ }
98
+
99
+ void
100
+ natsMutex_Destroy(natsMutex *m)
101
+ {
102
+ if (m == NULL)
103
+ return;
104
+
105
+ pthread_mutex_destroy(m);
106
+ NATS_FREE(m);
107
+ }
@@ -0,0 +1,105 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include "../natsp.h"
4
+ #include "../mem.h"
5
+ #include "../comsock.h"
6
+
7
+ void
8
+ natsSys_Init(void)
9
+ {
10
+ // Would do anything that needs to be initialized when
11
+ // the libary loads, specific to unix.
12
+ }
13
+
14
+ natsStatus
15
+ natsSock_Init(natsSockCtx *ctx)
16
+ {
17
+ memset(ctx, 0, sizeof(natsSockCtx));
18
+
19
+ ctx->fd = NATS_SOCK_INVALID;
20
+
21
+
22
+ return natsSock_CreateFDSet(&ctx->fdSet);
23
+ }
24
+
25
+ void
26
+ natsSock_Clear(natsSockCtx *ctx)
27
+ {
28
+ natsSock_DestroyFDSet(ctx->fdSet);
29
+ }
30
+
31
+ natsStatus
32
+ natsSock_WaitReady(int waitMode, natsSockCtx *ctx)
33
+ {
34
+ struct timeval *timeout = NULL;
35
+ int res;
36
+ fd_set *fdSet = ctx->fdSet;
37
+ natsSock sock = ctx->fd;
38
+ natsDeadline *deadline = &(ctx->deadline);
39
+
40
+ FD_ZERO(fdSet);
41
+ FD_SET(sock, fdSet);
42
+
43
+ if (deadline != NULL)
44
+ timeout = natsDeadline_GetTimeout(deadline);
45
+
46
+ switch (waitMode)
47
+ {
48
+ case WAIT_FOR_READ: res = select((int) (sock + 1), fdSet, NULL, NULL, timeout); break;
49
+ case WAIT_FOR_WRITE: res = select((int) (sock + 1), NULL, fdSet, NULL, timeout); break;
50
+ case WAIT_FOR_CONNECT: res = select((int) (sock + 1), NULL, fdSet, NULL, timeout); break;
51
+ default: abort();
52
+ }
53
+
54
+ if (res == NATS_SOCK_ERROR)
55
+ return nats_setError(NATS_IO_ERROR, "select error: %d", res);
56
+
57
+ if ((res == 0) || !FD_ISSET(sock, fdSet))
58
+ return nats_setDefaultError(NATS_TIMEOUT);
59
+
60
+ return NATS_OK;
61
+ }
62
+
63
+ natsStatus
64
+ natsSock_SetBlocking(natsSock fd, bool blocking)
65
+ {
66
+ int flags;
67
+
68
+ if ((flags = fcntl(fd, F_GETFL)) == -1)
69
+ return nats_setError(NATS_SYS_ERROR, "fcntl error: %d", errno);
70
+
71
+ if (blocking)
72
+ flags &= ~O_NONBLOCK;
73
+ else
74
+ flags |= O_NONBLOCK;
75
+
76
+ if (fcntl(fd, F_SETFL, flags) == -1)
77
+ return nats_setError(NATS_SYS_ERROR, "fcntl error: %d", errno);
78
+
79
+ return NATS_OK;
80
+ }
81
+
82
+ bool
83
+ natsSock_IsConnected(natsSock fd)
84
+ {
85
+ int res;
86
+ int error = 0;
87
+ socklen_t errorLen = (socklen_t) sizeof(int);
88
+
89
+ res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &errorLen);
90
+ if ((res == NATS_SOCK_ERROR) || (error != 0))
91
+ return false;
92
+
93
+ return true;
94
+ }
95
+
96
+ natsStatus
97
+ natsSock_Flush(natsSock fd)
98
+ {
99
+ if (fsync(fd) != 0)
100
+ return nats_setError(NATS_IO_ERROR,
101
+ "Error flushing socket. Error: %d",
102
+ NATS_SOCK_GET_ERROR);
103
+
104
+ return NATS_OK;
105
+ }