Dion Mendel wrote:
> Ramana Gollamudi wrote:
> 
>> I tried to see if bindata works but the documentation is unclear 
>> on whether it can handle multiple TLVS that may exist in different
>> combination in each packet?
> 
> I am the author of BinData.  Yes BinData handles optional TLVs
> (type-length-value).  You didn't specify if your TLVs are nested or
> flat.  An example of flat TLVs is given below.  Nested TLVs are
> slightly more complicated and require more code so I haven't provided
> an example.  Contact me offlist if you need more details.
> 
> Here is an example of a data format (MyFormat) supporting multiple,
> optional, unordered TLVs.
> 
> class CustomType1 < BinData::Record
>   int8   :a
>   int8   :b
>   ...
> end
> 
> ...
> 
> class Tlv < BinData::Record
>   endian :big
>   uint8  :t
>   uint32 :l
>   choice :v, :selection => :chooser do
>     string "unknown", :read_length => :l
>     custom_type_1 "v1"
>     custom_type_2 "v2"
>     ...
>   end
> 
>   def chooser
>     if t == xx and l == yy
>       "v1"
>     elsif t == ww and l == zz
>       ...
>     else
>       "unknown"
>     end
>   end
> end
> 
> class MyFormat < BinData::Record
>   ...
>   array :options, :type => :tlv,
>         :read_until => lambda { my_terminating_condition }
> end

Dion,

Thanks for clarifying the use of BinData for this. My protocol does not 
have nested TLVs. There are only flat TLVs. The message consists of a 
header and a bunch of different TLVs. The header has an identifier whose 
value identifies the message type. The TLVs are identified by the Type 
field that is the first byte in the TLVs.
I will try you choice - selection to see if this works.

Thanks,

Ramana
-- 
Posted via http://www.ruby-forum.com/.