Module: ZMQ::Util

Included in:
CommonSocketBehavior, Context, Message, Poller
Defined in:
lib/ffi-rzmq/util.rb

Overview

These methods don’t belong to any specific class. They get included in the #Context, #Socket and #Poller classes.

Class Method Summary collapse

Class Method Details

.bind_to_random_tcp_port(host = '127.0.0.1', max_tries = 500) ⇒ Object

Attempts to bind to a random tcp port on host up to max_tries times. Returns the port number upon success or nil upon failure.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ffi-rzmq/util.rb', line 48

def self.bind_to_random_tcp_port host = '127.0.0.1', max_tries = 500
  tries = 0
  rc = -1

  while !resultcode_ok?(rc) && tries < max_tries
    tries += 1
    random = random_port
    rc = socket.bind "tcp://#{host}:#{random}"
  end

  resultcode_ok?(rc) ? random : nil
end

.errnoObject

Returns the errno as set by the libzmq library.



21
22
23
# File 'lib/ffi-rzmq/util.rb', line 21

def self.errno
  LibZMQ.zmq_errno
end

.error_stringObject

Returns a string corresponding to the currently set #errno. These error strings are defined by libzmq.



28
29
30
# File 'lib/ffi-rzmq/util.rb', line 28

def self.error_string
  LibZMQ.zmq_strerror(errno).read_string
end

.nonblocking_flagObject



66
67
68
# File 'lib/ffi-rzmq/util.rb', line 66

def self.nonblocking_flag
  NOBLOCK
end

.resultcode_ok?(rc) ⇒ Boolean

Returns true when rc is greater than or equal to 0, false otherwise.

We use the >= test because zmq_poll() returns the number of sockets that had a read or write event triggered. So, a >= 0 result means it succeeded.

Returns:

  • (Boolean)


15
16
17
# File 'lib/ffi-rzmq/util.rb', line 15

def self.resultcode_ok? rc
  rc >= 0
end

.versionObject

Returns an array of the form [major, minor, patch] to represent the version of libzmq.

Class method! Invoke as: ZMQ::Util.version



37
38
39
40
41
42
43
# File 'lib/ffi-rzmq/util.rb', line 37

def self.version
  major = FFI::MemoryPointer.new :int
  minor = FFI::MemoryPointer.new :int
  patch = FFI::MemoryPointer.new :int
  LibZMQ.zmq_version major, minor, patch
  [major.read_int, minor.read_int, patch.read_int]
end