Class: ZMQ::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/zmq/handler.rb

Direct Known Subclasses

DefaultHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Raises:

  • (TypeError)


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

#pollitemObject (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_readableObject

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

Raises:

  • (NotImplementedError)


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_writableObject

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

Raises:

  • (NotImplementedError)


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

#recvObject

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