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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/ext/nats/nats.c +64 -8
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abb5c32131873c0aaa14167195df0ca4907e2b53
4
- data.tar.gz: e9aef42d402f201419a0f388dc8761e80266ce94
3
+ metadata.gz: 59303adca1de3eae3cc6f715fed26fef9d69c15b
4
+ data.tar.gz: 1c6a2f9fb87c2386935b1d856d299564b6669ef4
5
5
  SHA512:
6
- metadata.gz: 37b54b49ff9c650799dbcb0a39050cd8846140535b8326e71513682cd9efda1ba37edf5d0e12489211223191680c62d8fe1dbff9542708ac49fb2dfd9a317cce
7
- data.tar.gz: 8edfc4e9306562278265093c9c0a439c3e41560be52eee12859508257ca680a22ce2b276730752dd2c9bd5a8a6de0534ca25d7fba4d7c5846bad9f1d9a4aebfd
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 uri) {
38
+ static VALUE ruby_nats_connect(VALUE self, VALUE params) {
26
39
  Connection *conn = NULL;
27
40
  Data_Get_Struct(self, Connection, conn);
28
- natsStatus s = natsConnection_Connect(&(conn->conn), NULL);
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
- rb_raise(rb_eException, natsStatus_GetText(s));
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
- // if (s == NATS_OK) {
46
- // s = natsConnection_FlushTimeconn(conn->conn, 1000);
47
- // s = natsConnection_Flush(nc);
48
- // }
102
+ if (s == NATS_OK) {
103
+ s = natsConnection_Flush(conn->conn);
104
+ }
49
105
 
50
106
  if (s != NATS_OK) {
51
- rb_raise(rb_eException, natsStatus_GetText(s));
107
+ ruby_nats_raise(s);
52
108
  }
53
109
 
54
110
  return self;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nats-native
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ron Elliott