knot-ruby 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 431bf7db78b2a1b8e1698f53eec8ae8726ef3aca3a9c3b38c77f06ef592fd9c9
4
- data.tar.gz: 8dcf78f6703e9c1c00149f629684b1cda175f20f2148c9780cd4eead1a9e78fb
3
+ metadata.gz: 955cc51d2b5bc2c9057fbac7501aa4cd4ddae6a41769a414d9f3dad99da233ec
4
+ data.tar.gz: 7e63b760071edf2236e09e2148a447d6f3a6ea11ddef129fc98e6160ea43217e
5
5
  SHA512:
6
- metadata.gz: 68642c8b64d37524a7ee4b53187e444dfe003fc5e47ee6208954b078c9849fabbfcd5080472a7bd42f2fa9721cac6060a04ff26233ba3ee891698cd4227ff919
7
- data.tar.gz: 7bcbe887741d86b4fdc09212ae92a8ffc08e824aaaf9ec82fa225e4faf3a5db9434ab99bfff14cc0e918f24f9df6ea792b074f66c1a58ba7a25f8b12d32c1dfd
6
+ metadata.gz: 9c8056ca3f3f7f2b97eb2dbc57047f2d6222a76e2b1d1794a07b016037cb976e6c9d6510e236f26aeb6d9182d98a82ee27f141f5e7bd291ec2d0992847829b6f
7
+ data.tar.gz: 8f85e0815f1e6bbcc39c357be9ee3d3922201f9fa58aafa576e6d69db1dd8a2d18601d547db2299621fe2bd223745a91987fc6b8a986634e1ec63dd94b8c7811
data/lib/knot/protocol.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'iounpack'
2
- require 'stringio'
2
+ require 'pathname'
3
+ require 'socket'
4
+ require 'logger'
3
5
  require_relative 'errors'
4
6
 
5
7
  module Knot
@@ -72,71 +74,93 @@ end
72
74
  # end
73
75
  #end
74
76
 
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
77
+ class Knot::Protocol::Code
78
+ include Comparable
79
+ attr_reader :name, :code, :cname, :description
80
+
81
+ def initialize name, code, cname, description
82
+ raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name
83
+ raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code
84
+ @name, @code, @cname, @description = name, code, cname, description
85
+ freeze
86
+ end
87
+
88
+ def === x
89
+ case x
90
+ when self.class then @id == x.id
91
+ when Symbol then @name == x
92
+ when String then @name == x.to_sym
93
+ when Integer then @code == x
94
+ else nil
95
+ end
96
+ end
97
+
98
+ def <=>( x) @id <=> x.id end
99
+ def to_s() @name.to_s end
100
+ def to_sym() @name end
101
+ def to_i() @code end
102
+ end
103
+
104
+ module Knot::Protocol::Codes
105
+ include Enumerable
106
+ def [] k
107
+ case k
108
+ when Symbol
109
+ self::Name[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}"
110
+ when Integer
111
+ self::Code[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}"
112
+ else
113
+ raise ArgumentError, "Unknown Codes-Type: #{k}"
95
114
  end
115
+ end
116
+
117
+ def each &exe
118
+ block_given? ? self::Codes.each( &exe) : self::Codes.to_enum( :each)
119
+ end
120
+ end
96
121
 
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
-
103
- Idx = [
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.'),
122
+ module Knot::Protocol::Idx
123
+ extend Knot::Protocol::Codes
124
+
125
+ Codes = [
126
+ Knot::Protocol::Code.new( :command, 0x10, :CMD, 'Control command name.'),
127
+ Knot::Protocol::Code.new( :flags, 0x11, :FLAGS, 'Control command flags.'),
128
+ Knot::Protocol::Code.new( :error, 0x12, :ERROR, 'Error message.'),
129
+ Knot::Protocol::Code.new( :section, 0x13, :SECTION, 'Configuration section name.'),
130
+ Knot::Protocol::Code.new( :item, 0x14, :ITEM, 'Configuration item name.'),
131
+ Knot::Protocol::Code.new( :id, 0x15, :ID, 'Congiguration item identifier.'),
132
+ Knot::Protocol::Code.new( :zone, 0x16, :ZONE, 'Zone name.'),
133
+ Knot::Protocol::Code.new( :owner, 0x17, :OWNER, 'Zone record owner'),
134
+ Knot::Protocol::Code.new( :ttl, 0x18, :TTL, 'Zone record TTL.'),
135
+ Knot::Protocol::Code.new( :type, 0x19, :TYPE, 'Zone record type name.'),
136
+ Knot::Protocol::Code.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'),
137
+ Knot::Protocol::Code.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'),
116
138
  ]
117
139
  Name = {}
118
140
  Code = {}
119
141
 
120
- Idx.each do |id|
142
+ Codes.each do |id|
121
143
  Code[id.to_i] = id
122
144
  Name[id.to_sym] = id
123
145
  end
146
+ end
124
147
 
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
148
+ module Knot::Protocol::Types
149
+ extend Knot::Protocol::Codes
136
150
 
137
- def each &exe
138
- block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
139
- end
151
+ Codes = [
152
+ Knot::Protocol::Code.new( :end, 0x00, :END, 'Type END.'),
153
+ Knot::Protocol::Code.new( :data, 0x01, :DATA, 'Type DATA.'),
154
+ Knot::Protocol::Code.new( :extra, 0x02, :EXTRA, 'Type EXTRA.'),
155
+ Knot::Protocol::Code.new( :block, 0x03, :BLOCK, 'Type BLOCK.'),
156
+ ]
157
+
158
+ Name = {}
159
+ Code = {}
160
+
161
+ Codes.each do |id|
162
+ Code[id.to_i] = id
163
+ Name[id.to_sym] = id
140
164
  end
141
165
  end
142
166
 
@@ -164,11 +188,11 @@ class Knot::Protocol
164
188
  #sock.write [1].pack( 'c')
165
189
  data[:flags] ||= ''
166
190
  ds =
167
- Idx::Idx.
191
+ Idx.
168
192
  select {|n| data[n.to_sym] }.
169
193
  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|
194
+ s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b
195
+ #Idx.each do |n|
172
196
  # v = data[n.to_sym]&.to_s&.b
173
197
  # sock.write [n.to_i, v.size, v].pack( 'c na*') if v
174
198
  #end
@@ -211,16 +235,16 @@ class Knot::Protocol
211
235
  loop do
212
236
  t = sock.unpack1 'c'
213
237
  case t
214
- when 0, 3
238
+ when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block]
215
239
  return ret
216
- when 1, 2
240
+ when Knot::Protocol::Types[:data], Knot::Protocol::Types[:extra]
217
241
  type = t
218
242
  ret.push( r = {})
219
243
  else
220
244
  raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty?
221
- i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}"
245
+ i = Idx[t] or raise Knot::Errors::EINVAL, "Unknown index: #{t}"
222
246
  l = sock.unpack1 'n'
223
- r[i] = sock.read( l)
247
+ r[i.to_sym] = sock.read( l)
224
248
  end
225
249
  end
226
250
  ensure
data/lib/knot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Knot
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Knauf