NetworkStackTrafficGen.cc

00001 //
00002 // This program is free software: you can redistribute it and/or modify
00003 // it under the terms of the GNU Lesser General Public License as published by
00004 // the Free Software Foundation, either version 3 of the License, or
00005 // (at your option) any later version.
00006 //
00007 // This program is distributed in the hope that it will be useful,
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU Lesser General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU Lesser General Public License
00013 // along with this program.  If not, see http://www.gnu.org/licenses/.
00014 //
00015 
00016 #include "NetworkStackTrafficGen.h"
00017 #include "NetwToMacControlInfo.h"
00018 #include <cassert>
00019 #include <Packet.h>
00020 #include <BaseMacLayer.h>
00021 
00022 
00023 Define_Module(NetworkStackTrafficGen);
00024 
00025 void NetworkStackTrafficGen::initialize(int stage)
00026 {
00027   BaseLayer::initialize(stage);
00028 
00029   if(stage == 0) {
00030     world = FindModule<BaseWorldUtility*>::findGlobalModule();
00031     delayTimer = new cMessage("delay-timer", SEND_BROADCAST_TIMER);
00032 
00033     arp = FindModule<BaseArp*>::findSubModule(findHost());
00034     myNetwAddr = arp->myNetwAddr(this);
00035 
00036     packetLength = par("packetLength");
00037     packetTime = par("packetTime");
00038     pppt = par("packetsPerPacketTime");
00039     burstSize = par("burstSize");
00040     destination = par("destination");
00041 
00042     nbPacketDropped = 0;
00043 
00044     Packet p(1);
00045     catPacket = world->getCategory(&p);
00046   } else if (stage == 1) {
00047     if(burstSize > 0) {
00048       remainingBurst = burstSize;
00049       scheduleAt(dblrand() * packetTime * burstSize / pppt, delayTimer);
00050     }
00051   } else {
00052 
00053   }
00054 }
00055 
00056 NetworkStackTrafficGen::~NetworkStackTrafficGen() {
00057   cancelAndDelete(delayTimer);
00058 }
00059 
00060 
00061 void NetworkStackTrafficGen::finish()
00062 {
00063   recordScalar("dropped", nbPacketDropped);
00064 }
00065 
00066 void NetworkStackTrafficGen::handleSelfMsg(cMessage *msg)
00067 {
00068   switch( msg->getKind() )
00069   {
00070   case SEND_BROADCAST_TIMER:
00071     assert(msg == delayTimer);
00072 
00073 
00074     sendBroadcast();
00075 
00076     remainingBurst--;
00077 
00078     if(remainingBurst == 0) {
00079       remainingBurst = burstSize;
00080       scheduleAt(simTime() + (dblrand()*1.4+0.3)*packetTime * burstSize / pppt, msg);
00081     } else {
00082       scheduleAt(simTime() + packetTime * 2, msg);
00083     }
00084 
00085     break;
00086   default:
00087     EV << "Unkown selfmessage! -> delete, kind: "<<msg->getKind() <<endl;
00088     delete msg;
00089   }
00090 }
00091 
00092 
00093 void NetworkStackTrafficGen::handleLowerMsg(cMessage *msg)
00094 {
00095   Packet p(packetLength, 1, 0);
00096   world->publishBBItem(catPacket, &p, -1);
00097 
00098   delete msg;
00099   msg = 0;
00100 }
00101 
00102 
00103 void NetworkStackTrafficGen::handleLowerControl(cMessage *msg)
00104 {
00105   if(msg->getKind() == BaseMacLayer::PACKET_DROPPED) {
00106     nbPacketDropped++;
00107   }
00108   delete msg;
00109   msg = 0;
00110 }
00111 
00112 void NetworkStackTrafficGen::sendBroadcast()
00113 {
00114   NetwPkt *pkt = new NetwPkt("BROADCAST_MESSAGE", BROADCAST_MESSAGE);
00115   pkt->setBitLength(packetLength);
00116 
00117   pkt->setSrcAddr(myNetwAddr);
00118   pkt->setDestAddr(destination);
00119 
00120   pkt->setControlInfo(new NetwToMacControlInfo(destination));
00121 
00122   Packet p(packetLength, 0, 1);
00123   world->publishBBItem(catPacket, &p, -1);
00124 
00125   sendDown(pkt);
00126 }
00127