knot-ruby 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 102f6dc54dc3b034d4b71e890c921cdd3067ef32999cf92e829b56e6cf661266
4
- data.tar.gz: 847a1e73410dd0dbe17793c7c2819e22879dfbe47f728ec0625e02b88dcb7c2a
3
+ metadata.gz: 431bf7db78b2a1b8e1698f53eec8ae8726ef3aca3a9c3b38c77f06ef592fd9c9
4
+ data.tar.gz: 8dcf78f6703e9c1c00149f629684b1cda175f20f2148c9780cd4eead1a9e78fb
5
5
  SHA512:
6
- metadata.gz: e87bfff66910c223c3046dbc0ea63929d7c60d62434e06f1dccac5f2bca3ffc8b9e0652b32c1f4137ff6912cca50f26855dd989a64ec8f2a1c2f741f34307637
7
- data.tar.gz: bcd68e1fa196ec799970697180b2dd375c6816da752c721dab9b0bf4bb82405d5909f090f5f10185713ee4c28166af0a64f103ff926ede60a519fc41a13a2e6a
6
+ metadata.gz: 68642c8b64d37524a7ee4b53187e444dfe003fc5e47ee6208954b078c9849fabbfcd5080472a7bd42f2fa9721cac6060a04ff26233ba3ee891698cd4227ff919
7
+ data.tar.gz: 7bcbe887741d86b4fdc09212ae92a8ffc08e824aaaf9ec82fa225e4faf3a5db9434ab99bfff14cc0e918f24f9df6ea792b074f66c1a58ba7a25f8b12d32c1dfd
@@ -127,37 +127,39 @@ class Knot::Conf
127
127
  self.commit
128
128
  end
129
129
 
130
- def parse_item k
131
- case k
130
+ def parse_item kv
131
+ case kv
132
132
  when Hash
133
- case k.keys.sort
134
- when %w[section], %w[id section], %w[item section], %w[id item section]
135
- k
133
+ r = {}
134
+ kv.each {|k,v| r[k.to_s.to_sym] = v }
135
+ case r.keys.sort
136
+ when %i[section], %i[id section], %i[item section], %i[id item section]
137
+ r
136
138
  else
137
- raise ArgumentError, "Invalid Item-format"
139
+ raise ArgumentError, "Invalid Item-format: #{k}"
138
140
  end
139
141
 
140
142
  when Array
141
- case k.length
142
- when 1 then {section: k[0]}
143
- when 2 then {section: k[0], item: k[1]}
144
- when 3 then {section: k[0], id: k[1], item: k[2]}
145
- else raise ArgumentError, "Invalid Item-format"
143
+ case kv.length
144
+ when 1 then {section: kv[0]}
145
+ when 2 then {section: kv[0], item: kv[1]}
146
+ when 3 then {section: kv[0], id: kv[1], item: kv[2]}
147
+ else raise ArgumentError, "Invalid Item-format: #{kv}"
146
148
  end
147
149
 
148
150
  when /\A
149
- (?<section> [a-z0-9_-]+ )
150
- (?: \[ (?<id> [a-z0-9_.-]+) \] )?
151
- (?: \. (?<item>[a-z0-9_-]+) )?
151
+ (?<section> [a-z0-9_-]+ )
152
+ (?: \[ (?<id> [a-z0-9_.-]+) \] )?
153
+ (?: \. (?<item>[a-z0-9_-]+) )?
152
154
  \z/xi
153
155
 
154
- $~.named_captures.delete_if {|_,v| v.nil? }
156
+ $~.named_captures.delete_if {|_,v| v.nil? }
155
157
 
156
158
  when nil
157
159
  {}
158
160
 
159
161
  else
160
- raise ArgumentError, "Invalid Item-format"
162
+ raise ArgumentError, "Invalid Item-format: #{kv}"
161
163
  end
162
164
  end
163
165
 
data/lib/knot/protocol.rb CHANGED
@@ -53,7 +53,7 @@ module Knot::Protocol::Type
53
53
  end
