nng 0.1.0.alpha.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 00d81bda694b3cd8462da25c8164aa36f2f3deef9c193137676685dfcca23b8f
4
+ data.tar.gz: cc226d519edf60e5d6c11f07cd712a256f5a2bb38ff173b0d2d35afd308189da
5
+ SHA512:
6
+ metadata.gz: 46a65accea3794de74022cb7d053a80f508089dd10e920e82ba3ff18a02013e21c66010c85bdcc536a07183cf11464a84a0d47e728c9c6d0720f2ad280344e37
7
+ data.tar.gz: 85a52e9c5814801b73a2ec7ea8bcba92a1f5d053f3c04dcc56c45c9d7bd8279532fad11b080109d5a08895e51bc7c32e685d70dd0bf40ff86ab717bc31ae2070
@@ -0,0 +1,21 @@
1
+ #
2
+ # Copyright (c) 2021 Adib Saad
3
+ #
4
+ require 'mkmf'
5
+ dir_config('nng')
6
+
7
+ def header?
8
+ have_header('nng/nng.h') ||
9
+ find_header('nng/nng.h', '/opt/local/include', '/usr/local/include', '/usr/include')
10
+ end
11
+
12
+ def library?
13
+ have_library('nng', 'nng_fini') ||
14
+ find_library('nng', 'nng_fini', '/opt/local/lib', '/usr/local/lib', '/usr/lib')
15
+ end
16
+
17
+ if header? && library?
18
+ create_makefile("nng/rbnng")
19
+ else
20
+ raise "Couldn't find nng library. try setting --with-nng-dir=<path> to tell me where it is."
21
+ end
data/ext/rbnng/msg.c ADDED
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #include "msg.h"
7
+ #include <nng/protocol/reqrep0/rep.h>
8
+ #include <ruby.h>
9
+ #include <signal.h>
10
+
11
+ VALUE rbnng_MsgClass = Qnil;
12
+
13
+ void
14
+ msg_free(void* ptr)
15
+ {
16
+ RbnngMsg* p_rbnngMsg = ptr;
17
+ if (p_rbnngMsg->msg) {
18
+ nng_msg_free(p_rbnngMsg->msg);
19
+ }
20
+ xfree(p_rbnngMsg);
21
+ }
22
+
23
+ static VALUE
24
+ msg_alloc(VALUE klass)
25
+ {
26
+ RbnngMsg* p_rbnngMsg = ZALLOC(RbnngMsg);
27
+ return rb_data_object_wrap(klass, p_rbnngMsg, 0, msg_free);
28
+ }
29
+
30
+ static VALUE
31
+ msg_body(VALUE self)
32
+ {
33
+ RbnngMsg* p_rbnngMsg;
34
+ Data_Get_Struct(self, RbnngMsg, p_rbnngMsg);
35
+ if (p_rbnngMsg->msg) {
36
+ return rb_str_new(nng_msg_body(p_rbnngMsg->msg),
37
+ nng_msg_len(p_rbnngMsg->msg));
38
+ } else {
39
+ return rb_str_new_cstr("");
40
+ }
41
+ }
42
+
43
+ static VALUE
44
+ msg_header(VALUE self)
45
+ {
46
+ RbnngMsg* p_rbnngMsg;
47
+ Data_Get_Struct(self, RbnngMsg, p_rbnngMsg);
48
+ if (p_rbnngMsg->msg) {
49
+ return rb_str_new(nng_msg_header(p_rbnngMsg->msg),
50
+ nng_msg_header_len(p_rbnngMsg->msg));
51
+ } else {
52
+ return rb_str_new_cstr("");
53
+ }
54
+ }
55
+
56
+ void
57
+ rbnng_msg_Init(VALUE nng_module)
58
+ {
59
+ rbnng_MsgClass = rb_define_class_under(nng_module, "Msg", rb_cObject);
60
+ rb_define_alloc_func(rbnng_MsgClass, msg_alloc);
61
+ rb_define_method(rbnng_MsgClass, "body", msg_body, 0);
62
+ rb_define_method(rbnng_MsgClass, "header", msg_header, 0);
63
+ }
data/ext/rbnng/msg.h ADDED
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #ifndef RBNNG_MSG_H
7
+ #define RBNNG_MSG_H
8
+
9
+ #include <nng/nng.h>
10
+ #include <ruby.h>
11
+
12
+ extern VALUE rbnng_MsgClass;
13
+
14
+ typedef struct
15
+ {
16
+ nng_msg* msg;
17
+ } RbnngMsg;
18
+
19
+ extern void
20
+ rbnng_msg_Init(VALUE nng_module);
21
+
22
+ #endif
data/ext/rbnng/rbnng.c ADDED
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #include <nng/nng.h>
7
+ #include <ruby.h>
8
+
9
+ #include "rbnng.h"
10
+ #include "socket.h"
11
+ #include "msg.h"
12
+
13
+ VALUE rbnng_exceptionClass = Qnil;
14
+
15
+ static VALUE
16
+ library_version(VALUE self_)
17
+ {
18
+ return rb_ary_new3(3,
19
+ INT2NUM(NNG_MAJOR_VERSION),
20
+ INT2NUM(NNG_MINOR_VERSION),
21
+ INT2NUM(NNG_PATCH_VERSION));
22
+ }
23
+
24
+ void
25
+ Init_rbnng()
26
+ {
27
+ VALUE nng_module = rb_define_module("NNG");
28
+ rb_define_singleton_method(nng_module, "nng_version", library_version, 0);
29
+ rbnng_exceptionClass = rb_define_class_under(nng_module, "Error", rb_eRuntimeError);
30
+
31
+ rbnng_rep0_Init(nng_module);
32
+ rbnng_req0_Init(nng_module);
33
+ rbnng_msg_Init(nng_module);
34
+ }
data/ext/rbnng/rbnng.h ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #ifndef RBNNG_H
7
+ #define RBNNG_H
8
+
9
+ #include <ruby.h>
10
+
11
+ extern VALUE rbnng_exceptionClass;
12
+
13
+ #endif
data/ext/rbnng/rep0.c ADDED
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #include "msg.h"
7
+ #include "rbnng.h"
8
+ #include "socket.h"
9
+ #include <nng/protocol/reqrep0/rep.h>
10
+ #include <ruby.h>
11
+ #include <ruby/thread.h>
12
+
13
+ static VALUE
14
+ socket_rep0_listen(VALUE self, VALUE url)
15
+ {
16
+ Check_Type(url, T_STRING);
17
+ int rv;
18
+ RbnngSocket* p_rbnngSocket;
19
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
20
+ if ((rv = nng_listen(p_rbnngSocket->socket, StringValueCStr(url), NULL, 0)) !=
21
+ 0) {
22
+ rb_raise(rbnng_exceptionClass, "nng_listen %d", rv);
23
+ }
24
+ return self;
25
+ }
26
+
27
+ void*
28
+ rep0_get_msg_blocking(void* data)
29
+ {
30
+ VALUE self = (VALUE)data;
31
+ RbnngSocket* p_rbnngSocket;
32
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
33
+
34
+ nng_aio* p_aio;
35
+ int rv;
36
+ if ((rv = nng_aio_alloc(&p_aio, 0, 0)) != 0) {
37
+ rb_raise(rbnng_exceptionClass, "nng_aio_alloc %d", rv);
38
+ }
39
+
40
+ nng_ctx_recv(p_rbnngSocket->ctx, p_aio);
41
+ nng_aio_wait(p_aio);
42
+
43
+ if ((rv = nng_aio_result(p_aio)) != 0) {
44
+ rb_raise(rbnng_exceptionClass, "nng_aio_result %d", rv);
45
+ }
46
+
47
+ nng_msg* p_msg = nng_aio_get_msg(p_aio);
48
+ nng_aio_free(p_aio);
49
+ return p_msg;
50
+ }
51
+
52
+ static VALUE
53
+ socket_rep0_get_msg(VALUE self)
54
+ {
55
+ nng_msg* nng_msg_ptr =
56
+ rb_thread_call_without_gvl(rep0_get_msg_blocking, self, 0, 0);
57
+
58
+ if (nng_msg_ptr) {
59
+ RbnngMsg* rbnng_new_msg_ptr;
60
+ VALUE newMsg = rb_class_new_instance(0, 0, rbnng_MsgClass);
61
+ Data_Get_Struct(newMsg, RbnngMsg, rbnng_new_msg_ptr);
62
+ rbnng_new_msg_ptr->msg = nng_msg_ptr;
63
+ return newMsg;
64
+ } else {
65
+ VALUE newMsg = rb_class_new_instance(0, 0, rbnng_MsgClass);
66
+ return newMsg;
67
+ }
68
+ }
69
+
70
+ void*
71
+ rep0_send_msg_blocking(void* data)
72
+ {
73
+ RbnngSendMsgReq* p_sendMsgReq = (RbnngSendMsgReq*)data;
74
+ RbnngSocket* p_rbnngSocket;
75
+ Data_Get_Struct(p_sendMsgReq->socketObj, RbnngSocket, p_rbnngSocket);
76
+
77
+ nng_aio* p_aio;
78
+ int rv;
79
+ if ((rv = nng_aio_alloc(&p_aio, 0, 0)) != 0) {
80
+ rb_raise(rbnng_exceptionClass, "nng_aio_alloc %d", rv);
81
+ }
82
+
83
+ nng_msg* p_msg;
84
+ if ((rv = nng_msg_alloc(&p_msg, 0)) != 0) {
85
+ rb_raise(rbnng_exceptionClass, "nng_msg_alloc %d", rv);
86
+ }
87
+
88
+ nng_msg_clear(p_msg);
89
+ nng_msg_append(p_msg,
90
+ StringValuePtr(p_sendMsgReq->nextMsg),
91
+ RSTRING_LEN(p_sendMsgReq->nextMsg));
92
+
93
+ nng_aio_set_msg(p_aio, p_msg);
94
+ nng_ctx_send(p_rbnngSocket->ctx, p_aio);
95
+ nng_aio_wait(p_aio);
96
+
97
+ if ((rv = nng_aio_result(p_aio)) != 0) {
98
+ rb_raise(rbnng_exceptionClass, "nng_aio_result %d", rv);
99
+ }
100
+
101
+ nng_aio_free(p_aio);
102
+ }
103
+
104
+ static VALUE
105
+ socket_rep0_send_msg(VALUE self, VALUE rb_strMsg)
106
+ {
107
+ Check_Type(rb_strMsg, T_STRING);
108
+
109
+ RbnngSendMsgReq sendMsgReq = {
110
+ .socketObj = self,
111
+ .nextMsg = rb_strMsg,
112
+ };
113
+ rb_thread_call_without_gvl(rep0_send_msg_blocking, &sendMsgReq, 0, 0);
114
+ }
115
+
116
+ static VALUE
117
+ socket_rep0_initialize(VALUE self)
118
+ {
119
+ int rv;
120
+ RbnngSocket* p_rbnngSocket;
121
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
122
+ if ((rv = nng_rep0_open(&p_rbnngSocket->socket)) != 0) {
123
+ rb_raise(rbnng_exceptionClass, "nng_rep0_open %d", rv);
124
+ }
125
+
126
+ if ((rv = nng_ctx_open(&p_rbnngSocket->ctx, p_rbnngSocket->socket)) != 0) {
127
+ rb_raise(rbnng_exceptionClass, "nng_ctx_open %d", rv);
128
+ }
129
+
130
+ return self;
131
+ }
132
+
133
+ void
134
+ rbnng_rep0_Init(VALUE nng_module)
135
+ {
136
+ VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
137
+ VALUE rbnng_SocketRep0Class =
138
+ rb_define_class_under(rbnng_SocketModule, "Rep0", rb_cObject);
139
+ rb_define_alloc_func(rbnng_SocketRep0Class, socket_alloc);
140
+ rb_define_method(
141
+ rbnng_SocketRep0Class, "initialize", socket_rep0_initialize, 0);
142
+ rb_define_method(rbnng_SocketRep0Class, "get_msg", socket_rep0_get_msg, 0);
143
+ rb_define_method(rbnng_SocketRep0Class, "send_msg", socket_rep0_send_msg, 1);
144
+ rb_define_method(rbnng_SocketRep0Class, "listen", socket_rep0_listen, 1);
145
+ }
data/ext/rbnng/req0.c ADDED
@@ -0,0 +1,123 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #include "msg.h"
7
+ #include "rbnng.h"
8
+ #include "socket.h"
9
+ #include <nng/protocol/reqrep0/req.h>
10
+ #include <ruby.h>
11
+ #include <ruby/thread.h>
12
+
13
+ static VALUE
14
+ socket_req0_dial(VALUE self, VALUE url)
15
+ {
16
+ Check_Type(url, T_STRING);
17
+ int rv;
18
+ RbnngSocket* p_rbnngSocket;
19
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
20
+ if ((rv = nng_dial(p_rbnngSocket->socket, StringValueCStr(url), NULL, 0)) !=
21
+ 0) {
22
+ rb_raise(rbnng_exceptionClass, "nng_dial %d", rv);
23
+ }
24
+ return self;
25
+ }
26
+
27
+ void*
28
+ req0_get_msg_blocking(void* data)
29
+ {
30
+ VALUE self = (VALUE)data;
31
+ RbnngSocket* p_rbnngSocket;
32
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
33
+
34
+ int rv;
35
+ nng_msg* p_msg;
36
+ if ((rv = nng_recvmsg(p_rbnngSocket->socket, &p_msg, 0)) != 0) {
37
+ rb_raise(rbnng_exceptionClass, "nng_recvmsg %d", rv);
38
+ }
39
+
40
+ return p_msg;
41
+ }
42
+
43
+ static VALUE
44
+ socket_req0_get_msg(VALUE self)
45
+ {
46
+ nng_msg* p_msg =
47
+ rb_thread_call_without_gvl(req0_get_msg_blocking, self, 0, 0);
48
+
49
+ if (p_msg) {
50
+ RbnngMsg* p_newMsg;
51
+ VALUE newMsg = rb_class_new_instance(0, 0, rbnng_MsgClass);
52
+ Data_Get_Struct(newMsg, RbnngMsg, p_newMsg);
53
+ p_newMsg->msg = p_msg;
54
+ return newMsg;
55
+ } else {
56
+ VALUE newMsg = rb_funcall(rbnng_MsgClass, rb_intern("new"), 0);
57
+ return newMsg;
58
+ }
59
+ }
60
+
61
+ void*
62
+ req0_send_msg_blocking(void* data)
63
+ {
64
+ RbnngSendMsgReq* p_sendMsgReq = (RbnngSendMsgReq*)data;
65
+ RbnngSocket* p_rbnngSocket;
66
+ Data_Get_Struct(p_sendMsgReq->socketObj, RbnngSocket, p_rbnngSocket);
67
+ int rv;
68
+ nng_msg* msg;
69
+ if ((rv = nng_msg_alloc(&msg, 0)) != 0) {
70
+ rb_raise(rbnng_exceptionClass, "nng_msg_alloc %d", rv);
71
+ }
72
+
73
+ nng_msg_append(msg,
74
+ StringValuePtr(p_sendMsgReq->nextMsg),
75
+ RSTRING_LEN(p_sendMsgReq->nextMsg));
76
+
77
+ if ((rv = nng_sendmsg(p_rbnngSocket->socket, msg, 0)) != 0) {
78
+ rb_raise(rbnng_exceptionClass, "nng_sendmsg %d", rv);
79
+ }
80
+
81
+ nng_msg_free(msg);
82
+
83
+ return 0;
84
+ }
85
+
86
+ static VALUE
87
+ socket_req0_send_msg(VALUE self, VALUE rb_strMsg)
88
+ {
89
+ Check_Type(rb_strMsg, T_STRING);
90
+ RbnngSendMsgReq sendMsgReq = {
91
+ .socketObj = self,
92
+ .nextMsg = rb_strMsg,
93
+ };
94
+ rb_thread_call_without_gvl(req0_send_msg_blocking, &sendMsgReq, 0, 0);
95
+
96
+ return Qnil;
97
+ }
98
+
99
+ static VALUE
100
+ socket_req0_initialize(VALUE self)
101
+ {
102
+ RbnngSocket* p_rbnngSocket;
103
+ Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
104
+ int rv = nng_req0_open(&p_rbnngSocket->socket);
105
+ if (rv != 0) {
106
+ rb_raise(rbnng_exceptionClass, "nng_req0_open %d", rv);
107
+ }
108
+ return self;
109
+ }
110
+
111
+ void
112
+ rbnng_req0_Init(VALUE nng_module)
113
+ {
114
+ VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
115
+ VALUE rbnng_SocketRep0Class =
116
+ rb_define_class_under(rbnng_SocketModule, "Req0", rb_cObject);
117
+ rb_define_alloc_func(rbnng_SocketRep0Class, socket_alloc);
118
+ rb_define_method(
119
+ rbnng_SocketRep0Class, "initialize", socket_req0_initialize, 0);
120
+ rb_define_method(rbnng_SocketRep0Class, "get_msg", socket_req0_get_msg, 0);
121
+ rb_define_method(rbnng_SocketRep0Class, "send_msg", socket_req0_send_msg, 1);
122
+ rb_define_method(rbnng_SocketRep0Class, "dial", socket_req0_dial, 1);
123
+ }
@@ -0,0 +1,20 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+ #include "socket.h"
6
+
7
+ static void
8
+ socket_free(void* ptr)
9
+ {
10
+ RbnngSocket* p_rbnngSocket = (RbnngSocket*)ptr;
11
+ nng_close(p_rbnngSocket->socket);
12
+ xfree(p_rbnngSocket);
13
+ }
14
+
15
+ VALUE
16
+ socket_alloc(VALUE klass)
17
+ {
18
+ RbnngSocket* p_rbnngSocket = ZALLOC(RbnngSocket);
19
+ return rb_data_object_wrap(klass, p_rbnngSocket, 0, socket_free);
20
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) 2021 Adib Saad
3
+ *
4
+ */
5
+
6
+ #ifndef RBNNG_SOCKET_H
7
+ #define RBNNG_SOCKET_H
8
+
9
+ #include <nng/nng.h>
10
+ #include <ruby.h>
11
+ #include <stdbool.h>
12
+ #include <pthread.h>
13
+
14
+ typedef struct
15
+ {
16
+ nng_socket socket;
17
+ // Not all socket types will use a context
18
+ nng_ctx ctx;
19
+ } RbnngSocket;
20
+
21
+ typedef struct {
22
+ VALUE socketObj; // A socket class instnace
23
+ VALUE nextMsg; // Ruby string
24
+ } RbnngSendMsgReq;
25
+
26
+ extern void
27
+ rbnng_rep0_Init(VALUE nng_module);
28
+ extern void
29
+ rbnng_req0_Init(VALUE nng_module);
30
+ extern VALUE
31
+ socket_alloc(VALUE);
32
+
33
+ #endif
@@ -0,0 +1,3 @@
1
+ module NNG
2
+ VERSION = '0.1.0.alpha.0'
3
+ end
data/lib/nng.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'nng/rbnng' # Import the extension
2
+ require 'nng/version'
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nng
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.alpha.0
5
+ platform: ruby
6
+ authors:
7
+ - Adib Saad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - adib.saad@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/rbnng/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - "./ext/rbnng/msg.c"
22
+ - "./ext/rbnng/msg.h"
23
+ - "./ext/rbnng/rbnng.c"
24
+ - "./ext/rbnng/rbnng.h"
25
+ - "./ext/rbnng/rep0.c"
26
+ - "./ext/rbnng/req0.c"
27
+ - "./ext/rbnng/socket.c"
28
+ - "./ext/rbnng/socket.h"
29
+ - "./lib/nng.rb"
30
+ - "./lib/nng/version.rb"
31
+ - ext/rbnng/extconf.rb
32
+ homepage: https://github.com/adibsaad/rbnng
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - ext
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">"
49
+ - !ruby/object:Gem::Version
50
+ version: 1.3.1
51
+ requirements: []
52
+ rubygems_version: 3.1.6
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Ruby bindings for nng (nanomsg-ng).
56
+ test_files: []