OpenTTCN/Knowledge base/Standard codec explained/Custom/CPP/CD impl.cpp

From OpenTTCN

Jump to: navigation, search

  OpenTTCN DocZone

  Home | Developer's corner | Knowledge base | Working documents | Documentation | OpenTTCN IDE | Tutorials | Training | How do I | Frequently asked questions | Technical support


CD_impl.cpp


Back to main article >>

#include "CD_impl.h"

#include <StartHere.h>
#include <StartHereCD.h>
#include <AdapterException.h>
#include <util/CodecHelper.h>

#include <tci/TciValues.h>
#include <tci/TciCdRequired.h>

#include <isl/FormatConverter.h>

#include <vector>
#include <memory>
#include <iostream>

#include <math.h>
#include <stdlib.h>

using namespace std;

using OpenTTCN::ISL::FormatConverter;
using OpenTTCN::SDK::AdapterException;
using OpenTTCN::SDK::Util::CodecHelper;

static inline std::string intToStr(int v)
{
    return OpenTTCN::ISL::FormatConverter::intToStr(v);
}

CD_impl :: CD_impl() : _delegate(OpenTTCN::SDK::StartHereCD::
    getCodecComponent("http://www.openttcn.com/Codecs/2011/Standard"))
{
}

CD_impl :: ~CD_impl()
{
}

TriMessage* CD_impl :: encode(const TciValue* value_)
{
    const ORG_ETSI_TTCN3_TCI::TciType& valueType = value_->getType();
    TciTypeClass typeClass = valueType.getTypeClass();
    std::string typeName = valueType.getName();

    if (typeClass == ORG_ETSI_TTCN3_TCI::TCI_ADDRESS)
    {
        typeClass = ORG_ETSI_TTCN3_TCI::TCI_INTEGER; // put the real address type class here
    }

    // cout << "encode(): value = " << ((value_) ? value_->toString() : "null") << endl;

    auto_ptr<TriMessage> msg(_delegate->encode(value_));

    // Wrap the encoded message into an envelope preceded by type identifier:

    string typeId = value_->getType().getName();

    if (typeId == "address")
    {
        typeId = "integer"; // put the real address type name here
    }

    string enc = "type=" + typeId + ";length=" + intToStr(msg->getBitsDataLen()) + ";value=" +
        FormatConverter::charstringToOctetstring(msg->getData(), CodecHelper::getLengthInOctets(
        msg->getBitsDataLen()));

    // cout << "Encoded " << value_->toString() << " to " << enc << endl;

    return TriMessage::create((const unsigned char *) enc.c_str(), enc.size() << 3);
}

TciValue* CD_impl :: decode(const TriMessage* message_,
    const TciType* decodingHypothesis_)
{
    int i, len;

    TciCdRequired* req = OpenTTCN::SDK::StartHereCD::getTciCdRequired();

    if (!message_->getBitsDataLen()) return 0; // possibly missing address

    string s((char *) message_->getData(),
        CodecHelper::getLengthInOctets(message_->getBitsDataLen()));

    vector<string> elems = FormatConverter::split(s, ';');

    string type = "";
    string length = "";
    string value = "";

    len = elems.size();
    for (i = 0; i < len; i++)
    {
        string elem = elems[i];
        vector<string> subelems = FormatConverter::split(elem, '=');

        if (subelems[0] == "type") type = subelems[1];
        else if (subelems[0] == "length") length = subelems[1];
        else if (subelems[0] == "value") value = subelems[1];
        else
        {
            throw AdapterException("Unrecognized element tag: " + elem + ".");
        }
    }

    const TciType* t = req->getTypeForName(type);
    std::string v = FormatConverter::octetstringToCharstring(value);

    auto_ptr<TriMessage> msg(TriMessage::create(
        (const unsigned char *) v.c_str(), v.size() << 3));

    return _delegate->decode(msg.get(), t);
}
Views
Personal tools