Class: Mycroft::Client
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Messages
#broadcast, #connect_to_mycroft, #down, #in_use, #query, #query_fail, #query_success, #send_manifest, #up
Methods included from Helpers
#parse_message, #send_message, #update_dependencies
Constructor Details
#initialize(host, port) ⇒ Client
Returns a new instance of Client.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 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
|
Class Method Details
.on(type, &block) ⇒ Object
30
31
32
33
|
# File 'lib/mycroft/client.rb', line 30
def self.on(type, &block)
@@handlers ||= {}
@@handlers[type] = block
end
|
Instance Method Details
#run ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# 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 ⇒ Object
24
25
26
27
28
|
# File 'lib/mycroft/client.rb', line 24
def setup
send_manifest
instance_eval &@@handlers['CONNECT'] unless @@handlers['CONNECT'].nil?
run
end
|
#shutdown ⇒ Object
49
50
51
52
53
|
# File 'lib/mycroft/client.rb', line 49
def shutdown
instance_eval &@@handlers['CONNECTION_CLOSED'] unless @@handlers['CONNECTION_CLOSED'].nil?
down
@client.close
end
|