GenericPacketParser.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00023 #include <omnetpp.h>
00024 
00025 #include "GenericPacketParser.h"
00026 
00027 Define_Module(GenericPacketParser);
00028 
00029 char* GenericPacketParser::encapsulatePayload(cPacket *msg, unsigned int* length)
00030 {
00031     commBuffer.reset();
00032     commBuffer.packObject(msg);
00033 
00034     *length = commBuffer.getMessageSize();
00035     char* byte_buf = new char[*length];
00036     memcpy(byte_buf, commBuffer.getBuffer(), *length);
00037 
00038     return byte_buf;
00039 }
00040 
00041 cPacket* GenericPacketParser::decapsulatePayload(char* buf, unsigned int length)
00042 {
00043     cPacket *msg = NULL;
00044 
00045     commBuffer.reset();
00046     commBuffer.allocateAtLeast(length);
00047     memcpy(commBuffer.getBuffer(), buf, length);
00048     commBuffer.setMessageSize(length);
00049 
00050     try {
00051         msg = check_and_cast<cPacket*>(commBuffer.unpackObject());
00052         if (!commBuffer.isBufferEmpty()) {
00053             ev << "[GenericPacketParser::decapsulatePayload()]\n"
00054                << "    Parsing of payload failed: buffer size mismatch"
00055                << endl;
00056             delete msg;
00057             return NULL;
00058         }
00059 //    } catch (cRuntimeError err) {
00060 //    FIXME:
00061 //    the above does, for some reason, not work. So we catch everything,
00062 //    which may prevent the simulation from terminating correctly while
00063 //    parsing a message.
00064     } catch (...) {
00065         ev << "[GenericPacketParser::decapsulatePayload()]\n"
00066            << "    Parsing of payload failed"
00067            << endl;
00068         delete msg;
00069         return NULL;
00070     }
00071 
00072     return msg;
00073 }
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3