Class: EventMachine::ZeroMQ::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/em-zeromq/context.rb

Constant Summary collapse

READABLES =
[ ZMQ::SUB, ZMQ::PULL, ZMQ::ROUTER, ZMQ::DEALER, ZMQ::REP, ZMQ::REQ ]
WRITABLES =
[ ZMQ::PUB, ZMQ::PUSH, ZMQ::ROUTER, ZMQ::DEALER, ZMQ::REP, ZMQ::REQ ]

Instance Method Summary collapse

Constructor Details

#initialize(threads_or_context) ⇒ Context

Returns a new instance of Context.



13
14
15
16
17
18
19
# File 'lib/em-zeromq/context.rb', line 13

def initialize(threads_or_context)
  if threads_or_context.is_a?(ZMQ::Context)
    @context = threads_or_context
  else
    @context = ZMQ::Context.new(threads_or_context)
  end
end

Instance Method Details

#socket(socket_type, handler = nil) ⇒ Object

Create a socket in this context.

Parameters:

  • socket_type (Integer)

    One of ZMQ::REQ, ZMQ::REP, ZMQ::PULL, ZMQ::PUSH, ZMQ::ROUTER, ZMQ::DEALER

  • handler (Object) (defaults to: nil)

    an object which respond to on_readable(socket, parts) and can respond to on_writeable(socket)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/em-zeromq/context.rb', line 30

def socket(socket_type, handler = nil)
  zmq_socket = @context.socket(socket_type)
  
  fd = []
  if zmq_socket.getsockopt(ZMQ::FD, fd) < 0
    raise "Unable to get socket FD: #{ZMQ::Util.error_string}"
  end
  
  
  EM.watch(fd[0], EventMachine::ZeroMQ::Socket, zmq_socket, socket_type, handler).tap do |s|
    s.register_readable if READABLES.include?(socket_type)
    s.register_writable if WRITABLES.include?(socket_type)
    
    yield(s) if block_given?
  end
end