OverSim
GenericPacketParser.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
23 #include <omnetpp.h>
24 
25 #include "GenericPacketParser.h"
26 
28 
29 char* GenericPacketParser::encapsulatePayload(cPacket *msg, unsigned int* length)
30 {
31  commBuffer.reset();
33 
34  *length = commBuffer.getMessageSize();
35  char* byte_buf = new char[*length];
36  memcpy(byte_buf, commBuffer.getBuffer(), *length);
37 
38  return byte_buf;
39 }
40 
41 cPacket* GenericPacketParser::decapsulatePayload(char* buf, unsigned int length)
42 {
43  cPacket *msg = NULL;
44 
45  commBuffer.reset();
46  commBuffer.allocateAtLeast(length);
47  memcpy(commBuffer.getBuffer(), buf, length);
48  commBuffer.setMessageSize(length);
49 
50  try {
51  msg = check_and_cast<cPacket*>(commBuffer.unpackObject());
52  if (!commBuffer.isBufferEmpty()) {
53  ev << "[GenericPacketParser::decapsulatePayload()]\n"
54  << " Parsing of payload failed: buffer size mismatch"
55  << endl;
56  delete msg;
57  return NULL;
58  }
59 // } catch (cRuntimeError err) {
60 // FIXME:
61 // the above does, for some reason, not work. So we catch everything,
62 // which may prevent the simulation from terminating correctly while
63 // parsing a message.
64  } catch (...) {
65  ev << "[GenericPacketParser::decapsulatePayload()]\n"
66  << " Parsing of payload failed"
67  << endl;
68  delete msg;
69  return NULL;
70  }
71 
72  return msg;
73 }