Class: Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = {}) ⇒ Router

Returns a new instance of Router.



16
17
18
# File 'lib/simple_gate/router.rb', line 16

def initialize(paths={})
  @paths = paths
end

Instance Attribute Details

#pathsObject

Graph of server connections. It’s a Hash of Arrays, which contains strings. Keys are server names with a connection to other servers. Each of these servers is a string in the associated Array. An example graph with three servers ‘foo’, ‘bar’ and ‘baz’.

router = Router.new
router.paths = {
  'foo' => %w[bar],
  'bar' => ['baz']
}
router.find('foo', 'baz') #=> ['foo', 'bar', 'baz']


14
15
16
# File 'lib/simple_gate/router.rb', line 14

def paths
  @paths
end

Instance Method Details

#find(start, target, current_route = []) ⇒ Object

A simple pathfinder. Returns a route as Array or nil. Uses a very naieve depth-first recursive full-graph search to find the shortest route.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simple_gate/router.rb', line 23

def find(start, target, current_route = [])
  return [target] if start == target
  return nil unless paths.has_key?(start)

  # Map all possible paths to the target.
  # Skip nodes we have already visited
  next_nodes = paths[start] - current_route
  routes = next_nodes.map do |next_node|
    find(next_node, target, current_route + [start])
  end

  # Reduce the collection to the shortest path
  shortest_route = routes.compact.inject(nil) {|shortest,possibility|
    next possibility if shortest.nil?
    possibility.size < shortest.size ? possibility : shortest
  }
  return [start] + shortest_route if shortest_route
  nil
end