Module: MeshChat::Net::Client

Defined in:
lib/meshchat/net/client.rb

Class Method Summary collapse

Class Method Details

.node_for(location: nil, uid: nil, node: nil) ⇒ Node

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/meshchat/net/client.rb', line 49

def node_for(location: nil, uid: nil, node: nil)
  unless node
    node = Models::Entry.find_by_location(location) if location
    node = Models::Entry.find_by_uid(uid) if uid && !node
  end

  # TODO: also check for public key?
  # without the public key, the message is sent in cleartext. :-   if !(node && node.location)
    Display.alert "Node not found, or does not have a location"
    return
  end

  node
end

.payload_for(node, message) ⇒ Object

creates a json payload



41
42
43
44
# File 'lib/meshchat/net/client.rb', line 41

def self.payload_for(node, message)
  request = MeshChat::Net::Request.new(node, message)
  { message: request.payload }
end

.send(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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/meshchat/net/client.rb', line 12

def send(location: nil, uid: nil, node: nil, message: nil)
  # verify node is valid
  node = self.node_for(location: location, uid: uid, node: node)

  Thread.new(node, message) do |node, message|
    payload = Client.payload_for(node, message)

    begin
      Curl::Easy.http_post(node.location, payload.to_json) do |c|
        c.headers['Accept'] = 'application/json'
        c.headers['Content-Type'] = 'application/json'
        if MeshChat::Settings.debug?
          puts message.render
          c.verbose = true
          c.on_debug do |type, data|
            puts data
          end
        end
      end
    rescue => e
      node.update(online: false)
      Display.info "#{node.alias_name} has ventured offline"
      Display.debug("#{message.class.name}: Issue connectiong to #{node.alias_name}@#{node.location}")
      Display.debug(e.message)
    end
  end
end