DuplicatesGenerator.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2009 Thomas Reschka
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include "DuplicatesGenerator.h"
00019 
00020 Define_Module(DuplicatesGenerator);
00021 
00022 void DuplicatesGenerator::initialize()
00023 {
00024     numPackets = 0;
00025     numDuplicated = 0;
00026     generateFurtherDuplicates = false;
00027 
00028     WATCH(numPackets);
00029     WATCH(numDuplicated);
00030     WATCH(generateFurtherDuplicates);
00031 
00032     const char *vector = par("duplicatesVector");
00033     parseVector(vector);
00034 
00035     if (duplicatesVector.size()==0)
00036         {EV << "DuplicatesGenerator: Empty duplicatesVector" << endl;}
00037     else
00038     {
00039         EV << "DuplicatesGenerator: duplicatesVector=" << vector << endl;
00040         generateFurtherDuplicates = true;
00041     }
00042 }
00043 
00044 void DuplicatesGenerator::handleMessage(cMessage *msg)
00045 {
00046     numPackets++;
00047 
00048     if (generateFurtherDuplicates)
00049     {
00050         if (numPackets==duplicatesVector[0])
00051         {
00052             EV << "DuplicatesGenerator: Duplicating packet number " << numPackets << " " << msg << endl;
00053             cMessage *dupmsg = (cMessage*) msg->dup();
00054             send(dupmsg, "out");
00055             numDuplicated++;
00056             duplicatesVector.erase(duplicatesVector.begin());
00057             if (duplicatesVector.size()==0)
00058             {
00059                 EV << "DuplicatesGenerator: End of duplicatesVector reached." << endl;
00060                 generateFurtherDuplicates = false;
00061             }
00062         }
00063     }
00064     send(msg, "out");
00065 }
00066 
00067 void DuplicatesGenerator::parseVector(const char *vector)
00068 {
00069     const char *v = vector;
00070     while (*v)
00071     {
00072         // parse packet numbers
00073         while (isspace(*v)) v++;
00074         if (!*v || *v==';') break;
00075         if (!isdigit(*v))
00076             throw cRuntimeError("syntax error in duplicatesVector: packet number expected");
00077         if (duplicatesVector.size()>0 && duplicatesVector.back() >= (unsigned int)atoi(v))
00078             throw cRuntimeError("syntax error in duplicatesVector: packet numbers in ascending order expected");
00079 
00080         duplicatesVector.push_back(atoi(v));
00081         while (isdigit(*v)) v++;
00082 
00083         // skip delimiter
00084         while (isspace(*v)) v++;
00085         if (!*v) break;
00086         if (*v!=';')
00087             throw cRuntimeError("syntax error in duplicatesVector: separator ';' missing");
00088         v++;
00089         while (isspace(*v)) v++;
00090     }
00091 }
00092 
00093 void DuplicatesGenerator::finish()
00094 {
00095     recordScalar("total packets", numPackets);
00096     recordScalar("total duplicated", numDuplicated);
00097 }