Class: Meshchat::Network::Message::NodeList

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

Instance Attribute Summary

Attributes inherited from Base

#_message, #_message_dispatcher, #_message_factory, #_sender_location, #_sender_name, #_sender_uid, #_time_received, #payload

Instance Method Summary collapse

Methods inherited from Base

#client, #client_version, #display, #encrypt_for, #initialize, #render, #sender, #sender_location, #sender_name, #sender_uid, #time_received, #time_received_as_date, #type

Constructor Details

This class inherits a constructor from Meshchat::Network::Message::Base

Instance Method Details

#handleObject



10
11
12
13
# File 'lib/meshchat/network/message/node_list.rb', line 10

def handle
  respond
  nil
end

#messageObject



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

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/meshchat/network/message/node_list.rb', line 17

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

  Display.debug('node_list#respond: me: ' + we_only_have.to_s)
  Display.debug('node_list#respond: they: ' + they_only_have.to_s)

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

  uid = payload['sender']['uid']

  if we_only_have.present?
    respond_with_what_we_have(we_only_have, they_only_have, uid)
  else
    respond_with_confirmation_of_in_sync(uid)
  end
end

#respond_with_confirmation_of_in_sync(uid) ⇒ Object



39
40
41
42
43
44
# File 'lib/meshchat/network/message/node_list.rb', line 39

def respond_with_confirmation_of_in_sync(uid)
  Display.debug 'node lists are in sync'
  nlh_message = _message_factory.create(NODE_LIST_HASH)
  # lists are in sync, confirm with hash
  _message_dispatcher.send_message(uid: uid, message: nlh_message)
end

#respond_with_what_we_have(we_only_have, they_only_have, uid) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/meshchat/network/message/node_list.rb', line 46

def respond_with_what_we_have(we_only_have, they_only_have, uid)
  Display.debug 'we have nodes that they do not'

  we_only_have_message = _message_factory.create(
    NODE_LIST_DIFF,
    data: { message: we_only_have }
  )

  they_only_have_message = _message_factory.create(
    NODE_LIST_DIFF,
    data: { message: they_only_have }
  )

  # give the sender our list
  _message_dispatcher.send_message(
    uid: uid,
    message: we_only_have_message
  )

  # give people we know about
  # (but the sender of the Node List may not know about)
  # our node list diff
  Node.online.each do |node|
    _message_dispatcher.send_message(
      node: node,
      message: they_only_have_message
    )
  end
end