class Mycroft::Client

Public Class Methods

new(host, port) click to toggle source
# File lib/mycroft/client.rb, line 8
def initialize(host, port)
  @host = host
  @port = port
  @name ||= 'mycroft'
  connect_to_mycroft
  if @threaded
    Thread.new do
      setup
    end
  else
    setup
  end
rescue Exception => e
  instance_exec(e, &@@handlers['ERROR']) unless @@handlers['ERROR'].nil?
end
on(type, &block) click to toggle source
# File lib/mycroft/client.rb, line 30
def self.on(type, &block)
  @@handlers ||= {}
  @@handlers[type] = block
end

Public Instance Methods

run() click to toggle source
# File lib/mycroft/client.rb, line 35
def run
  loop do
    size = @client.readline
    data = @client.read(size.to_i)
    parsed = parse_message(data)
    unless @@handlers[parsed[:type]].nil?
      instance_exec(parsed[:data], &@@handlers[parsed[:type]])
    end
    on_event_loop if methods.include?(:on_event_loop)
  end
ensure
  shutdown
end
setup() click to toggle source
# File lib/mycroft/client.rb, line 24
def setup
  send_manifest
  instance_eval &@@handlers['CONNECT'] unless @@handlers['CONNECT'].nil?
  run
end
shutdown() click to toggle source
# File lib/mycroft/client.rb, line 49
def shutdown
  instance_eval &@@handlers['CONNECTION_CLOSED'] unless @@handlers['CONNECTION_CLOSED'].nil?
  down
  @client.close
end