Class: Isomorfeus::Speednode::Runtime::VMCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/speednode/runtime/vm_command.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket, cmd, arguments) ⇒ VMCommand

Returns a new instance of VMCommand.



5
6
7
8
9
# File 'lib/isomorfeus/speednode/runtime/vm_command.rb', line 5

def initialize(socket, cmd, arguments)
  @socket = socket
  @cmd = cmd
  @arguments = arguments
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isomorfeus/speednode/runtime/vm_command.rb', line 11

def execute
  result = ''
  message = ::Oj.dump({ 'cmd' => @cmd, 'args' => @arguments }, mode: :strict)
  message = message + "\x04"
  bytes_to_send = message.bytesize
  sent_bytes = 0

  if ::ExecJS.windows?
    @socket.write(message)
    begin
      result << @socket.read
    end until result.end_with?("\x04")
  else
    sent_bytes = @socket.sendmsg(message)
    if sent_bytes < bytes_to_send
      while sent_bytes < bytes_to_send
        sent_bytes += @socket.sendmsg(message.byteslice((sent_bytes)..-1))
      end
    end

    begin
      result << @socket.recvmsg()[0]
    end until result.end_with?("\x04")
  end
  ::Oj.load(result.chop!, mode: :strict)
end