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 +4 -4
- data/lib/knot/protocol.rb +88 -72
- 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: 7dcc321520bfdad7567809f0e0a831205c27c32274cc28f34418216b8ccb7943
|
4
|
+
data.tar.gz: 8ddc6e7390566d2e310f1f97600d10f290957c51b8310d5575d92849be43b9f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 '
|
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
|
|
@@ -159,21 +183,13 @@ class Knot::Protocol
|
|
159
183
|
|
160
184
|
def snd sock: nil, **data
|
161
185
|
rsock = sock || @sock
|
162
|
-
|
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
|
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 =
|
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
|
230
|
+
when Knot::Protocol::Types[:end], Knot::Protocol::Types[:block]
|
215
231
|
return ret
|
216
|
-
when
|
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
|
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