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
@@ -84,8 +84,8 @@ module ZMQ
84
84
  # puts "value1 is #{message.value1}"
85
85
  #
86
86
  class Message
87
-
88
- # Recommended way to create a standard message. A Message object is
87
+
88
+ # Recommended way to create a standard message. A Message object is
89
89
  # returned upon success, nil when allocation fails.
90
90
  #
91
91
  def self.create message = nil
@@ -129,7 +129,7 @@ module ZMQ
129
129
  data_buffer.write_string bytes, len
130
130
 
131
131
  # use libC to call free on the data buffer; earlier versions used an
132
- # FFI::Function here that called back into Ruby, but Rubinius won't
132
+ # FFI::Function here that called back into Ruby, but Rubinius won't
133
133
  # support that and there are issues with the other runtimes too
134
134
  LibZMQ.zmq_msg_init_data @pointer, data_buffer, len, LibC::Free, nil
135
135
  end
@@ -182,42 +182,36 @@ module ZMQ
182
182
  #
183
183
  def close
184
184
  rc = 0
185
-
185
+
186
186
  if @pointer
187
187
  rc = LibZMQ.zmq_msg_close @pointer
188
188
  @pointer = nil
189
189
  end
190
-
190
+
191
191
  rc
192
192
  end
193
-
193
+
194
194
  # cache the msg size so we don't have to recalculate it when creating
195
195
  # each new instance
196
- @msg_size = LibZMQ::Msg.size
197
-
196
+ @msg_size = LibZMQ::Message.size
197
+
198
198
  def self.msg_size() @msg_size; end
199
199
 
200
200
  end # class Message
201
-
202
- if LibZMQ.version3?
203
- class Message
204
- # Version3 only
205
- #
206
- def get(property)
207
- LibZMQ.zmq_msg_get(@pointer, property)
208
- end
209
-
210
- # Version3 only
211
- #
212
- # Returns true if this message has additional parts coming.
213
- #
214
- def more?
215
- Util.resultcode_ok?(get(MORE))
216
- end
217
-
218
- def set(property, value)
219
- LibZMQ.zmq_msg_set(@pointer, property, value)
220
- end
201
+
202
+ class Message
203
+ def get(property)
204
+ LibZMQ.zmq_msg_get(@pointer, property)
205
+ end
206
+
207
+ # Returns true if this message has additional parts coming.
208
+ #
209
+ def more?
210
+ Util.resultcode_ok?(get(MORE))
211
+ end
212
+
213
+ def set(property, value)
214
+ LibZMQ.zmq_msg_set(@pointer, property, value)
221
215
  end
222
216
  end
223
217
 
@@ -254,7 +248,7 @@ module ZMQ
254
248
  #
255
249
  def copy_in_bytes bytes, len
256
250
  rc = super(bytes, len)
257
-
251
+
258
252
  # make sure we have a way to deallocate this memory if the object goes
259
253
  # out of scope
260
254
  define_finalizer
@@ -296,7 +290,7 @@ module ZMQ
296
290
  # cache the msg size so we don't have to recalculate it when creating
297
291
  # each new instance
298
292
  # need to do this again because ivars are not inheritable
299
- @msg_size = LibZMQ::Msg.size
293
+ @msg_size = LibZMQ::Message.size
300
294
 
301
295
  end # class ManagedMessage
302
296
 
@@ -150,22 +150,11 @@ module ZMQ
150
150
  # milliseconds, so we need to convert that value to
151
151
  # microseconds for the library.
152
152
  #
153
- if LibZMQ.version2?
154
- def adjust timeout
155
- if :blocking == timeout || -1 == timeout
156
- -1
157
- else
158
- (timeout * 1000).to_i
159
- end
160
- end
161
- else
162
- # version3 changed units from microseconds to milliseconds
163
- def adjust timeout
164
- if :blocking == timeout || -1 == timeout
165
- -1
166
- else
167
- timeout.to_i
168
- end
153
+ def adjust timeout
154
+ if :blocking == timeout || -1 == timeout
155
+ -1
156
+ else
157
+ timeout.to_i
169
158
  end
