nng 0.1.0.alpha.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/rbnng/bus0.c +43 -0
- data/ext/rbnng/exceptions.c +217 -0
- data/ext/rbnng/exceptions.h +12 -0
- data/ext/rbnng/msg.c +9 -11
- data/ext/rbnng/msg.h +1 -1
- data/ext/rbnng/pair.c +65 -0
- data/ext/rbnng/pub0.c +36 -0
- data/ext/rbnng/pull0.c +36 -0
- data/ext/rbnng/push0.c +36 -0
- data/ext/rbnng/rbnng.c +11 -7
- data/ext/rbnng/rbnng.h +21 -1
- data/ext/rbnng/rep0.c +34 -29
- data/ext/rbnng/req0.c +10 -95
- data/ext/rbnng/respondent0.c +40 -0
- data/ext/rbnng/socket.c +106 -0
- data/ext/rbnng/socket.h +11 -8
- data/ext/rbnng/sub0.c +41 -0
- data/ext/rbnng/surveyor0.c +40 -0
- data/lib/nng/version.rb +1 -1
- metadata +13 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '059fd5f5fe9ba741fbbbfc57ead871921b2ead9eb5fb5f9ddce30b99cbcb8e8f'
|
4
|
+
data.tar.gz: 6cfb7d0644296c8d2c99f50c7626717139d40a24a59e70c87bb81a29a9db6845
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be9d6c590d943a1396cbe3e3c3cb7b6086d52633fe4cf23e03c7c8664d051c6d8183bea23cfb7daf4121df2cbb86589600f4756b6f3c859bd93c946820a33d85
|
7
|
+
data.tar.gz: 4417ea199f3eb0bd1130cd92c375986b5e27ab72c2a68c1cfb7204740fdb3274882e3910bebf99c2b983f4bd803b3e3476e401ea8c57faa12415a3a093f3c7c9
|
data/ext/rbnng/bus0.c
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/bus0/bus.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_bus0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_bus0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
if ((rv = nng_socket_set_ms(p_rbnngSocket->socket, NNG_OPT_RECVTIMEO, 100)) != 0) {
|
23
|
+
raise_error(rv);
|
24
|
+
return Qnil;
|
25
|
+
}
|
26
|
+
|
27
|
+
return self;
|
28
|
+
}
|
29
|
+
|
30
|
+
void
|
31
|
+
rbnng_bus0_Init(VALUE nng_module)
|
32
|
+
{
|
33
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
34
|
+
VALUE rbnng_SocketBus0Class =
|
35
|
+
rb_define_class_under(rbnng_SocketModule, "Bus0", rb_cObject);
|
36
|
+
rb_define_alloc_func(rbnng_SocketBus0Class, socket_alloc);
|
37
|
+
rb_define_method(
|
38
|
+
rbnng_SocketBus0Class, "initialize", socket_bus0_initialize, 0);
|
39
|
+
rb_define_method(rbnng_SocketBus0Class, "get_msg", socket_get_msg, 0);
|
40
|
+
rb_define_method(rbnng_SocketBus0Class, "send_msg", socket_send_msg, 1);
|
41
|
+
rb_define_method(rbnng_SocketBus0Class, "listen", socket_listen, 1);
|
42
|
+
rb_define_method(rbnng_SocketBus0Class, "dial", socket_dial, 1);
|
43
|
+
}
|
@@ -0,0 +1,217 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include <nng/nng.h>
|
6
|
+
#include <ruby.h>
|
7
|
+
|
8
|
+
VALUE rbnng_eBase = Qnil;
|
9
|
+
|
10
|
+
VALUE rbnng_eInterrupted = Qnil;
|
11
|
+
VALUE rbnng_eOutOfMemory = Qnil;
|
12
|
+
VALUE rbnng_eInvalidArgument = Qnil;
|
13
|
+
VALUE rbnng_eResourceBusy = Qnil;
|
14
|
+
VALUE rbnng_eTimedOut = Qnil;
|
15
|
+
VALUE rbnng_eConnectionRefused = Qnil;
|
16
|
+
VALUE rbnng_eObjectClosed = Qnil;
|
17
|
+
VALUE rbnng_eTryAgain = Qnil;
|
18
|
+
VALUE rbnng_eNotSupported = Qnil;
|
19
|
+
VALUE rbnng_eAddressInUse = Qnil;
|
20
|
+
VALUE rbnng_eIncorrectState = Qnil;
|
21
|
+
VALUE rbnng_eEntryNotFound = Qnil;
|
22
|
+
VALUE rbnng_eProtocolError = Qnil;
|
23
|
+
VALUE rbnng_eDestinationUnreachable = Qnil;
|
24
|
+
VALUE rbnng_eAddressInvalid = Qnil;
|
25
|
+
VALUE rbnng_ePermissionDenied = Qnil;
|
26
|
+
VALUE rbnng_eMessageTooLarge = Qnil;
|
27
|
+
VALUE rbnng_eConnectionReset = Qnil;
|
28
|
+
VALUE rbnng_eConnectionAborted = Qnil;
|
29
|
+
VALUE rbnng_eOperationCanceled = Qnil;
|
30
|
+
VALUE rbnng_eOutOfFiles = Qnil;
|
31
|
+
VALUE rbnng_eOutOfSpace = Qnil;
|
32
|
+
VALUE rbnng_eResourceAlreadyExists = Qnil;
|
33
|
+
VALUE rbnng_eReadOnlyResource = Qnil;
|
34
|
+
VALUE rbnng_eWriteOnlyResource = Qnil;
|
35
|
+
VALUE rbnng_eCryptographicError = Qnil;
|
36
|
+
VALUE rbnng_ePeerCouldNotBeAuthenticated = Qnil;
|
37
|
+
VALUE rbnng_eOptionRequiresArgument = Qnil;
|
38
|
+
VALUE rbnng_eAmbiguousOption = Qnil;
|
39
|
+
VALUE rbnng_eIncorrectType = Qnil;
|
40
|
+
VALUE rbnng_eConnectionShutdown = Qnil;
|
41
|
+
VALUE rbnng_eInternalErrorDetected = Qnil;
|
42
|
+
|
43
|
+
void
|
44
|
+
rbnng_exceptions_Init(VALUE nng_module)
|
45
|
+
{
|
46
|
+
VALUE errorModule = rb_define_module_under(nng_module, "Error");
|
47
|
+
rbnng_eBase = rb_define_class_under(errorModule, "Error", rb_eRuntimeError);
|
48
|
+
|
49
|
+
rbnng_eInterrupted =
|
50
|
+
rb_define_class_under(errorModule, "Interrupted", rbnng_eBase);
|
51
|
+
rbnng_eOutOfMemory =
|
52
|
+
rb_define_class_under(errorModule, "OutOfMemory", rbnng_eBase);
|
53
|
+
rbnng_eInvalidArgument =
|
54
|
+
rb_define_class_under(errorModule, "InvalidArgument", rbnng_eBase);
|
55
|
+
rbnng_eResourceBusy =
|
56
|
+
rb_define_class_under(errorModule, "ResourceBusy", rbnng_eBase);
|
57
|
+
rbnng_eTimedOut = rb_define_class_under(errorModule, "TimedOut", rbnng_eBase);
|
58
|
+
rbnng_eConnectionRefused =
|
59
|
+
rb_define_class_under(errorModule, "ConnectionRefused", rbnng_eBase);
|
60
|
+
rbnng_eObjectClosed =
|
61
|
+
rb_define_class_under(errorModule, "ObjectClosed", rbnng_eBase);
|
62
|
+
rbnng_eTryAgain = rb_define_class_under(errorModule, "TryAgain", rbnng_eBase);
|
63
|
+
rbnng_eNotSupported =
|
64
|
+
rb_define_class_under(errorModule, "NotSupported", rbnng_eBase);
|
65
|
+
rbnng_eAddressInUse =
|
66
|
+
rb_define_class_under(errorModule, "AddressInUse", rbnng_eBase);
|
67
|
+
rbnng_eIncorrectState =
|
68
|
+
rb_define_class_under(errorModule, "IncorrectState", rbnng_eBase);
|
69
|
+
rbnng_eEntryNotFound =
|
70
|
+
rb_define_class_under(errorModule, "EntryNotFound", rbnng_eBase);
|
71
|
+
rbnng_eProtocolError =
|
72
|
+
rb_define_class_under(errorModule, "ProtocolError", rbnng_eBase);
|
73
|
+
rbnng_eDestinationUnreachable =
|
74
|
+
rb_define_class_under(errorModule, "DestinationUnreachable", rbnng_eBase);
|
75
|
+
rbnng_eAddressInvalid =
|
76
|
+
rb_define_class_under(errorModule, "AddressInvalid", rbnng_eBase);
|
77
|
+
rbnng_ePermissionDenied =
|
78
|
+
rb_define_class_under(errorModule, "PermissionDenied", rbnng_eBase);
|
79
|
+
rbnng_eMessageTooLarge =
|
80
|
+
rb_define_class_under(errorModule, "MessageTooLarge", rbnng_eBase);
|
81
|
+
rbnng_eConnectionReset =
|
82
|
+
rb_define_class_under(errorModule, "ConnectionReset", rbnng_eBase);
|
83
|
+
rbnng_eConnectionAborted =
|
84
|
+
rb_define_class_under(errorModule, "ConnectionAborted", rbnng_eBase);
|
85
|
+
rbnng_eOperationCanceled =
|
86
|
+
rb_define_class_under(errorModule, "OperationCanceled", rbnng_eBase);
|
87
|
+
rbnng_eOutOfFiles =
|
88
|
+
rb_define_class_under(errorModule, "OutOfFiles", rbnng_eBase);
|
89
|
+
rbnng_eOutOfSpace =
|
90
|
+
rb_define_class_under(errorModule, "OutOfSpace", rbnng_eBase);
|
91
|
+
rbnng_eResourceAlreadyExists =
|
92
|
+
rb_define_class_under(errorModule, "ResourceAlreadyExists", rbnng_eBase);
|
93
|
+
rbnng_eReadOnlyResource =
|
94
|
+
rb_define_class_under(errorModule, "ReadOnlyResource", rbnng_eBase);
|
95
|
+
rbnng_eWriteOnlyResource =
|
96
|
+
rb_define_class_under(errorModule, "WriteOnlyResource", rbnng_eBase);
|
97
|
+
rbnng_eCryptographicError =
|
98
|
+
rb_define_class_under(errorModule, "CryptographicError", rbnng_eBase);
|
99
|
+
rbnng_ePeerCouldNotBeAuthenticated = rb_define_class_under(
|
100
|
+
errorModule, "PeerCouldNotBeAuthenticated", rbnng_eBase);
|
101
|
+
rbnng_eOptionRequiresArgument =
|
102
|
+
rb_define_class_under(errorModule, "OptionRequiresArgument", rbnng_eBase);
|
103
|
+
rbnng_eAmbiguousOption =
|
104
|
+
rb_define_class_under(errorModule, "AmbiguousOption", rbnng_eBase);
|
105
|
+
rbnng_eIncorrectType =
|
106
|
+
rb_define_class_under(errorModule, "IncorrectType", rbnng_eBase);
|
107
|
+
rbnng_eConnectionShutdown =
|
108
|
+
rb_define_class_under(errorModule, "ConnectionShutdown", rbnng_eBase);
|
109
|
+
rbnng_eInternalErrorDetected =
|
110
|
+
rb_define_class_under(errorModule, "InternalErrorDetected", rbnng_eBase);
|
111
|
+
}
|
112
|
+
|
113
|
+
void
|
114
|
+
raise_error(int nng_errno)
|
115
|
+
{
|
116
|
+
switch (nng_errno) {
|
117
|
+
case NNG_EINTR:
|
118
|
+
rb_raise(rbnng_eInterrupted, "");
|
119
|
+
break;
|
120
|
+
case NNG_ENOMEM:
|
121
|
+
rb_raise(rbnng_eOutOfMemory, "");
|
122
|
+
break;
|
123
|
+
case NNG_EINVAL:
|
124
|
+
rb_raise(rbnng_eInvalidArgument, "");
|
125
|
+
break;
|
126
|
+
case NNG_EBUSY:
|
127
|
+
rb_raise(rbnng_eResourceBusy, "");
|
128
|
+
break;
|
129
|
+
case NNG_ETIMEDOUT:
|
130
|
+
rb_raise(rbnng_eTimedOut, "");
|
131
|
+
break;
|
132
|
+
case NNG_ECONNREFUSED:
|
133
|
+
rb_raise(rbnng_eConnectionRefused, "");
|
134
|
+
break;
|
135
|
+
case NNG_ECLOSED:
|
136
|
+
rb_raise(rbnng_eObjectClosed, "");
|
137
|
+
break;
|
138
|
+
case NNG_EAGAIN:
|
139
|
+
rb_raise(rbnng_eTryAgain, "");
|
140
|
+
break;
|
141
|
+
case NNG_ENOTSUP:
|
142
|
+
rb_raise(rbnng_eNotSupported, "");
|
143
|
+
break;
|
144
|
+
case NNG_EADDRINUSE:
|
145
|
+
rb_raise(rbnng_eAddressInUse, "");
|
146
|
+
break;
|
147
|
+
case NNG_ESTATE:
|
148
|
+
rb_raise(rbnng_eIncorrectState, "");
|
149
|
+
break;
|
150
|
+
case NNG_ENOENT:
|
151
|
+
rb_raise(rbnng_eEntryNotFound, "");
|
152
|
+
break;
|
153
|
+
case NNG_EPROTO:
|
154
|
+
rb_raise(rbnng_eProtocolError, "");
|
155
|
+
break;
|
156
|
+
case NNG_EUNREACHABLE:
|
157
|
+
rb_raise(rbnng_eDestinationUnreachable, "");
|
158
|
+
break;
|
159
|
+
case NNG_EADDRINVAL:
|
160
|
+
rb_raise(rbnng_eAddressInvalid, "");
|
161
|
+
break;
|
162
|
+
case NNG_EPERM:
|
163
|
+
rb_raise(rbnng_ePermissionDenied, "");
|
164
|
+
break;
|
165
|
+
case NNG_EMSGSIZE:
|
166
|
+
rb_raise(rbnng_eMessageTooLarge, "");
|
167
|
+
break;
|
168
|
+
case NNG_ECONNRESET:
|
169
|
+
rb_raise(rbnng_eConnectionReset, "");
|
170
|
+
break;
|
171
|
+
case NNG_ECONNABORTED:
|
172
|
+
rb_raise(rbnng_eConnectionAborted, "");
|
173
|
+
break;
|
174
|
+
case NNG_ECANCELED:
|
175
|
+
rb_raise(rbnng_eOperationCanceled, "");
|
176
|
+
break;
|
177
|
+
case NNG_ENOFILES:
|
178
|
+
rb_raise(rbnng_eOutOfFiles, "");
|
179
|
+
break;
|
180
|
+
case NNG_ENOSPC:
|
181
|
+
rb_raise(rbnng_eOutOfSpace, "");
|
182
|
+
break;
|
183
|
+
case NNG_EEXIST:
|
184
|
+
rb_raise(rbnng_eResourceAlreadyExists, "");
|
185
|
+
break;
|
186
|
+
case NNG_EREADONLY:
|
187
|
+
rb_raise(rbnng_eReadOnlyResource, "");
|
188
|
+
break;
|
189
|
+
case NNG_EWRITEONLY:
|
190
|
+
rb_raise(rbnng_eWriteOnlyResource, "");
|
191
|
+
break;
|
192
|
+
case NNG_ECRYPTO:
|
193
|
+
rb_raise(rbnng_eCryptographicError, "");
|
194
|
+
break;
|
195
|
+
case NNG_EPEERAUTH:
|
196
|
+
rb_raise(rbnng_ePeerCouldNotBeAuthenticated, "");
|
197
|
+
break;
|
198
|
+
case NNG_ENOARG:
|
199
|
+
rb_raise(rbnng_eOptionRequiresArgument, "");
|
200
|
+
break;
|
201
|
+
case NNG_EAMBIGUOUS:
|
202
|
+
rb_raise(rbnng_eAmbiguousOption, "");
|
203
|
+
break;
|
204
|
+
case NNG_EBADTYPE:
|
205
|
+
rb_raise(rbnng_eIncorrectType, "");
|
206
|
+
break;
|
207
|
+
case NNG_ECONNSHUT:
|
208
|
+
rb_raise(rbnng_eConnectionShutdown, "");
|
209
|
+
break;
|
210
|
+
case NNG_EINTERNAL:
|
211
|
+
rb_raise(rbnng_eInternalErrorDetected, "");
|
212
|
+
break;
|
213
|
+
default:
|
214
|
+
rb_raise(rbnng_eBase, "errno %d, %s", nng_errno, nng_strerror(nng_errno));
|
215
|
+
break;
|
216
|
+
}
|
217
|
+
}
|
data/ext/rbnng/msg.c
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
* Copyright (c) 2021 Adib Saad
|
3
3
|
*
|
4
4
|
*/
|
5
|
-
|
6
5
|
#include "msg.h"
|
7
6
|
#include <nng/protocol/reqrep0/rep.h>
|
8
7
|
#include <ruby.h>
|
@@ -11,11 +10,10 @@
|
|
11
10
|
VALUE rbnng_MsgClass = Qnil;
|
12
11
|
|
13
12
|
void
|
14
|
-
msg_free(
|
13
|
+
msg_free(RbnngMsg* p_rbnngMsg)
|
15
14
|
{
|
16
|
-
|
17
|
-
|
18
|
-
nng_msg_free(p_rbnngMsg->msg);
|
15
|
+
if (p_rbnngMsg->p_msg) {
|
16
|
+
nng_msg_free(p_rbnngMsg->p_msg);
|
19
17
|
}
|
20
18
|
xfree(p_rbnngMsg);
|
21
19
|
}
|
@@ -32,9 +30,9 @@ msg_body(VALUE self)
|
|
32
30
|
{
|
33
31
|
RbnngMsg* p_rbnngMsg;
|
34
32
|
Data_Get_Struct(self, RbnngMsg, p_rbnngMsg);
|
35
|
-
if (p_rbnngMsg->
|
36
|
-
return rb_str_new(nng_msg_body(p_rbnngMsg->
|
37
|
-
nng_msg_len(p_rbnngMsg->
|
33
|
+
if (p_rbnngMsg->p_msg) {
|
34
|
+
return rb_str_new(nng_msg_body(p_rbnngMsg->p_msg),
|
35
|
+
nng_msg_len(p_rbnngMsg->p_msg));
|
38
36
|
} else {
|
39
37
|
return rb_str_new_cstr("");
|
40
38
|
}
|
@@ -45,9 +43,9 @@ msg_header(VALUE self)
|
|
45
43
|
{
|
46
44
|
RbnngMsg* p_rbnngMsg;
|
47
45
|
Data_Get_Struct(self, RbnngMsg, p_rbnngMsg);
|
48
|
-
if (p_rbnngMsg->
|
49
|
-
return rb_str_new(nng_msg_header(p_rbnngMsg->
|
50
|
-
nng_msg_header_len(p_rbnngMsg->
|
46
|
+
if (p_rbnngMsg->p_msg) {
|
47
|
+
return rb_str_new(nng_msg_header(p_rbnngMsg->p_msg),
|
48
|
+
nng_msg_header_len(p_rbnngMsg->p_msg));
|
51
49
|
} else {
|
52
50
|
return rb_str_new_cstr("");
|
53
51
|
}
|
data/ext/rbnng/msg.h
CHANGED
data/ext/rbnng/pair.c
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/pair0/pair.h>
|
9
|
+
#include <nng/protocol/pair1/pair.h>
|
10
|
+
#include <ruby.h>
|
11
|
+
#include <ruby/thread.h>
|
12
|
+
|
13
|
+
static VALUE
|
14
|
+
socket_pair0_initialize(VALUE self)
|
15
|
+
{
|
16
|
+
RbnngSocket* p_rbnngSocket;
|
17
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
18
|
+
int rv;
|
19
|
+
if ((rv = nng_pair0_open(&p_rbnngSocket->socket)) != 0) {
|
20
|
+
raise_error(rv);
|
21
|
+
return Qnil;
|
22
|
+
}
|
23
|
+
|
24
|
+
return self;
|
25
|
+
}
|
26
|
+
|
27
|
+
static VALUE
|
28
|
+
socket_pair1_initialize(VALUE self)
|
29
|
+
{
|
30
|
+
RbnngSocket* p_rbnngSocket;
|
31
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
32
|
+
int rv;
|
33
|
+
if ((rv = nng_pair1_open(&p_rbnngSocket->socket)) != 0) {
|
34
|
+
raise_error(rv);
|
35
|
+
return Qnil;
|
36
|
+
}
|
37
|
+
|
38
|
+
return self;
|
39
|
+
}
|
40
|
+
|
41
|
+
void
|
42
|
+
rbnng_pair_Init(VALUE nng_module)
|
43
|
+
{
|
44
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
45
|
+
|
46
|
+
VALUE rbnng_SocketPair1Class =
|
47
|
+
rb_define_class_under(rbnng_SocketModule, "Pair1", rb_cObject);
|
48
|
+
rb_define_alloc_func(rbnng_SocketPair1Class, socket_alloc);
|
49
|
+
rb_define_method(
|
50
|
+
rbnng_SocketPair1Class, "initialize", socket_pair1_initialize, 0);
|
51
|
+
rb_define_method(rbnng_SocketPair1Class, "get_msg", socket_get_msg, 0);
|
52
|
+
rb_define_method(rbnng_SocketPair1Class, "send_msg", socket_send_msg, 1);
|
53
|
+
rb_define_method(rbnng_SocketPair1Class, "listen", socket_listen, 1);
|
54
|
+
rb_define_method(rbnng_SocketPair1Class, "dial", socket_dial, 1);
|
55
|
+
|
56
|
+
VALUE rbnng_SocketPair0Class =
|
57
|
+
rb_define_class_under(rbnng_SocketModule, "Pair0", rb_cObject);
|
58
|
+
rb_define_alloc_func(rbnng_SocketPair0Class, socket_alloc);
|
59
|
+
rb_define_method(
|
60
|
+
rbnng_SocketPair0Class, "initialize", socket_pair0_initialize, 0);
|
61
|
+
rb_define_method(rbnng_SocketPair0Class, "get_msg", socket_get_msg, 0);
|
62
|
+
rb_define_method(rbnng_SocketPair0Class, "send_msg", socket_send_msg, 1);
|
63
|
+
rb_define_method(rbnng_SocketPair0Class, "listen", socket_listen, 1);
|
64
|
+
rb_define_method(rbnng_SocketPair0Class, "dial", socket_dial, 1);
|
65
|
+
}
|
data/ext/rbnng/pub0.c
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/pubsub0/pub.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_pub0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_pub0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
return self;
|
23
|
+
}
|
24
|
+
|
25
|
+
void
|
26
|
+
rbnng_pub0_Init(VALUE nng_module)
|
27
|
+
{
|
28
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
29
|
+
VALUE rbnng_SocketPub0Class =
|
30
|
+
rb_define_class_under(rbnng_SocketModule, "Pub0", rb_cObject);
|
31
|
+
rb_define_alloc_func(rbnng_SocketPub0Class, socket_alloc);
|
32
|
+
rb_define_method(
|
33
|
+
rbnng_SocketPub0Class, "initialize", socket_pub0_initialize, 0);
|
34
|
+
rb_define_method(rbnng_SocketPub0Class, "send_msg", socket_send_msg, 1);
|
35
|
+
rb_define_method(rbnng_SocketPub0Class, "listen", socket_listen, 1);
|
36
|
+
}
|
data/ext/rbnng/pull0.c
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/pipeline0/pull.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_pull0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_pull0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
return self;
|
23
|
+
}
|
24
|
+
|
25
|
+
void
|
26
|
+
rbnng_pull0_Init(VALUE nng_module)
|
27
|
+
{
|
28
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
29
|
+
VALUE rbnng_SocketPull0Class =
|
30
|
+
rb_define_class_under(rbnng_SocketModule, "Pull0", rb_cObject);
|
31
|
+
rb_define_alloc_func(rbnng_SocketPull0Class, socket_alloc);
|
32
|
+
rb_define_method(
|
33
|
+
rbnng_SocketPull0Class, "initialize", socket_pull0_initialize, 0);
|
34
|
+
rb_define_method(rbnng_SocketPull0Class, "get_msg", socket_get_msg, 0);
|
35
|
+
rb_define_method(rbnng_SocketPull0Class, "listen", socket_listen, 1);
|
36
|
+
}
|
data/ext/rbnng/push0.c
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/pipeline0/push.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_push0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_push0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
return self;
|
23
|
+
}
|
24
|
+
|
25
|
+
void
|
26
|
+
rbnng_push0_Init(VALUE nng_module)
|
27
|
+
{
|
28
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
29
|
+
VALUE rbnng_SocketPush0Class =
|
30
|
+
rb_define_class_under(rbnng_SocketModule, "Push0", rb_cObject);
|
31
|
+
rb_define_alloc_func(rbnng_SocketPush0Class, socket_alloc);
|
32
|
+
rb_define_method(
|
33
|
+
rbnng_SocketPush0Class, "initialize", socket_push0_initialize, 0);
|
34
|
+
rb_define_method(rbnng_SocketPush0Class, "send_msg", socket_send_msg, 1);
|
35
|
+
rb_define_method(rbnng_SocketPush0Class, "dial", socket_dial, 1);
|
36
|
+
}
|
data/ext/rbnng/rbnng.c
CHANGED
@@ -2,15 +2,12 @@
|
|
2
2
|
* Copyright (c) 2021 Adib Saad
|
3
3
|
*
|
4
4
|
*/
|
5
|
-
|
6
5
|
#include <nng/nng.h>
|
7
6
|
#include <ruby.h>
|
8
7
|
|
8
|
+
#include "msg.h"
|
9
9
|
#include "rbnng.h"
|
10
10
|
#include "socket.h"
|
11
|
-
#include "msg.h"
|
12
|
-
|
13
|
-
VALUE rbnng_exceptionClass = Qnil;
|
14
11
|
|
15
12
|
static VALUE
|
16
13
|
library_version(VALUE self_)
|
@@ -26,9 +23,16 @@ Init_rbnng()
|
|
26
23
|
{
|
27
24
|
VALUE nng_module = rb_define_module("NNG");
|
28
25
|
rb_define_singleton_method(nng_module, "nng_version", library_version, 0);
|
29
|
-
|
30
|
-
|
26
|
+
rbnng_exceptions_Init(nng_module);
|
27
|
+
rbnng_msg_Init(nng_module);
|
31
28
|
rbnng_rep0_Init(nng_module);
|
32
29
|
rbnng_req0_Init(nng_module);
|
33
|
-
|
30
|
+
rbnng_pub0_Init(nng_module);
|
31
|
+
rbnng_sub0_Init(nng_module);
|
32
|
+
rbnng_pair_Init(nng_module);
|
33
|
+
rbnng_bus0_Init(nng_module);
|
34
|
+
rbnng_surveyor0_Init(nng_module);
|
35
|
+
rbnng_respondent0_Init(nng_module);
|
36
|
+
rbnng_push0_Init(nng_module);
|
37
|
+
rbnng_pull0_Init(nng_module);
|
34
38
|
}
|
data/ext/rbnng/rbnng.h
CHANGED
@@ -6,8 +6,28 @@
|
|
6
6
|
#ifndef RBNNG_H
|
7
7
|
#define RBNNG_H
|
8
8
|
|
9
|
+
#include "exceptions.h"
|
9
10
|
#include <ruby.h>
|
10
11
|
|
11
|
-
extern
|
12
|
+
extern void
|
13
|
+
rbnng_rep0_Init(VALUE nng_module);
|
14
|
+
extern void
|
15
|
+
rbnng_req0_Init(VALUE nng_module);
|
16
|
+
extern void
|
17
|
+
rbnng_pair_Init(VALUE nng_module);
|
18
|
+
extern void
|
19
|
+
rbnng_pub0_Init(VALUE nng_module);
|
20
|
+
extern void
|
21
|
+
rbnng_sub0_Init(VALUE nng_module);
|
22
|
+
extern void
|
23
|
+
rbnng_bus0_Init(VALUE nng_module);
|
24
|
+
extern void
|
25
|
+
rbnng_surveyor0_Init(VALUE nng_module);
|
26
|
+
extern void
|
27
|
+
rbnng_respondent0_Init(VALUE nng_module);
|
28
|
+
extern void
|
29
|
+
rbnng_push0_Init(VALUE nng_module);
|
30
|
+
extern void
|
31
|
+
rbnng_pull0_Init(VALUE nng_module);
|
12
32
|
|
13
33
|
#endif
|
data/ext/rbnng/rep0.c
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
* Copyright (c) 2021 Adib Saad
|
3
3
|
*
|
4
4
|
*/
|
5
|
-
|
6
5
|
#include "msg.h"
|
7
6
|
#include "rbnng.h"
|
8
7
|
#include "socket.h"
|
@@ -19,51 +18,49 @@ socket_rep0_listen(VALUE self, VALUE url)
|
|
19
18
|
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
20
19
|
if ((rv = nng_listen(p_rbnngSocket->socket, StringValueCStr(url), NULL, 0)) !=
|
21
20
|
0) {
|
22
|
-
|
21
|
+
raise_error(rv);
|
23
22
|
}
|
24
|
-
return self;
|
25
23
|
}
|
26
24
|
|
27
25
|
void*
|
28
|
-
rep0_get_msg_blocking(
|
26
|
+
rep0_get_msg_blocking(RbnngSocket* p_rbnngSocket)
|
29
27
|
{
|
30
|
-
VALUE self = (VALUE)data;
|
31
|
-
RbnngSocket* p_rbnngSocket;
|
32
|
-
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
33
|
-
|
34
28
|
nng_aio* p_aio;
|
35
29
|
int rv;
|
36
30
|
if ((rv = nng_aio_alloc(&p_aio, 0, 0)) != 0) {
|
37
|
-
|
31
|
+
return rv;
|
38
32
|
}
|
39
33
|
|
40
34
|
nng_ctx_recv(p_rbnngSocket->ctx, p_aio);
|
41
35
|
nng_aio_wait(p_aio);
|
42
36
|
|
43
37
|
if ((rv = nng_aio_result(p_aio)) != 0) {
|
44
|
-
|
38
|
+
return rv;
|
45
39
|
}
|
46
40
|
|
47
41
|
nng_msg* p_msg = nng_aio_get_msg(p_aio);
|
42
|
+
p_rbnngSocket->p_getMsgResult = p_msg;
|
48
43
|
nng_aio_free(p_aio);
|
49
|
-
return
|
44
|
+
return 0;
|
50
45
|
}
|
51
46
|
|
52
47
|
static VALUE
|
53
48
|
socket_rep0_get_msg(VALUE self)
|
54
49
|
{
|
55
|
-
|
56
|
-
|
50
|
+
RbnngSocket* p_rbnngSocket;
|
51
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
52
|
+
int rv =
|
53
|
+
rb_thread_call_without_gvl(rep0_get_msg_blocking, p_rbnngSocket, 0, 0);
|
57
54
|
|
58
|
-
if (
|
59
|
-
RbnngMsg*
|
55
|
+
if (rv == 0) {
|
56
|
+
RbnngMsg* p_newMsg;
|
60
57
|
VALUE newMsg = rb_class_new_instance(0, 0, rbnng_MsgClass);
|
61
|
-
Data_Get_Struct(newMsg, RbnngMsg,
|
62
|
-
|
58
|
+
Data_Get_Struct(newMsg, RbnngMsg, p_newMsg);
|
59
|
+
p_newMsg->p_msg = p_rbnngSocket->p_getMsgResult;
|
63
60
|
return newMsg;
|
64
61
|
} else {
|
65
|
-
|
66
|
-
return
|
62
|
+
raise_error(rv);
|
63
|
+
return Qnil;
|
67
64
|
}
|
68
65
|
}
|
69
66
|
|
@@ -77,28 +74,32 @@ rep0_send_msg_blocking(void* data)
|
|
77
74
|
nng_aio* p_aio;
|
78
75
|
int rv;
|
79
76
|
if ((rv = nng_aio_alloc(&p_aio, 0, 0)) != 0) {
|
80
|
-
|
77
|
+
return rv;
|
81
78
|
}
|
82
79
|
|
83
80
|
nng_msg* p_msg;
|
84
81
|
if ((rv = nng_msg_alloc(&p_msg, 0)) != 0) {
|
85
|
-
|
82
|
+
return rv;
|
86
83
|
}
|
87
84
|
|
88
85
|
nng_msg_clear(p_msg);
|
89
|
-
nng_msg_append(p_msg,
|
90
|
-
|
91
|
-
|
86
|
+
rv = nng_msg_append(p_msg,
|
87
|
+
StringValuePtr(p_sendMsgReq->nextMsg),
|
88
|
+
RSTRING_LEN(p_sendMsgReq->nextMsg));
|
89
|
+
if (rv != 0) {
|
90
|
+
return rv;
|
91
|
+
}
|
92
92
|
|
93
93
|
nng_aio_set_msg(p_aio, p_msg);
|
94
94
|
nng_ctx_send(p_rbnngSocket->ctx, p_aio);
|
95
95
|
nng_aio_wait(p_aio);
|
96
96
|
|
97
97
|
if ((rv = nng_aio_result(p_aio)) != 0) {
|
98
|
-
|
98
|
+
return rv;
|
99
99
|
}
|
100
100
|
|
101
101
|
nng_aio_free(p_aio);
|
102
|
+
return 0;
|
102
103
|
}
|
103
104
|
|
104
105
|
static VALUE
|
@@ -110,21 +111,25 @@ socket_rep0_send_msg(VALUE self, VALUE rb_strMsg)
|
|
110
111
|
.socketObj = self,
|
111
112
|
.nextMsg = rb_strMsg,
|
112
113
|
};
|
113
|
-
|
114
|
+
int rv =
|
115
|
+
rb_thread_call_without_gvl(rep0_send_msg_blocking, &sendMsgReq, 0, 0);
|
116
|
+
if (rv != 0) {
|
117
|
+
raise_error(rv);
|
118
|
+
}
|
114
119
|
}
|
115
120
|
|
116
121
|
static VALUE
|
117
122
|
socket_rep0_initialize(VALUE self)
|
118
123
|
{
|
119
|
-
int rv;
|
120
124
|
RbnngSocket* p_rbnngSocket;
|
121
125
|
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
126
|
+
int rv;
|
122
127
|
if ((rv = nng_rep0_open(&p_rbnngSocket->socket)) != 0) {
|
123
|
-
|
128
|
+
raise_error(rv);
|
124
129
|
}
|
125
130
|
|
126
131
|
if ((rv = nng_ctx_open(&p_rbnngSocket->ctx, p_rbnngSocket->socket)) != 0) {
|
127
|
-
|
132
|
+
raise_error(rv);
|
128
133
|
}
|
129
134
|
|
130
135
|
return self;
|
data/ext/rbnng/req0.c
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
* Copyright (c) 2021 Adib Saad
|
3
3
|
*
|
4
4
|
*/
|
5
|
-
|
6
5
|
#include "msg.h"
|
7
6
|
#include "rbnng.h"
|
8
7
|
#include "socket.h"
|
@@ -11,100 +10,16 @@
|
|
11
10
|
#include <ruby/thread.h>
|
12
11
|
|
13
12
|
static VALUE
|
14
|
-
|
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)
|
13
|
+
socket_req0_initialize(VALUE self)
|
29
14
|
{
|
30
|
-
VALUE self = (VALUE)data;
|
31
15
|
RbnngSocket* p_rbnngSocket;
|
32
16
|
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
33
|
-
|
34
17
|
int rv;
|
35
|
-
|
36
|
-
|
37
|
-
|
18
|
+
if ((rv = nng_req0_open(&p_rbnngSocket->socket)) != 0) {
|
19
|
+
raise_error(rv);
|
20
|
+
return Qnil;
|
38
21
|
}
|
39
22
|
|
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
23
|
return self;
|
109
24
|
}
|
110
25
|
|
@@ -112,12 +27,12 @@ void
|
|
112
27
|
rbnng_req0_Init(VALUE nng_module)
|
113
28
|
{
|
114
29
|
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
115
|
-
VALUE
|
30
|
+
VALUE rbnng_SocketReq0Class =
|
116
31
|
rb_define_class_under(rbnng_SocketModule, "Req0", rb_cObject);
|
117
|
-
rb_define_alloc_func(
|
32
|
+
rb_define_alloc_func(rbnng_SocketReq0Class, socket_alloc);
|
118
33
|
rb_define_method(
|
119
|
-
|
120
|
-
rb_define_method(
|
121
|
-
rb_define_method(
|
122
|
-
rb_define_method(
|
34
|
+
rbnng_SocketReq0Class, "initialize", socket_req0_initialize, 0);
|
35
|
+
rb_define_method(rbnng_SocketReq0Class, "get_msg", socket_get_msg, 0);
|
36
|
+
rb_define_method(rbnng_SocketReq0Class, "send_msg", socket_send_msg, 1);
|
37
|
+
rb_define_method(rbnng_SocketReq0Class, "dial", socket_dial, 1);
|
123
38
|
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/survey0/respond.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_respondent0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_respondent0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
return self;
|
23
|
+
}
|
24
|
+
|
25
|
+
void
|
26
|
+
rbnng_respondent0_Init(VALUE nng_module)
|
27
|
+
{
|
28
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
29
|
+
VALUE rbnng_SocketRespondent0Class =
|
30
|
+
rb_define_class_under(rbnng_SocketModule, "Respondent0", rb_cObject);
|
31
|
+
rb_define_alloc_func(rbnng_SocketRespondent0Class, socket_alloc);
|
32
|
+
rb_define_method(rbnng_SocketRespondent0Class,
|
33
|
+
"initialize",
|
34
|
+
socket_respondent0_initialize,
|
35
|
+
0);
|
36
|
+
rb_define_method(
|
37
|
+
rbnng_SocketRespondent0Class, "send_msg", socket_send_msg, 1);
|
38
|
+
rb_define_method(rbnng_SocketRespondent0Class, "get_msg", socket_get_msg, 0);
|
39
|
+
rb_define_method(rbnng_SocketRespondent0Class, "dial", socket_dial, 1);
|
40
|
+
}
|
data/ext/rbnng/socket.c
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
*
|
4
4
|
*/
|
5
5
|
#include "socket.h"
|
6
|
+
#include "msg.h"
|
7
|
+
#include "rbnng.h"
|
8
|
+
#include <ruby.h>
|
9
|
+
#include <ruby/thread.h>
|
6
10
|
|
7
11
|
static void
|
8
12
|
socket_free(void* ptr)
|
@@ -18,3 +22,105 @@ socket_alloc(VALUE klass)
|
|
18
22
|
RbnngSocket* p_rbnngSocket = ZALLOC(RbnngSocket);
|
19
23
|
return rb_data_object_wrap(klass, p_rbnngSocket, 0, socket_free);
|
20
24
|
}
|
25
|
+
|
26
|
+
void*
|
27
|
+
socket_get_msg_blocking(RbnngSocket* p_rbnngSocket)
|
28
|
+
{
|
29
|
+
nng_msg* p_msg = NULL;
|
30
|
+
int rv;
|
31
|
+
if ((rv = nng_recvmsg(p_rbnngSocket->socket, &p_msg, 0)) != 0) {
|
32
|
+
return rv;
|
33
|
+
}
|
34
|
+
|
35
|
+
p_rbnngSocket->p_getMsgResult = p_msg;
|
36
|
+
return 0;
|
37
|
+
}
|
38
|
+
|
39
|
+
VALUE
|
40
|
+
socket_get_msg(VALUE self)
|
41
|
+
{
|
42
|
+
RbnngSocket* p_rbnngSocket;
|
43
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
44
|
+
|
45
|
+
int rv =
|
46
|
+
rb_thread_call_without_gvl(socket_get_msg_blocking, p_rbnngSocket, 0, 0);
|
47
|
+
|
48
|
+
if (rv == 0) {
|
49
|
+
RbnngMsg* p_newMsg;
|
50
|
+
VALUE newMsg = rb_class_new_instance(0, 0, rbnng_MsgClass);
|
51
|
+
Data_Get_Struct(newMsg, RbnngMsg, p_newMsg);
|
52
|
+
p_newMsg->p_msg = p_rbnngSocket->p_getMsgResult;
|
53
|
+
return newMsg;
|
54
|
+
} else {
|
55
|
+
raise_error(rv);
|
56
|
+
return Qnil;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
void*
|
61
|
+
socket_send_msg_blocking(void* data)
|
62
|
+
{
|
63
|
+
RbnngSendMsgReq* p_sendMsgReq = (RbnngSendMsgReq*)data;
|
64
|
+
RbnngSocket* p_rbnngSocket;
|
65
|
+
Data_Get_Struct(p_sendMsgReq->socketObj, RbnngSocket, p_rbnngSocket);
|
66
|
+
int rv;
|
67
|
+
nng_msg* p_msg;
|
68
|
+
if ((rv = nng_msg_alloc(&p_msg, 0)) != 0) {
|
69
|
+
return rv;
|
70
|
+
}
|
71
|
+
|
72
|
+
rv = nng_msg_append(p_msg,
|
73
|
+
StringValuePtr(p_sendMsgReq->nextMsg),
|
74
|
+
RSTRING_LEN(p_sendMsgReq->nextMsg));
|
75
|
+
if (rv != 0) {
|
76
|
+
return rv;
|
77
|
+
}
|
78
|
+
|
79
|
+
// nng_sendmsg takes ownership of p_msg, so no need to free it afterwards.
|
80
|
+
if ((rv = nng_sendmsg(p_rbnngSocket->socket, p_msg, 0)) != 0) {
|
81
|
+
return rv;
|
82
|
+
}
|
83
|
+
|
84
|
+
return 0;
|
85
|
+
}
|
86
|
+
|
87
|
+
VALUE
|
88
|
+
socket_send_msg(VALUE self, VALUE rb_strMsg)
|
89
|
+
{
|
90
|
+
Check_Type(rb_strMsg, T_STRING);
|
91
|
+
RbnngSendMsgReq sendMsgReq = {
|
92
|
+
.socketObj = self,
|
93
|
+
.nextMsg = rb_strMsg,
|
94
|
+
};
|
95
|
+
int rv =
|
96
|
+
rb_thread_call_without_gvl(socket_send_msg_blocking, &sendMsgReq, 0, 0);
|
97
|
+
if (rv != 0) {
|
98
|
+
raise_error(rv);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
VALUE
|
103
|
+
socket_dial(VALUE self, VALUE url)
|
104
|
+
{
|
105
|
+
Check_Type(url, T_STRING);
|
106
|
+
RbnngSocket* p_rbnngSocket;
|
107
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
108
|
+
int rv;
|
109
|
+
if ((rv = nng_dial(p_rbnngSocket->socket, StringValueCStr(url), 0, 0)) != 0) {
|
110
|
+
raise_error(rv);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
VALUE
|
115
|
+
socket_listen(VALUE self, VALUE url)
|
116
|
+
{
|
117
|
+
Check_Type(url, T_STRING);
|
118
|
+
RbnngSocket* p_rbnngSocket;
|
119
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
120
|
+
|
121
|
+
int rv;
|
122
|
+
if ((rv = nng_listen(p_rbnngSocket->socket, StringValueCStr(url), NULL, 0)) !=
|
123
|
+
0) {
|
124
|
+
raise_error(rv);
|
125
|
+
}
|
126
|
+
}
|
data/ext/rbnng/socket.h
CHANGED
@@ -7,27 +7,30 @@
|
|
7
7
|
#define RBNNG_SOCKET_H
|
8
8
|
|
9
9
|
#include <nng/nng.h>
|
10
|
+
#include <pthread.h>
|
10
11
|
#include <ruby.h>
|
11
12
|
#include <stdbool.h>
|
12
|
-
#include <pthread.h>
|
13
13
|
|
14
14
|
typedef struct
|
15
15
|
{
|
16
16
|
nng_socket socket;
|
17
17
|
// Not all socket types will use a context
|
18
18
|
nng_ctx ctx;
|
19
|
+
|
20
|
+
// for get_msg
|
21
|
+
nng_msg* p_getMsgResult;
|
19
22
|
} RbnngSocket;
|
20
23
|
|
21
|
-
typedef struct
|
24
|
+
typedef struct
|
25
|
+
{
|
22
26
|
VALUE socketObj; // A socket class instnace
|
23
27
|
VALUE nextMsg; // Ruby string
|
24
28
|
} RbnngSendMsgReq;
|
25
29
|
|
26
|
-
extern
|
27
|
-
|
28
|
-
extern
|
29
|
-
|
30
|
-
extern VALUE
|
31
|
-
socket_alloc(VALUE);
|
30
|
+
extern VALUE socket_alloc(VALUE);
|
31
|
+
extern VALUE socket_get_msg(VALUE);
|
32
|
+
extern VALUE socket_send_msg(VALUE, VALUE);
|
33
|
+
extern VALUE socket_dial(VALUE, VALUE);
|
34
|
+
extern VALUE socket_listen(VALUE, VALUE);
|
32
35
|
|
33
36
|
#endif
|
data/ext/rbnng/sub0.c
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
*/
|
5
|
+
#include "msg.h"
|
6
|
+
#include "rbnng.h"
|
7
|
+
#include "socket.h"
|
8
|
+
#include <nng/protocol/pubsub0/sub.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
socket_sub0_initialize(VALUE self)
|
13
|
+
{
|
14
|
+
RbnngSocket* p_rbnngSocket;
|
15
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
16
|
+
int rv;
|
17
|
+
if ((rv = nng_sub0_open(&p_rbnngSocket->socket)) != 0) {
|
18
|
+
raise_error(rv);
|
19
|
+
return Qnil;
|
20
|
+
}
|
21
|
+
|
22
|
+
if ((rv = nng_socket_set(p_rbnngSocket->socket, NNG_OPT_SUB_SUBSCRIBE, "", 0)) != 0) {
|
23
|
+
raise_error(rv);
|
24
|
+
return Qnil;
|
25
|
+
}
|
26
|
+
|
27
|
+
return self;
|
28
|
+
}
|
29
|
+
|
30
|
+
void
|
31
|
+
rbnng_sub0_Init(VALUE nng_module)
|
32
|
+
{
|
33
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
34
|
+
VALUE rbnng_SocketSub0Class =
|
35
|
+
rb_define_class_under(rbnng_SocketModule, "Sub0", rb_cObject);
|
36
|
+
rb_define_alloc_func(rbnng_SocketSub0Class, socket_alloc);
|
37
|
+
rb_define_method(
|
38
|
+
rbnng_SocketSub0Class, "initialize", socket_sub0_initialize, 0);
|
39
|
+
rb_define_method(rbnng_SocketSub0Class, "get_msg", socket_get_msg, 0);
|
40
|
+
rb_define_method(rbnng_SocketSub0Class, "dial", socket_dial, 1);
|
41
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2021 Adib Saad
|
3
|
+
*
|
4
|
+
|
5
|
+
// NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 50));
|
6
|
+
// NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, 100));
|
7
|
+
*/
|
8
|
+
#include "msg.h"
|
9
|
+
#include "rbnng.h"
|
10
|
+
#include "socket.h"
|
11
|
+
#include <nng/protocol/survey0/survey.h>
|
12
|
+
#include <ruby.h>
|
13
|
+
|
14
|
+
static VALUE
|
15
|
+
socket_surveyor0_initialize(VALUE self)
|
16
|
+
{
|
17
|
+
RbnngSocket* p_rbnngSocket;
|
18
|
+
Data_Get_Struct(self, RbnngSocket, p_rbnngSocket);
|
19
|
+
int rv;
|
20
|
+
if ((rv = nng_surveyor0_open(&p_rbnngSocket->socket)) != 0) {
|
21
|
+
raise_error(rv);
|
22
|
+
return Qnil;
|
23
|
+
}
|
24
|
+
|
25
|
+
return self;
|
26
|
+
}
|
27
|
+
|
28
|
+
void
|
29
|
+
rbnng_surveyor0_Init(VALUE nng_module)
|
30
|
+
{
|
31
|
+
VALUE rbnng_SocketModule = rb_define_module_under(nng_module, "Socket");
|
32
|
+
VALUE rbnng_SocketSurveyor0Class =
|
33
|
+
rb_define_class_under(rbnng_SocketModule, "Surveyor0", rb_cObject);
|
34
|
+
rb_define_alloc_func(rbnng_SocketSurveyor0Class, socket_alloc);
|
35
|
+
rb_define_method(
|
36
|
+
rbnng_SocketSurveyor0Class, "initialize", socket_surveyor0_initialize, 0);
|
37
|
+
rb_define_method(rbnng_SocketSurveyor0Class, "send_msg", socket_send_msg, 1);
|
38
|
+
rb_define_method(rbnng_SocketSurveyor0Class, "get_msg", socket_get_msg, 0);
|
39
|
+
rb_define_method(rbnng_SocketSurveyor0Class, "listen", socket_listen, 1);
|
40
|
+
}
|
data/lib/nng/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adib Saad
|
@@ -18,14 +18,24 @@ extensions:
|
|
18
18
|
- ext/rbnng/extconf.rb
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- "./ext/rbnng/bus0.c"
|
22
|
+
- "./ext/rbnng/exceptions.c"
|
23
|
+
- "./ext/rbnng/exceptions.h"
|
21
24
|
- "./ext/rbnng/msg.c"
|
22
25
|
- "./ext/rbnng/msg.h"
|
26
|
+
- "./ext/rbnng/pair.c"
|
27
|
+
- "./ext/rbnng/pub0.c"
|
28
|
+
- "./ext/rbnng/pull0.c"
|
29
|
+
- "./ext/rbnng/push0.c"
|
23
30
|
- "./ext/rbnng/rbnng.c"
|
24
31
|
- "./ext/rbnng/rbnng.h"
|
25
32
|
- "./ext/rbnng/rep0.c"
|
26
33
|
- "./ext/rbnng/req0.c"
|
34
|
+
- "./ext/rbnng/respondent0.c"
|
27
35
|
- "./ext/rbnng/socket.c"
|
28
36
|
- "./ext/rbnng/socket.h"
|
37
|
+
- "./ext/rbnng/sub0.c"
|
38
|
+
- "./ext/rbnng/surveyor0.c"
|
29
39
|
- "./lib/nng.rb"
|
30
40
|
- "./lib/nng/version.rb"
|
31
41
|
- ext/rbnng/extconf.rb
|
@@ -45,9 +55,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
55
|
version: '0'
|
46
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
57
|
requirements:
|
48
|
-
- - "
|
58
|
+
- - ">="
|
49
59
|
- !ruby/object:Gem::Version
|
50
|
-
version:
|
60
|
+
version: '0'
|
51
61
|
requirements: []
|
52
62
|
rubygems_version: 3.1.6
|
53
63
|
signing_key:
|