ffi-rzmq 1.0.3 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +5 -6
  3. data/AUTHORS.txt +7 -1
  4. data/History.txt +79 -0
  5. data/README.rdoc +67 -29
  6. data/Rakefile +15 -0
  7. data/examples/README.rdoc +1 -3
  8. data/examples/{v3api/latency_measurement.rb → latency_measurement.rb} +1 -2
  9. data/examples/{v3api/local_lat.rb → local_lat.rb} +1 -1
  10. data/examples/{v3api/local_lat_poll.rb → local_lat_poll.rb} +4 -4
  11. data/examples/{v3api/local_throughput.rb → local_throughput.rb} +1 -1
  12. data/examples/{v3api/pub.rb → pub.rb} +1 -1
  13. data/examples/{v2api/publish_subscribe.rb → publish_subscribe.rb} +2 -2
  14. data/examples/{v2api/remote_lat.rb → remote_lat.rb} +1 -1
  15. data/examples/{v3api/remote_throughput.rb → remote_throughput.rb} +4 -3
  16. data/examples/repreq_over_curve.rb +60 -0
  17. data/examples/{v2api/reqrep_poll.rb → reqrep_poll.rb} +3 -4
  18. data/examples/{v2api/request_response.rb → request_response.rb} +1 -2
  19. data/examples/{v3api/sub.rb → sub.rb} +1 -2
  20. data/examples/{v3api/throughput_measurement.rb → throughput_measurement.rb} +1 -1
  21. data/examples/{v3api/xreqxrep_poll.rb → xreqxrep_poll.rb} +6 -7
  22. data/ffi-rzmq.gemspec +4 -4
  23. data/lib/ffi-rzmq/context.rb +26 -54
  24. data/lib/ffi-rzmq/device.rb +8 -4
  25. data/lib/ffi-rzmq/exceptions.rb +3 -0
  26. data/lib/ffi-rzmq/message.rb +24 -30
  27. data/lib/ffi-rzmq/poll.rb +5 -16
  28. data/lib/ffi-rzmq/poll_item.rb +0 -1
  29. data/lib/ffi-rzmq/socket.rb +132 -282
  30. data/lib/ffi-rzmq/util.rb +28 -36
  31. data/lib/ffi-rzmq/version.rb +1 -1
  32. data/lib/ffi-rzmq.rb +3 -3
  33. data/lib/io_extensions.rb +1 -1
  34. data/spec/context_spec.rb +18 -23
  35. data/spec/device_spec.rb +13 -12
  36. data/spec/message_spec.rb +13 -13
  37. data/spec/multipart_spec.rb +11 -11
  38. data/spec/nonblocking_recv_spec.rb +22 -22
  39. data/spec/poll_spec.rb +49 -49
  40. data/spec/pushpull_spec.rb +12 -11
  41. data/spec/reqrep_spec.rb +11 -11
  42. data/spec/socket_spec.rb +110 -197
  43. data/spec/spec_helper.rb +3 -11
  44. data/spec/support/generate_keys_and_certs.rb +41 -0
  45. data/spec/support/test.crt +26 -13
  46. data/spec/support/test.key +49 -13
  47. data/spec/util_spec.rb +29 -0
  48. data/travis_build_script.sh +27 -0
  49. metadata +83 -104
  50. data/examples/v2api/latency_measurement.rb +0 -139
  51. data/examples/v2api/local_lat.rb +0 -58
  52. data/examples/v2api/local_lat_poll.rb +0 -66
  53. data/examples/v2api/local_throughput.rb +0 -58
  54. data/examples/v2api/pub.rb +0 -46
  55. data/examples/v2api/remote_throughput.rb +0 -39
  56. data/examples/v2api/sub.rb +0 -74
  57. data/examples/v2api/throughput_measurement.rb +0 -138
  58. data/examples/v2api/xreqxrep_poll.rb +0 -93
  59. data/examples/v3api/publish_subscribe.rb +0 -82
  60. data/examples/v3api/remote_lat.rb +0 -71
  61. data/examples/v3api/reqrep_poll.rb +0 -62
  62. data/examples/v3api/request_response.rb +0 -40
  63. data/lib/ffi-rzmq/constants.rb +0 -187
  64. data/lib/ffi-rzmq/libc.rb +0 -19
  65. data/lib/ffi-rzmq/libzmq.rb +0 -283
@@ -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)
@@ -136,15 +135,14 @@ module ZMQ
136
135
  pointer.write_int value
137
136
 
138
137
  elsif 2 == @option_lookup[name]