170
159
  end
171
160
 
@@ -1,8 +1,7 @@
1
1
 
2
2
  module ZMQ
3
3
 
4
- module CommonSocketBehavior
5
-
4
+ class Socket
6
5
  attr_reader :socket, :name
7
6
 
8
7
  # Allocates a socket of type +type+ for sending and receiving data.
@@ -70,15 +69,15 @@ module ZMQ
70
69
 
71
70
  context_ptr = context_ptr.pointer if context_ptr.kind_of?(ZMQ::Context)
72
71
 
73
- unless context_ptr.null?
72
+ if context_ptr.nil? || context_ptr.null?
73
+ raise ContextError.new 'zmq_socket', 0, ETERM, "Context pointer was null"
74
+ else
74
75
  @socket = LibZMQ.zmq_socket context_ptr, type
75
76
  if @socket && !@socket.null?
76
77
  @name = SocketTypeNameMap[type]
77
78
  else
78
79
  raise ContextError.new 'zmq_socket', 0, ETERM, "Socket pointer was null"
79
80
  end
80
- else
81
- raise ContextError.new 'zmq_socket', 0, ETERM, "Context pointer was null"
82
81
  end
83
82
 
84
83
  @longlong_cache = @int_cache = nil
@@ -118,7 +117,7 @@ module ZMQ
118
117
  # Returns 0 when the operation completed successfully.
119
118
  # Returns -1 when this operation failed.
120
119
  #
121
- # With a -1 return code, the user must check ZMQ.errno to determine the
120
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
122
121
  # cause.
123
122
  #
124
123
  # rc = socket.setsockopt(ZMQ::LINGER, 1_000)
@@ -193,7 +192,7 @@ module ZMQ
193
192
  # depending upon the value of the socket option ZMQ::LINGER.
194
193
  #
195
194
  # Returns 0 upon success *or* when the socket has already been closed.
196
- # Returns -1 when the operation fails. Check ZMQ.errno for the error code.
195
+ # Returns -1 when the operation fails. Check ZMQ::Util.errno for the error code.
197
196
  #
198
197
  # rc = socket.close
199
198
  # puts("Given socket was invalid!") unless 0 == rc
@@ -215,15 +214,15 @@ module ZMQ
215
214
  #
216
215
  # +flags+ may take two values:
217
216
  # * 0 (default) - blocking operation
218
- # * ZMQ::NonBlocking - non-blocking operation
217
+ # * ZMQ::DONTWAIT - non-blocking operation
219
218
  # * ZMQ::SNDMORE - this message is part of a multi-part message
220
219
  #
221
220
  # Returns 0 when the message was successfully enqueued.
222
221
  # Returns -1 under two conditions.
223
222
  # 1. The message could not be enqueued
224
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
223
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
225
224
  #
226
- # With a -1 return code, the user must check ZMQ.errno to determine the
225
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
227
226
  # cause.
228
227
  #
229
228
  def sendmsg message, flags = 0
@@ -233,14 +232,14 @@ module ZMQ
233
232
  # Helper method to make a new #Message instance out of the +string+ passed
234
233
  # in for transmission.
235
234
  #
236
- # +flags+ may be ZMQ::NonBlocking and ZMQ::SNDMORE.
235
+ # +flags+ may be ZMQ::DONTWAIT and ZMQ::SNDMORE.
237
236
  #
238
237
  # Returns 0 when the message was successfully enqueued.
239
238
  # Returns -1 under two conditions.
240
239
  # 1. The message could not be enqueued
241
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
240
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
242
241
  #
243
- # With a -1 return code, the user must check ZMQ.errno to determine the
242
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
244
243
  # cause.
245
244
  #
