class VpsAdmin::CLI::Commands::VpsRemoteControl::InputHandler

Attributes

buffer[RW]

Public Class Methods

new() click to toggle source
# File lib/vpsadmin/cli/commands/vps_remote_console.rb, line 17
def initialize
  @private_buffer = ''
  @buffer = ''
  @end_seq = ["\r", "\e", "."]
  @end_i = 0
end

Public Instance Methods

receive_data(data) click to toggle source

Data is checked on the presence of the end sequence. The first character in the sequence (ENTER) can be read multiple times in a row and it is to be forwarded.

When the second character in the end sequence is read, it is not forwarded, but stored in a private buffer. If the sequence is later broken, the private buffer is forwarded and reset.

If the whole end sequence is read, EM event loop is stopped.

# File lib/vpsadmin/cli/commands/vps_remote_console.rb, line 33
def receive_data(data)
  data.each_char do |char|
    if char == @end_seq[ @end_i ]
      if @end_i == @end_seq.size-1
        EM.stop
        return
      end

      @end_i += 1

      if @end_i == 1
        @buffer += char

      else
        @private_buffer += char
      end

    elsif char == @end_seq.first
      @buffer += char

    else
      @end_i = 0

      unless @private_buffer.empty?
        @buffer += @private_buffer
        @private_buffer.clear
      end

      @buffer += char
    end
  end
end