Class: Legion::Gaia::ChannelRegistry

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

Instance Method Summary collapse

Constructor Details

#initializeChannelRegistry

Returns a new instance of ChannelRegistry.



6
7
8
9
# File 'lib/legion/gaia/channel_registry.rb', line 6

def initialize
  @adapters = {}
  @mutex = Mutex.new
end

Instance Method Details

#active_adaptersObject



33
34
35
# File 'lib/legion/gaia/channel_registry.rb', line 33

def active_adapters
  @mutex.synchronize { @adapters.values.select(&:started?) }
end

#active_channelsObject



29
30
31
# File 'lib/legion/gaia/channel_registry.rb', line 29

def active_channels
  @mutex.synchronize { @adapters.keys }
end

#adapter_for(channel_id) ⇒ Object



25
26
27
# File 'lib/legion/gaia/channel_registry.rb', line 25

def adapter_for(channel_id)
  @mutex.synchronize { @adapters[channel_id] }
end

#deliver(output_frame) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/legion/gaia/channel_registry.rb', line 41

def deliver(output_frame)
  adapter = adapter_for(output_frame.channel_id)
  return { delivered: false, reason: :no_adapter } unless adapter
  return { delivered: false, reason: :adapter_stopped } unless adapter.started?

  rendered = adapter.translate_outbound(output_frame)
  adapter.deliver(rendered)
  { delivered: true, channel_id: output_frame.channel_id }
end

#register(adapter) ⇒ Object



11
12
13
14
15
# File 'lib/legion/gaia/channel_registry.rb', line 11

def register(adapter)
  @mutex.synchronize do
    @adapters[adapter.channel_id] = adapter
  end
end

#sizeObject



37
38
39
# File 'lib/legion/gaia/channel_registry.rb', line 37

def size
  @mutex.synchronize { @adapters.size }
end

#start_allObject



51
52
53
# File 'lib/legion/gaia/channel_registry.rb', line 51

def start_all
  @mutex.synchronize { @adapters.each_value(&:start) }
end

#stop_allObject



55
56
57
# File 'lib/legion/gaia/channel_registry.rb', line 55

def stop_all
  @mutex.synchronize { @adapters.each_value(&:stop) }
end

#unregister(channel_id) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/legion/gaia/channel_registry.rb', line 17

def unregister(channel_id)
  @mutex.synchronize do
    adapter = @adapters.delete(channel_id)
    adapter&.stop
    adapter
  end
end