246
245
  def send_string string, flags = 0
@@ -252,14 +251,14 @@ module ZMQ
252
251
  # passed in for transmission. Every element of +parts+ should be
253
252
  # a String.
254
253
  #
255
- # +flags+ may be ZMQ::NonBlocking.
254
+ # +flags+ may be ZMQ::DONTWAIT.
256
255
  #
257
256
  # Returns 0 when the messages were successfully enqueued.
258
257
  # Returns -1 under two conditions.
259
258
  # 1. A message could not be enqueued
260
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
259
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
261
260
  #
262
- # With a -1 return code, the user must check ZMQ.errno to determine the
261
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
263
262
  # cause.
264
263
  #
265
264
  def send_strings parts, flags = 0
@@ -270,14 +269,14 @@ module ZMQ
270
269
  # passed in for transmission. Every element of +parts+ should be
271
270
  # a Message (or subclass).
272
271
  #
273
- # +flags+ may be ZMQ::NonBlocking.
272
+ # +flags+ may be ZMQ::DONTWAIT.
274
273
  #
275
274
  # Returns 0 when the messages were successfully enqueued.
276
275
  # Returns -1 under two conditions.
277
276
  # 1. A message could not be enqueued
278
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
277
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
279
278
  #
280
- # With a -1 return code, the user must check ZMQ.errno to determine the
279
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
281
280
  # cause.
282
281
  #
283
282
  def sendmsgs parts, flags = 0
@@ -290,9 +289,9 @@ module ZMQ
290
289
  # Returns 0 when the message was successfully enqueued.
291
290
  # Returns -1 under two conditions.
292
291
  # 1. The message could not be enqueued
293
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
292
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
294
293
  #
295
- # With a -1 return code, the user must check ZMQ.errno to determine the
294
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
296
295
  # cause.
297
296
  #
298
297
  def send_and_close message, flags = 0
@@ -305,14 +304,14 @@ module ZMQ
305
304
  #
306
305
  # +flags+ may take two values:
307
306
  # 0 (default) - blocking operation
308
- # ZMQ::NonBlocking - non-blocking operation
307
+ # ZMQ::DONTWAIT - non-blocking operation
309
308
  #
310
309
  # Returns 0 when the message was successfully dequeued.
311
310
  # Returns -1 under two conditions.
312
311
  # 1. The message could not be dequeued
313
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
312
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
314
313
  #
315
- # With a -1 return code, the user must check ZMQ.errno to determine the
314
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
316
315
  # cause.
317
316
  #
318
317
  # The application code is responsible for handling the +message+ object lifecycle
@@ -326,14 +325,14 @@ module ZMQ
326
325
  # Helper method to make a new #Message instance and convert its payload
327
326
  # to a string.
328
327
  #
329
- # +flags+ may be ZMQ::NonBlocking.
328
+ # +flags+ may be ZMQ::DONTWAIT.
330
329
  #
331
330
  # Returns 0 when the message was successfully dequeued.
332
331
  # Returns -1 under two conditions.
333
332
  # 1. The message could not be dequeued
334
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
333
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
335
334
  #
336
- # With a -1 return code, the user must check ZMQ.errno to determine the
335
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
337
336
  # cause.
338
337
  #
339
338
  # The application code is responsible for handling the +message+ object lifecycle
@@ -349,7 +348,7 @@ module ZMQ
349
348
 
350
349
  # Receive a multipart message as a list of strings.
351
350
  #
352
- # +flag+ may be ZMQ::NonBlocking. Any other flag will be
351
+ # +flag+ may be ZMQ::DONTWAIT. Any other flag will be
353
352
  # removed.
354
353
  #
355
354
  def recv_strings list, flag = 0
@@ -369,11 +368,11 @@ module ZMQ
369
368
  # Receive a multipart message as an array of objects
370
369
  # (by default these are instances of Message).
371
370
  #
