rbczmq 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
data/ext/rbczmq/context.c CHANGED
@@ -303,8 +303,6 @@ static VALUE rb_czmq_ctx_socket(VALUE obj, VALUE type)
303
303
  sock->state = ZMQ_SOCKET_PENDING;
304
304
  sock->endpoint = Qnil;
305
305
  sock->thread = rb_thread_current();
306
- sock->recv_timeout = ZMQ_SOCKET_DEFAULT_TIMEOUT;
307
- sock->send_timeout = ZMQ_SOCKET_DEFAULT_TIMEOUT;
308
306
  rb_obj_call_init(socket, 0, NULL);
309
307
  return socket;
310
308
  }
@@ -23,8 +23,15 @@
23
23
  #define ZmqAssertSysError() if (zmq_errno() != 0 && zmq_errno() != EAGAIN) ZmqRaiseSysError();
24
24
  #define ZmqAssert(rc) \
25
25
  if (rc == -1) { \
26
- ZmqAssertSysError(); \
27
26
  if (rc == ENOMEM) rb_memerror(); \
27
+ switch (zmq_errno()) { \
28
+ case EMTHREAD: \
29
+ rb_raise(rb_eZmqError, "There are no I/O thread available to complete this operation. Please initialize the ZMQ context with at least one I/O thread eg. ZMQ::Context.new(1)"); \
30
+ break; \
31
+ default : \
32
+ ZmqAssertSysError(); \
33
+ break; \
34
+ } \
28
35
  return Qfalse; \
29
36
  }
30
37
  #define ZmqAssertObjOnAlloc(obj, wrapper) \
data/ext/rbczmq/socket.c CHANGED
@@ -42,8 +42,6 @@ void rb_czmq_mark_sock(void *ptr)
42
42
  zclock_log ("I: %s socket %p, context %p: GC mark", zsocket_type_str(sock->socket), sock, sock->ctx);
43
43
  rb_gc_mark(sock->endpoint);
44
44
  rb_gc_mark(sock->thread);
45
- rb_gc_mark(sock->recv_timeout);
46
- rb_gc_mark(sock->send_timeout);
47
45
  }
48
46
  }
49
47
 
@@ -219,7 +217,7 @@ static VALUE rb_czmq_socket_bind(VALUE obj, VALUE endpoint)
219
217
  args.socket = sock;
220
218
  args.endpoint = StringValueCStr(endpoint);
221
219
  rc = (int)rb_thread_blocking_region(rb_czmq_nogvl_socket_bind, (void *)&args, RUBY_UBF_IO, 0);
222
- if (rc == -1) ZmqRaiseSysError();
220
+ ZmqAssert(rc);
223
221
  if (sock->verbose)
224
222
  zclock_log ("I: %s socket %p: bound \"%s\"", zsocket_type_str(sock->socket), obj, StringValueCStr(endpoint));
225
223
  sock->state = ZMQ_SOCKET_BOUND;
@@ -303,6 +301,8 @@ try_writable:
303
301
  return (VALUE)zstr_send_nowait(socket->socket, args->msg);
304
302
  } else {
305
303
  rb_thread_wait_fd(zsocket_fd(socket->socket));
304
+ if (zsocket_sndtimeo(socket->socket) != -1)
305
+ return (VALUE)zstr_send(socket->socket, args->msg);
306
306
  goto try_writable;
307
307
  }
308
308
  #endif
@@ -326,6 +326,8 @@ try_writable:
326
326
  if ((zsocket_events(socket->socket) & ZMQ_POLLOUT) == ZMQ_POLLOUT) {
327
327
  return (VALUE)zstr_sendm(socket->socket, args->msg);
328
328
  } else {
329
+ if (zsocket_sndtimeo(socket->socket) != -1)
330
+ return (VALUE)zstr_sendm(socket->socket, args->msg);
329
331
  rb_thread_wait_fd(zsocket_fd(socket->socket));
330
332
  goto try_writable;
331
333
  }
@@ -420,6 +422,8 @@ try_readable:
420
422
  return (VALUE)zlist_pop(socket->str_buffer);
421
423
  } else {
422
424
  rb_thread_wait_fd(zsocket_fd(socket->socket));
425
+ if (zsocket_rcvtimeo(socket->socket) != -1)
426
+ return zstr_recv(socket->socket);
423
427
  goto try_readable;
424
428
  }
425
429
  #endif
@@ -784,92 +788,6 @@ static VALUE rb_czmq_socket_recv_message(VALUE obj)
784
788
  return rb_czmq_alloc_message(message);
785
789
  }
