Class: ZMQMachine::Device::Forwarder

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

Overview

Used in conjunction with PUB/SUB sockets to allow multiple publishers to all publish to the same “bus.”

The basic mechanics are that the program contains 1 (or more) publishers that broadcast to the same bus. Connecting to an intermediate queue device allows for the publishers to have all of their traffic aggregated to a single port.

Example:

# the queue creates sockets and binds to both given addresses; all messages get
# republished from +incoming+ to +outgoing+
config = ZM::Device::Configuration.new
config.reactor = reactor
config.incoming_endpoint = "tcp://192.168.0.100:5050"
config.outgoing_endpoint = "tcp://192.168.0.100:5051"
config.verbose = false
config.linger = 10 # ms
config.hwm = 0
config.topic = '' # get everything
forwarder = ZM::Device::Forwarder.new(config)

# the +pub_handler+ internally calls "connect" to the incoming address given above
pub1 = reactor.pub_socket(pub_handler)
pub2 = reactor.pub_socket(pub_handler)

# the +sub_handler+ internally calls "connect" to the outgoing address given above
subscriber = reactor.sub_socket(sub_handler)

Defined Under Namespace

Classes: Handler

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Forwarder

Forwards all messages received by the incoming address to the outgoing address.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/zm/device/forwarder.rb', line 140

def initialize(config)
  @reactor = config.reactor
  incoming = Address.from_string(config.incoming_endpoint.to_s)
  outgoing = Address.from_string(config.outgoing_endpoint.to_s)

  # setup the handlers for processing messages
  @handler_in = Handler.new(config, incoming)
  @handler_out = Handler.new(config, outgoing)

  # create each socket and pass in the appropriate handler
  @incoming_sock = @reactor.sub_socket(@handler_in)
  @outgoing_sock = @reactor.pub_socket(@handler_out)

  # set each handler's outgoing socket
  @handler_in.socket_out = @outgoing_sock
  @handler_out.socket_out = @incoming_sock
end