class Glottis::Handlers::RemoteOutputHandler

This class manages a TCP connection with a valyx server.

Constants

POLL_INTERVAL
PROTOCOL
REMOTE_PATHS

Public Class Methods

new(outgoing, host, port) click to toggle source
Calls superclass method
# File lib/glottis/handlers/remote_output_handler.rb, line 18
def initialize(outgoing, host, port)
  @outgoing = outgoing
  @host = host
  @port = port

  setup_http

  super do
    @http.start
    request_session

    loop do
      send_queued
    end
  end
end

Public Instance Methods

cleanup() click to toggle source
# File lib/glottis/handlers/remote_output_handler.rb, line 35
def cleanup
  @http.finish if @http.started?
end

Private Instance Methods

if_not_ok(http_req, ok_code = 200) { |body| ... } click to toggle source

given the result of a .get or .post, converts the response code, and executes the (side effecting) block if the response was not successful. calls the block with a message describing the error.

# File lib/glottis/handlers/remote_output_handler.rb, line 66
def if_not_ok(http_req, ok_code = 200, &_b)
  yield http_req.body if Integer(http_req.code) != ok_code
end
request_session() click to toggle source
# File lib/glottis/handlers/remote_output_handler.rb, line 48
def request_session
  Client.logger.info('making initial request for session...')

  session_req = @http.get(REMOTE_PATHS.fetch(:get_session))

  response = JSON.parse(session_req.body)

  if Integer(session_req.code) != 201
    raise "failed to retrieve session: #{response}"
  end

  @sid = response['sid']
  Client.logger.info("current session id: #{@sid}")
end
send_queued() click to toggle source
# File lib/glottis/handlers/remote_output_handler.rb, line 70
def send_queued
  Client.logger.info("processing message with #{@outgoing.size} messages outgoing")

  message_data = {
    from: @sid,
    to: '*',
    msg: @outgoing.pop
  }.to_json

  post_req = @http.post(REMOTE_PATHS.fetch(:post_message), message_data)

  if_not_ok(post_req) do |err|
    Client.logger.warn("failed to post message: #{message_data} #{err}")
  end
end
setup_http() click to toggle source
# File lib/glottis/handlers/remote_output_handler.rb, line 41
def setup_http
  @http = Net::HTTP.new(@host, @port)
  # TODO: make these configurable somewhere
  @http.open_timeout = 1
  @http.read_timeout = 1
end