372
- # +flag+ may be ZMQ::NonBlocking. Any other flag will be
371
+ # +flag+ may be ZMQ::DONTWAIT. Any other flag will be
373
372
  # removed.
374
373
  #
375
374
  def recvmsgs list, flag = 0
376
- flag = NonBlocking if dontwait?(flag)
375
+ flag = DONTWAIT if dontwait?(flag)
377
376
 
378
377
  message = @receiver_klass.new
379
378
  rc = recvmsg message, flag
@@ -426,24 +425,96 @@ module ZMQ
426
425
  rc
427
426
  end
428
427
 
428
+ # Get the options set on this socket.
429
+ #
430
+ # +name+ determines the socket option to request
431
+ # +array+ should be an empty array; a result of the proper type
432
+ # (numeric, string, boolean) will be inserted into
433
+ # the first position.
434
+ #
435
+ # Valid +option_name+ values:
436
+ # ZMQ::RCVMORE - true or false
437
+ # ZMQ::HWM - integer
438
+ # ZMQ::SWAP - integer
439
+ # ZMQ::AFFINITY - bitmap in an integer
440
+ # ZMQ::IDENTITY - string
441
+ # ZMQ::RATE - integer
442
+ # ZMQ::RECOVERY_IVL - integer
443
+ # ZMQ::SNDBUF - integer
444
+ # ZMQ::RCVBUF - integer
445
+ # ZMQ::FD - fd in an integer
446
+ # ZMQ::EVENTS - bitmap integer
447
+ # ZMQ::LINGER - integer measured in milliseconds
448
+ # ZMQ::RECONNECT_IVL - integer measured in milliseconds
449
+ # ZMQ::BACKLOG - integer
450
+ # ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds
451
+ # ZMQ::IPV4ONLY - integer
452
+ #
453
+ # Returns 0 when the operation completed successfully.
454
+ # Returns -1 when this operation failed.
455
+ #
456
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
457
+ # cause.
458
+ #
459
+ # # retrieve high water mark
460
+ # array = []
461
+ # rc = socket.getsockopt(ZMQ::HWM, array)
462
+ # hwm = array.first if ZMQ::Util.resultcode_ok?(rc)
463
+ #
464
+ def getsockopt name, array
465
+ rc = __getsockopt__ name, array
466
+
467
+ if Util.resultcode_ok?(rc) && (RCVMORE == name)
468
+ # convert to boolean
469
+ array[0] = 1 == array[0]
470
+ end
471
+
472
+ rc
473
+ end
474
+
475
+ # Convenience method for getting the value of the socket IDENTITY.
476
+ #
477
+ def identity
478
+ array = []
479
+ getsockopt IDENTITY, array
480
+ array.at(0)
481
+ end
482
+
483
+ # Convenience method for setting the value of the socket IDENTITY.
484
+ #
485
+ def identity=(value)
486
+ setsockopt IDENTITY, value.to_s
487
+ end
488
+
489
+ # Disconnect the socket from the given +endpoint+.
490
+ #
491
+ def disconnect(endpoint)
492
+ LibZMQ.zmq_disconnect(socket, endpoint)
493
+ end
494
+
495
+ # Unbind the socket from the given +endpoint+.
496
+ #
497
+ def unbind(endpoint)
498
+ LibZMQ.zmq_unbind(socket, endpoint)
499
+ end
500
+
429
501
 
430
502
  private
431
-
503
+
432
504
  def send_multiple(parts, flags, method_name)
433
505
  if !parts || parts.empty?
434
506
  -1
435
507
  else
436
- flags = NonBlocking if dontwait?(flags)
508
+ flags = DONTWAIT if dontwait?(flags)
437
509
  rc = 0
438
-
510
+
439
511
  parts[0..-2].each do |part|
440
512
  rc = send(method_name, part, (flags | ZMQ::SNDMORE))
441
513
  break unless Util.resultcode_ok?(rc)
