Class: RTunnel::Command
- Inherits:
-
Object
- Object
- RTunnel::Command
- Defined in:
- lib/rtunnel/commands.rb
Direct Known Subclasses
ConnectionCommand, GenerateSessionKeyCommand, KeepAliveCommand, RemoteListenCommand, SetSessionKeyCommand
Defined Under Namespace
Classes: Registry
Constant Summary collapse
Class Method Summary collapse
-
.command_code(code) ⇒ Object
subclasses must call this to register and declare their command code.
-
.decode(io) ⇒ Object
Decode a Command instance from a IO / IOString.
-
.printable_codes ⇒ Object
Printable string containing all the codes and their classes.
- .registry ⇒ Object
Instance Method Summary collapse
-
#encode(io) ⇒ Object
Encode this command to a IO / IOString.
-
#initialize_from_io(io) ⇒ Object
subclasses should override this and call super before performing their own initialization.
-
#to_encoded_str ⇒ Object
Produce a string with an encoding of this command.
-
#to_s ⇒ Object
subclasses should override this (and add to the result) to provide a debug string.
Class Method Details
.command_code(code) ⇒ Object
subclasses must call this to register and declare their command code
41 42 43 |
# File 'lib/rtunnel/commands.rb', line 41 def self.command_code(code) registry.register self, code end |
.decode(io) ⇒ Object
Decode a Command instance from a IO / IOString.
70 71 72 73 74 75 76 77 |
# File 'lib/rtunnel/commands.rb', line 70 def self.decode(io) return nil unless code = io.getc klass = registry.class_for code.chr return nil unless klass command = klass.new command.initialize_from_io io end |
.printable_codes ⇒ Object
Printable string containing all the codes and their classes.
80 81 82 83 84 85 86 |
# File 'lib/rtunnel/commands.rb', line 80 def self.printable_codes printable = '' registry.codes_and_classes.each do |code_and_class| printable << "#{code_and_class.first}: #{code_and_class.last}\n" end return printable end |
.registry ⇒ Object
36 37 38 |
# File 'lib/rtunnel/commands.rb', line 36 def self.registry @@registry end |
Instance Method Details
#encode(io) ⇒ Object
Encode this command to a IO / IOString.
58 59 60 |
# File 'lib/rtunnel/commands.rb', line 58 def encode(io) io.write RTunnel::Command.registry.code_for(self.class) end |
#initialize_from_io(io) ⇒ Object
subclasses should override this and call super before performing their own initialization
53 54 55 |
# File 'lib/rtunnel/commands.rb', line 53 def initialize_from_io(io) return self end |
#to_encoded_str ⇒ Object
Produce a string with an encoding of this command.
63 64 65 66 67 |
# File 'lib/rtunnel/commands.rb', line 63 def to_encoded_str string_io = StringIO.new self.encode string_io string_io.string end |
#to_s ⇒ Object
subclasses should override this (and add to the result) to provide a debug string
47 48 49 |
# File 'lib/rtunnel/commands.rb', line 47 def to_s self.class.name end |