Class: EventMachine::XS::Context

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

Constant Summary collapse

READABLES =
[ ::XS::SUB, ::XS::PULL, ::XS::ROUTER, ::XS::DEALER, ::XS::REP, ::XS::REQ, ::XS::RESPONDENT, ::XS::SURVEYOR ]
WRITABLES =
[ ::XS::PUB, ::XS::PUSH, ::XS::ROUTER, ::XS::DEALER, ::XS::REP, ::XS::REQ, ::XS::RESPONDENT, ::XS::SURVEYOR ]

Instance Method Summary collapse

Constructor Details

#initialize(io_threads = nil) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
13
14
15
16
# File 'lib/em-xs/context.rb', line 8

def initialize(io_threads = nil)
  @context = ::XS::Context.new
  if io_threads
    rc = @context.setctxopt(::XS::IO_THREADS, io_threads)
    unless ::XS::Util.resultcode_ok?(rc)
      ::XS::raise_error('xs_setctxopt', rc)
    end
  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)



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

def socket(socket_type, handler = nil)
  klass = Socket.get_class_for_type(socket_type)
  unless klass
    raise "Unsupported socket type: #{socket_type}"
  end
  
  socket = @context.socket(socket_type)
  
  fd = []
  rc = socket.getsockopt(::XS::FD, fd)
  unless ::XS::Util.resultcode_ok?(rc)
    raise "Unable to get socket FD: #{::XS::Util.error_string}"
  end
  
  EM.watch(fd[0], klass, 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