Class: RTunnel::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rtunnel/commands.rb

Defined Under Namespace

Classes: Registry

Constant Summary collapse

@@registry =
Registry.new

Class Method Summary collapse

Instance Method Summary collapse

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_codesObject

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

.registryObject



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_strObject

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_sObject

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