diff --git a/lib/ffi-rzmq/constants.rb b/lib/ffi-rzmq/constants.rb index e73ba4a..d101d96 100644 --- a/lib/ffi-rzmq/constants.rb +++ b/lib/ffi-rzmq/constants.rb @@ -133,6 +133,7 @@ module ZMQ MULTICAST_HOPS = 25 RCVTIMEO = 27 SNDTIMEO = 28 + IPV4ONLY = 31 # Send/recv options DONTWAIT = 1 diff --git a/lib/ffi-rzmq/context.rb b/lib/ffi-rzmq/context.rb index 868978d..459f1ca 100644 --- a/lib/ffi-rzmq/context.rb +++ b/lib/ffi-rzmq/context.rb @@ -41,7 +41,6 @@ module ZMQ # # class Context - include ZMQ::Util attr_reader :context, :pointer @@ -55,7 +54,7 @@ def initialize io_threads = 1 @sockets = [] @context = LibZMQ.zmq_init io_threads @pointer = @context - error_check 'zmq_init', (@context.nil? || @context.null?) ? -1 : 0 + ZMQ::Util.error_check 'zmq_init', (@context.nil? || @context.null?) ? -1 : 0 define_finalizer end diff --git a/lib/ffi-rzmq/message.rb b/lib/ffi-rzmq/message.rb index b349a0a..3f1f465 100644 --- a/lib/ffi-rzmq/message.rb +++ b/lib/ffi-rzmq/message.rb @@ -84,7 +84,6 @@ module ZMQ # puts "value1 is #{message.value1}" # class Message - include ZMQ::Util # Recommended way to create a standard message. A Message object is # returned upon success, nil when allocation fails. diff --git a/lib/ffi-rzmq/poll.rb b/lib/ffi-rzmq/poll.rb index 5663dda..d8198d0 100644 --- a/lib/ffi-rzmq/poll.rb +++ b/lib/ffi-rzmq/poll.rb @@ -2,7 +2,6 @@ module ZMQ class Poller - include ZMQ::Util attr_reader :readables, :writables diff --git a/lib/ffi-rzmq/socket.rb b/lib/ffi-rzmq/socket.rb index 0783b6d..f4b857e 100644 --- a/lib/ffi-rzmq/socket.rb +++ b/lib/ffi-rzmq/socket.rb @@ -2,7 +2,6 @@ module ZMQ module CommonSocketBehavior - include ZMQ::Util attr_reader :socket, :name @@ -686,6 +685,7 @@ class Socket # ZMQ::RECONNECT_IVL - integer measured in milliseconds # ZMQ::BACKLOG - integer # ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds + # ZMQ::IPV4ONLY - integer # # Returns 0 when the operation completed successfully. # Returns -1 when this operation failed. @@ -720,22 +720,11 @@ def __recvmsg__(socket, address, flags) LibZMQ.zmq_recvmsg(socket, address, flags) end - def int_option? name - super(name) || - RECONNECT_IVL_MAX == name || - RCVHWM == name || - SNDHWM == name || - RATE == name || - RECOVERY_IVL == name || - SNDBUF == name || - RCVBUF == name - end - def populate_option_lookup super() # integer options - [RECONNECT_IVL_MAX, RCVHWM, SNDHWM, RATE, RECOVERY_IVL, SNDBUF, RCVBUF].each { |option| @option_lookup[option] = 0 } + [RECONNECT_IVL_MAX, RCVHWM, SNDHWM, RATE, RECOVERY_IVL, SNDBUF, RCVBUF, IPV4ONLY].each { |option| @option_lookup[option] = 0 } end # these finalizer-related methods cannot live in the CommonSocketBehavior diff --git a/lib/ffi-rzmq/util.rb b/lib/ffi-rzmq/util.rb index 23beb58..969a21b 100644 --- a/lib/ffi-rzmq/util.rb +++ b/lib/ffi-rzmq/util.rb @@ -1,10 +1,9 @@ module ZMQ - # These methods don't belong to any specific class. They get included - # in the #Context, #Socket and #Poller classes. + # General utility methods. # - module Util + class Util # Returns true when +rc+ is greater than or equal to 0, false otherwise. # @@ -57,23 +56,15 @@ def self.bind_to_random_tcp_port host = '127.0.0.1', max_tries = 500 resultcode_ok?(rc) ? random : nil end - - - private - - # generate a random port between 10_000 and 65534 - def self.random_port - rand(55534) + 10_000 - end - + # :doc: - # Called by most library methods to verify there were no errors during + # Called to verify whether there were any errors during # operation. If any are found, raise the appropriate #ZeroMQError. # # When no error is found, this method returns +true+ which is behavior # used internally by #send and #recv. # - def error_check source, result_code + def self.error_check source, result_code if -1 == result_code raise_error source, result_code end @@ -82,7 +73,15 @@ def error_check source, result_code true end - def raise_error source, result_code + + private + + # generate a random port between 10_000 and 65534 + def self.random_port + rand(55534) + 10_000 + end + + def self.raise_error source, result_code if 'zmq_init' == source || 'zmq_socket' == source raise ContextError.new source, result_code, ZMQ::Util.errno, ZMQ::Util.error_string @@ -96,7 +95,7 @@ def raise_error source, result_code end end - def eagain? + def self.eagain? EAGAIN == ZMQ::Util.errno end diff --git a/spec/socket_spec.rb b/spec/socket_spec.rb index e849698..6f17090 100644 --- a/spec/socket_spec.rb +++ b/spec/socket_spec.rb @@ -418,6 +418,37 @@ module ZMQ array[0].should == value end end # context using option ZMQ::BACKLOG + + context "using option ZMQ::IPV4ONLY" do + it "should enable use of IPV6 sockets when set to 0" do + value = 0 + socket.setsockopt ZMQ::IPV4ONLY, value + array = [] + rc = socket.getsockopt(ZMQ::IPV4ONLY, array) + rc.should == 0 + array[0].should == value + end + + it "should default to a value of 1" do + value = 1 + array = [] + rc = socket.getsockopt(ZMQ::IPV4ONLY, array) + rc.should == 0 + array[0].should == value + end + + it "returns -1 given a negative value" do + value = -1 + rc = socket.setsockopt ZMQ::IPV4ONLY, value + rc.should == -1 + end + + it "returns -1 given a value > 1" do + value = 2 + rc = socket.setsockopt ZMQ::IPV4ONLY, value + rc.should == -1 + end + end # context using option ZMQ::IPV4ONLY end # context #setsockopt