knot-ruby 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/knot/protocol.rb +86 -62
- data/lib/knot/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 955cc51d2b5bc2c9057fbac7501aa4cd4ddae6a41769a414d9f3dad99da233ec
|
4
|
+
data.tar.gz: 7e63b760071edf2236e09e2148a447d6f3a6ea11ddef129fc98e6160ea43217e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 '
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
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
|
-
|
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
|
-
|
126
|
-
|
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
|
-
|
138
|
-
|
139
|
-
|
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
|
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 =
|
171
|
-
#Idx
|
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
|
238
|
+
when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block]
|
215
239
|
return ret
|
216
|
-
when
|
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
|
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