442
514
  end
443
-
515
+
444
516
  Util.resultcode_ok?(rc) ? send(method_name, parts[-1], flags) : rc
445
517
  end
446
-
447
518
  end
448
519
 
449
520
  def __getsockopt__ name, array
@@ -485,277 +556,57 @@ module ZMQ
485
556
  alloc_pointer(255, 255)
486
557
 
487
558
  else
488
- # uh oh, someone passed in an unknown option; use a slop buffer
559
+ # uh oh, someone passed in an unknown option; return nil
489
560
  @int_cache ||= alloc_pointer(:int32, 4)
490
561
  end
491
562
  end
492
563
 
493
- def populate_option_lookup
494
- # integer options
495
- [EVENTS, LINGER, RCVTIMEO, SNDTIMEO, RECONNECT_IVL, FD, TYPE, BACKLOG].each { |option| @option_lookup[option] = 0 }
496
-
497
- # long long options
498
- [RCVMORE, AFFINITY].each { |option| @option_lookup[option] = 1 }
499
-
500
- # string options
501
- [SUBSCRIBE, UNSUBSCRIBE].each { |option| @option_lookup[option] = 2 }
502
- end
503
-
504
564
  def release_cache
505
565
  @longlong_cache = nil
506
566
  @int_cache = nil
507
567
  end
508
568
 
509
569
  def dontwait?(flags)
510
- (NonBlocking & flags) == NonBlocking
570
+ (DONTWAIT & flags) == DONTWAIT
511
571
  end
512
572
  alias :noblock? :dontwait?
513
-
573
+
514
574
  def alloc_pointer(kind, length)
515
575
  pointer = FFI::MemoryPointer.new :size_t
516
576
  pointer.write_int(length)
517
577
  [FFI::MemoryPointer.new(kind), pointer]
518
578
  end
519
- end # module CommonSocketBehavior
520
579
 
521
-
522
- module IdentitySupport
523
-
524
- # Convenience method for getting the value of the socket IDENTITY.
525
- #
526
- def identity
527
- array = []
528
- getsockopt IDENTITY, array
529
- array.at(0)
580
+ def __sendmsg__(socket, address, flags)
581
+ LibZMQ.zmq_sendmsg(socket, address, flags)
530
582
  end
531
583
 
532
- # Convenience method for setting the value of the socket IDENTITY.
533
- #
534
- def identity=(value)
535
- setsockopt IDENTITY, value.to_s
584
+ def __recvmsg__(socket, address, flags)
585
+ LibZMQ.zmq_recvmsg(socket, address, flags)
536
586
  end
537
587
 
538
-
539
- private
540
-
541
588
  def populate_option_lookup
