knot-ruby 0.2.1 → 0.3.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: 431bf7db78b2a1b8e1698f53eec8ae8726ef3aca3a9c3b38c77f06ef592fd9c9
4
- data.tar.gz: 8dcf78f6703e9c1c00149f629684b1cda175f20f2148c9780cd4eead1a9e78fb
3
+ metadata.gz: 7dcc321520bfdad7567809f0e0a831205c27c32274cc28f34418216b8ccb7943
4
+ data.tar.gz: 8ddc6e7390566d2e310f1f97600d10f290957c51b8310d5575d92849be43b9f9
5
5
  SHA512:
6
- metadata.gz: 68642c8b64d37524a7ee4b53187e444dfe003fc5e47ee6208954b078c9849fabbfcd5080472a7bd42f2fa9721cac6060a04ff26233ba3ee891698cd4227ff919
7
- data.tar.gz: 7bcbe887741d86b4fdc09212ae92a8ffc08e824aaaf9ec82fa225e4faf3a5db9434ab99bfff14cc0e918f24f9df6ea792b074f66c1a58ba7a25f8b12d32c1dfd
6
+ metadata.gz: 7fd70face0a5d90c2bd95e7fbb950de2351f6d5877e571435602705ad61dff6fa00a91b593a899e7a956e783d42ff0b29f7321eba60ac05034d9e64ae36717e6
7
+ data.tar.gz: a78d11ca483eeb906b04901f71e2d6468d7066fc31c72b88e7c66d4707e24eacc9ae5a22cfeaa9fb35fa6ef3495606fe520c53eaab1e3043b3d8907352970155
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
 
@@ -159,21 +183,13 @@ class Knot::Protocol
159
183
 
160
184
  def snd sock: nil, **data
161
185
  rsock = sock || @sock
162
- #s = ''.b
163
- #sock = StringIO.new s
164
- #sock.write [1].pack( 'c')
186
+ data = Hash[ *data.map {|k,v| [k.to_sym, v]}.flatten]
165
187
  data[:flags] ||= ''
166
188
  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
189
+ Idx.
190
+ select {|n| p [n.to_sym, data[n.to_sym]]; data[n.to_sym] }.
191
+ map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v] }
192
+ s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b
177
193
  if 0 >= @logger.sev_threshold
178
194
  @logger.debug "send data #{data.inspect}"
179
195
  @logger.debug "send raw #{s.inspect}"
@@ -211,16 +227,16 @@ class Knot::Protocol
211
227
  loop do
212
228
  t = sock.unpack1 'c'
213
229
  case t
214
- when 0, 3
230
+ when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block]
215
231
  return ret
216
- when 1, 2
232
+ when Knot::Protocol::Types[:data], Knot::Protocol::Types[:extra]
217
233
  type = t
218
234
  ret.push( r = {})
219
235
  else
220
236
  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}"
237
+ i = Idx[t] or raise Knot::Errors::EINVAL, "Unknown index: #{t}"
222
238
  l = sock.unpack1 'n'
223
- r[i] = sock.read( l)
239
+ r[i.to_sym] = sock.read( l)
224
240
  end
225
241
  end
226
242
  ensure
data/lib/knot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Knot
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.1"
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Knauf