ffi-rzmq 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +1 -1
  3. data/AUTHORS.txt +5 -1
  4. data/History.txt +19 -0
  5. data/README.rdoc +2 -4
  6. data/examples/README.rdoc +1 -3
  7. data/examples/{v3api/latency_measurement.rb → latency_measurement.rb} +1 -2
  8. data/examples/{v3api/local_lat.rb → local_lat.rb} +1 -1
  9. data/examples/{v3api/local_lat_poll.rb → local_lat_poll.rb} +4 -4
  10. data/examples/{v3api/local_throughput.rb → local_throughput.rb} +1 -1
  11. data/examples/{v3api/pub.rb → pub.rb} +1 -1
  12. data/examples/{v2api/publish_subscribe.rb → publish_subscribe.rb} +2 -2
  13. data/examples/{v2api/remote_lat.rb → remote_lat.rb} +1 -1
  14. data/examples/{v3api/remote_throughput.rb → remote_throughput.rb} +4 -3
  15. data/examples/{v2api/reqrep_poll.rb → reqrep_poll.rb} +3 -4
  16. data/examples/{v2api/request_response.rb → request_response.rb} +1 -2
  17. data/examples/{v3api/sub.rb → sub.rb} +1 -2
  18. data/examples/{v3api/throughput_measurement.rb → throughput_measurement.rb} +1 -1
  19. data/examples/{v3api/xreqxrep_poll.rb → xreqxrep_poll.rb} +6 -7
  20. data/ffi-rzmq.gemspec +4 -4
  21. data/lib/ffi-rzmq.rb +2 -3
  22. data/lib/ffi-rzmq/context.rb +25 -53
  23. data/lib/ffi-rzmq/device.rb +8 -4
  24. data/lib/ffi-rzmq/message.rb +24 -30
  25. data/lib/ffi-rzmq/poll.rb +5 -16
  26. data/lib/ffi-rzmq/socket.rb +129 -278
  27. data/lib/ffi-rzmq/util.rb +11 -36
  28. data/lib/ffi-rzmq/version.rb +1 -1
  29. data/spec/context_spec.rb +3 -8
  30. data/spec/device_spec.rb +10 -9
  31. data/spec/nonblocking_recv_spec.rb +7 -7
  32. data/spec/pushpull_spec.rb +8 -7
  33. data/spec/reqrep_spec.rb +6 -6
  34. data/spec/socket_spec.rb +43 -130
  35. data/spec/spec_helper.rb +3 -11
  36. metadata +77 -104
  37. data/examples/v2api/latency_measurement.rb +0 -139
  38. data/examples/v2api/local_lat.rb +0 -58
  39. data/examples/v2api/local_lat_poll.rb +0 -66
  40. data/examples/v2api/local_throughput.rb +0 -58
  41. data/examples/v2api/pub.rb +0 -46
  42. data/examples/v2api/remote_throughput.rb +0 -39
  43. data/examples/v2api/sub.rb +0 -74
  44. data/examples/v2api/throughput_measurement.rb +0 -138
  45. data/examples/v2api/xreqxrep_poll.rb +0 -93
  46. data/examples/v3api/publish_subscribe.rb +0 -82
  47. data/examples/v3api/remote_lat.rb +0 -71
  48. data/examples/v3api/reqrep_poll.rb +0 -62
  49. data/examples/v3api/request_response.rb +0 -40
  50. data/lib/ffi-rzmq/constants.rb +0 -187
  51. data/lib/ffi-rzmq/libc.rb +0 -19
  52. data/lib/ffi-rzmq/libzmq.rb +0 -283
@@ -28,19 +28,6 @@ module ZMQ
28
28
  LibZMQ.zmq_strerror(errno).read_string
29
29
  end
30
30
 
