Class: Panteao::BdiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/panteao_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '127.0.0.1', port = 0, project: nil, dev: false) ⇒ BdiClient

Returns a new instance of BdiClient.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/panteao_client.rb', line 91

def initialize(host = '127.0.0.1', port = 0, project: nil, dev: false)
  @dev = dev
  if project
    if port == 0
      port = self.class.get_free_port
    end
    bin = self.class.find_binary
    unless File.exist?(bin)
      current_dir = File.expand_path(File.dirname(__FILE__))
      bin = File.join(current_dir, Gem.win_platform? ? 'panteao-engine.exe' : 'panteao-engine')
      self.class.download_engine(bin)
    end
    
    @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(bin, project, '--port', port.to_s)
    @pid = @wait_thr.pid
    self.class.read_logs(@stdout)
    self.class.read_logs(@stderr)
    sleep 0.8
  elsif port == 0
    port = 44444
  end

  @socket = TCPSocket.new(host, port)
  while (line = @socket.gets)
    break if line.include?('"type":"mas_ready"')
  end

  @handlers = {}
  @running = true
  @thread = Thread.new { listen }
end

Class Method Details

.download_engine(bin_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/panteao_client.rb', line 16

def self.download_engine(bin_path)
  is_win = Gem.win_platform?
  is_mac = /darwin/ =~ RUBY_PLATFORM
  os_name = is_win ? 'win32' : (is_mac ? 'darwin' : 'linux')
  
  arch = (/arm/ =~ RUBY_PLATFORM || /aarch64/ =~ RUBY_PLATFORM) ? 'arm64' : 'x64'
  
  pkg_name = "panteao-engine-#{os_name}-#{arch}"
  url = "https://registry.npmjs.org/#{pkg_name}/-/#{pkg_name}-#{VERSION}.tgz"
  
  
  uri = URI(url)
  response = Net::HTTP.get_response(uri)
  
  raise "Failed to download engine: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
  
  sio = StringIO.new(response.body)
  gz = Zlib::GzipReader.new(sio)
  tar = Gem::Package::TarReader.new(gz)
  
  tar.each do |entry|
    if entry.full_name.end_with?('panteao-engine') || entry.full_name.end_with?('panteao-engine.exe')
      FileUtils.mkdir_p(File.dirname(bin_path))
      File.open(bin_path, 'wb') { |f| f.write(entry.read) }
      FileUtils.chmod(0755, bin_path)
      return
    end
  end
  raise "Binary not found in tarball"
end

.find_binaryObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/panteao_client.rb', line 72

def self.find_binary
  is_win = Gem.win_platform?
  bin_name = is_win ? 'panteao-engine.exe' : 'panteao-engine'
  
  current_dir = File.expand_path(File.dirname(__FILE__))
  cand1 = File.join(current_dir, bin_name)
  return cand1 if File.exist?(cand1)
  cand2 = File.join(current_dir, 'bin', bin_name)
  return cand2 if File.exist?(cand2)
  
  cwd = Dir.pwd
  cand3 = File.join(cwd, bin_name)
  return cand3 if File.exist?(cand3)
  cand4 = File.join(cwd, 'bin', bin_name)
  return cand4 if File.exist?(cand4)
  
  bin_name
end

.get_free_portObject



65
66
67
68
69
70
# File 'lib/panteao_client.rb', line 65

def self.get_free_port
  server = TCPServer.new('127.0.0.1', 0)
  port = server.addr[1]
  server.close
  port
end

.read_logs(io) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/panteao_client.rb', line 47

def self.read_logs(io)
  Thread.new do
    begin
      io.each_line do |line|
        line = line.strip
        next if line.empty?
        if line.start_with?('[') && (idx2 = line.index(']'))
          name = line[1...idx2].split('.').last
          puts "\e[36m[#{name}]\e[0m #{line[(idx2 + 2)..-1]}"
        else
          puts "\e[36m[MAS]\e[0m #{line}"
        end
      end
    rescue
    end
  end
end

Instance Method Details

#closeObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/panteao_client.rb', line 223

def close
  @running = false
  @socket.close rescue nil
  @thread.join rescue nil
  if @pid
    Process.kill('KILL', @pid) rescue nil
    Process.wait(@pid) rescue nil
    @pid = nil
  end
end

#register_action(action_name, &block) ⇒ Object



133
134
135
# File 'lib/panteao_client.rb', line 133

def register_action(action_name, &block)
  @handlers[action_name] = block
end

#send_msg(performative, sender, receiver, content) ⇒ Object



123
124
125
126
# File 'lib/panteao_client.rb', line 123

def send_msg(performative, sender, receiver, content)
  msg = { type: 'message', performative: performative, sender: sender, receiver: receiver, content: content }
  @socket.puts(msg.to_json)
end

#send_perception(action, perception) ⇒ Object



128
129
130
131
# File 'lib/panteao_client.rb', line 128

def send_perception(action, perception)
  payload = { type: 'perception', action: action, perception: perception }.to_json + "\n"
  @socket.write(payload)
end