Class: Protocol::HTTY::Framer

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/htty/framer.rb

Overview

Encode and decode HTTY chunks on top of byte-oriented IO objects.

Constant Summary collapse

ESC =
"\e"
DCS =
"#{ESC}P"
ST =
"#{ESC}\\"
PREFIX =
"HTTY;1;"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output = input) ⇒ Framer

Create a framer around the given input and output streams.



21
22
23
24
# File 'lib/protocol/htty/framer.rb', line 21

def initialize(input, output = input)
	@input = input
	@output = output
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



26
27
28
# File 'lib/protocol/htty/framer.rb', line 26

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



27
28
29
# File 'lib/protocol/htty/framer.rb', line 27

def output
  @output
end

Instance Method Details

#closeObject

Close the wrapped input and output streams. If input and output are the same object, it is only closed once.



66
67
68
69
# File 'lib/protocol/htty/framer.rb', line 66

def close
	@output.close if @output.respond_to?(:close)
	@input.close if !@input.equal?(@output) && @input.respond_to?(:close)
end

#closed?Boolean

Check whether the output stream has been closed.

Returns:

  • (Boolean)


73
74
75
# File 'lib/protocol/htty/framer.rb', line 73

def closed?
	@output.respond_to?(:closed?) && @output.closed?
end

#flushObject

Flush the output stream if it supports flushing.



59
60
61
# File 'lib/protocol/htty/framer.rb', line 59

def flush
	@output.flush if @output.respond_to?(:flush)
end

#read_chunkObject

Read the next HTTY chunk from the input stream. Non-HTTY terminal output is ignored until a valid chunk prefix is found.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/protocol/htty/framer.rb', line 43

def read_chunk
	while payload = read_payload
		if payload.start_with?("HTTY;") && !payload.start_with?(PREFIX)
			raise ProtocolError, "Unsupported HTTY chunk version: #{payload.inspect}"
		end
		
		next unless payload.start_with?(PREFIX)
		encoded = payload.delete_prefix(PREFIX)
		return Base64.strict_decode64(encoded)
	end
	
	return nil
end

#write_chunk(payload) ⇒ Object

Write a single HTTY chunk to the output stream.



32
33
34
35
# File 'lib/protocol/htty/framer.rb', line 32

def write_chunk(payload)
	encoded = Base64.strict_encode64(payload.to_s.b)
	@output.write("#{DCS}#{PREFIX}#{encoded}#{ST}")
end