nats-native 1.4.2 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/nats/nats.c +64 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59303adca1de3eae3cc6f715fed26fef9d69c15b
|
4
|
+
data.tar.gz: 1c6a2f9fb87c2386935b1d856d299564b6669ef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba317889fc69b34c99517cc2babb3cb545365c54cff5a9231d86268a08256c2c2758a881ff289fd531a7290e290daf60b761f8fbd214c39fe3a681de2a420c73
|
7
|
+
data.tar.gz: aacf77c70074a08a334f704a6e3d0862283793d31cb290f3314d79721e878f054e24acf88ba96d23c1ecf76949effe9a2a64588030aebb523399904d2333c34d
|
data/ext/nats/nats.c
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#include <stdio.h>
|
1
2
|
#include <nats/nats.h>
|
2
3
|
#include <ruby.h>
|
3
4
|
|
@@ -5,6 +6,18 @@ typedef struct {
|
|
5
6
|
natsConnection *conn;
|
6
7
|
} Connection;
|
7
8
|
|
9
|
+
static void ruby_nats_raise(natsStatus s) {
|
10
|
+
rb_raise(rb_eException, natsStatus_GetText(s));
|
11
|
+
}
|
12
|
+
|
13
|
+
static void ruby_nats_on_error(
|
14
|
+
natsConnection *nc,
|
15
|
+
natsSubscription *sub,
|
16
|
+
natsStatus err,
|
17
|
+
void *closure) {
|
18
|
+
ruby_nats_raise(err);
|
19
|
+
}
|
20
|
+
|
8
21
|
static void ruby_nats_destroy(void *self) {
|
9
22
|
((Connection*) self)->conn = NULL;
|
10
23
|
free(self);
|
@@ -22,13 +35,57 @@ static VALUE ruby_nats_close(VALUE self) {
|
|
22
35
|
return self;
|
23
36
|
}
|
24
37
|
|
25
|
-
static VALUE ruby_nats_connect(VALUE self, VALUE
|
38
|
+
static VALUE ruby_nats_connect(VALUE self, VALUE params) {
|
26
39
|
Connection *conn = NULL;
|
27
40
|
Data_Get_Struct(self, Connection, conn);
|
28
|
-
|
41
|
+
|
42
|
+
natsOptions *opts = NULL;
|
43
|
+
natsOptions_Create(&opts);
|
44
|
+
natsOptions_SetErrorHandler(opts, ruby_nats_on_error, NULL);
|
45
|
+
|
46
|
+
if (RTEST(params)) {
|
47
|
+
VALUE val;
|
48
|
+
|
49
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("allow_reconnect")));
|
50
|
+
RTEST(val) && natsOptions_SetAllowReconnect(opts, RTEST(val));
|
51
|
+
|
52
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("dont_randomize_servers")));
|
53
|
+
RTEST(val) && natsOptions_SetNoRandomize(opts, RTEST(val));
|
54
|
+
|
55
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("max_reconnect")));
|
56
|
+
RTEST(val) && natsOptions_SetMaxReconnect(opts, NUM2INT(val));
|
57
|
+
|
58
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("reconnect_time_wait")));
|
59
|
+
RTEST(val) && natsOptions_SetReconnectWait(opts, NUM2INT(val));
|
60
|
+
|
61
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("servers")));
|
62
|
+
|
63
|
+
if (RTEST(val)) {
|
64
|
+
int array_len = RARRAY_LEN(val);
|
65
|
+
VALUE *val_arr = RARRAY_PTR(val);
|
66
|
+
char **servers = malloc(sizeof(char*) * array_len);
|
67
|
+
|
68
|
+
if (servers == NULL) {
|
69
|
+
rb_raise(rb_eException, "Unable to allocate memory for server uri array");
|
70
|
+
}
|
71
|
+
|
72
|
+
for(int i = 0; i < array_len; i++) {
|
73
|
+
char* uri = StringValuePtr(val_arr[i]);
|
74
|
+
servers[i] = uri;
|
75
|
+
}
|
76
|
+
|
77
|
+
natsOptions_SetServers(opts, servers, array_len);
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
val = rb_hash_aref(params, ID2SYM(rb_intern("timeout")));
|
82
|
+
RTEST(val) && natsOptions_SetTimeout(opts, NUM2INT(val));
|
83
|
+
}
|
84
|
+
|
85
|
+
natsStatus s = natsConnection_Connect(&(conn->conn), opts);
|
29
86
|
|
30
87
|
if (s != NATS_OK) {
|
31
|
-
|
88
|
+
ruby_nats_raise(s);
|
32
89
|
}
|
33
90
|
|
34
91
|
return self;
|
@@ -42,13 +99,12 @@ static VALUE ruby_nats_publish(VALUE self, VALUE subject, VALUE data) {
|
|
42
99
|
StringValuePtr(subject),
|
43
100
|
StringValuePtr(data));
|
44
101
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
// }
|
102
|
+
if (s == NATS_OK) {
|
103
|
+
s = natsConnection_Flush(conn->conn);
|
104
|
+
}
|
49
105
|
|
50
106
|
if (s != NATS_OK) {
|
51
|
-
|
107
|
+
ruby_nats_raise(s);
|
52
108
|
}
|
53
109
|
|
54
110
|
return self;
|