A ZMQ::Pollitem instance is compulsary on init, with support for optional arguments if a subclasses do require them.
pub = ctx.bind(:PUB, “tcp://127.0.0.1:5000”) # lower level API item = ZMQ::Pollitem.new(pub) item.handler = ZMQ::Handler.new(pub)
class ProducerHandler < ZMQ::Handler
def initialize(item, producer) super @producer = producer end def on_writable @producer.work end
end
ZMQ::Loop.bind(:PUB, "tcp://127.0.0.1:5000", ProducerHandler, producer) # higher level API
# File lib/zmq/handler.rb, line 26 def initialize(pollitem, *args) raise TypeError.new("#{pollitem.inspect} is not a valid ZMQ::Pollitem instance") unless ZMQ::Pollitem === pollitem @pollitem = pollitem end
Callback for error conditions such as pollable item errors on poll and exceptions raised in callbacks. Receives an exception instance as argument and raises by default.
handler.on_error(err) => raise
# File lib/zmq/handler.rb, line 60 def on_error(exception) raise exception end
Callback invoked from ZMQ::Loop handlers when the pollable item is ready for reading. Subclasses are expected to implement this contract as the default just raises NotImplementedError. It’s reccommended to read in a non-blocking manner from within this callback.
def on_readable
msgs << recv
end
# File lib/zmq/handler.rb, line 39 def on_readable raise NotImplementedError, "ZMQ handlers are expected to implement an #on_readable contract" end
Callback invoked from ZMQ::Loop handlers when the pollable item is ready for writing. Subclasses are expected to implement this contract as the default just raises NotImplementedError. It’s reccommended to write data out as fast as possible from within this callback.
def on_writable
send buffer.shift
end
# File lib/zmq/handler.rb, line 51 def on_writable raise NotImplementedError, "ZMQ handlers are expected to implement an #on_writable contract" end
API that allows handlers to receive data regardless of the underlying pollable item type (ZMQ::Socket or IO).
# File lib/zmq/handler.rb, line 72 def recv pollitem.recv end
API that allows handlers to send data regardless of the underlying pollable item type (ZMQ::Socket or IO).
# File lib/zmq/handler.rb, line 66 def send(*args) pollitem.send(*args) end
Generated with the Darkfish Rdoc Generator 2.