class Meshchat::Network::Local::Connection

Attributes

_message_dispatcher[R]
_message_factory[R]

Public Class Methods

new(dispatcher, message_factory) click to toggle source
# File lib/meshchat/network/local/connection.rb, line 10
def initialize(dispatcher, message_factory)
  @_message_factory = message_factory
  @_message_dispatcher = dispatcher

  # async, won't prevent us from sending
  start_server
end

Public Instance Methods

create_http_request(location, payload, &error_callback) click to toggle source
# File lib/meshchat/network/local/connection.rb, line 33
def create_http_request(location, payload, &error_callback)
  # TODO: what about https?
  #       maybe do the regex match: /https?:\/\//
  location = 'http://' + location unless location.include?('http://')
  http = EventMachine::HttpRequest.new(location).post(
    body: payload,
    head: {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json'
    }
  )

  http.errback &error_callback
  # example things available in the callback
  # p http.response_header.status
  # p http.response_header
  # p http.response
  http.callback { Display.debug http.response_header.status }
end
payload_for(encrypted_message) click to toggle source
# File lib/meshchat/network/local/connection.rb, line 53
def payload_for(encrypted_message)
  { message: encrypted_message }.to_json
end
send_message(node, encrypted_message, &error_callback) click to toggle source

@param [Node] node - the node describing the person you're sending a message to @param [JSON] encrypted_message - the message intended for the person at the location @param [Block] error_callback - what to do in case of failure

# File lib/meshchat/network/local/connection.rb, line 28
def send_message(node, encrypted_message, &error_callback)
  payload = payload_for(encrypted_message)
  create_http_request(node.location_on_network, payload, &error_callback)
end
start_server() click to toggle source
# File lib/meshchat/network/local/connection.rb, line 18
def start_server
  port = APP_CONFIG.user['port']
  Display.info "listening on port #{port}"
  EM.start_server('0.0.0.0', port,
    Network::Local::Server, _message_dispatcher)
end