OverSim
GenericPacketParser Class Reference

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

#include <GenericPacketParser.h>

Inheritance diagram for GenericPacketParser:
PacketParser

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
- Public Member Functions inherited from PacketParser
virtual void initialize ()
 Called on initialisation.
virtual void handleMessage (cMessage *msg)
 Is called if the modules receives a message.

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
bufthe buffer to extract the message from
lengththe 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
msgthe message to serialize
lengththe 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();
*length = commBuffer.getMessageSize();
char* byte_buf = new char[*length];
memcpy(byte_buf, commBuffer.getBuffer(), *length);
return byte_buf;
}

Member Data Documentation

cNetCommBuffer GenericPacketParser::commBuffer
private

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: