Class: Codec::BaseComposed
- Inherits:
-
Base
- Object
- Base
- Codec::BaseComposed
show all
- Defined in:
- lib/codec/composed.rb
Instance Attribute Summary
Attributes inherited from Base
#id
Instance Method Summary
collapse
Methods inherited from Base
#decode_with_length, #encode_with_length, #eval_length, #get_length, #get_sub_codecs
Constructor Details
Returns a new instance of BaseComposed.
3
4
5
6
|
# File 'lib/codec/composed.rb', line 3
def initialize(id)
@id = id
@subCodecs = []
end
|
Instance Method Details
#add_sub_codec(id_field, codec) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/codec/composed.rb', line 59
def add_sub_codec(id_field,codec)
if codec.nil?
raise InitializeException, "Invalid codec reference in subcodec #{id_field} for codec #{@id}"
end
@subCodecs << [id_field, codec]
end
|
#build_each_field(buf, length) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/codec/composed.rb', line 28
def build_each_field(buf,length)
msg = Field.new(@id)
working_buf = buf[0,length]
@subCodecs.each{|id,codec|
Logger.debug "Parsing struct field #{@id} : #{id}"
if working_buf.length == 0
Logger.debug "Not enough data to decode #{@id} : #{id}"
else
f,working_buf = codec.decode(working_buf)
f.set_id(id)
msg.add_sub_field(f)
end
}
return msg,working_buf
end
|
#build_field(buf, length) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/codec/composed.rb', line 44
def build_field(buf,length)
msg,working_buf = build_each_field(buf,length)
if working_buf.length > 0
if @length_unknown
@remain = working_buf
else
f = Field.new("PADDING")
f.set_value(working_buf.unpack("H*").first)
msg.add_sub_field(f)
end
end
return msg
end
|
#decode(buf) ⇒ Object
8
9
10
|
# File 'lib/codec/composed.rb', line 8
def decode(buf)
return build_each_field(buf,buf.length)
end
|
#encode(field) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/codec/composed.rb', line 12
def encode(field)
return "" if field.empty?
subfields = field.get_value
composed_encoder = subfields.zip(@subCodecs).collect {|sf,sc|
if sf.get_id != sc.first
raise EncodingException, "subfield #{sf.first} not correspond to subcodec #{sc.first}"
end
[sc.last,sf]
}
out = ""
composed_encoder.each do |subcodec,subfield|
out += subcodec.encode(subfield)
end
return out
end
|