54
54
 
55
55
  #class Knot::KnotC
56
- # attr_accessor :debug, :binary
56
+ # attr_accessor :binary
57
57
  #
58
58
  # def initialize path = nil, binary: nil
59
59
  # @path = path
@@ -73,43 +73,77 @@ end
73
73
  #end
74
74
 
75
75
  module Knot::Protocol::Idx
76
+ class Id
77
+ include Comparable
78
+ attr_reader :name, :code, :cname, :description
79
+
80
+ def initialize name, code, cname, description
81
+ raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name
82
+ raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code
83
+ @name, @code, @cname, @description = name, code, cname, description
84
+ freeze
85
+ end
86
+
87
+ def === x
88
+ case x
89
+ when self.class then self == x
90
+ when Symbol then @name == x
91
+ when String then @name == x.to_sym
92
+ when Integer then @code == x
93
+ else nil
94
+ end
95
+ end
96
+
97
+ def <=>( x) @id <=> x.id end
98
+ def to_s() @name.to_s end
99
+ def to_sym() @name end
100
+ def to_i() @code end
101
+ end
102
+
76
103
  Idx = [
77
- :command, # 10, :CMD, # Control command name.
78
- :flags, # 11, :FLAGS, # Control command flags.
79
- :error, # 12, :ERROR, # Error message.
80
- :section, # 13, :SECTION, # Configuration section name.
81
- :item, # 14, :ITEM, # Configuration item name.
82
- :id, # 15, :ID, # Congiguration item identifier.
83
- :zone, # 16, :ZONE, # Zone name.
84
- :owner, # 17, :OWNER, # Zone record owner
85
- :ttl, # 18, :TTL, # Zone record TTL.
86
- :type, # 19, :TYPE, # Zone record type name.
87
- :data, # 1a, :DATA, # Configuration item/zone record data.
88
- :filter, # 1b, :FILTER, # An option or a filter for output data processing.
104
+ Id.new( :command, 0x10, :CMD, 'Control command name.'),
105
+ Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'),
106
+ Id.new( :error, 0x12, :ERROR, 'Error message.'),
107
+ Id.new( :section, 0x13, :SECTION, 'Configuration section name.'),
108
+ Id.new( :item, 0x14, :ITEM, 'Configuration item name.'),
109
+ Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'),
110
+ Id.new( :zone, 0x16, :ZONE, 'Zone name.'),
111
+ Id.new( :owner, 0x17, :OWNER, 'Zone record owner'),
112
+ Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'),
113
+ Id.new( :type, 0x19, :TYPE, 'Zone record type name.'),
114
+ Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'),
115
+ Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'),
89
116
  ]
90
117
  Name = {}
91
118
  Code = {}
92
- Idx.each_with_index do |v, i|
93
- Code[0x10+i] = v
94
- Name[v] = i
119
+
120
+ Idx.each do |id|
121
+ Code[id.to_i] = id
122
+ Name[id.to_sym] = id
95
123
  end
