TrafficGen.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 "TrafficGen.h"
00017 #include "NetwControlInfo.h"
00018 #include <cassert>
00019 #include <Packet.h>
00020 #include <BaseNetwLayer.h>
00021 
00022 
00023 Define_Module(TrafficGen);
00024 
00025 void TrafficGen::initialize(int stage)
00026 {
00027   BaseApplLayer::initialize(stage);
00028 
00029   if(stage == 0) {
00030     world = FindModule<BaseWorldUtility*>::findGlobalModule();
00031     delayTimer = new cMessage("delay-timer", SEND_PACKET_TIMER);
00032 
00033     packetTime = par("packetTime");
00034     pppt = par("packetsPerPacketTime");
00035     burstSize = par("burstSize");
00036 
00037     nbPacketDropped = 0;
00038 
00039     Packet p(1);
00040     catPacket = world->getCategory(&p);
00041   } else if (stage == 1) {
00042     if(burstSize > 0) {
00043       remainingBurst = burstSize;
00044       scheduleAt(dblrand() * packetTime * burstSize / pppt, delayTimer);
00045     }
00046   } else {
00047 
00048   }
00049 }
00050 
00051 TrafficGen::~TrafficGen() {
00052   cancelAndDelete(delayTimer);
00053 }
00054 
00055 void TrafficGen::finish()
00056 {
00057   recordScalar("dropped", nbPacketDropped);
00058 }
00059 
00060 void TrafficGen::handleSelfMsg(cMessage *msg)
00061 {
00062   switch( msg->getKind() )
00063   {
00064   case SEND_PACKET_TIMER:
00065     assert(msg == delayTimer);
00066 
00067 
00068     sendBroadcast();
00069 
00070     remainingBurst--;
00071 
00072     if(remainingBurst == 0) {
00073       remainingBurst = burstSize;
00074       scheduleAt(simTime() + (dblrand()*1.4+0.3)*packetTime * burstSize / pppt, msg);
00075     } else {
00076       scheduleAt(simTime() + packetTime * 2, msg);
00077     }
00078 
00079     break;
00080   default:
00081     EV << "Unkown selfmessage! -> delete, kind: "<<msg->getKind() <<endl;
00082     delete msg;
00083   }
00084 }
00085 
00086 
00087 void TrafficGen::handleLowerMsg(cMessage *msg)
00088 {
00089   cPacket* pkt = static_cast<cPacket*>(msg);
00090   Packet p(pkt->getBitLength(), 1, 0);
00091   world->publishBBItem(catPacket, &p, -1);
00092 
00093   delete msg;
00094   msg = 0;
00095 }
00096 
00097 
00098 void TrafficGen::sendBroadcast()
00099 {
00100   ApplPkt *pkt = new ApplPkt("BROADCAST_MESSAGE", TRAFFIC_GEN_PACKET);
00101   pkt->setDestAddr(-1);
00102   // we use the host modules getIndex() as a appl address
00103   pkt->setSrcAddr( myApplAddr() );
00104   pkt->setBitLength(headerLength);
00105 
00106   // set the control info to tell the network layer the layer 3
00107   // address;
00108   pkt->setControlInfo( new NetwControlInfo(L3BROADCAST) );
00109 
00110   debugEV << "Sending broadcast packet!\n";
00111   sendDown( pkt );
00112 }
00113