Class: Meshchat::Network::Incoming::MessageProcessor

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

Overview

decodes an encrypted message and handles it. also update’s the info of the sender

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network: NETWORK_LOCAL, message_dispatcher: nil, location: nil) ⇒ MessageProcessor

Returns a new instance of MessageProcessor.



11
12
13
14
15
16
# File 'lib/meshchat/network/incoming/message_processor.rb', line 11

def initialize(network: NETWORK_LOCAL, message_dispatcher: nil, location: nil)
  @_network = network
  @_message_dispatcher = message_dispatcher
  @_message_factory = message_dispatcher._message_factory
  @_location = location
end

Instance Attribute Details

#_locationObject (readonly)

Returns the value of attribute _location.



8
9
10
# File 'lib/meshchat/network/incoming/message_processor.rb', line 8

def _location
  @_location
end

#_message_dispatcherObject (readonly)

Returns the value of attribute _message_dispatcher.



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

def _message_dispatcher
  @_message_dispatcher
end

#_message_factoryObject (readonly)

Returns the value of attribute _message_factory.



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

def _message_factory
  @_message_factory
end

#_networkObject (readonly)

Returns the value of attribute _network.



8
9
10
# File 'lib/meshchat/network/incoming/message_processor.rb', line 8

def _network
  @_network
end

Instance Method Details

#is_processing_for_local?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/meshchat/network/incoming/message_processor.rb', line 69

def is_processing_for_local?
  _network != NETWORK_RELAY
end

#is_processing_for_relay?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/meshchat/network/incoming/message_processor.rb', line 65

def is_processing_for_relay?
  _network == NETWORK_RELAY
end

#process(encoded_message) ⇒ Object

Parameters:

  • encoded_message (String)
    • the encrypted message as a string



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/meshchat/network/incoming/message_processor.rb', line 19

def process(encoded_message)
  request = MessageDecryptor.new(encoded_message, _message_factory)
  message = request.message

  Debug.receiving_message(message)

  # show the message to the user, and update the information
  # we have on the sender, so that we may reply to the
  # correct location
  update_sender_info(request._json)
  Display.present_message message
end

#update_sender_info(json) ⇒ Object

Parameters:

  • encoded_message (String)
    • the encrypted message as a string

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/meshchat/network/incoming/message_processor.rb', line 33

def update_sender_info(json)
  sender = json['sender']
  # Note that sender['location'] should always reference
  # the sender's local network address
  network_location = sender['location']

  # if the sender isn't currently marked as active,
  # perform the server list exchange
  node = Node.find_by_uid(sender['uid'])
  raise Errors::Forbidden, 'node not found' if node.nil?

  # if we are receiving a message from a node we had previously
  # known to be offline, we need to do the node list hash dance
  # with them to see if they know of any new members to the network
  unless node.online?
    node.update(on_local_network: true) if is_processing_for_local?
    node.update(on_relay: true) if is_processing_for_relay?

    nlh = _message_factory.create(Message::NODE_LIST_HASH)
    _message_dispatcher.send_message(node: node, message: nlh)
  end

  # update the node's location/alias
  # as they can change this info willy nilly
  attributes = {
    location_on_network: network_location,
    alias_name: sender['alias']
  }
  attributes[:location_of_relay] = _location if is_processing_for_relay?
  node.update(attributes)
end