138
+ # Strings are treated as pointers by FFI so we'll just pass it through
139
139
  length ||= value.size
140
+ pointer = value
140
141
 
141
- # note: not checking errno for failed memory allocations :(
142
- pointer = LibC.malloc length
143
- pointer.write_string value
144
142
  end
145
143
 
146
144
  rc = LibZMQ.zmq_setsockopt @socket, name, pointer, length
147
- LibC.free(pointer) unless pointer.nil? || pointer.null?
145
+ LibC.free(pointer) unless pointer.is_a?(String) || pointer.nil? || pointer.null?
148
146
  rc
149
147
  end
150
148
 
@@ -193,7 +191,7 @@ module ZMQ
193
191
  # depending upon the value of the socket option ZMQ::LINGER.
194
192
  #
195
193
  # 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.
194
+ # Returns -1 when the operation fails. Check ZMQ::Util.errno for the error code.
197
195
  #
198
196
  # rc = socket.close
199
197
  # puts("Given socket was invalid!") unless 0 == rc
@@ -215,15 +213,15 @@ module ZMQ
215
213
  #
216
214
  # +flags+ may take two values:
217
215
  # * 0 (default) - blocking operation
218
- # * ZMQ::NonBlocking - non-blocking operation
216
+ # * ZMQ::DONTWAIT - non-blocking operation
219
217
  # * ZMQ::SNDMORE - this message is part of a multi-part message
220
218
  #
221
219
  # Returns 0 when the message was successfully enqueued.
222
220
  # Returns -1 under two conditions.
223
221
  # 1. The message could not be enqueued
224
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
222
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
225
223
  #
226
- # With a -1 return code, the user must check ZMQ.errno to determine the
224
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
227
225
  # cause.
228
226
  #
229
227
  def sendmsg message, flags = 0
@@ -233,14 +231,14 @@ module ZMQ
233
231
  # Helper method to make a new #Message instance out of the +string+ passed
234
232
  # in for transmission.
235
233
  #
236
- # +flags+ may be ZMQ::NonBlocking and ZMQ::SNDMORE.
234
+ # +flags+ may be ZMQ::DONTWAIT and ZMQ::SNDMORE.
237
235
  #
238
236
  # Returns 0 when the message was successfully enqueued.
239
237
  # Returns -1 under two conditions.
240
238
  # 1. The message could not be enqueued
241
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
239
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
242
240
  #
243
- # With a -1 return code, the user must check ZMQ.errno to determine the
241
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
244
242
  # cause.
245
243
  #
246
244
  def send_string string, flags = 0
@@ -252,14 +250,14 @@ module ZMQ
252
250
  # passed in for transmission. Every element of +parts+ should be
253
251
  # a String.
254
252
  #
255
- # +flags+ may be ZMQ::NonBlocking.
253
+ # +flags+ may be ZMQ::DONTWAIT.
256
254
  #
257
255
  # Returns 0 when the messages were successfully enqueued.
258
256
  # Returns -1 under two conditions.
259
257
  # 1. A message could not be enqueued
260
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
258
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
261
259
  #
262
- # With a -1 return code, the user must check ZMQ.errno to determine the
260
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
263
261
  # cause.
264
262
  #
265
263
  def send_strings parts, flags = 0
@@ -270,14 +268,14 @@ module ZMQ
270
268
  # passed in for transmission. Every element of +parts+ should be
271
269
  # a Message (or subclass).
272
270
  #
273
- # +flags+ may be ZMQ::NonBlocking.
271
+ # +flags+ may be ZMQ::DONTWAIT.
274
272
  #
275
273
  # Returns 0 when the messages were successfully enqueued.
276
274
  # Returns -1 under two conditions.
277
275
  # 1. A message could not be enqueued
278
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
276
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
279
277
  #
280
- # With a -1 return code, the user must check ZMQ.errno to determine the
278
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
281
279
  # cause.
282
280
  #
283
281
  def sendmsgs parts, flags = 0
@@ -290,9 +288,9 @@ module ZMQ
290
288
  # Returns 0 when the message was successfully enqueued.
291
289
  # Returns -1 under two conditions.
292
290
  # 1. The message could not be enqueued
293
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
291
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
294
292
  #
295
- # With a -1 return code, the user must check ZMQ.errno to determine the
293
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
296
294
  # cause.
297
295
  #
298
296
  def send_and_close message, flags = 0
@@ -305,14 +303,14 @@ module ZMQ
305
303
  #
306
304
  # +flags+ may take two values:
307
305
  # 0 (default) - blocking operation
