class Meshchat::Network::Dispatcher

Attributes

_local_client[R]

standard peer-to-peer message sending

_message_factory[R]

creates messages

_relay_client[R]

the action cable client ( web socket / connection beyond the firewall)

- responsible for the relay server if the http client can't find the recipient

Public Class Methods

new() click to toggle source
# File lib/meshchat/network/dispatcher.rb, line 15
def initialize
  @_message_factory = Message::Factory.new(self)
  @_local_client = Local::Connection.new(self, @_message_factory)
  @_relay_client = Remote::Connection.new(self, @_message_factory)
end

Public Instance Methods

send_message(location: nil, uid: nil, node: nil, message: nil) click to toggle source

@note Either the location, node, or uid should be present

@param [String] location (Optional) location of target @param [String] uid (Optional) uid of target @param [Node] node (Optional) target @param [Message] message (Required) what to send to the target

# File lib/meshchat/network/dispatcher.rb, line 27
def send_message(location: nil, uid: nil, node: nil, message: nil)
  # verify node is valid
  node = Node.for(location: location, uid: uid, node: node)
  # don't proceed if we don't have a node
  return unless node
  # don't send to ourselves
  return if APP_CONFIG.user['uid'] == node.uid

  # everything is valid so far... DISPATCH!
  dispatch!(node, message)
end
send_to_all(message, ignore_offline_status: false) click to toggle source
# File lib/meshchat/network/dispatcher.rb, line 39
def send_to_all(message, ignore_offline_status: false)
  nodes = ignore_offline_status ? Node.all : Node.online
  nodes.each { |node| send_message(node: node, message: message) }
end

Private Instance Methods

dispatch!(node, message) click to toggle source
# File lib/meshchat/network/dispatcher.rb, line 46
def dispatch!(node, message)
  Debug.message_being_dispatched(node, message)

  message = encrypted_message(node, message)

  # determine last known sending method
  if node.on_local_network?
    try_dispatching_over_local_network_first(node, message)
  else
    try_dispatching_over_the_relay_first(node, message)
  end
end
encrypted_message(node, message) click to toggle source
# File lib/meshchat/network/dispatcher.rb, line 79
def encrypted_message(node, message)
  message.encrypt_for(node)
rescue => e
  Display.debug e.message
  Display.debug e.backtrace
  Debug.encryption_failed(node)
end
try_dispatching_over_local_network_first(node, message) click to toggle source

this attempts to send over http to the local network, if that fails, the passed block will be invoked

# File lib/meshchat/network/dispatcher.rb, line 61
def try_dispatching_over_local_network_first(node, message)
  _local_client.send_message(node, message) do
    Debug.not_on_local_network(node)
    node.update(on_local_network: false)
    _relay_client.send_message(node, message)
  end
end
try_dispatching_over_the_relay_first(node, message) click to toggle source

this attempts to send over the relay first if that fails, the passed block will be invked

# File lib/meshchat/network/dispatcher.rb, line 71
def try_dispatching_over_the_relay_first(node, message)
  # Due to the constant-connection nature of web-sockets,
  # The sending via http client will happen if the node's-
  # on_local_network property is true.
  # node.update(on_local_network: true)
  _relay_client.send_message(node, message)
end