Public Member Functions | Private Attributes

GenericPacketParser Class Reference

A message parser using the cMemCommBuffer to serialize cmessages. More...

#include <GenericPacketParser.h>

Inheritance diagram for GenericPacketParser:
PacketParser

List of all members.

Public Member Functions

char * encapsulatePayload (cPacket *msg, unsigned int *length)
 serializes messages in a buffer
cPacket * decapsulatePayload (char *buf, unsigned int length)
 deserializes messages from a char[] of size length

Private Attributes

cNetCommBuffer commBuffer
 the buffer used to encapsulate and decapsulate messages

Detailed Description

A message parser using the cMemCommBuffer to serialize cmessages.

Author:
Bernhard Heep

Definition at line 37 of file GenericPacketParser.h.


Member Function Documentation

cPacket * GenericPacketParser::decapsulatePayload ( char *  buf,
unsigned int  length 
) [virtual]

deserializes messages from a char[] of size length

Parameters:
buf the buffer to extract the message from
length the length of the buffer
Returns:
the decapsulated cPacket

Implements PacketParser.

Definition at line 41 of file GenericPacketParser.cc.

{
    cPacket *msg = NULL;

    commBuffer.reset();
    commBuffer.allocateAtLeast(length);
    memcpy(commBuffer.getBuffer(), buf, length);
    commBuffer.setMessageSize(length);

    try {
        msg = check_and_cast<cPacket*>(commBuffer.unpackObject());
        if (!commBuffer.isBufferEmpty()) {
            ev << "[GenericPacketParser::decapsulatePayload()]\n"
               << "    Parsing of payload failed: buffer size mismatch"
               << endl;
            delete msg;
            return NULL;
        }
//    } catch (cRuntimeError err) {
//    FIXME:
//    the above does, for some reason, not work. So we catch everything,
//    which may prevent the simulation from terminating correctly while
//    parsing a message.
    } catch (...) {
        ev << "[GenericPacketParser::decapsulatePayload()]\n"
           << "    Parsing of payload failed"
           << endl;
        delete msg;
        return NULL;
    }

    return msg;
}

char * GenericPacketParser::encapsulatePayload ( cPacket *  msg,
unsigned int *  length 
) [virtual]

serializes messages in a buffer

Parameters:
msg the message to serialize
length the length of the message
Returns:
the encapsulated messages as a char[] of size length

Implements PacketParser.

Definition at line 29 of file GenericPacketParser.cc.

{
    commBuffer.reset();
    commBuffer.packObject(msg);

    *length = commBuffer.getMessageSize();
    char* byte_buf = new char[*length];
    memcpy(byte_buf, commBuffer.getBuffer(), *length);

    return byte_buf;
}


Member Data Documentation

the buffer used to encapsulate and decapsulate messages

Definition at line 59 of file GenericPacketParser.h.

Referenced by decapsulatePayload(), and encapsulatePayload().


The documentation for this class was generated from the following files: