Class: ZMQ::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-rzmq/util.rb

Overview

General utility methods.

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.



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

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.



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

def self.errno
  LibZMQ.zmq_errno
end

.error_check(source, result_code) ⇒ Object

:doc: 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.



67
68
69
70
71
72
73
74
# File 'lib/ffi-rzmq/util.rb', line 67

def self.error_check source, result_code
  if -1 == result_code
    raise_error source, result_code
  end

  # used by Socket::send/recv, ignored by others
  true
end

.error_stringObject

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



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

def self.error_string
  LibZMQ.zmq_strerror(errno).read_string
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)


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

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



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

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