31
- # Returns an array of the form [major, minor, patch] to represent the
32
- # version of libzmq.
33
- #
34
- # Class method! Invoke as: ZMQ::Util.version
35
- #
36
- def self.version
37
- major = FFI::MemoryPointer.new :int
38
- minor = FFI::MemoryPointer.new :int
39
- patch = FFI::MemoryPointer.new :int
40
- LibZMQ.zmq_version major, minor, patch
41
- [major.read_int, minor.read_int, patch.read_int]
42
- end
43
-
44
31
  # Attempts to bind to a random tcp port on +host+ up to +max_tries+
45
32
  # times. Returns the port number upon success or nil upon failure.
46
33
  #
@@ -56,7 +43,7 @@ module ZMQ
56
43
 
57
44
  resultcode_ok?(rc) ? random : nil
58
45
  end
59
-
46
+
60
47
  # :doc:
61
48
  # Called to verify whether there were any errors during
62
49
  # operation. If any are found, raise the appropriate #ZeroMQError.
@@ -90,38 +77,26 @@ module ZMQ
90
77
 
91
78
  else
92
79
  raise ZeroMQError.new source, result_code, -1,
93
- "Source [#{source}] does not match any zmq_* strings, rc [#{result_code}], errno [#{ZMQ::Util.errno}], error_string [#{ZMQ::Util.error_string}]"
80
+ "Source [#{source}] does not match any zmq_* strings, rc [#{result_code}], errno [#{ZMQ::Util.errno}], error_string [#{ZMQ::Util.error_string}]"
94
81
  end
95
82
  end
96
83
 
97
84
  def self.eagain?
98
85
  EAGAIN == ZMQ::Util.errno
99
86
  end
100
-
101
- if LibZMQ.version2?
102
- def self.context_error?(source)
103
- 'zmq_init' == source ||
104
- 'zmq_socket' == source
105
- end
106
-
107
- def self.message_error?(source)
108
- ['zmq_msg_init', 'zmq_msg_init_data', 'zmq_msg_copy', 'zmq_msg_move'].include?(source)
109
- end
110
-
111
- elsif LibZMQ.version3?
112
- def self.context_error?(source)
113
- 'zmq_ctx_new' == source ||
87
+
88
+ def self.context_error?(source)
89
+ 'zmq_ctx_new' == source ||
114
90
  'zmq_ctx_set' == source ||
115
91
  'zmq_ctx_get' == source ||
116
92
  'zmq_ctx_destory' == source ||
117
93
  'zmq_ctx_set_monitor' == source
118
- end
119
-
120
- def self.message_error?(source)
121
- ['zmq_msg_init', 'zmq_msg_init_data', 'zmq_msg_copy', 'zmq_msg_move', 'zmq_msg_close', 'zmq_msg_get',
122
- 'zmq_msg_more', 'zmq_msg_recv', 'zmq_msg_send', 'zmq_msg_set'].include?(source)
123
- end
124
- end # if LibZMQ.version...?
94
+ end
95
+
96
+ def self.message_error?(source)
97
+ ['zmq_msg_init', 'zmq_msg_init_data', 'zmq_msg_copy', 'zmq_msg_move', 'zmq_msg_close', 'zmq_msg_get',
98
+ 'zmq_msg_more', 'zmq_msg_recv', 'zmq_msg_send', 'zmq_msg_set'].include?(source)
99
+ end
125
100
 
126
101
  end # module Util
127
102
 
@@ -1,3 +1,3 @@
1
1
  module ZMQ
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -84,14 +84,9 @@ module ZMQ
84
84
 
85
85
  it "should call the correct library function to terminate the context" do
86
86
  ctx = Context.new
87
-
88
- if LibZMQ.version2?
89
- LibZMQ.should_receive(:zmq_term).and_return(0)
90
- ctx.terminate
91
- else
92
- LibZMQ.should_receive(:zmq_ctx_destroy).with(ctx.pointer).and_return(0)
93
- ctx.terminate
94
- end
87
+
88
+ LibZMQ.should_receive(:zmq_ctx_destroy).with(ctx.pointer).and_return(0)
89
+ ctx.terminate
95
90
  end
