TestApplication.cc

00001 #include "TestApplication.h"
00002 
00003 
00004 Define_Module(TestApplication);
00005 
00006 void TestApplication::initialize(int stage) {
00007     BaseModule::initialize(stage);
00008 
00009 
00010     if (stage == 0) {
00011         // begin by connecting the gates
00012         // to allow messages exchange
00013         dataOut = findGate("lowerGateOut");
00014         dataIn = findGate("lowerGateIn");
00015         ctrlOut = findGate("lowerControlOut");
00016         ctrlIn = findGate("lowerControlIn");
00017 
00018 
00019         // Retrieve parameters
00020         debug = par("debug").boolValue();
00021         stats = par("stats").boolValue();
00022         trace = par("trace").boolValue();
00023 
00024         isTransmitting = false;
00025 
00026         nbPackets = par("nbPackets");
00027         trafficParam = par("trafficParam").doubleValue();
00028         nodeAddr = par("nodeAddr");
00029   dstAddr = par("dstAddr");
00030         flood = par("flood").boolValue();
00031         PAYLOAD_SIZE = par("payloadSize"); // data field size
00032         PAYLOAD_SIZE = PAYLOAD_SIZE * 8; // convert to bits
00033         // Configure internal state variables and objects
00034         nbPacketsReceived = 0;
00035         remainingPackets = nbPackets;
00036 
00037         INITIAL_DELAY = 5; // initial delay before sending first packet
00038 
00039         // start timer if needed
00040         if (nodeAddr != 0 && remainingPackets > 0) {
00041             delayTimer = new cMessage("app-delay-timer");
00042             scheduleAt(simTime() + INITIAL_DELAY +uniform(0,0.001), delayTimer); // we add a small shift to avoid systematic collisions
00043         } else {
00044             delayTimer = 0;
00045         }
00046 
00047         if (stats) {
00048             // we should collect statistics
00049             cModule *host = getParentModule();
00050             int nbNodes = host->size();
00051             for (int i = 0; i < nbNodes; i++) {
00052                 std::ostringstream oss;
00053                 oss << "latency";
00054                 oss << i;
00055                 cStdDev aLatency(oss.str().c_str());
00056                 latencies.push_back(aLatency);
00057             }
00058         }
00059 
00060         if (trace) {
00061             // record all packet arrivals
00062             latenciesRaw.setName("rawLatencies");
00063         }
00064 
00065     }
00066 
00067 }
00068 
00069 TestApplication::~TestApplication() {
00070   if (delayTimer)
00071         cancelAndDelete(delayTimer);
00072 }
00073 
00074 void TestApplication::finish() {
00075 
00076     if (stats) {
00077         recordScalar("Packets received", nbPacketsReceived);
00078         recordScalar("Aggregate latency", testStat.getMean());
00079         for (unsigned int i = 0; i < latencies.size(); i++) {
00080             cStdDev aLatency = latencies[i];
00081             aLatency.record();
00082         }
00083     }
00084 }
00085 
00086 void TestApplication::handleMessage(cMessage * msg) {
00087   debugEV << "In TestApplication::handleMessage" << endl;
00088     if (msg == delayTimer) {
00089         if (debug) {
00090           debugEV << "  processing application timer." << endl;
00091         }
00092         if (! isTransmitting) {
00093       // create and send a new message
00094       ApplPkt* msg = new ApplPkt("SomeData");
00095       msg->setBitLength(PAYLOAD_SIZE);
00096       msg->setSrcAddr(nodeAddr);
00097       msg->setDestAddr(dstAddr);
00098       NetwControlInfo* cInfo = new NetwControlInfo(0);
00099       msg->setControlInfo(cInfo);
00100       if (debug) {
00101         debugEV << " sending down new data message to Aloha MAC layer for radio transmission." << endl;
00102       }
00103       send(msg, dataOut);
00104       isTransmitting = true;
00105       // update internal state
00106       remainingPackets--;
00107     }
00108         // reschedule timer if appropriate
00109         if (remainingPackets > 0) {
00110             if (!flood && !delayTimer->isScheduled()) {
00111                 scheduleAt(simTime() + exponential(trafficParam) + 0.001, delayTimer);
00112             }
00113         } else {
00114             cancelAndDelete(delayTimer);
00115             delayTimer = 0;
00116         }
00117     } else if (msg->getArrivalGateId() == dataIn) {
00118         // we received a data message from someone else !
00119         ApplPkt* m = dynamic_cast<ApplPkt*>(msg);
00120         if (debug)
00121           debugEV << "I (" << nodeAddr << ") received a message from node "
00122                 << m->getSrcAddr() << " of size " << m->getBitLength() << "." << endl;
00123         nbPacketsReceived++;
00124 
00125         if (stats) {
00126             simtime_t theLatency = msg->getArrivalTime() - msg->getCreationTime();
00127             latencies[m->getSrcAddr()].collect(theLatency.dbl());
00128             testStat.collect(theLatency.dbl());
00129         }
00130 
00131         if (trace) {
00132             simtime_t theLatency = msg->getArrivalTime() - msg->getCreationTime();
00133             latenciesRaw.record(theLatency.dbl());
00134         }
00135 
00136         delete msg;
00137     } else if (msg->getArrivalGateId() == ctrlIn) {
00138       debugEV << "Received a control message." << endl;
00139         // msg announces end of transmission.
00140         if (msg->getKind() ==BaseMacLayer::TX_OVER) {
00141           isTransmitting = false;
00142             if (remainingPackets > 0 && flood && !delayTimer->isScheduled()) {
00143                 scheduleAt(simTime() + 0.001*001 + uniform(0, 0.001*0.001), delayTimer);
00144             }
00145         }
00146         delete msg;
00147     } else {
00148         // default case
00149         if (debug) {
00150       ApplPkt* m = static_cast<ApplPkt*>(msg);
00151       debugEV << "I (" << nodeAddr << ") received a message from node "
00152           << (static_cast<ApplPkt*>(msg))->getSrcAddr() << " of size " << m->getBitLength() << "." << endl;
00153         }
00154         delete msg;
00155     }
00156 }
00157