Protocol-Types like Idx. Codes for sharing code.
This commit is contained in:
parent
2255c784bc
commit
e9321d4de6
|
@ -1,5 +1,7 @@
|
||||||
require 'iounpack'
|
require 'iounpack'
|
||||||
require 'stringio'
|
require 'pathname'
|
||||||
|
require 'socket'
|
||||||
|
require 'logger'
|
||||||
require_relative 'errors'
|
require_relative 'errors'
|
||||||
|
|
||||||
module Knot
|
module Knot
|
||||||
|
@ -72,71 +74,93 @@ end
|
||||||
# end
|
# end
|
||||||
#end
|
#end
|
||||||
|
|
||||||
module Knot::Protocol::Idx
|
class Knot::Protocol::Code
|
||||||
class Id
|
include Comparable
|
||||||
include Comparable
|
attr_reader :name, :code, :cname, :description
|
||||||
attr_reader :name, :code, :cname, :description
|
|
||||||
|
|
||||||
def initialize 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 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
|
raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code
|
||||||
@name, @code, @cname, @description = name, code, cname, description
|
@name, @code, @cname, @description = name, code, cname, description
|
||||||
freeze
|
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
|
end
|
||||||
|
|
||||||
Idx = [
|
def === x
|
||||||
Id.new( :command, 0x10, :CMD, 'Control command name.'),
|
case x
|
||||||
Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'),
|
when self.class then @id == x.id
|
||||||
Id.new( :error, 0x12, :ERROR, 'Error message.'),
|
when Symbol then @name == x
|
||||||
Id.new( :section, 0x13, :SECTION, 'Configuration section name.'),
|
when String then @name == x.to_sym
|
||||||
Id.new( :item, 0x14, :ITEM, 'Configuration item name.'),
|
when Integer then @code == x
|
||||||
Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'),
|
else nil
|
||||||
Id.new( :zone, 0x16, :ZONE, 'Zone name.'),
|
end
|
||||||
Id.new( :owner, 0x17, :OWNER, 'Zone record owner'),
|
end
|
||||||
Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'),
|
|
||||||
Id.new( :type, 0x19, :TYPE, 'Zone record type name.'),
|
def <=>( x) @id <=> x.id end
|
||||||
Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'),
|
def to_s() @name.to_s end
|
||||||
Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'),
|
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 = {}
|
Name = {}
|
||||||
Code = {}
|
Code = {}
|
||||||
|
|
||||||
Idx.each do |id|
|
Codes.each do |id|
|
||||||
Code[id.to_i] = id
|
Code[id.to_i] = id
|
||||||
Name[id.to_sym] = id
|
Name[id.to_sym] = id
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class <<self
|
module Knot::Protocol::Types
|
||||||
def [] k
|
extend Knot::Protocol::Codes
|
||||||
case k
|
|
||||||
when Symbol
|
|
||||||
Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
|
||||||
when Integer
|
|
||||||
Code[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
|
||||||
else
|
|
||||||
raise ArgumentError, "Unknown Idx-Type: #{k}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def each &exe
|
Codes = [
|
||||||
block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
|
Knot::Protocol::Code.new( :end, 0x00, :END, 'Type END.'),
|
||||||
end
|
Knot::Protocol::Code.new( :data, 0x01, :DATA, 'Type DATA.'),
|
||||||
|
Knot::Protocol::Code.new( :extra, 0x02, :EXTRA, 'Type EXTRA.'),
|
||||||
|
Knot::Protocol::Code.new( :block, 0x03, :BLOCK, 'Type BLOCK.'),
|
||||||
|
]
|
||||||
|
|
||||||
|
Name = {}
|
||||||
|
Code = {}
|
||||||
|
|
||||||
|
Codes.each do |id|
|
||||||
|
Code[id.to_i] = id
|
||||||
|
Name[id.to_sym] = id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -164,11 +188,11 @@ class Knot::Protocol
|
||||||
#sock.write [1].pack( 'c')
|
#sock.write [1].pack( 'c')
|
||||||
data[:flags] ||= ''
|
data[:flags] ||= ''
|
||||||
ds =
|
ds =
|
||||||
Idx::Idx.
|
Idx.
|
||||||
select {|n| 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 ] }
|
map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] }
|
||||||
s = (ds.flatten+[3]).pack( ('c na*'*ds.length)+'c').b
|
s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b
|
||||||
#Idx::Idx.each do |n|
|
#Idx.each do |n|
|
||||||
# v = data[n.to_sym]&.to_s&.b
|
# v = data[n.to_sym]&.to_s&.b
|
||||||
# sock.write [n.to_i, v.size, v].pack( 'c na*') if v
|
# sock.write [n.to_i, v.size, v].pack( 'c na*') if v
|
||||||
#end
|
#end
|
||||||
|
@ -211,16 +235,16 @@ class Knot::Protocol
|
||||||
loop do
|
loop do
|
||||||
t = sock.unpack1 'c'
|
t = sock.unpack1 'c'
|
||||||
case t
|
case t
|
||||||
when 0, 3
|
when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block]
|
||||||
return ret
|
return ret
|
||||||
when 1, 2
|
when Knot::Protocol::Types[:data], Knot::Protocol::Types[:extra]
|
||||||
type = t
|
type = t
|
||||||
ret.push( r = {})
|
ret.push( r = {})
|
||||||
else
|
else
|
||||||
raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty?
|
raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty?
|
||||||
i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}"
|
i = Idx[t] or raise Knot::Errors::EINVAL, "Unknown index: #{t}"
|
||||||
l = sock.unpack1 'n'
|
l = sock.unpack1 'n'
|
||||||
r[i] = sock.read( l)
|
r[i.to_sym] = sock.read( l)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
|
|
Loading…
Reference in a new issue