Class: Meshchat::Network::Remote::RelayPool
- Inherits:
-
Object
- Object
- Meshchat::Network::Remote::RelayPool
- Defined in:
- lib/meshchat/network/remote/relay_pool.rb
Constant Summary collapse
- CHANNEL =
This channel is determine by the server, see github.com/NullVoxPopuli/mesh-relay/blob/master/app/channels/mesh_relay_channel.rb
'MeshRelayChannel'
Instance Attribute Summary collapse
-
#_active_relay ⇒ Object
Returns the value of attribute _active_relay.
-
#_available_relays ⇒ Object
Returns the value of attribute _available_relays.
-
#_known_relays ⇒ Object
Returns the value of attribute _known_relays.
-
#_message_dispatcher ⇒ Object
Returns the value of attribute _message_dispatcher.
-
#_message_queue ⇒ Object
Returns the value of attribute _message_queue.
-
#_waiting_for_subscription ⇒ Object
Returns the value of attribute _waiting_for_subscription.
Instance Method Summary collapse
- #deplete_queue ⇒ Object
- #ensure_connection ⇒ Object
- #ensure_relay ⇒ Object
-
#find_initial_relay ⇒ Array
TODO: add logic for just selecting the first available relay.
-
#initialize(message_dispatcher) ⇒ RelayPool
constructor
A new instance of RelayPool.
- #send_payload(payload) ⇒ Object
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 = @_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_relay ⇒ Object
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_relays ⇒ Object
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_relays ⇒ Object
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_dispatcher ⇒ Object
Returns the value of attribute _message_dispatcher.
10 11 12 |
# File 'lib/meshchat/network/remote/relay_pool.rb', line 10 def @_message_dispatcher end |
#_message_queue ⇒ Object
Returns the value of attribute _message_queue.
13 14 15 |
# File 'lib/meshchat/network/remote/relay_pool.rb', line 13 def @_message_queue end |
#_waiting_for_subscription ⇒ Object
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_queue ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/meshchat/network/remote/relay_pool.rb', line 47 def deplete_queue until .empty? if _active_relay.subscribed? payload = .pop _active_relay.send_now(payload) end end end |
#ensure_connection ⇒ Object
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_relay ⇒ Object
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_relay ⇒ Array
TODO: add logic for just selecting the first available relay.
we only need one connection.
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, , lambda do self._waiting_for_subscription = false deplete_queue end) end |
#send_payload(payload) ⇒ Object
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? << payload ensure_connection do deplete_queue end end |