96
- def self.[] k
97
- case k
98
- when Symbol
99
- Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
100
- when Integer
101
- Idx[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
102
- else
103
- raise ArgumentError, "Unknown Idx-Type: #{k}"
124
+
125
+ class <<self
126
+ def [] k
127
+ case k
128
+ when Symbol
129
+ Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
130
+ when Integer
131
+ Code[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
132
+ else
133
+ raise ArgumentError, "Unknown Idx-Type: #{k}"
134
+ end
135
+ end
136
+
137
+ def each &exe
138
+ block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
104
139
  end
105
140
  end
106
141
  end
107
142
 
108
143
  class Knot::Protocol
109
- attr_reader :sock, :conf, :zones
110
- attr_accessor :debug
144
+ attr_reader :sock, :conf, :zones, :logger
111
145
 
112
- def initialize path_or_sock = nil
146
+ def initialize path_or_sock = nil, logger: nil
113
147
  case path_or_sock
114
148
  when String, Pathname
115
149
  @sock = UNIXSocket.new path_or_sock.to_s
@@ -118,24 +152,32 @@ class Knot::Protocol
118
152
  when nil
119
153
  @sock = UNIXSocket.new '/run/knot/knot.sock'
120
154
  end
121
- @debug = false
155
+ @logger = logger || Logger.new(STDERR)
122
156
  @conf = Knot::Conf.new self
123
157
  @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
124
158
  end
125
159
 
126
160
  def snd sock: nil, **data
127
161
  rsock = sock || @sock
128
- s = ''
129
- sock = StringIO.new s
130
- sock.write [1].pack( 'c')
162
+ #s = ''.b
163
+ #sock = StringIO.new s
164
+ #sock.write [1].pack( 'c')
131
165
  data[:flags] ||= ''
132
- Idx::Idx.each_with_index do |n, i|
133
- v = data[n]&.to_s
134
- sock.write [0x10+i, v.size, v].pack( 'c na*') if v
166
+ ds =
167
+ Idx::Idx.
168
+ select {|n| data[n.to_sym] }.
169
+ map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] }
170
+ s = (ds.flatten+[3]).pack( ('c na*'*ds.length)+'c').b
171
+ #Idx::Idx.each do |n|
172
+ # v = data[n.to_sym]&.to_s&.b
173
+ # sock.write [n.to_i, v.size, v].pack( 'c na*') if v
174
+ #end
175
+ #sock.write [3].pack( 'c')
176
+ #sock.flush
177
+ if 0 >= @logger.sev_threshold
178
+ @logger.debug "send data #{data.inspect}"
179
+ @logger.debug "send raw #{s.inspect}"
135
180
  end
136
- sock.write [3].pack( 'c')
137
- sock.flush
138
- STDERR.puts( {data: data, _: s}.inspect) if @debug
139
181
  rsock.write s
140
182
  rsock.flush
141
183
  end
@@ -144,28 +186,28 @@ class Knot::Protocol
144
186
  attr_reader :str
145
187
 
146
188
  def initialize sock, str = nil
147
- @str, @sock = str || '', sock
189
+ @str, @sock = str || ''.b, sock
148
190
  end
149
191
 
150
192
  def unpack pattern
151
- IOUnpack.new(pattern).unpack self
193
+ IOUnpack.new( pattern).unpack self
152
194
  end
153
195
 
154
196
  def unpack1 pattern
155
- IOUnpack.new(pattern).unpack1 self
197
+ IOUnpack.new( pattern).unpack1 self
156
198
  end
157
199
 
158
200
  def read n
159
201
  s = @sock.read n
160
- @str.insert -1, s
161
- s
202
+ @str.insert -1, s unless s.nil?
203
+ s || ''
162
204
  end
163
205
  end
164
206
 
165
207
  def rcv sock: nil
166
208
  ret, r = [], nil
167
209
  sock = sock || @sock
168
- sock = RecordIO.new sock if @debug
210
+ sock = RecordIO.new sock if 0 >= @logger.sev_threshold
169
211
  loop do
170
212
  t = sock.unpack1 'c'
171
213
  case t
@@ -182,7 +224,10 @@ class Knot::Protocol
182
224
  end
183
225
  end
184
226
  ensure
185
- STDERR.puts( {rcvd: ret, read: sock.str}.inspect) if @debug
227
+ if RecordIO === sock
228
+ @logger.debug "rcvd raw #{sock.str.inspect}"
229
+ @logger.debug "rcvd data #{ret.inspect}"
230
+ end
186
231
  ret
187
232
  end
188
233
 
data/lib/knot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Knot
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Knauf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-28 00:00:00.000000000 Z
11
+ date: 2022-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: iounpack
@@ -64,8 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubyforge_project:
68
- rubygems_version: 2.7.6.2
67
+ rubygems_version: 3.2.5
69
68
  signing_key:
70
69
  specification_version: 4
71
70
  summary: Provides interface to knot-server.