Class: ZMQMachine::Socket::Sub

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/zm/sockets/sub.rb

Instance Attribute Summary

Attributes included from Base

#kind, #poll_options, #raw_socket

Instance Method Summary collapse

Methods included from Base

#attach, #bind, #connect, create, #identity, #identity=, #inspect, #resume_read, #resume_write, #send_message, #send_message_string, #send_messages

Constructor Details

#initialize(context, handler) ⇒ Sub

Returns a new instance of Sub.



44
45
46
47
48
49
# File 'lib/zm/sockets/sub.rb', line 44

def initialize context, handler
  @poll_options = ZMQ::POLLIN
  @kind = :sub

  super
end

Instance Method Details

#on_attach(handler) ⇒ Object

Attach a handler to the SUB socket.

A SUB socket may *only*receive messages.

This socket expects its handler to implement the #on_readable method. The #on_readable method will be called whenever a message may be dequeued without blocking.

For error handling purposes, the handler must also implement #on_readable_error.

Raises:

  • (ArgumentError)


63
64
65
66
67
# File 'lib/zm/sockets/sub.rb', line 63

def on_attach handler
  raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
  raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
  super
end

#subscribe(topic) ⇒ Object



69
70
71
# File 'lib/zm/sockets/sub.rb', line 69

def subscribe topic
  @raw_socket.setsockopt(ZMQ::SUBSCRIBE, topic)
end

#subscribe_allObject



73
74
75
# File 'lib/zm/sockets/sub.rb', line 73

def subscribe_all
  subscribe ''
end