786
790
 
787
- /*
788
- * call-seq:
789
- * sock.recv_timeout = 5 => nil
790
- *
791
- * Sets a receive timeout for this socket.
792
- *
793
- * === Examples
794
- * ctx = ZMQ::Context.new
795
- * sock = ctx.socket(:REP)
796
- * sock.recv_timeout = 5 => nil
797
- *
798
- */
799
-
800
- static VALUE rb_czmq_socket_set_recv_timeout(VALUE obj, VALUE timeout)
801
- {
802
- zmq_sock_wrapper *sock = NULL;
803
- GetZmqSocket(obj);
804
- if (TYPE(timeout) != T_FIXNUM && TYPE(timeout) != T_FLOAT) rb_raise(rb_eTypeError, "wrong timeout type %s (expected Fixnum or Float)", RSTRING_PTR(rb_obj_as_string(timeout)));
805
- sock->recv_timeout = timeout;
806
- return Qnil;
807
- }
808
-
809
- /*
810
- * call-seq:
811
- * sock.recv_timeout => Fixnum or nil
812
- *
813
- * Returns the recv timeout currently associated with this socket.
814
- *
815
- * === Examples
816
- * ctx = ZMQ::Context.new
817
- * sock = ctx.socket(:REP)
818
- * sock.recv_timeout = 5
819
- * sock.recv_timeout => 5
820
- *
821
- */
822
-
823
- static VALUE rb_czmq_socket_recv_timeout(VALUE obj)
824
- {
825
- zmq_sock_wrapper *sock = NULL;
826
- GetZmqSocket(obj);
827
- return sock->recv_timeout;
828
- }
829
-
830
- /*
831
- * call-seq:
832
- * sock.send_timeout = 5 => nil
833
- *
834
- * Sets a send timeout for this socket.
835
- *
836
- * === Examples
837
- * ctx = ZMQ::Context.new
838
- * sock = ctx.socket(:REP)
839
- * sock.send_timeout = 5 => nil
840
- *
841
- */
842
-
843
- static VALUE rb_czmq_socket_set_send_timeout(VALUE obj, VALUE timeout)
844
- {
845
- zmq_sock_wrapper *sock = NULL;
846
- GetZmqSocket(obj);
847
- if (TYPE(timeout) != T_FIXNUM && TYPE(timeout) != T_FLOAT) rb_raise(rb_eTypeError, "wrong timeout type %s (expected Fixnum or Float)", RSTRING_PTR(rb_obj_as_string(timeout)));
848
- sock->send_timeout = timeout;
849
- return Qnil;
850
- }
851
-
852
- /*
853
- * call-seq:
854
- * sock.send_timeout => Fixnum or nil
855
- *
856
- * Returns the send timeout currently associated with this socket.
857
- *
858
- * === Examples
859
- * ctx = ZMQ::Context.new
860
- * sock = ctx.socket(:REP)
861
- * sock.send_timeout = 5
862
- * sock.send_timeout => 5
863
- *
864
- */
865
-
866
- static VALUE rb_czmq_socket_send_timeout(VALUE obj)
867
- {
868
- zmq_sock_wrapper *sock = NULL;
869
- GetZmqSocket(obj);
870
- return sock->send_timeout;
871
- }
872
-
873
791
  /*
874
792
  * call-seq:
875
793
  * sock.hwm => Fixnum
@@ -1601,10 +1519,6 @@ void _init_rb_czmq_socket()
1601
1519
  rb_define_method(rb_cZmqSocket, "recv_frame", rb_czmq_socket_recv_frame, 0);
1602
1520
  rb_define_method(rb_cZmqSocket, "recv_frame_nonblock", rb_czmq_socket_recv_frame_nonblock, 0);
1603
1521
  rb_define_method(rb_cZmqSocket, "recv_message", rb_czmq_socket_recv_message, 0);
1604
- rb_define_method(rb_cZmqSocket, "recv_timeout=", rb_czmq_socket_set_recv_timeout, 1);
1605
- rb_define_method(rb_cZmqSocket, "recv_timeout", rb_czmq_socket_recv_timeout, 0);
1606
- rb_define_method(rb_cZmqSocket, "send_timeout=", rb_czmq_socket_set_send_timeout, 1);
1607
- rb_define_method(rb_cZmqSocket, "send_timeout", rb_czmq_socket_send_timeout, 0);
1608
1522
 
1609
1523
  rb_define_method(rb_cZmqSocket, "hwm", rb_czmq_socket_opt_hwm, 0);
1610
1524
  rb_define_method(rb_cZmqSocket, "hwm=", rb_czmq_socket_set_opt_hwm, 1);
data/ext/rbczmq/socket.h CHANGED
@@ -22,12 +22,8 @@ typedef struct {
22
22
  #endif
23
23
  VALUE endpoint;
24
24
  VALUE thread;
25
- VALUE recv_timeout;
26
- VALUE send_timeout;
27
25
  } zmq_sock_wrapper;
28
26
 
29
- #define ZMQ_SOCKET_DEFAULT_TIMEOUT Qnil
30
-
31
27
  #define ZmqAssertSocket(obj) ZmqAssertType(obj, rb_cZmqSocket, "ZMQ::Socket")
32
28
  #define GetZmqSocket(obj) \
33
29
  ZmqAssertSocket(obj); \
data/lib/zmq/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ZMQ
4
- VERSION = "0.6"
4
+ VERSION = "0.7"
5
5
  end
data/test/test_socket.rb CHANGED
@@ -3,6 +3,42 @@
3
3
  require File.join(File.dirname(__FILE__), 'helper')
4
4
 
5
5
  class TestZmqSocket < ZmqTestCase
6
+ def test_recv_timeout
7
+ ctx = ZMQ::Context.new
8
+ rep = ctx.bind(:REP, "inproc://test.socket-recv-timeout")
9
+ start = Time.now.to_f
10
+ rep.rcvtimeo = 300
11
+ rep.recv
12
+ elapsed = Time.now.to_f - start
13
+ assert_in_delta elapsed, 0.29, 0.32
14
+ ensure
15
+ ctx.destroy
16
+ end
17
+
18
+ def test_send_timeout
19
+ ctx = ZMQ::Context.new
20
+ rep = ctx.socket(:REP)
21
+ rep.hwm = 1
22
+ rep.bind("inproc://test.socket-send-timeout")
23
+ req = ctx.connect(:REQ, "inproc://test.socket-send-timeout")
24
+ # Cannot test much other than normal transfer's not disrupted
25
+ req.sndtimeo = 100
26
+ req.send "msg"
27
+ assert_equal "msg", rep.recv
28
+ ensure
29
+ ctx.destroy
30
+ end
31
+
32
+ def test_connect_no_io_thread
33
+ ctx = ZMQ::Context.new(0)
34
+ sock = ctx.socket(:REP)
35
+ assert_raises ZMQ::Error do
36
+ sock.bind("tcp://127.0.0.1:*")
37
+ end
38
+ ensure
39
+ ctx.destroy
40
+ end
41
+
6
42
  def test_fd
7
43
  ctx = ZMQ::Context.new
8
44
  sock = ctx.socket(:REP)
@@ -54,32 +90,6 @@ class TestZmqSocket < ZmqTestCase
54
90
  ctx.destroy
55
91
  end
56
92
 
57
- def test_recv_timeout
58
- ctx = ZMQ::Context.new
59
- sock = ctx.socket(:REP)
60
- assert_nil sock.recv_timeout
61
- sock.recv_timeout = 10
62
- assert_equal 10, sock.recv_timeout
63
- assert_raises TypeError do
64
- sock.recv_timeout = :x
65
- end
66
- ensure
67
- ctx.destroy
68
- end
69
-
70
- def test_send_timeout
71
- ctx = ZMQ::Context.new
72
- sock = ctx.socket(:REP)
73
- assert_nil sock.send_timeout
74
- sock.send_timeout = 10
75
- assert_equal 10, sock.send_timeout
76
- assert_raises TypeError do
77
- sock.send_timeout = :x
78
- end
79
- ensure
80
- ctx.destroy
81
- end
82
-
83
93
  def test_gc_context_reaped
84
94
  pub = ZMQ::Context.new.socket(:PUB)
85
95
  GC.start
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbczmq
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- version: "0.6"
8
+ - 7
9
+ version: "0.7"
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Lourens Naud\xC3\xA9"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-10 00:00:00 Z
18
+ date: 2012-05-12 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake-compiler
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  requirements: []
174
174
 
175
175
  rubyforge_project:
176
- rubygems_version: 1.8.23
176
+ rubygems_version: 1.8.15
177
177
  signing_key:
178
178
  specification_version: 3
179
179
  summary: "Ruby extension for CZMQ - High-level C Binding for \xC3\x98MQ (http://czmq.zeromq.org)"
@@ -204,4 +204,3 @@ test_files:
204
204
  - test/test_threading.rb
205
205
  - test/test_timer.rb
206
206
  - test/test_zmq.rb
207
- has_rdoc: true