Class: Legion::Gaia::Router::RouterBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/gaia/router/router_bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_registry:, worker_routing: nil, allowed_worker_ids: []) ⇒ RouterBridge

Returns a new instance of RouterBridge.



9
10
11
12
13
# File 'lib/legion/gaia/router/router_bridge.rb', line 9

def initialize(channel_registry:, worker_routing: nil, allowed_worker_ids: [])
  @channel_registry = channel_registry
  @worker_routing = worker_routing || WorkerRouting.new(allowed_worker_ids: allowed_worker_ids)
  @started = false
end

Instance Attribute Details

#channel_registryObject (readonly)

Returns the value of attribute channel_registry.



7
8
9
# File 'lib/legion/gaia/router/router_bridge.rb', line 7

def channel_registry
  @channel_registry
end

#startedObject (readonly)

Returns the value of attribute started.



7
8
9
# File 'lib/legion/gaia/router/router_bridge.rb', line 7

def started
  @started
end

#worker_routingObject (readonly)

Returns the value of attribute worker_routing.



7
8
9
# File 'lib/legion/gaia/router/router_bridge.rb', line 7

def worker_routing
  @worker_routing
end

Instance Method Details

#route_inbound(input_frame) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/gaia/router/router_bridge.rb', line 27

def route_inbound(input_frame)
  return { routed: false, reason: :not_started } unless started?

  identity = extract_identity(input_frame)
  worker_id = @worker_routing.resolve_worker_id(identity)
  worker_id ||= @worker_routing.resolve_from_db(identity) if identity

  return offline_response(input_frame) unless worker_id

  publish_input_frame(input_frame, worker_id: worker_id)
end

#route_outbound(payload) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/gaia/router/router_bridge.rb', line 39

def route_outbound(payload)
  return { delivered: false, reason: :not_started } unless started?

  frame = reconstruct_output_frame(payload)
  return { delivered: false, reason: :invalid_frame } unless frame

  adapter = @channel_registry.adapter_for(frame.channel_id)
  return { delivered: false, reason: :no_adapter, channel_id: frame.channel_id } unless adapter

  rendered = adapter.translate_outbound(frame)
  deliver_result = deliver_output(adapter, rendered, frame)
  { delivered: true, channel_id: frame.channel_id, frame_id: frame.id }.merge(deliver_result)
end

#startObject



15
16
17
# File 'lib/legion/gaia/router/router_bridge.rb', line 15

def start
  @started = true
end

#started?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/legion/gaia/router/router_bridge.rb', line 23

def started?
  @started == true
end

#stopObject



19
20
21
# File 'lib/legion/gaia/router/router_bridge.rb', line 19

def stop
  @started = false
end