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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, address) ⇒ Handler

Returns a new instance of Handler.



74
75
76
77
78
79
80
81
# File 'lib/zm/device/forwarder.rb', line 74

def initialize(config, address)
  @reactor = config.reactor
  @address = address
  @verbose = config.verbose
  @config = config

  @messages = []
end

Instance Attribute Details

#socket_outObject

Returns the value of attribute socket_out.



72
73
74
# File 'lib/zm/device/forwarder.rb', line 72

def socket_out
  @socket_out
end

Instance Method Details

#error_check(rc) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/zm/device/forwarder.rb', line 126

def error_check(rc)
  if ZMQ::Util.resultcode_ok?(rc)
    false
  else
    @reactor.log(:error, "Operation failed, errno [#{ZMQ::Util.errno}] description [#{ZMQ::Util.error_string}]")
    caller(1).each { |callstack| @reactor.log(:callstack, callstack) }
    true
  end
end

#on_attach(socket) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/zm/device/forwarder.rb', line 83

def on_attach(socket)
  set_options(socket)
  rc = socket.bind(@address)
  error_check(rc)

  error_check(socket.subscribe_all) if :sub == socket.kind
end

#on_readable(socket, messages) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/zm/device/forwarder.rb', line 95

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)
    error_check(rc)
    messages.each { |message| message.close }
  end
end

#on_readable_error(socket, return_code) ⇒ Object



105
106
107
# File 'lib/zm/device/forwarder.rb', line 105

def on_readable_error(socket, return_code)
  @reactor.log(:error, "#{self.class}#on_readable_error, rc [#{return_code}], errno [#{ZMQ::Util.errno}], descr [#{ZMQ::Util.error_string}]")
end

#on_writable(socket) ⇒ Object



91
92
93
# File 'lib/zm/device/forwarder.rb', line 91

def on_writable(socket)
  @reactor.deregister_writable(socket)
end

#set_options(socket) ⇒ Object



111
112
113
114
# File 'lib/zm/device/forwarder.rb', line 111

def set_options(socket)
  error_check(socket.raw_socket.setsockopt(ZMQ::HWM, @config.hwm))
  error_check(socket.raw_socket.setsockopt(ZMQ::LINGER, @config.linger))
end