542
- super()
543
-
544
- # string options
545
- [IDENTITY].each { |option| @option_lookup[option] = 2 }
546
- end
547
-
548
- end # module IdentitySupport
549
-
550
-
551
- if LibZMQ.version2?
552
-
553
- class Socket
554
- # Inclusion order is *important* since later modules may have a call
555
- # to #super. We want those calls to go up the chain in a particular
556
- # order
557
- include CommonSocketBehavior
558
- include IdentitySupport
559
-
560
- # Get the options set on this socket.
561
- #
562
- # +name+ determines the socket option to request
563
- # +array+ should be an empty array; a result of the proper type
564
- # (numeric, string, boolean) will be inserted into
565
- # the first position.
566
- #
567
- # Valid +option_name+ values:
568
- # ZMQ::RCVMORE - true or false
569
- # ZMQ::HWM - integer
570
- # ZMQ::SWAP - integer
571
- # ZMQ::AFFINITY - bitmap in an integer
572
- # ZMQ::IDENTITY - string
573
- # ZMQ::RATE - integer
574
- # ZMQ::RECOVERY_IVL - integer
575
- # ZMQ::MCAST_LOOP - true or false
576
- # ZMQ::SNDBUF - integer
577
- # ZMQ::RCVBUF - integer
578
- # ZMQ::FD - fd in an integer
579
- # ZMQ::EVENTS - bitmap integer
580
- # ZMQ::LINGER - integer measured in milliseconds
581
- # ZMQ::RECONNECT_IVL - integer measured in milliseconds
582
- # ZMQ::BACKLOG - integer
583
- # ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds
584
- #
585
- # Returns 0 when the operation completed successfully.
586
- # Returns -1 when this operation failed.
587
- #
588
- # With a -1 return code, the user must check ZMQ.errno to determine the
589
- # cause.
590
- #
591
- # # retrieve high water mark
592
- # array = []
593
- # rc = socket.getsockopt(ZMQ::HWM, array)
594
- # hwm = array.first if ZMQ::Util.resultcode_ok?(rc)
595
- #
596
- def getsockopt name, array
597
- rc = __getsockopt__ name, array
598
-
599
- if Util.resultcode_ok?(rc) && (RCVMORE == name || MCAST_LOOP == name)
600
- # convert to boolean
601
- array[0] = 1 == array[0]
602
- end
603
-
604
- rc
605
- end
606
-
589
+ IntegerSocketOptions.each { |option| @option_lookup[option] = 0 }
607
590
 
608
- private
609
-
610
- def __sendmsg__(socket, address, flags)
611
- LibZMQ.zmq_send(socket, address, flags)
612
- end
613
-
614
- def __recvmsg__(socket, address, flags)
615
- LibZMQ.zmq_recv(socket, address, flags)
616
- end
617
-
618
- def populate_option_lookup
619
- super()
620
-
621
- # integer options
622
- [RECONNECT_IVL_MAX].each { |option| @option_lookup[option] = 0 }
623
-
624
- # long long options
625
- [HWM, SWAP, RATE, RECOVERY_IVL, RECOVERY_IVL_MSEC, MCAST_LOOP, SNDBUF, RCVBUF].each { |option| @option_lookup[option] = 1 }
626
- end
627
-
628
- # these finalizer-related methods cannot live in the CommonSocketBehavior
629
- # module; they *must* be in the class definition directly
630
-
631
- def define_finalizer
632
- ObjectSpace.define_finalizer(self, self.class.close(@socket, Process.pid))
633
- end
634
-
635
- def remove_finalizer
636
- ObjectSpace.undefine_finalizer self
637
- end
591
+ LongLongSocketOptions.each { |option| @option_lookup[option] = 1 }
638
592
 
