Class: Meshchat::Network::Dispatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



15
16
17
18
19
# 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

Instance Attribute Details

#_local_clientObject (readonly)

standard peer-to-peer message sending



9
10
11
# File 'lib/meshchat/network/dispatcher.rb', line 9

def _local_client
  @_local_client
end

#_message_factoryObject (readonly)

creates messages



6
7
8
# File 'lib/meshchat/network/dispatcher.rb', line 6

def _message_factory
  @_message_factory
end

#_relay_clientObject (readonly)

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

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


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

def _relay_client
  @_relay_client
end

Instance Method Details

#send_message(location: nil, uid: nil, node: nil, message: nil) ⇒ Object

Note:

Either the location, node, or uid should be present

Parameters:

  • location (String) (defaults to: nil)

    (Optional) location of target

  • uid (String) (defaults to: nil)

    (Optional) uid of target

  • node (Node) (defaults to: nil)

    (Optional) target

  • message (Message) (defaults to: nil)

    (Required) what to send to the target



27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
# 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