Class: Buster::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/buster/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, routes = {}) ⇒ Router

Returns a new instance of Router.



4
5
6
7
# File 'lib/buster/router.rb', line 4

def initialize(context, routes = {})
  @context = context
  @routes = routes
end

Instance Method Details

#connect_routes(reply_socket, poller) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/buster/router.rb', line 9

def connect_routes(reply_socket, poller)
  local = @context.socket(ZMQ::DEALER)
  local.bind("inproc://routes")

  sockets = @routes.map do |pattern, uri|
    remote = @context.socket(ZMQ::DEALER)
    result = remote.connect(uri)

    poller.pipe remote, reply_socket
    [pattern, remote]
  end

  poller.register(local) do |s|
    s.recv_strings(msgs = [])
    message_name = msgs[0]
    #TODO: More robust routing
    remote = sockets.detect([nil,nil]){|x| x[0].match message_name}[1]
    if remote.nil?
      puts "No remote matching '#{message_name}'"
      return
    end
    remote.send_strings(msgs)
  end
end