639
- def self.close socket, pid
640
- Proc.new do
641
- LibZMQ.zmq_close(socket) if socket && !socket.nil? && Process.pid == pid
642
- end
643
- end
644
- end # class Socket for version2
645
-
646
- end # LibZMQ.version2?
647
-
648
-
649
- if LibZMQ.version3?
650
- class Socket
651
- include CommonSocketBehavior
652
- include IdentitySupport
653
-
654
- # Get the options set on this socket.
655
- #
656
- # +name+ determines the socket option to request
657
- # +array+ should be an empty array; a result of the proper type
658
- # (numeric, string, boolean) will be inserted into
659
- # the first position.
660
- #
661
- # Valid +option_name+ values:
662
- # ZMQ::RCVMORE - true or false
663
- # ZMQ::HWM - integer
664
- # ZMQ::SWAP - integer
665
- # ZMQ::AFFINITY - bitmap in an integer
666
- # ZMQ::IDENTITY - string
667
- # ZMQ::RATE - integer
668
- # ZMQ::RECOVERY_IVL - integer
669
- # ZMQ::SNDBUF - integer
670
- # ZMQ::RCVBUF - integer
671
- # ZMQ::FD - fd in an integer
672
- # ZMQ::EVENTS - bitmap integer
673
- # ZMQ::LINGER - integer measured in milliseconds
674
- # ZMQ::RECONNECT_IVL - integer measured in milliseconds
675
- # ZMQ::BACKLOG - integer
676
- # ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds
677
- # ZMQ::IPV4ONLY - integer
678
- #
679
- # Returns 0 when the operation completed successfully.
680
- # Returns -1 when this operation failed.
681
- #
682
- # With a -1 return code, the user must check ZMQ.errno to determine the
683
- # cause.
684
- #
685
- # # retrieve high water mark
686
- # array = []
687
- # rc = socket.getsockopt(ZMQ::HWM, array)
688
- # hwm = array.first if ZMQ::Util.resultcode_ok?(rc)
689
- #
690
- def getsockopt name, array
691
- rc = __getsockopt__ name, array
692
-
693
- if Util.resultcode_ok?(rc) && (RCVMORE == name)
694
- # convert to boolean
695
- array[0] = 1 == array[0]
696
- end
697
-
698
- rc
699
- end
700
-
701
- # Version3 only
702
- #
703
- # Disconnect the socket from the given +endpoint+.
704
- #
705
- def disconnect(endpoint)
706
- LibZMQ.zmq_disconnect(socket, endpoint)
707
- end
708
-
709
- # Version3 only
710
- #
711
- # Unbind the socket from the given +endpoint+.
712
- #
713
- def unbind(endpoint)
714
- LibZMQ.zmq_unbind(socket, endpoint)
715
- end
716
-
717
-
718
- private
719
-
720
- def __sendmsg__(socket, address, flags)
721
- LibZMQ.zmq_sendmsg(socket, address, flags)
722
- end
723
-
724
- def __recvmsg__(socket, address, flags)
725
- LibZMQ.zmq_recvmsg(socket, address, flags)
726
- end
727
-
728
- def populate_option_lookup
729
- super()
730
-
731
- # integer options
732
- [RECONNECT_IVL_MAX, RCVHWM, SNDHWM, RATE, RECOVERY_IVL, SNDBUF, RCVBUF, IPV4ONLY,
733
- ROUTER_BEHAVIOR, TCP_KEEPALIVE, TCP_KEEPALIVE_CNT,
734
- TCP_KEEPALIVE_IDLE, TCP_KEEPALIVE_INTVL, TCP_ACCEPT_FILTER, MULTICAST_HOPS
735
- ].each { |option| @option_lookup[option] = 0 }
736
-
737
- # long long options
738
- [MAXMSGSIZE].each { |option| @option_lookup[option] = 1 }
739
-
740
- # string options
741
- [LAST_ENDPOINT].each { |option| @option_lookup[option] = 2 }
742
- end
593
+ StringSocketOptions.each { |option| @option_lookup[option] = 2 }
594
+ end
743
595
 
744
- # these finalizer-related methods cannot live in the CommonSocketBehavior
745
- # module; they *must* be in the class definition directly
596
+ # these finalizer-related methods cannot live in the CommonSocketBehavior
597
+ # module; they *must* be in the class definition directly
746
598
 
747
- def define_finalizer
748
- ObjectSpace.define_finalizer(self, self.class.close(@socket, Process.pid))
749
- end
599
+ def define_finalizer
600
+ ObjectSpace.define_finalizer(self, self.class.close(@socket, Process.pid))
601
+ end
750
602
 
751
- def remove_finalizer
752
- ObjectSpace.undefine_finalizer self
753
- end
603
+ def remove_finalizer
604
+ ObjectSpace.undefine_finalizer self
605
+ end
754
606
 
755
- def self.close socket, pid
756
- Proc.new { LibZMQ.zmq_close socket if Process.pid == pid }
757
- end
758
- end # Socket for version3
759
- end # LibZMQ.version3?
607
+ def self.close socket, pid
608
+ Proc.new { LibZMQ.zmq_close socket if Process.pid == pid }
609
+ end
610
+ end # Socket
760
611
 
761
612
  end # module ZMQ