96
91
  end # context terminate
97
92
 
@@ -5,7 +5,7 @@ module ZMQ
5
5
  describe Device do
6
6
  include APIHelper
7
7
 
8
- before(:all) do
8
+ before(:each) do
9
9
  @ctx = Context.new
10
10
  poller_setup
11
11
  @front_endpoint = "inproc://device_front_test"
@@ -13,7 +13,7 @@ module ZMQ
13
13
  @mutex = Mutex.new
14
14
  end
15
15
 
16
- after(:all) do
16
+ after(:each) do
17
17
  @ctx.terminate
18
18
  end
19
19
 
@@ -26,7 +26,9 @@ module ZMQ
26
26
  front = @ctx.socket(ZMQ::PUSH)
27
27
  front.bind(@front_endpoint)
28
28
  @mutex.synchronize { @device_thread = true }
29
- Device.new(ZMQ::STREAMER, back, front)
29
+ puts "create streamer device and running..."
30
+ Device.new(back, front)
31
+ puts "device exited"
30
32
  back.close
31
33
  front.close
32
34
  end
@@ -34,12 +36,11 @@ module ZMQ
34
36
 
35
37
  def wait_for_device
36
38
  loop do
37
- can_break = false
38
- @mutex.synchronize do
39
- can_break = true if @device_thread
40
- end
39
+ can_break = @mutex.synchronize { @device_thread }
40
+
41
41
  break if can_break
42
42
  end
43
+ puts "broke out of wait_for_device loop"
43
44
  end
44
45
 
45
46
  it "should create a device without error given valid opts" do
@@ -61,7 +62,7 @@ module ZMQ
61
62
  end
62
63
 
63
64
  res = ''
64
- rc = puller.recv_string(res, ZMQ::NonBlocking)
65
+ rc = puller.recv_string(res, ZMQ::DONTWAIT)
65
66
  res.should == "hello"
66
67
 
67
68
  pusher.close
@@ -70,7 +71,7 @@ module ZMQ
70
71
 
71
72
  it "should raise an ArgumentError when trying to pass non-socket objects into the device" do
72
73
  lambda {
73
- Device.new(ZMQ::STREAMER, 1,2)
74
+ Device.new(1,2)
74
75
  }.should raise_exception(ArgumentError)
75
76
  end
76
77
  end
@@ -12,19 +12,19 @@ module ZMQ
12
12
 
13
13
  it "returns -1 when there are no messages to read" do
14
14
  array = []
15
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
15
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
16
16
  Util.resultcode_ok?(rc).should be_false
17
17
  end
18
18
 
19
19
  it "gets EAGAIN when there are no messages to read" do
20
20
  array = []
21
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
21
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
22
22
  ZMQ::Util.errno.should == ZMQ::EAGAIN
23
23
  end
24
24
 
25
25
  it "returns the given array unmodified when there are no messages to read" do
26
26
  array = []
27
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
27
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
28
28
  array.size.should be_zero
29
29
  end
30
30
 
@@ -39,7 +39,7 @@ module ZMQ
39
39
  end
40
40
 
41
41
  array = []
42
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
42
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
43
43
  Util.resultcode_ok?(rc).should be_true
44
44
  array.size.should == 1
45
45
  end
@@ -52,7 +52,7 @@ module ZMQ
52
52
  end
53
53
 
54
54
  array = []
55
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
55
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
56
56
  Util.resultcode_ok?(rc).should be_true
57
57
  array.size.should == 10
58
58
  end
@@ -68,7 +68,7 @@ module ZMQ
68
68
  end
69
69
 
70
70
  array = []
71
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
71
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
72
72
  Util.resultcode_ok?(rc).should be_true
73
73
  array.size.should == 1 + 1 # extra 1 for envelope
74
74
  end
