Class: Meshchat::Network::Remote::RelayPool

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/network/remote/relay_pool.rb

Constant Summary collapse

CHANNEL =
'MeshRelayChannel'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_dispatcher) ⇒ RelayPool

Returns a new instance of RelayPool.



14
15
16
17
18
19
20
# File 'lib/meshchat/network/remote/relay_pool.rb', line 14

def initialize(message_dispatcher)
  @_message_dispatcher = message_dispatcher
  @_known_relays = APP_CONFIG.user['relays'] || []
  @_available_relays = APP_CONFIG.user['relays'] || []

  find_initial_relay if @_known_relays.present?
end

Instance Attribute Details

#_active_relayObject

Returns the value of attribute _active_relay.



11
12
13
# File 'lib/meshchat/network/remote/relay_pool.rb', line 11

def _active_relay
  @_active_relay
end

#_available_relaysObject

Returns the value of attribute _available_relays.



12
13
14
# File 'lib/meshchat/network/remote/relay_pool.rb', line 12

def _available_relays
  @_available_relays
end

#_known_relaysObject

Returns the value of attribute _known_relays.



12
13
14
# File 'lib/meshchat/network/remote/relay_pool.rb', line 12

def _known_relays
  @_known_relays
end

#_message_dispatcherObject

Returns the value of attribute _message_dispatcher.



10
11
12
# File 'lib/meshchat/network/remote/relay_pool.rb', line 10

def _message_dispatcher
  @_message_dispatcher
end

Instance Method Details

#ensure_active_connection!(&block) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/meshchat/network/remote/relay_pool.rb', line 41

def ensure_active_connection!(&block)
  # TODO: make action_cable_client return a boolean
  if !_active_relay.connected?
    find_initial_relay(connected: block)
  else
    yield
  end
end

#find_initial_relay(connected: nil) ⇒ Array

TODO: add logic for just selecting the first available relay.

we only need one connection.

Returns:

  • (Array)

    an array of action cable clients



25
26
27
28
29
30
# File 'lib/meshchat/network/remote/relay_pool.rb', line 25

def find_initial_relay(connected: nil)
  url = _known_relays.first
  @_active_relay = Relay.new(
    url, _message_dispatcher,
    connected: connected)
end

#send_payload(payload) ⇒ Object

Parameters:

  • payload (Hash)
    • the message payload



33
34
35
36
37
38
39
# File 'lib/meshchat/network/remote/relay_pool.rb', line 33

def send_payload(payload)
  return if _active_relay.blank?

  ensure_active_connection! do
    _active_relay.perform('chat', payload)
  end
end