rb-zmq 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2010-02-01
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/README.txt ADDED
@@ -0,0 +1,40 @@
1
+ = rb_zmq
2
+
3
+ * http://www.zeromq.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ This gem comprises the ruby bindings for ZeroMQ
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Posix signal handling is a bit goofy, so things like handling SIGTERM (ctrl-c) at the console have to be done by hand.
12
+
13
+ == SYNOPSIS:
14
+
15
+ bind_to = 'tcp://127.0.0.1:5555'
16
+ message_size = 1
17
+ roundtrip_count = 100
18
+
19
+ ctx = Context.new(1, 1, 0)
20
+ s = Socket.new(ctx, REP);
21
+ s.bind(bind_to);
22
+
23
+ for i in 0...roundtrip_count do
24
+ msg = s.recv(0)
25
+ s.send(msg, 0)
26
+ end
27
+
28
+ sleep 1
29
+
30
+
31
+ == REQUIREMENTS:
32
+
33
+ * The libzmq must be installed & loadable
34
+
35
+ == INSTALL:
36
+
37
+ * gem install rb-zmq
38
+
39
+ == LICENSE:
40
+ This software is licensed under the same terms as ZeroMQ itself.
data/extconf.rb ADDED
@@ -0,0 +1,28 @@
1
+ #
2
+ # Copyright (c) 2007-2010 iMatix Corporation
3
+ #
4
+ # This file is part of 0MQ.
5
+ #
6
+ # 0MQ is free software; you can redistribute it and/or modify it under
7
+ # the terms of the Lesser GNU General Public License as published by
8
+ # the Free Software Foundation; either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # 0MQ is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # Lesser GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the Lesser GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'mkmf'
20
+ dir_config('zmq')
21
+ if have_library('zmq', 'zmq_init') and have_header('zmq.h')
22
+ puts "cool, I found your zmq install..."
23
+ create_makefile("zmq")
24
+ else
25
+ raise "Couldn't find zmq library. try setting --with-zmq-dir=<path> to tell me where it is."
26
+ end
27
+
28
+
data/lib/rb_zmq.rb ADDED
@@ -0,0 +1 @@
1
+ require 'librbzmq'
data/rbzmq.cpp ADDED
@@ -0,0 +1,286 @@
1
+ /*
2
+ Copyright (c) 2007-2010 iMatix Corporation
3
+
4
+ This file is part of 0MQ.
5
+
6
+ 0MQ is free software; you can redistribute it and/or modify it under
7
+ the terms of the Lesser GNU General Public License as published by
8
+ the Free Software Foundation; either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ 0MQ is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ Lesser GNU General Public License for more details.
15
+
16
+ You should have received a copy of the Lesser GNU General Public License
17
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ #include <assert.h>
21
+ #include <errno.h>
22
+ #include <string.h>
23
+ #include <ruby.h>
24
+
25
+ #include "zmq.h"
26
+
27
+ static void context_free (void *ctx)
28
+ {
29
+ if (ctx) {
30
+ int rc = zmq_term (ctx);
31
+ assert (rc == 0);
32
+ }
33
+ }
34
+
35
+ static VALUE context_alloc (VALUE class_)
36
+ {
37
+ return rb_data_object_alloc (class_, NULL, 0, context_free);
38
+ }
39
+
40
+ static VALUE context_initialize (VALUE self_, VALUE app_threads_,
41
+ VALUE io_threads_, VALUE flags_)
42
+ {
43
+ assert (!DATA_PTR (self_));
44
+ void *ctx = zmq_init (NUM2INT (app_threads_), NUM2INT (io_threads_),
45
+ NUM2INT (flags_));
46
+ if (!ctx) {
47
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
48
+ return Qnil;
49
+ }
50
+
51
+ DATA_PTR (self_) = (void*) ctx;
52
+ return self_;
53
+ }
54
+
55
+ static void socket_free (void *s)
56
+ {
57
+ if (s) {
58
+ int rc = zmq_close (s);
59
+ assert (rc == 0);
60
+ }
61
+ }
62
+
63
+ static VALUE socket_alloc (VALUE class_)
64
+ {
65
+ return rb_data_object_alloc (class_, NULL, 0, socket_free);
66
+ }
67
+
68
+ static VALUE socket_initialize (VALUE self_, VALUE context_, VALUE type_)
69
+ {
70
+ assert (!DATA_PTR (self_));
71
+
72
+ if (strcmp (rb_obj_classname (context_), "Context") != 0) {
73
+ rb_raise (rb_eArgError, "expected Context object");
74
+ return Qnil;
75
+ }
76
+
77
+ void *s = zmq_socket (DATA_PTR (context_), NUM2INT (type_));
78
+ if (!s) {
79
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
80
+ return Qnil;
81
+ }
82
+
83
+ DATA_PTR (self_) = (void*) s;
84
+ return self_;
85
+ }
86
+
87
+
88
+ static VALUE socket_setsockopt (VALUE self_, VALUE option_,
89
+ VALUE optval_)
90
+ {
91
+
92
+ int rc = 0;
93
+
94
+ switch (NUM2INT (option_)) {
95
+ case ZMQ_HWM:
96
+ case ZMQ_LWM:
97
+ case ZMQ_SWAP:
98
+ case ZMQ_AFFINITY:
99
+ case ZMQ_RATE:
100
+ case ZMQ_RECOVERY_IVL:
101
+ case ZMQ_MCAST_LOOP:
102
+ {
103
+ long optval = FIX2LONG (optval_);
104
+
105
+ // Forward the code to native 0MQ library.
106
+ rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_),
107
+ (void *) &optval, 4);
108
+ }
109
+ break;
110
+
111
+ case ZMQ_IDENTITY:
112
+ case ZMQ_SUBSCRIBE:
113
+ case ZMQ_UNSUBSCRIBE:
114
+
115
+ // Forward the code to native 0MQ library.
116
+ rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_),
117
+ (void *) StringValueCStr (optval_), RSTRING_LEN (optval_));
118
+ break;
119
+
120
+ default:
121
+ rc = -1;
122
+ errno = EINVAL;
123
+ }
124
+
125
+ if (rc != 0) {
126
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
127
+ return Qnil;
128
+ }
129
+
130
+ return self_;
131
+ }
132
+
133
+
134
+ static VALUE socket_bind (VALUE self_, VALUE addr_)
135
+ {
136
+ assert (DATA_PTR (self_));
137
+
138
+ int rc = zmq_bind (DATA_PTR (self_), rb_string_value_cstr (&addr_));
139
+ if (rc != 0) {
140
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
141
+ return Qnil;
142
+ }
143
+
144
+ return Qnil;
145
+ }
146
+
147
+ static VALUE socket_connect (VALUE self_, VALUE addr_)
148
+ {
149
+ assert (DATA_PTR (self_));
150
+
151
+ int rc = zmq_connect (DATA_PTR (self_), rb_string_value_cstr (&addr_));
152
+ if (rc != 0) {
153
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
154
+ return Qnil;
155
+ }
156
+
157
+ return Qnil;
158
+ }
159
+
160
+ static VALUE socket_send (VALUE self_, VALUE msg_, VALUE flags_)
161
+ {
162
+ assert (DATA_PTR (self_));
163
+
164
+ Check_Type (msg_, T_STRING);
165
+
166
+ zmq_msg_t msg;
167
+ int rc = zmq_msg_init_size (&msg, RSTRING_LEN (msg_));
168
+ if (rc != 0) {
169
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
170
+ return Qnil;
171
+ }
172
+ memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), RSTRING_LEN (msg_));
173
+
174
+ rc = zmq_send (DATA_PTR (self_), &msg, NUM2INT (flags_));
175
+ if (rc != 0 && errno == EAGAIN) {
176
+ rc = zmq_msg_close (&msg);
177
+ assert (rc == 0);
178
+ return Qfalse;
179
+ }
180
+
181
+ if (rc != 0) {
182
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
183
+ rc = zmq_msg_close (&msg);
184
+ assert (rc == 0);
185
+ return Qnil;
186
+ }
187
+
188
+ rc = zmq_msg_close (&msg);
189
+ assert (rc == 0);
190
+ return Qtrue;
191
+ }
192
+
193
+ static VALUE socket_flush (VALUE self_)
194
+ {
195
+ assert (DATA_PTR (self_));
196
+
197
+ int rc = zmq_flush (DATA_PTR (self_));
198
+ if (rc != 0) {
199
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
200
+ return Qnil;
201
+ }
202
+
203
+ return Qnil;
204
+ }
205
+
206
+ static VALUE socket_recv (VALUE self_, VALUE flags_)
207
+ {
208
+ assert (DATA_PTR (self_));
209
+
210
+ zmq_msg_t msg;
211
+ int rc = zmq_msg_init (&msg);
212
+ assert (rc == 0);
213
+
214
+ rc = zmq_recv (DATA_PTR (self_), &msg, NUM2INT (flags_));
215
+ if (rc != 0 && errno == EAGAIN) {
216
+ rc = zmq_msg_close (&msg);
217
+ assert (rc == 0);
218
+ return Qnil;
219
+ }
220
+
221
+ if (rc != 0) {
222
+ rb_raise (rb_eRuntimeError, zmq_strerror (errno));
223
+ rc = zmq_msg_close (&msg);
224
+ assert (rc == 0);
225
+ return Qnil;
226
+ }
227
+
228
+ VALUE message = rb_str_new ((char*) zmq_msg_data (&msg),
229
+ zmq_msg_size (&msg));
230
+ rc = zmq_msg_close (&msg);
231
+ assert (rc == 0);
232
+ return message;
233
+ }
234
+
235
+ extern "C" void Init_librbzmq ()
236
+ {
237
+ VALUE context_type = rb_define_class ("Context", rb_cObject);
238
+ rb_define_alloc_func (context_type, context_alloc);
239
+ rb_define_method (context_type, "initialize",
240
+ (VALUE(*)(...)) context_initialize, 3);
241
+
242
+ VALUE socket_type = rb_define_class ("Socket", rb_cObject);
243
+ rb_define_alloc_func (socket_type, socket_alloc);
244
+ rb_define_method (socket_type, "initialize",
245
+ (VALUE(*)(...)) socket_initialize, 2);
246
+ rb_define_method (socket_type, "setsockopt",
247
+ (VALUE(*)(...)) socket_setsockopt, 2);
248
+ rb_define_method (socket_type, "bind",
249
+ (VALUE(*)(...)) socket_bind, 1);
250
+ rb_define_method (socket_type, "connect",
251
+ (VALUE(*)(...)) socket_connect, 1);
252
+ rb_define_method (socket_type, "send",
253
+ (VALUE(*)(...)) socket_send, 2);
254
+ rb_define_method (socket_type, "flush",
255
+ (VALUE(*)(...)) socket_flush, 0);
256
+ rb_define_method (socket_type, "recv",
257
+ (VALUE(*)(...)) socket_recv, 1);
258
+
259
+ rb_define_global_const ("HWM", INT2NUM (ZMQ_HWM));
260
+ rb_define_global_const ("LWM", INT2NUM (ZMQ_LWM));
261
+ rb_define_global_const ("SWAP", INT2NUM (ZMQ_SWAP));
262
+ rb_define_global_const ("AFFINITY", INT2NUM (ZMQ_AFFINITY));
263
+ rb_define_global_const ("IDENTITY", INT2NUM (ZMQ_IDENTITY));
264
+ rb_define_global_const ("SUBSCRIBE", INT2NUM (ZMQ_SUBSCRIBE));
265
+ rb_define_global_const ("UNSUBSCRIBE", INT2NUM (ZMQ_UNSUBSCRIBE));
266
+ rb_define_global_const ("RATE", INT2NUM (ZMQ_RATE));
267
+ rb_define_global_const ("RECOVERY_IVL", INT2NUM (ZMQ_RECOVERY_IVL));
268
+ rb_define_global_const ("MCAST_LOOP", INT2NUM (ZMQ_MCAST_LOOP));
269
+ rb_define_global_const ("SNDBUF", INT2NUM (ZMQ_SNDBUF));
270
+ rb_define_global_const ("RCVBUF", INT2NUM (ZMQ_RCVBUF));
271
+
272
+ rb_define_global_const ("NOBLOCK", INT2NUM (ZMQ_NOBLOCK));
273
+ rb_define_global_const ("NOFLUSH", INT2NUM (ZMQ_NOFLUSH));
274
+
275
+ rb_define_global_const ("P2P", INT2NUM (ZMQ_P2P));
276
+ rb_define_global_const ("SUB", INT2NUM (ZMQ_SUB));
277
+ rb_define_global_const ("PUB", INT2NUM (ZMQ_PUB));
278
+ rb_define_global_const ("REQ", INT2NUM (ZMQ_REQ));
279
+ rb_define_global_const ("REP", INT2NUM (ZMQ_REP));
280
+ rb_define_global_const ("XREQ", INT2NUM (ZMQ_XREQ));
281
+ rb_define_global_const ("XREP", INT2NUM (ZMQ_XREP));
282
+ rb_define_global_const ("UPSTREAM", INT2NUM (ZMQ_UPSTREAM));
283
+ rb_define_global_const ("DOWNSTREAM", INT2NUM (ZMQ_DOWNSTREAM));
284
+
285
+ rb_define_global_const ("POLL", INT2NUM (ZMQ_POLL));
286
+ }
@@ -0,0 +1,10 @@
1
+ require "test/unit"
2
+
3
+ class TestRbZmq < Test::Unit::TestCase
4
+ def test_sanity
5
+ require 'rubygems'
6
+ gem 'rb-zmq'
7
+ require 'librbzmq'
8
+ assert Context.new 1,1,0
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb-zmq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - McClain Looney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-01 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.2.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: gemcutter
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.5.0
44
+ version:
45
+ description: This gem comprises the ruby bindings for ZeroMQ
46
+ email:
47
+ - m@loonsoft.com
48
+ executables: []
49
+
50
+ extensions:
51
+ - extconf.rb
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - README.txt
55
+ files:
56
+ - History.txt
57
+ - README.txt
58
+ - extconf.rb
59
+ - rbzmq.cpp
60
+ - lib/rb_zmq.rb
61
+ has_rdoc: true
62
+ homepage: http://www.zeromq.org
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --main
68
+ - README.txt
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: rb-zmq
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: This gem comprises the ruby bindings for ZeroMQ
90
+ test_files:
91
+ - test/test_rb_zmq.rb