class Meshchat::Network::Remote::RelayPool
Constants
- CHANNEL
This channel is determine by the server, see github.com/NullVoxPopuli/mesh-relay/blob/master/app/channels/mesh_relay_channel.rb
Attributes
_active_relay[RW]
_available_relays[RW]
_known_relays[RW]
_message_dispatcher[RW]
_message_queue[RW]
_waiting_for_subscription[RW]
Public Class Methods
new(message_dispatcher)
click to toggle source
# 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
Public Instance Methods
deplete_queue()
click to toggle source
# 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_connection() { || ... }
click to toggle source
# 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_relay()
click to toggle source
# 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_relay()
click to toggle source
TODO: add logic for just selecting the first available relay.
we only need one connection.
@return [Array] an array of action cable clients
# 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)
click to toggle source
@param [Hash] payload - the message payload
# 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