@@ -81,7 +81,7 @@ module ZMQ
81
81
  end
82
82
 
83
83
  array = []
84
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
84
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
85
85
  Util.resultcode_ok?(rc).should be_true
86
86
  array.size.should == 10 + 1 # add 1 for the envelope
87
87
  end
@@ -44,11 +44,11 @@ module ZMQ
44
44
 
45
45
  poll_it_for_read(@pull) do
46
46
  rc = @push.sendmsg sent_message
47
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
47
+ rc.should == string.size
48
48
  end
49
49
 
50
- rc = @pull.recvmsg received_message, ZMQ::NonBlocking
51
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
50
+ rc = @pull.recvmsg received_message, ZMQ::DONTWAIT
51
+ rc.should == string.size
52
52
  received_message.copy_out_string.should == string
53
53
  end
54
54
 
@@ -71,15 +71,16 @@ module ZMQ
71
71
  sockets << @pull
72
72
 
73
73
  sockets.each do |socket|
74
- threads << Thread.new do
74
+ thr = Thread.new do
75
75
  buffer = ''
76
76
  rc = socket.recv_string buffer
77
- version2? ? (rc.should == 0) : (rc.should == buffer.size)
77
+ rc.should == buffer.size
78
78
  mutex.synchronize { received << buffer }
79
79
  socket.close
80
80
  end
81
+ threads << thr
81
82
  end
82
-
83
+
83
84
  count.times { @push.send_string(string) }
84
85
 
85
86
  threads.each {|t| t.join}
@@ -98,7 +99,7 @@ module ZMQ
98
99
  buffer = ''
99
100
  rc = 0
100
101
  mutex.synchronize { rc = @pull.recv_string buffer }
101
- version2? ? (rc.should == 0) : (rc.should == buffer.size)
102
+ rc.should == buffer.size
102
103
  mutex.synchronize { received << buffer }
103
104
  end
104
105
  end
@@ -54,9 +54,9 @@ module ZMQ
54
54
  received_message = Message.new
55
55
 
56
56
  rc = @ping.sendmsg(Message.new(string))
57
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
57
+ rc.should == string.size
58
58
  rc = @pong.recvmsg received_message
59
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
59
+ rc.should == string.size
60
60
 
61
61
  received_message.copy_out_string.should == string
62
62
  end
@@ -66,12 +66,12 @@ module ZMQ
66
66
  received_message = Message.new
67
67
 
68
68
  poll_it_for_read(@pong) do
69
- rc = @ping.sendmsg(Message.new(string), ZMQ::NonBlocking)
70
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
69
+ rc = @ping.sendmsg(Message.new(string), ZMQ::DONTWAIT)
70
+ rc.should == string.size
71
71
  end
72
72
 
73
- rc = @pong.recvmsg received_message, ZMQ::NonBlocking
74
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
73
+ rc = @pong.recvmsg received_message, ZMQ::DONTWAIT
74
+ rc.should == string.size
75
75
 
76
76
  received_message.copy_out_string.should == string
77
77
  end
@@ -7,11 +7,8 @@ module ZMQ
7
7
  describe Socket do
8
8
  include APIHelper
9
9
 
10
- socket_types = if LibZMQ.version2?
11
- [ZMQ::REQ, ZMQ::REP, ZMQ::DEALER, ZMQ::ROUTER, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR]
12
- elsif LibZMQ.version3?
10
+ socket_types =
13
11
  [ZMQ::REQ, ZMQ::REP, ZMQ::DEALER, ZMQ::ROUTER, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR, ZMQ::XPUB, ZMQ::XSUB]
14
- end
15
12
 
16
13
  context "when initializing" do
17
14
  before(:all) { @ctx = Context.new }
@@ -173,123 +170,48 @@ module ZMQ
173
170
  end
174
171
  end # context using option ZMQ::IDENTITY
175
172
 
