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;
00044 
00045     commBuffer.reset();
00046     //commBuffer.pack(buf, length);
00047     commBuffer.allocateAtLeast(length);
00048     memcpy(commBuffer.getBuffer(), buf, length);
00049     commBuffer.setMessageSize(length);
00050 
00051     try {
00052         msg = check_and_cast<cPacket*>(commBuffer.unpackObject());
00053               commBuffer.assertBufferEmpty();
00054 //    } catch (cRuntimeError err) {
00055 //    FIXME:
00056 //    the above does, for some reason, not work. So we catch everyting,
00057 //    which may prevent the simulation from terminating corectly while
00058 //    parsing a message.
00059     } catch (...) {
00060         ev << "[GenericPacketParser::decapsulatePayload()]\n"
00061            << "    Parsing of payload failed"
00062            << endl;
00063         return NULL;
00064     }
00065 
00066     return msg;
00067 }

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5