Class: MeshChat::Message::NodeList

Inherits:
Base
  • Object
show all
Defined in:
lib/meshchat/message/node_list.rb

Instance Attribute Summary

Attributes inherited from Base

#payload, #sender_location, #sender_name, #sender_uid, #time_recieved

Instance Method Summary collapse

Methods inherited from Base

#client, #client_version, #display, #initialize, #render, #type

Constructor Details

This class inherits a constructor from MeshChat::Message::Base

Instance Method Details

#handleObject



8
9
10
11
# File 'lib/meshchat/message/node_list.rb', line 8

def handle
  respond
  return
end

#messageObject



4
5
6
# File 'lib/meshchat/message/node_list.rb', line 4

def message
  @message ||= Node.as_json
end

#respondObject

only need to respond if this server has node entries that the sender of this message doesn’t have



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/meshchat/message/node_list.rb', line 15

def respond
  received_list = message
  we_only_have, they_only_have = Node.diff(received_list)

  if they_only_have.present?
    they_only_have.each do |n|
      Node.from_json(n).save!
    end
  end

  if we_only_have.present?
    location = payload['sender']['location']

    node = Node.find_by_location(location)

    # give the sender our list
    MeshChat::Net::Client.send(
      node: node,
      message: NodeListDiff.new(message: we_only_have)
    )

    # give the network their list
    Node.online.each do |entry|
      MeshChat::Net::Client.send(
        node: entry,
        message: NodeListDiff.new(message: they_only_have)
      )
    end
  end
end