173
+ context "using option ZMQ::IPV4ONLY" do
174
+ it "should enable use of IPV6 sockets when set to 0" do
175
+ value = 0
176
+ socket.setsockopt ZMQ::IPV4ONLY, value
177
+ array = []
178
+ rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
179
+ rc.should == 0
180
+ array[0].should == value
181
+ end
176
182
 
177
- if version2?
178
-
179
- context "using option ZMQ::HWM" do
180
- it "should set the high water mark given a positive value" do
181
- hwm = 4
182
- socket.setsockopt ZMQ::HWM, hwm
183
- array = []
184
- rc = socket.getsockopt(ZMQ::HWM, array)
185
- rc.should == 0
186
- array[0].should == hwm
187
- end
188
- end # context using option ZMQ::HWM
189
-
190
-
191
- context "using option ZMQ::SWAP" do
192
- it "should set the swap value given a positive value" do
193
- swap = 10_000
194
- socket.setsockopt ZMQ::SWAP, swap
195
- array = []
196
- rc = socket.getsockopt(ZMQ::SWAP, array)
197
- rc.should == 0
198
- array[0].should == swap
199
- end
200
-
201
- it "returns -1 given a negative value" do
202
- swap = -10_000
203
- rc = socket.setsockopt(ZMQ::SWAP, swap)
204
- rc.should == -1
205
- end
206
- end # context using option ZMQ::SWP
207
-
208
-
209
- context "using option ZMQ::MCAST_LOOP" do
210
- it "should enable the multicast loopback given a 1 (true) value" do
211
- socket.setsockopt ZMQ::MCAST_LOOP, 1
212
- array = []
213
- rc = socket.getsockopt(ZMQ::MCAST_LOOP, array)
214
- rc.should == 0
215
- array[0].should be_true
216
- end
217
-
218
- it "should disable the multicast loopback given a 0 (false) value" do
219
- socket.setsockopt ZMQ::MCAST_LOOP, 0
220
- array = []
221
- rc = socket.getsockopt(ZMQ::MCAST_LOOP, array)
222
- rc.should == 0
223
- array[0].should be_false
224
- end
225
- end # context using option ZMQ::MCAST_LOOP
226
-
227
-
228
- context "using option ZMQ::RECOVERY_IVL_MSEC" do
229
- it "should set the time interval for saving messages measured in milliseconds given a positive value" do
230
- value = 200
231
- socket.setsockopt ZMQ::RECOVERY_IVL_MSEC, value
232
- array = []
233
- rc = socket.getsockopt(ZMQ::RECOVERY_IVL_MSEC, array)
234
- rc.should == 0
235
- array[0].should == value
236
- end
237
-
238
- it "should default to a value of -1" do
239
- value = -1
240
- array = []
241
- rc = socket.getsockopt(ZMQ::RECOVERY_IVL_MSEC, array)
242
- rc.should == 0
243
- array[0].should == value
244
- end
245
- end # context using option ZMQ::RECOVERY_IVL_MSEC
246
-
247
- else # version3 or higher
248
-
249
- context "using option ZMQ::IPV4ONLY" do
250
- it "should enable use of IPV6 sockets when set to 0" do
251
- value = 0
252
- socket.setsockopt ZMQ::IPV4ONLY, value
253
- array = []
254
- rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
255
- rc.should == 0
256
- array[0].should == value
257
- end
258
-
259
- it "should default to a value of 1" do
260
- value = 1
261
- array = []
262
- rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
263
- rc.should == 0
264
- array[0].should == value
265
- end
266
-
267
- it "returns -1 given a negative value" do
268
- value = -1
269
- rc = socket.setsockopt ZMQ::IPV4ONLY, value
270
- rc.should == -1
271
- end
183
+ it "should default to a value of 1" do
184
+ value = 1
185
+ array = []
186
+ rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
187
+ rc.should == 0
188
+ array[0].should == value
189
+ end
272
190
 