308
- # ZMQ::NonBlocking - non-blocking operation
306
+ # ZMQ::DONTWAIT - non-blocking operation
309
307
  #
310
308
  # Returns 0 when the message was successfully dequeued.
311
309
  # Returns -1 under two conditions.
312
310
  # 1. The message could not be dequeued
313
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
311
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
314
312
  #
315
- # With a -1 return code, the user must check ZMQ.errno to determine the
313
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
316
314
  # cause.
317
315
  #
318
316
  # The application code is responsible for handling the +message+ object lifecycle
@@ -326,14 +324,14 @@ module ZMQ
326
324
  # Helper method to make a new #Message instance and convert its payload
327
325
  # to a string.
328
326
  #
329
- # +flags+ may be ZMQ::NonBlocking.
327
+ # +flags+ may be ZMQ::DONTWAIT.
330
328
  #
331
329
  # Returns 0 when the message was successfully dequeued.
332
330
  # Returns -1 under two conditions.
333
331
  # 1. The message could not be dequeued
334
- # 2. When +flags+ is set with ZMQ::NonBlocking and the socket returned EAGAIN.
332
+ # 2. When +flags+ is set with ZMQ::DONTWAIT and the socket returned EAGAIN.
335
333
  #
336
- # With a -1 return code, the user must check ZMQ.errno to determine the
334
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
337
335
  # cause.
338
336
  #
339
337
  # The application code is responsible for handling the +message+ object lifecycle
@@ -349,7 +347,7 @@ module ZMQ
349
347
 
350
348
  # Receive a multipart message as a list of strings.
351
349
  #
352
- # +flag+ may be ZMQ::NonBlocking. Any other flag will be
350
+ # +flag+ may be ZMQ::DONTWAIT. Any other flag will be
353
351
  # removed.
354
352
  #
355
353
  def recv_strings list, flag = 0
@@ -369,11 +367,11 @@ module ZMQ
369
367
  # Receive a multipart message as an array of objects
370
368
  # (by default these are instances of Message).
371
369
  #
372
- # +flag+ may be ZMQ::NonBlocking. Any other flag will be
370
+ # +flag+ may be ZMQ::DONTWAIT. Any other flag will be
373
371
  # removed.
374
372
  #
375
373
  def recvmsgs list, flag = 0
376
- flag = NonBlocking if dontwait?(flag)
374
+ flag = DONTWAIT if dontwait?(flag)
377
375
 
378
376
  message = @receiver_klass.new
379
377
  rc = recvmsg message, flag
@@ -426,24 +424,96 @@ module ZMQ
426
424
  rc
427
425
  end
428
426
 
427
+ # Get the options set on this socket.
428
+ #
429
+ # +name+ determines the socket option to request
430
+ # +array+ should be an empty array; a result of the proper type
431
+ # (numeric, string, boolean) will be inserted into
432
+ # the first position.
433
+ #
434
+ # Valid +option_name+ values:
435
+ # ZMQ::RCVMORE - true or false
436
+ # ZMQ::HWM - integer
437
+ # ZMQ::SWAP - integer
438
+ # ZMQ::AFFINITY - bitmap in an integer
439
+ # ZMQ::IDENTITY - string
440
+ # ZMQ::RATE - integer
441
+ # ZMQ::RECOVERY_IVL - integer
442
+ # ZMQ::SNDBUF - integer
443
+ # ZMQ::RCVBUF - integer
444
+ # ZMQ::FD - fd in an integer
445
+ # ZMQ::EVENTS - bitmap integer
446
+ # ZMQ::LINGER - integer measured in milliseconds
447
+ # ZMQ::RECONNECT_IVL - integer measured in milliseconds
448
+ # ZMQ::BACKLOG - integer
449
+ # ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds
450
+ # ZMQ::IPV4ONLY - integer
451
+ #
452
+ # Returns 0 when the operation completed successfully.
453
+ # Returns -1 when this operation failed.
454
+ #
455
+ # With a -1 return code, the user must check ZMQ::Util.errno to determine the
456
+ # cause.
457
+ #
458
+ # # retrieve high water mark
459
+ # array = []
460
+ # rc = socket.getsockopt(ZMQ::HWM, array)
461
+ # hwm = array.first if ZMQ::Util.resultcode_ok?(rc)
462
+ #
463
+ def getsockopt name, array
464
+ rc = __getsockopt__ name, array
465
+
466
+ if Util.resultcode_ok?(rc) && (RCVMORE == name)
467
+ # convert to boolean
468
+ array[0] = 1 == array[0]
469
+ end
470
+
471
+ rc
472
+ end
473
+
474
+ # Convenience method for getting the value of the socket IDENTITY.
475
+ #
476
+ def identity
477
+ array = []
478
+ getsockopt IDENTITY, array
479
+ array.at(0)
480
+ end
481
+
482
+ # Convenience method for setting the value of the socket IDENTITY.
483
+ #
484
+ def identity=(value)
485
+ setsockopt IDENTITY, value.to_s
486
+ end
487
+
488
+ # Disconnect the socket from the given +endpoint+.
489
+ #
490
+ def disconnect(endpoint)
491
+ LibZMQ.zmq_disconnect(socket, endpoint)
492
+ end
493
+
494
+ # Unbind the socket from the given +endpoint+.
495
+ #
496
+ def unbind(endpoint)
497
+ LibZMQ.zmq_unbind(socket, endpoint)
498
+ end
499
+
429
500
 
