Class: ZMQ::Handler
- Inherits:
-
Object
- Object
- ZMQ::Handler
- Defined in:
- lib/zmq/handler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#pollitem ⇒ Object
readonly
The ZMQ::Pollitem instance wrapped by this handler.
Instance Method Summary collapse
-
#initialize(pollitem, *args) ⇒ Handler
constructor
A ZMQ::Pollitem instance is compulsary on init, with support for optional arguments if a subclasses do require them.
-
#on_error(exception) ⇒ Object
Callback for error conditions such as pollable item errors on poll and exceptions raised in callbacks.
-
#on_readable ⇒ Object
Callback invoked from ZMQ::Loop handlers when the pollable item is ready for reading.
-
#on_writable ⇒ Object
Callback invoked from ZMQ::Loop handlers when the pollable item is ready for writing.
-
#recv ⇒ Object
API that allows handlers to receive data regardless of the underlying pollable item type (ZMQ::Socket or IO).
-
#send(*args) ⇒ Object
API that allows handlers to send data regardless of the underlying pollable item type (ZMQ::Socket or IO).
Constructor Details
#initialize(pollitem, *args) ⇒ Handler
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
27 28 29 30 |
# File 'lib/zmq/handler.rb', line 27 def initialize(pollitem, *args) raise TypeError.new("#{pollitem.inspect} is not a valid ZMQ::Pollitem instance") unless ZMQ::Pollitem === pollitem @pollitem = pollitem end |
Instance Attribute Details
#pollitem ⇒ Object (readonly)
The ZMQ::Pollitem instance wrapped by this handler.
6 7 8 |
# File 'lib/zmq/handler.rb', line 6 def pollitem @pollitem end |
Instance Method Details
#on_error(exception) ⇒ Object
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
61 62 63 |
# File 'lib/zmq/handler.rb', line 61 def on_error(exception) raise exception end |
#on_readable ⇒ Object
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
40 41 42 |
# File 'lib/zmq/handler.rb', line 40 def on_readable raise NotImplementedError, "ZMQ handlers are expected to implement an #on_readable contract" end |
#on_writable ⇒ Object
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
52 53 54 |
# File 'lib/zmq/handler.rb', line 52 def on_writable raise NotImplementedError, "ZMQ handlers are expected to implement an #on_writable contract" end |
#recv ⇒ Object
API that allows handlers to receive data regardless of the underlying pollable item type (ZMQ::Socket or IO).
73 74 75 |
# File 'lib/zmq/handler.rb', line 73 def recv pollitem.recv end |
#send(*args) ⇒ Object
API that allows handlers to send data regardless of the underlying pollable item type (ZMQ::Socket or IO).
67 68 69 |
# File 'lib/zmq/handler.rb', line 67 def send(*args) pollitem.send(*args) end |