Class: VpsAdmin::CLI::Commands::VpsRemoteControl::InputHandler
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- VpsAdmin::CLI::Commands::VpsRemoteControl::InputHandler
- Defined in:
- lib/vpsadmin/cli/commands/vps_remote_console.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
Instance Method Summary collapse
-
#initialize ⇒ InputHandler
constructor
A new instance of InputHandler.
-
#receive_data(data) ⇒ Object
Data is checked on the presence of the end sequence.
Constructor Details
#initialize ⇒ InputHandler
Returns a new instance of InputHandler.
17 18 19 20 21 22 |
# File 'lib/vpsadmin/cli/commands/vps_remote_console.rb', line 17 def initialize @private_buffer = '' @buffer = '' @end_seq = ["\r", "\e", "."] @end_i = 0 end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
15 16 17 |
# File 'lib/vpsadmin/cli/commands/vps_remote_console.rb', line 15 def buffer @buffer end |
Instance Method Details
#receive_data(data) ⇒ Object
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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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 |