273
- it "returns -1 given a value > 1" do
274
- value = 2
275
- rc = socket.setsockopt ZMQ::IPV4ONLY, value
276
- rc.should == -1
277
- end
278
- end # context using option ZMQ::IPV4ONLY
191
+ it "returns -1 given a negative value" do
192
+ value = -1
193
+ rc = socket.setsockopt ZMQ::IPV4ONLY, value
194
+ rc.should == -1
195
+ end
279
196
 
280
- context "using option ZMQ::LAST_ENDPOINT" do
281
- it "should return last enpoint" do
282
- random_port = bind_to_random_tcp_port(socket, max_tries = 500)
283
- array = []
284
- rc = socket.getsockopt(ZMQ::LAST_ENDPOINT, array)
285
- ZMQ::Util.resultcode_ok?(rc).should == true
286
- endpoint_regex = %r{\Atcp://(.*):(\d+)\0\z}
287
- array[0].should =~ endpoint_regex
288
- Integer(array[0][endpoint_regex, 2]).should == random_port
289
- end
197
+ it "returns -1 given a value > 1" do
198
+ value = 2
199
+ rc = socket.setsockopt ZMQ::IPV4ONLY, value
200
+ rc.should == -1
290
201
  end
291
- end # version2? if/else block
202
+ end # context using option ZMQ::IPV4ONLY
292
203
 
204
+ context "using option ZMQ::LAST_ENDPOINT" do
205
+ it "should return last enpoint" do
206
+ random_port = bind_to_random_tcp_port(socket, max_tries = 500)
207
+ array = []
208
+ rc = socket.getsockopt(ZMQ::LAST_ENDPOINT, array)
209
+ ZMQ::Util.resultcode_ok?(rc).should == true
210
+ endpoint_regex = %r{\Atcp://(.*):(\d+)\0\z}
211
+ array[0].should =~ endpoint_regex
212
+ Integer(array[0][endpoint_regex, 2]).should == random_port
213
+ end
214
+ end
293
215
 
294
216
  context "using option ZMQ::SUBSCRIBE" do
295
217
  if ZMQ::SUB == socket_type
@@ -414,23 +336,13 @@ module ZMQ
414
336
  array[0].should == value
415
337
  end
416
338
 
417
- if (ZMQ::SUB == socket_type) && version3? || (defined?(ZMQ::XSUB) && ZMQ::XSUB == socket_type)
418
339
  it "should default to a value of 0" do
419
- value = 0
340
+ value = [SUB, XSUB].include?(socket_type) ? 0 : -1
420
341
  array = []
421
342
  rc = socket.getsockopt(ZMQ::LINGER, array)
422
343
  rc.should == 0
423
344
  array[0].should == value
424
345
  end
425
- else
426
- it "should default to a value of -1" do
427
- value = -1
428
- array = []
429
- rc = socket.getsockopt(ZMQ::LINGER, array)
430
- rc.should == 0
431
- array[0].should == value
432
- end
433
- end
434
346
  end # context using option ZMQ::LINGER
435
347
 
436
348
 
@@ -457,7 +369,8 @@ module ZMQ
457
369
  context "using option ZMQ::BACKLOG" do
458
370
  it "should set the maximum number of pending socket connections given a positive value" do
459
371
  value = 200
460
- socket.setsockopt ZMQ::BACKLOG, value
372
+ rc = socket.setsockopt ZMQ::BACKLOG, value
373
+ rc.should == 0
461
374
  array = []
462
375
  rc = socket.getsockopt(ZMQ::BACKLOG, array)
463
376
  rc.should == 0
@@ -514,8 +427,8 @@ module ZMQ
514
427
 
515
428
  class PollFD < FFI::Struct
516
429
  layout :fd, :int,
517
- :events, :short,
518
- :revents, :short
430
+ :events, :short,
431
+ :revents, :short
519
432
  end
520
433
  end # module LibSocket
521
434