binary strings encoded binary. Idx::Id instead of Symbol in Idx. parse_items only allowes Symbols as options.
This commit is contained in:
parent
6af5922217
commit
10645196d1
|
@ -127,22 +127,24 @@ class Knot::Conf
|
||||||
self.commit
|
self.commit
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_item k
|
def parse_item kv
|
||||||
case k
|
case kv
|
||||||
when Hash
|
when Hash
|
||||||
case k.keys.sort
|
r = {}
|
||||||
when %w[section], %w[id section], %w[item section], %w[id item section]
|
kv.each {|k,v| r[k.to_s.to_sym] = v }
|
||||||
k
|
case r.keys.sort
|
||||||
|
when %i[section], %i[id section], %i[item section], %i[id item section]
|
||||||
|
r
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Invalid Item-format"
|
raise ArgumentError, "Invalid Item-format: #{k}"
|
||||||
end
|
end
|
||||||
|
|
||||||
when Array
|
when Array
|
||||||
case k.length
|
case kv.length
|
||||||
when 1 then {section: k[0]}
|
when 1 then {section: kv[0]}
|
||||||
when 2 then {section: k[0], item: k[1]}
|
when 2 then {section: kv[0], item: kv[1]}
|
||||||
when 3 then {section: k[0], id: k[1], item: k[2]}
|
when 3 then {section: kv[0], id: kv[1], item: kv[2]}
|
||||||
else raise ArgumentError, "Invalid Item-format"
|
else raise ArgumentError, "Invalid Item-format: #{kv}"
|
||||||
end
|
end
|
||||||
|
|
||||||
when /\A
|
when /\A
|
||||||
|
@ -157,7 +159,7 @@ class Knot::Conf
|
||||||
{}
|
{}
|
||||||
|
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Invalid Item-format"
|
raise ArgumentError, "Invalid Item-format: #{kv}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -73,36 +73,71 @@ end
|
||||||
#end
|
#end
|
||||||
|
|
||||||
module Knot::Protocol::Idx
|
module Knot::Protocol::Idx
|
||||||
|
class Id
|
||||||
|
include Comparable
|
||||||
|
attr_reader :name, :code, :cname, :description
|
||||||
|
|
||||||
|
def initialize name, code, cname, description
|
||||||
|
raise ArgumentError, "Expecting Symbol for Idx::Id instead of: #{name.inspect}" if Symbol === name
|
||||||
|
raise ArgumentError, "Expecting Integer for Idx::Id instead of: #{code.inspect}" if 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 = [
|
Idx = [
|
||||||
:command, # 10, :CMD, # Control command name.
|
Id.new( :command, 0x10, :CMD, 'Control command name.'),
|
||||||
:flags, # 11, :FLAGS, # Control command flags.
|
Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'),
|
||||||
:error, # 12, :ERROR, # Error message.
|
Id.new( :error, 0x12, :ERROR, 'Error message.'),
|
||||||
:section, # 13, :SECTION, # Configuration section name.
|
Id.new( :section, 0x13, :SECTION, 'Configuration section name.'),
|
||||||
:item, # 14, :ITEM, # Configuration item name.
|
Id.new( :item, 0x14, :ITEM, 'Configuration item name.'),
|
||||||
:id, # 15, :ID, # Congiguration item identifier.
|
Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'),
|
||||||
:zone, # 16, :ZONE, # Zone name.
|
Id.new( :zone, 0x16, :ZONE, 'Zone name.'),
|
||||||
:owner, # 17, :OWNER, # Zone record owner
|
Id.new( :owner, 0x17, :OWNER, 'Zone record owner'),
|
||||||
:ttl, # 18, :TTL, # Zone record TTL.
|
Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'),
|
||||||
:type, # 19, :TYPE, # Zone record type name.
|
Id.new( :type, 0x19, :TYPE, 'Zone record type name.'),
|
||||||
:data, # 1a, :DATA, # Configuration item/zone record data.
|
Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'),
|
||||||
:filter, # 1b, :FILTER, # An option or a filter for output data processing.
|
Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'),
|
||||||
]
|
]
|
||||||
Name = {}
|
Name = {}
|
||||||
Code = {}
|
Code = {}
|
||||||
Idx.each_with_index do |v, i|
|
|
||||||
Code[0x10+i] = v
|
Idx.each do |id|
|
||||||
Name[v] = i
|
Code[id.to_i] = id
|
||||||
|
Name[id.to_sym] = id
|
||||||
end
|
end
|
||||||
def self.[] k
|
|
||||||
|
class <<self
|
||||||
|
def [] k
|
||||||
case k
|
case k
|
||||||
when Symbol
|
when Symbol
|
||||||
Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
||||||
when Integer
|
when Integer
|
||||||
Idx[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
Code[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Unknown Idx-Type: #{k}"
|
raise ArgumentError, "Unknown Idx-Type: #{k}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def each &exe
|
||||||
|
block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Knot::Protocol
|
class Knot::Protocol
|
||||||
|
@ -125,7 +160,7 @@ class Knot::Protocol
|
||||||
|
|
||||||
def snd sock: nil, **data
|
def snd sock: nil, **data
|
||||||
rsock = sock || @sock
|
rsock = sock || @sock
|
||||||
s = ''
|
s = ''.b
|
||||||
sock = StringIO.new s
|
sock = StringIO.new s
|
||||||
sock.write [1].pack( 'c')
|
sock.write [1].pack( 'c')
|
||||||
data[:flags] ||= ''
|
data[:flags] ||= ''
|
||||||
|
@ -135,7 +170,7 @@ class Knot::Protocol
|
||||||
end
|
end
|
||||||
sock.write [3].pack( 'c')
|
sock.write [3].pack( 'c')
|
||||||
sock.flush
|
sock.flush
|
||||||
STDERR.puts( {data: data, _: s}.inspect) if @debug
|
STDERR.puts( {send: data, _: s}.inspect) if @debug
|
||||||
rsock.write s
|
rsock.write s
|
||||||
rsock.flush
|
rsock.flush
|
||||||
end
|
end
|
||||||
|
@ -144,15 +179,15 @@ class Knot::Protocol
|
||||||
attr_reader :str
|
attr_reader :str
|
||||||
|
|
||||||
def initialize sock, str = nil
|
def initialize sock, str = nil
|
||||||
@str, @sock = str || '', sock
|
@str, @sock = str || ''.b, sock
|
||||||
end
|
end
|
||||||
|
|
||||||
def unpack pattern
|
def unpack pattern
|
||||||
IOUnpack.new(pattern).unpack self
|
IOUnpack.new( pattern).unpack self
|
||||||
end
|
end
|
||||||
|
|
||||||
def unpack1 pattern
|
def unpack1 pattern
|
||||||
IOUnpack.new(pattern).unpack1 self
|
IOUnpack.new( pattern).unpack1 self
|
||||||
end
|
end
|
||||||
|
|
||||||
def read n
|
def read n
|
||||||
|
@ -182,7 +217,7 @@ class Knot::Protocol
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
STDERR.puts( {rcvd: ret, read: sock.str}.inspect) if @debug
|
STDERR.puts( {rcvd: ret, _: sock.str}.inspect) if @debug
|
||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue