DropsGenerator.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 "DropsGenerator.h"
00019 
00020 Define_Module(DropsGenerator);
00021 
00022 void DropsGenerator::initialize()
00023 {
00024     numPackets = 0;
00025     numDropped = 0;
00026     generateFurtherDrops = false;
00027 
00028     WATCH(numPackets);
00029     WATCH(numDropped);
00030     WATCH(generateFurtherDrops);
00031 
00032     const char *vector = par("dropsVector");
00033     parseVector(vector);
00034 
00035     if (dropsVector.size()==0)
00036         {EV << "DropsGenerator: Empty dropsVector" << endl;}
00037     else
00038     {
00039         EV << "DropsGenerator: dropsVector=" << vector << endl;
00040         generateFurtherDrops = true;
00041     }
00042 }
00043 
00044 void DropsGenerator::handleMessage(cMessage *msg)
00045 {
00046     numPackets++;
00047 
00048     if (generateFurtherDrops)
00049     {
00050         if (numPackets==dropsVector[0])
00051         {
00052             EV << "DropsGenerator: Dropping packet number " << numPackets << " " << msg << endl;
00053             delete msg;
00054             numDropped++;
00055             dropsVector.erase(dropsVector.begin());
00056             if (dropsVector.size()==0)
00057             {
00058                 EV << "DropsGenerator: End of dropsVector reached." << endl;
00059                 generateFurtherDrops = false;
00060             }
00061         }
00062         else
00063             {send(msg, "out");}
00064     }
00065     else
00066         {send(msg, "out");}
00067 }
00068 
00069 void DropsGenerator::parseVector(const char *vector)
00070 {
00071     const char *v = vector;
00072     while (*v)
00073     {
00074         // parse packet numbers
00075         while (isspace(*v)) v++;
00076         if (!*v || *v==';') break;
00077         if (!isdigit(*v))
00078             throw cRuntimeError("syntax error in dropsVector: packet number expected");
00079         if (dropsVector.size()>0 && dropsVector.back() >= (unsigned int)atoi(v))
00080             throw cRuntimeError("syntax error in dropsVector: packet numbers in ascending order expected");
00081 
00082         dropsVector.push_back(atoi(v));
00083         while (isdigit(*v)) v++;
00084 
00085         // skip delimiter
00086         while (isspace(*v)) v++;
00087         if (!*v) break;
00088         if (*v!=';')
00089             throw cRuntimeError("syntax error in dropsVector: separator ';' missing");
00090         v++;
00091         while (isspace(*v)) v++;
00092     }
00093 }
00094 
00095 void DropsGenerator::finish()
00096 {
00097     recordScalar("total packets", numPackets);
00098     recordScalar("total dropped", numDropped);
00099 }