Class: ZMQ::Context

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ffi-rzmq/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#errno, #error_string, #version

Constructor Details

#initialize(io_threads) ⇒ Context

Recommended to just pass 1 for io_threads since most programs are not heavily threaded. The rule of thumb is to make io_threads equal to the number of application threads that will be accessing 0mq sockets within this context. The io_threads number specifies the size of the thread pool allocated by 0mq for processing incoming/outgoing messages.

Returns a context object. It’s necessary for passing to the #Socket constructor when allocating new sockets. All sockets live within a context. Sockets in one context may not be accessed from another context; doing so raises an exception.

To connect sockets between contexts, use inproc or ipc transport and set up a 0mq socket between them.

May raise a #ContextError.



31
32
33
34
35
36
37
38
# File 'lib/ffi-rzmq/context.rb', line 31

def initialize io_threads
  @sockets ||= []
  @context = LibZMQ.zmq_init io_threads
  @pointer = @context
  error_check ZMQ_INIT_STR, @context.null? ? 1 : 0

  define_finalizer
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/ffi-rzmq/context.rb', line 12

def context
  @context
end

#pointerObject (readonly)

Returns the value of attribute pointer.



12
13
14
# File 'lib/ffi-rzmq/context.rb', line 12

def pointer
  @pointer
end

Instance Method Details

#socket(type) ⇒ Object

Short-cut to allocate a socket for a specific context.

Takes several type values:

#ZMQ::REQ
#ZMQ::REP
#ZMQ::PUB
#ZMQ::SUB
#ZMQ::PAIR
#ZMQ::UPSTREAM
#ZMQ::DOWNSTREAM
#ZMQ::XREQ
#ZMQ::XREP

Returns a #ZMQ::Socket.

May raise a #ContextError or #SocketError.



77
78
79
80
81
# File 'lib/ffi-rzmq/context.rb', line 77

def socket type
  sock = Socket.new @context, type
  error_check ZMQ_SOCKET_STR, sock.nil? ? 1 : 0
  sock
end

#terminateObject

Call to release the context and any remaining data associated with past sockets. This will close any sockets that remain open; further calls to those sockets will raise failure exceptions.

Returns nil.

May raise a #ContextError.



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

def terminate
  unless @context.nil? || @context.null?
    result_code = LibZMQ.zmq_term @context
    error_check ZMQ_TERM_STR, result_code
    @context = nil
    @sockets = nil
    remove_finalizer
  end
  nil
end