OpenTTCN/Knowledge base/Writing Custom Codec Component
From OpenTTCN
Home | Developer's corner | Knowledge base | Working documents | Documentation | OpenTTCN IDE | Tutorials | Training | How do I | Frequently asked questions | Technical support |
Writing Custom Codec Component
This article discusses creation of custom codec components using OpenTTCN SDK for C# and OpenTTCN SDK for C++.
Codec components are fundamental building blocks used in creation of complex heterogeneous codecs and adapters.
A codec component typically implements homogeneous set of encoding rules, for example, BER encoding rules only (BER Codec Component), XML encoding rules only (XML Codec Component), or PER encoding rules only (PER Codec Component). A typical adapter can then freely mix several codec components in one application to satisfy target protocol heterogeneity requirements, where one part of the frame is coded for example using BER and another part of the frame is coded using XML. Complex assemblies of codec components can be conveniently implemented and used.
Each codec component has name and version at the very least. A codec component name is used to retrieve proper codec component when it is instantiated. A codec component may optionally provide facilities for instance parameterization through its SetProperty() method, allowing creation of more generic and more powerful codec components.
It is also possible to develop a custom codec component that is a composite of several codec components.
In C#, a codec component gets automatically plugged into the adapter assembly or binary once developed and is instantly available with no extra effort - all you have to do is to follow certain design conventions when developing codec component class. It may even come from another assembly as is the case with codec components coming with OpenTTCN SDK for C#.
In C++, a codec component factory has to be registered using an SDK method call in order for codec component implementation to become visible to the rest of the adapter code.
Contents |
Example frame format definition
In this article we develop a very simple encoder and decoder that expects a fixed frame format described as follows:
1 octet - frame type tag (in the range of 0..255)
2 octets - content length in big endian format (always a sequence of 0x00 0x04 octets)
4 octets - frame content
Here is the corresponding set of TTCN-3 types:
type integer Int8(0..255);
type integer Int16(0..65535);
type octetstring Oct4 length(4);
type record Message
{
Int8 tag,
Int16 length_,
Oct4 content
}
Requirements for implementation
A custom codec component written in C# must at the very least satisfy the following requirements:
- it must implement the
OpenTTCN.Sdk.ICodecComponentinterface; - it must be marked with the
OpenTTCN.Sdk.CodecComponentattribute to indicate that this class implements a codec component; - combination of its
ComponentName+ComponentVersionproperties must form a unique URI that uniquely identifies this codec component in the test setup; - it must implement
Encode()andDecode()methods.
No additional factory method registration is required to make codec component class written in C# available to the rest of the adapter - a codec component complying with the above requirements is instantly available after it is compiled.
A custom codec component written in C++ must at the very least satisfy the following requirements:
- it must implement the
OpenTTCN::SDK::CodecComponentabstract base class pure virtual methods; - combination of its
getComponentName()+getComponentVersion()methods must form a unique URI that uniquely identifies this codec component in the test setup; - it must implement
encode()anddecode()methods; - its factory method must be registered using
OpenTTCN::SDK::StartHereCD::registerCodecComponentFactory()call.
Implementation
Source code of the example custom codec component is presented below.
Observe the use of EncodeBuffer, DecodeBuffer, and FormatConverter classes specifically designed to simplify routine encoding and decoding tasks in non-text-based protocols.
Testing codec component
A small test for the developed codec component functionality also illustrating its use is presented below:
The test assumes some_app testing sesion/project. To run the test, we need a simple adapter wrapper around the developed codec component:
Results of running an example test campaign are presented below:
Source code
You can download the full source code developed in this article by following this link.
