Class: ZMQMachine::Device::Forwarder

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/devices/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+
forwarder = ZM::Device::Forwarder.new reactor, "tcp://192.168.0.100:5050", "tcp://192.168.0.100:5051"

# 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(reactor, incoming, outgoing, opts = {:verbose => false}) ⇒ Forwarder

Takes either a properly formatted string that can be converted into a ZM::Address or takes a ZM::Address directly.

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/zm/devices/forwarder.rb', line 111

def initialize reactor, incoming, outgoing, opts = {:verbose => false}
  incoming = Address.from_string incoming if incoming.kind_of? String
  outgoing = Address.from_string outgoing if outgoing.kind_of? String

  # setup the handlers for processing messages
  @handler_in = Handler.new reactor, incoming, opts
  @handler_out = Handler.new reactor, outgoing, opts

  # create each socket and pass in the appropriate handler
  @incoming = reactor.sub_socket @handler_in
  @outgoing = reactor.pub_socket @handler_out

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