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.



15
16
17
18
19
20
21
22
23
24
# File 'lib/meshchat/network/remote/relay_pool.rb', line 15

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

  find_initial_relay if @_known_relays.present?

  EM.add_periodic_timer(5) { ensure_relay }
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

#_message_queueObject

Returns the value of attribute _message_queue.



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

def _message_queue
  @_message_queue
end

#_waiting_for_subscriptionObject

Returns the value of attribute _waiting_for_subscription.



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

def _waiting_for_subscription
  @_waiting_for_subscription
end

Instance Method Details

#deplete_queueObject



47
48
49
50
51
52
53
54
# File 'lib/meshchat/network/remote/relay_pool.rb', line 47

def deplete_queue
  until _message_queue.empty?
    if _active_relay.subscribed?
      payload = _message_queue.pop
      _active_relay.send_now(payload)
    end
  end
end

#ensure_connectionObject



56
57
58
59
60
61
62
63
64
# File 'lib/meshchat/network/remote/relay_pool.rb', line 56

def ensure_connection
  if _active_relay.connected? && _active_relay.subscribed?
    yield
  else
    # if the relay isn't already connected,
    # it'll be built with a callback to deplete the queue
    ensure_relay
  end
end

#ensure_relayObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/meshchat/network/remote/relay_pool.rb', line 66

def ensure_relay
  return if _waiting_for_subscription
  return if _active_relay.subscribed?
  return if _active_relay.connected?

  # clear the previous node
  _active_relay = nil
  # re-connect
  find_initial_relay
end

#find_initial_relayArray

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

we only need one connection.

Returns:

  • (Array)

    an array of action cable clients



29
30
31
32
33
34
35
36
# File 'lib/meshchat/network/remote/relay_pool.rb', line 29

def find_initial_relay
  url = _known_relays.first
  self._waiting_for_subscription = true
  @_active_relay = Relay.new(url, _message_dispatcher, lambda do
    self._waiting_for_subscription = false
    deplete_queue
  end)
end

#send_payload(payload) ⇒ Object

Parameters:

  • payload (Hash)
    • the message payload



39
40
41
42
43
44
45
# File 'lib/meshchat/network/remote/relay_pool.rb', line 39

def send_payload(payload)
  return if _active_relay.blank?
  _message_queue << payload
  ensure_connection do
    deplete_queue
  end
end