Class: Bits::ExternalInterface::Interface
- Inherits:
-
Object
- Object
- Bits::ExternalInterface::Interface
- Includes:
- Logging
- Defined in:
- lib/bits/external_interface.rb
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#exitstatus ⇒ Object
readonly
Returns the value of attribute exitstatus.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #close ⇒ Object
-
#end ⇒ Object
end the child process by closing stdin.
- #info(atom) ⇒ Object
-
#initialize(id, args, stdin, stdout, pid) ⇒ Interface
constructor
A new instance of Interface.
- #ping ⇒ Object
- #request(type, command = {}) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(id, args, stdin, stdout, pid) ⇒ Interface
Returns a new instance of Interface.
13 14 15 16 17 18 19 20 21 |
# File 'lib/bits/external_interface.rb', line 13 def initialize(id, args, stdin, stdout, pid) @id = id @args = args @stdin = stdin @stdout = stdout @pid = pid @capabilities = [] @exitstatus = nil end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
11 12 13 |
# File 'lib/bits/external_interface.rb', line 11 def capabilities @capabilities end |
#exitstatus ⇒ Object (readonly)
Returns the value of attribute exitstatus.
11 12 13 |
# File 'lib/bits/external_interface.rb', line 11 def exitstatus @exitstatus end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/bits/external_interface.rb', line 11 def id @id end |
Instance Method Details
#close ⇒ Object
66 67 68 69 |
# File 'lib/bits/external_interface.rb', line 66 def close return if @stdin.nil? and @stdout.nil? reap_child end |
#end ⇒ Object
end the child process by closing stdin.
24 25 26 27 |
# File 'lib/bits/external_interface.rb', line 24 def end @stdin.close reap_child end |
#info(atom) ⇒ Object
29 30 31 |
# File 'lib/bits/external_interface.rb', line 29 def info(atom) type, response = request :info, :atom => atom end |
#ping ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bits/external_interface.rb', line 33 def ping begin type, response = request :ping rescue log.debug "problem while pinging interface '#{@id}': #{$!}" return nil end raise "Expected pong but got #{type}" unless type == :pong @capabilities = (response['capabilities'] || []).map(&:to_sym) return true end |
#request(type, command = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bits/external_interface.rb', line 46 def request(type, command={}) command[:__type__] = type write JSON.dump(command) data = read raise "empty response" if data.nil? response = JSON.load(data) response_type = response['__type__'].to_sym if response_type == :error error_text = response['text'] raise "Error in interface: #{error_text}" end [response_type, response] end |