Class: Bits::ExternalInterface::Interface

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/bits/external_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log

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

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



11
12
13
# File 'lib/bits/external_interface.rb', line 11

def capabilities
  @capabilities
end

#exitstatusObject (readonly)

Returns the value of attribute exitstatus.



11
12
13
# File 'lib/bits/external_interface.rb', line 11

def exitstatus
  @exitstatus
end

#idObject (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

#closeObject



66
67
68
69
# File 'lib/bits/external_interface.rb', line 66

def close
  return if @stdin.nil? and @stdout.nil?
  reap_child
end

#endObject

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

#pingObject



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