Class: ZMQ::Context

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

Overview

Recommended to use the default for io_threads since most programs will not saturate I/O.

The rule of thumb is to make io_threads equal to the number gigabits per second that the application will produce.

The io_threads number specifies the size of the thread pool allocated by 0mq for processing incoming/outgoing messages.

Returns a context object when allocation succeeds. It’s necessary for passing to the #Socket constructor when allocating new sockets. All sockets live within a context.

Also, Sockets should only be accessed from the thread where they were first created. Do not pass sockets between threads; pass in the context and allocate a new socket per thread. If you must use threads, then make sure to execute a full memory barrier (e.g. mutex) as you pass a socket from one thread to the next.

To connect sockets between contexts, use inproc or ipc transport and set up a 0mq socket between them. This is also the recommended technique for allowing sockets to communicate between threads.

context = ZMQ::Context.create
if context
  socket = context.socket(ZMQ::REQ)
  if socket
    ...
  else
    STDERR.puts "Socket allocation failed"
  end
else
  STDERR.puts "Context allocation failed"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Returns a new instance of Context.



55
56
57
58
59
60
61
# File 'lib/ffi-rzmq/context.rb', line 55

def initialize io_threads = 1
  @io_threads = io_threads
  @context = LibZMQ.zmq_init io_threads
  ZMQ::Util.error_check 'zmq_init', (@context.nil? || @context.null?) ? -1 : 0

  define_finalizer
end

Instance Attribute Details

#contextObject (readonly) Also known as: pointer

Returns the value of attribute context.



45
46
47
# File 'lib/ffi-rzmq/context.rb', line 45

def context
  @context
end

#io_threadsObject (readonly)

Returns the value of attribute io_threads.



45
46
47
# File 'lib/ffi-rzmq/context.rb', line 45

def io_threads
  @io_threads
end

#max_socketsObject (readonly)

Returns the value of attribute max_sockets.



45
46
47
# File 'lib/ffi-rzmq/context.rb', line 45

def max_sockets
  @max_sockets
end

Class Method Details

.create(opts = {}) ⇒ Object



51
52
53
# File 'lib/ffi-rzmq/context.rb', line 51

def self.create io_threads = 1
  new(io_threads) rescue nil
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::PULL
#ZMQ::PUSH
#ZMQ::DEALER
#ZMQ::ROUTER

Returns a #ZMQ::Socket when the allocation succeeds, nil if it fails.



137
138
139
140
141
142
143
144
145
146
# File 'lib/ffi-rzmq/context.rb', line 137

def socket type
  sock = nil
  begin
    sock = Socket.new @context, type
  rescue ContextError => e
    sock = nil
  end

  sock
end

#terminateObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/ffi-rzmq/context.rb', line 98

def terminate
  unless @context.nil? || @context.null?
    remove_finalizer
    rc = LibZMQ.zmq_term @context
    @context = nil
    rc
  else
    0
  end
end