diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 98bf7c6..cf1abf2 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -1,5 +1,7 @@ require 'iounpack' -require 'stringio' +require 'pathname' +require 'socket' +require 'logger' require_relative 'errors' module Knot @@ -72,71 +74,93 @@ end # end #end -module Knot::Protocol::Idx - class Id - include Comparable - attr_reader :name, :code, :cname, :description +class Knot::Protocol::Code + include Comparable + attr_reader :name, :code, :cname, :description - def initialize name, code, cname, description - raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name - raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code - @name, @code, @cname, @description = name, code, cname, description - freeze - end - - def === x - case x - when self.class then self == x - when Symbol then @name == x - when String then @name == x.to_sym - when Integer then @code == x - else nil - end - end - - def <=>( x) @id <=> x.id end - def to_s() @name.to_s end - def to_sym() @name end - def to_i() @code end + def initialize name, code, cname, description + raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name + raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code + @name, @code, @cname, @description = name, code, cname, description + freeze end - Idx = [ - Id.new( :command, 0x10, :CMD, 'Control command name.'), - Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'), - Id.new( :error, 0x12, :ERROR, 'Error message.'), - Id.new( :section, 0x13, :SECTION, 'Configuration section name.'), - Id.new( :item, 0x14, :ITEM, 'Configuration item name.'), - Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'), - Id.new( :zone, 0x16, :ZONE, 'Zone name.'), - Id.new( :owner, 0x17, :OWNER, 'Zone record owner'), - Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), - Id.new( :type, 0x19, :TYPE, 'Zone record type name.'), - Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), - Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), + def === x + case x + when self.class then @id == x.id + when Symbol then @name == x + when String then @name == x.to_sym + when Integer then @code == x + else nil + end + end + + def <=>( x) @id <=> x.id end + def to_s() @name.to_s end + def to_sym() @name end + def to_i() @code end +end + +module Knot::Protocol::Codes + include Enumerable + def [] k + case k + when Symbol + self::Name[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}" + when Integer + self::Code[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}" + else + raise ArgumentError, "Unknown Codes-Type: #{k}" + end + end + + def each &exe + block_given? ? self::Codes.each( &exe) : self::Codes.to_enum( :each) + end +end + +module Knot::Protocol::Idx + extend Knot::Protocol::Codes + + Codes = [ + Knot::Protocol::Code.new( :command, 0x10, :CMD, 'Control command name.'), + Knot::Protocol::Code.new( :flags, 0x11, :FLAGS, 'Control command flags.'), + Knot::Protocol::Code.new( :error, 0x12, :ERROR, 'Error message.'), + Knot::Protocol::Code.new( :section, 0x13, :SECTION, 'Configuration section name.'), + Knot::Protocol::Code.new( :item, 0x14, :ITEM, 'Configuration item name.'), + Knot::Protocol::Code.new( :id, 0x15, :ID, 'Congiguration item identifier.'), + Knot::Protocol::Code.new( :zone, 0x16, :ZONE, 'Zone name.'), + Knot::Protocol::Code.new( :owner, 0x17, :OWNER, 'Zone record owner'), + Knot::Protocol::Code.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), + Knot::Protocol::Code.new( :type, 0x19, :TYPE, 'Zone record type name.'), + Knot::Protocol::Code.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), + Knot::Protocol::Code.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), ] Name = {} Code = {} - Idx.each do |id| + Codes.each do |id| Code[id.to_i] = id Name[id.to_sym] = id end +end - class <