diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index e05a112..98bf7c6 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -1,7 +1,5 @@ require 'iounpack' -require 'pathname' -require 'socket' -require 'logger' +require 'stringio' require_relative 'errors' module Knot @@ -74,93 +72,71 @@ end # end #end -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 @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 + class Id + include Comparable + attr_reader :name, :code, :cname, :description - 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.'), + 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 + 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.'), ] Name = {} Code = {} - Codes.each do |id| + Idx.each do |id| Code[id.to_i] = id Name[id.to_sym] = id end -end -module Knot::Protocol::Types - extend Knot::Protocol::Codes + class <= @logger.sev_threshold @logger.debug "send data #{data.inspect}" @logger.debug "send raw #{s.inspect}" @@ -227,16 +211,16 @@ class Knot::Protocol loop do t = sock.unpack1 'c' case t - when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block] + when 0, 3 return ret - when Knot::Protocol::Types[:data], Knot::Protocol::Types[:extra] + when 1, 2 type = t ret.push( r = {}) else raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty? - i = Idx[t] or raise Knot::Errors::EINVAL, "Unknown index: #{t}" + i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}" l = sock.unpack1 'n' - r[i.to_sym] = sock.read( l) + r[i] = sock.read( l) end end ensure diff --git a/lib/knot/version.rb b/lib/knot/version.rb index 46de30f..a8d42ca 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.3.2" + VERSION = "0.2.1" end