Class: ZMQMachine::Device::Forwarder::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/devices/forwarder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor, address, opts = {}) ⇒ Handler

Returns a new instance of Handler.



68
69
70
71
72
73
74
75
# File 'lib/zm/devices/forwarder.rb', line 68

def initialize reactor, address, opts = {}
  @reactor = reactor
  @address = address
  @verbose = opts[:verbose] || false
  @opts = opts
  
  @messages = []
end

Instance Attribute Details

#socket_outObject

Returns the value of attribute socket_out.



66
67
68
# File 'lib/zm/devices/forwarder.rb', line 66

def socket_out
  @socket_out
end

Instance Method Details

#on_attach(socket) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/zm/devices/forwarder.rb', line 77

def on_attach socket
  socket.identity = "forwarder.#{Kernel.rand(999_999_999)}"
  set_options socket
  rc = socket.bind @address
  #FIXME: error handling!
  socket.subscribe_all if :sub == socket.kind
end

#on_readable(socket, messages) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/zm/devices/forwarder.rb', line 89

def on_readable socket, messages
  messages.each { |msg| @reactor.log(:device, "[fwd] [#{msg.copy_out_string}]") } if @verbose

  if @socket_out
    rc = socket_out.send_messages messages
    messages.each { |message| message.close }
  end
end

#on_writable(socket) ⇒ Object



85
86
87
# File 'lib/zm/devices/forwarder.rb', line 85

def on_writable socket
  @reactor.deregister_writable socket
end

#set_options(socket) ⇒ Object



98
99
100
101
# File 'lib/zm/devices/forwarder.rb', line 98

def set_options socket
  socket.raw_socket.setsockopt ZMQ::HWM, (@opts[:hwm] || 1)
  socket.raw_socket.setsockopt ZMQ::LINGER, (@opts[:linger] || 0)
end