From e9321d4de66f0bcaba67d8a9e15f7d8444280408 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 22:44:06 +0100 Subject: [PATCH 1/5] Protocol-Types like Idx. Codes for sharing code. --- lib/knot/protocol.rb | 146 +++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 61 deletions(-) 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 < Date: Tue, 13 Dec 2022 22:45:39 +0100 Subject: [PATCH 2/5] v0.3.0 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index a8d42ca..a66d07a 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.2.1" + VERSION = "0.3.0" end From 12639f766c85709309b579709f23e3e0127e8a16 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:38:43 +0100 Subject: [PATCH 3/5] Knot::Protocol#snd supports strings as key, too (calls to_sym) --- lib/knot/protocol.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index cf1abf2..48dcfea 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -183,21 +183,13 @@ class Knot::Protocol def snd sock: nil, **data rsock = sock || @sock - #s = ''.b - #sock = StringIO.new s - #sock.write [1].pack( 'c') + data = Hash[ *data.map {|k,v| [k.to_sym, v]}.flatten] data[:flags] ||= '' ds = Idx. - select {|n| data[n.to_sym] }. - map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] } + select {|n| p [n.to_sym, data[n.to_sym]]; data[n.to_sym] }. + map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v] } s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b - #Idx.each do |n| - # v = data[n.to_sym]&.to_s&.b - # sock.write [n.to_i, v.size, v].pack( 'c na*') if v - #end - #sock.write [3].pack( 'c') - #sock.flush if 0 >= @logger.sev_threshold @logger.debug "send data #{data.inspect}" @logger.debug "send raw #{s.inspect}" From 95cabef56784298d709fff8704d5297613c5a6c2 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:41:16 +0100 Subject: [PATCH 4/5] v0.3.1 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index a66d07a..b29fb5c 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.3.0" + VERSION = "0.3.1" end From b21f7c319de89f346a15662f95ecfd0bdfdf1688 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:43:18 +0100 Subject: [PATCH 5/5] v0.3.2 (debugging removed) --- lib/knot/protocol.rb | 2 +- lib/knot/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 48dcfea..e05a112 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -187,7 +187,7 @@ class Knot::Protocol data[:flags] ||= '' ds = Idx. - select {|n| p [n.to_sym, data[n.to_sym]]; data[n.to_sym] }. + select {|n| data[n.to_sym] }. map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v] } s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b if 0 >= @logger.sev_threshold diff --git a/lib/knot/version.rb b/lib/knot/version.rb index b29fb5c..46de30f 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.3.1" + VERSION = "0.3.2" end