430
501
  private
431
-
502
+
432
503
  def send_multiple(parts, flags, method_name)
433
504
  if !parts || parts.empty?
434
505
  -1
435
506
  else
436
- flags = NonBlocking if dontwait?(flags)
507
+ flags = DONTWAIT if dontwait?(flags)
437
508
  rc = 0
438
-
509
+
439
510
  parts[0..-2].each do |part|
440
511
  rc = send(method_name, part, (flags | ZMQ::SNDMORE))
441
512
  break unless Util.resultcode_ok?(rc)
442
513
  end
443
-
514
+
444
515
  Util.resultcode_ok?(rc) ? send(method_name, parts[-1], flags) : rc
445
516
  end
446
-
447
517
  end
448
518
 
449
519
  def __getsockopt__ name, array
@@ -485,277 +555,57 @@ module ZMQ
485
555
  alloc_pointer(255, 255)
486
556
 
487
557
  else
488
- # uh oh, someone passed in an unknown option; use a slop buffer
558
+ # uh oh, someone passed in an unknown option; return nil
489
559
  @int_cache ||= alloc_pointer(:int32, 4)
490
560
  end
491
561
  end
492
562
 
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
563
  def release_cache
505
564
  @longlong_cache = nil
506
565
  @int_cache = nil
507
566
  end
508
567
 
509
568
  def dontwait?(flags)
510
- (NonBlocking & flags) == NonBlocking
569
+ (DONTWAIT & flags) == DONTWAIT
511
570
  end
512
571
  alias :noblock? :dontwait?
513
-
572
+
514
573
  def alloc_pointer(kind, length)
515
574
  pointer = FFI::MemoryPointer.new :size_t
516
575
  pointer.write_int(length)
517
576
  [FFI::MemoryPointer.new(kind), pointer]
518
577
  end
519
- end # module CommonSocketBehavior
520
578
 
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)
579
+ def __sendmsg__(socket, address, flags)
580
+ LibZMQ.zmq_sendmsg(socket, address, flags)
530
581
  end
531
582
 
532
- # Convenience method for setting the value of the socket IDENTITY.
533
- #
534
- def identity=(value)
535
- setsockopt IDENTITY, value.to_s
583
+ def __recvmsg__(socket, address, flags)
584
+ LibZMQ.zmq_recvmsg(socket, address, flags)
536
585
  end
537
586
 
538
-
539
- private
540
-
541
587
  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
-
588
+ IntegerSocketOptions.each { |option| @option_lookup[option] = 0 }
607
589
 
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
590
+ LongLongSocketOptions.each { |option| @option_lookup[option] = 1 }
638
591
 
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
592
+ StringSocketOptions.each { |option| @option_lookup[option] = 2 }
593
+ end
743
594
 
744
- # these finalizer-related methods cannot live in the CommonSocketBehavior
745
- # module; they *must* be in the class definition directly
595
+ # these finalizer-related methods cannot live in the CommonSocketBehavior
596
+ # module; they *must* be in the class definition directly
746
597
 
747
- def define_finalizer
748
- ObjectSpace.define_finalizer(self, self.class.close(@socket, Process.pid))
749
- end
598
+ def define_finalizer
599
+ ObjectSpace.define_finalizer(self, self.class.close(@socket, Process.pid))
600
+ end
750
601
 
751
- def remove_finalizer
752
- ObjectSpace.undefine_finalizer self
753
- end
602
+ def remove_finalizer
603
+ ObjectSpace.undefine_finalizer self
604
+ end
754
605
 
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?
606
+ def self.close socket, pid
607
+ Proc.new { LibZMQ.zmq_close socket if Process.pid == pid }
608
+ end
609
+ end # Socket
760
610
 
761
611
  end # module ZMQ