KBRTestApp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU 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 General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <IPAddressResolver.h>
00025 #include <CommonMessages_m.h>
00026 #include <GlobalStatistics.h>
00027 #include <UnderlayConfigurator.h>
00028 #include <GlobalNodeList.h>
00029 
00030 #include "KBRTestApp.h"
00031 #include "KBRTestMessage_m.h"
00032 
00033 Define_Module(KBRTestApp);
00034 
00035 KBRTestApp::KBRTestApp()
00036 {
00037     testTimer = NULL;
00038 }
00039 
00040 KBRTestApp::~KBRTestApp()
00041 {
00042     cancelAndDelete(testTimer);
00043 }
00044 
00045 void KBRTestApp::initializeApp(int stage)
00046 {
00047     if (stage != MIN_STAGE_APP) {
00048         return;
00049     }
00050 
00051     lookupNodeIds = par("lookupNodeIds");
00052     mean = par("testMsgInterval");
00053     deviation = mean / 10;
00054     activeNetwInitPhase = par("activeNetwInitPhase");
00055     msgHandleBufSize = par("msgHandleBufSize");
00056 
00057     numDelivered = 0;
00058     bytesDelivered = 0;
00059     WATCH(numDelivered);
00060     WATCH(bytesDelivered);
00061 
00062     numDropped = 0;
00063     bytesDropped = 0;
00064     WATCH(numDropped);
00065     WATCH(bytesDropped);
00066 
00067     sequenceNumber = 0;
00068 
00069     nodeIsLeavingSoon = false;
00070 
00071     // initialize circular buffer
00072     if (msgHandleBufSize > 0) {
00073         mhBuf.resize(msgHandleBufSize);
00074         mhBufBegin = mhBuf.begin();
00075         mhBufEnd = mhBuf.end();
00076         mhBufNext = mhBufBegin;
00077     }
00078 
00079 #if 0
00080     bindToPort(1025);
00081     thisNode.port = 1025;
00082 #endif
00083 
00084     // start periodic timer
00085     testTimer = new cMessage("KBRTestApp Timer");
00086     scheduleAt(simTime() + truncnormal(mean, deviation), testTimer);
00087 }
00088 
00089 void KBRTestApp::handleTimerEvent(cMessage* msg)
00090 {
00091     if (msg == testTimer) {
00092         // schedule next timer event
00093         scheduleAt(simTime() + truncnormal(mean, deviation), msg);
00094 
00095         // do nothing if the network is still in the initialization phase
00096         if ((!activeNetwInitPhase && underlayConfigurator->isInInitPhase())
00097                 || underlayConfigurator->isSimulationEndingSoon()
00098                 || nodeIsLeavingSoon) {
00099 
00100             return;
00101         }
00102 
00103         OverlayKey destKey;
00104         if (lookupNodeIds) {
00105             destKey = globalNodeList->getBootstrapNode().key;
00106             // do nothing if there are currently no nodes in the network
00107             if (destKey.isUnspecified())
00108                 return;
00109         } else {
00110             // generate random destination key
00111             destKey = OverlayKey::random();
00112         }
00113 
00114         // create a 100 byte test message
00115         KBRTestMessage* testMsg = new KBRTestMessage("KBRTestMessage");
00116         testMsg->setId(getId());
00117         testMsg->setSeqNum(sequenceNumber++);
00118         testMsg->setByteLength(100);
00119         testMsg->setMeasurementPhase(globalStatistics->isMeasuring());
00120 
00121         RECORD_STATS(globalStatistics->sentKBRTestAppMessages++);
00122 
00123         callRoute(destKey, testMsg);
00124 
00125 #if 0
00126         thisNode.port = 1025;
00127         NodeHandle handle = globalNodeList->getBootstrapNode();
00128         handle.port = 1025;
00129         pingNode(handle, -1, -1, NULL, "TestPING", NULL, -1, UDP_TRANSPORT);
00130 
00131         LookupCall* call = new LookupCall();
00132         call->setBitLength(0);
00133         call->setKey(destKey);
00134         call->setNumSiblings(overlay->getMaxNumSiblings());
00135         sendInternalRpcCall(OVERLAY_COMP, call);
00136 #endif
00137 
00138     }
00139 }
00140 void KBRTestApp::pingResponse(PingResponse* response, cPolymorphic* context,
00141                               int rpcId, simtime_t rtt)
00142 {
00143     //std::cout << rtt << std::endl;
00144 }
00145 
00146 void KBRTestApp::handleRpcResponse(BaseResponseMessage* msg,
00147                                    cPolymorphic* context, int rpcId,
00148                                    simtime_t rtt)
00149 {
00150     RPC_SWITCH_START(msg)
00151     RPC_ON_RESPONSE(Lookup) {
00152         handleLookupResponse(_LookupResponse);
00153         EV << "[KBRTestApp::handleRpcResponse() @ " << overlay->getThisNode().ip
00154            << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00155            << "    Lookup RPC Response received: id=" << rpcId << "\n"
00156            << "    msg=" << *_LookupResponse << " rtt=" << rtt
00157            << endl;
00158         break;
00159     }
00160     RPC_SWITCH_END( )
00161 }
00162 
00163 void KBRTestApp::handleLookupResponse(LookupResponse* msg)
00164 {
00165     EV << "[KBRTestApp::handleLookupResponse() @ " << overlay->getThisNode().ip
00166        << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00167        << "    Lookup response for key " << msg->getKey()<< " : ";
00168     if (msg->getIsValid()) {
00169         for (uint32_t i=0; i<msg->getSiblingsArraySize(); i++) {
00170             EV << endl << "    " << msg->getSiblings(i);
00171         }
00172     } else {
00173         EV << "failed";
00174     }
00175 
00176     EV << endl;
00177 }
00178 
00179 void KBRTestApp::handleNodeLeaveNotification()
00180 {
00181     nodeIsLeavingSoon = true;
00182 }
00183 
00184 
00185 void KBRTestApp::deliver(OverlayKey& key, cMessage* msg)
00186 {
00187     KBRTestMessage* testMsg = check_and_cast<KBRTestMessage*>(msg);
00188     OverlayCtrlInfo* overlayCtrlInfo =
00189         check_and_cast<OverlayCtrlInfo*>(msg->removeControlInfo());
00190 
00191     if (overlay->getThisNode().key.isUnspecified())
00192         error("key");
00193 
00194     // check for duplicate
00195     if ((msgHandleBufSize > 0 )
00196             && checkSeen(overlayCtrlInfo->getSrcNode().key, testMsg->getSeqNum())) {
00197         EV << "[KBRTestApp::deliver() @ " << overlay->getThisNode().ip
00198            << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00199            << "    Duplicate dropped."
00200            << endl;
00201         delete overlayCtrlInfo;
00202         delete testMsg;
00203         return;
00204     }
00205 
00206     // Return statistical data to the sender.
00207     if (cModule* mod = simulation.getModule(testMsg->getId())) {
00208         if (KBRTestApp* sender = dynamic_cast<KBRTestApp*>(mod)) {
00209             if ((!lookupNodeIds) || (overlay->getThisNode().key == key)) {
00210                 if (testMsg->getMeasurementPhase() == true) {
00211                         sender->evaluateData((simTime() - testMsg->getCreationTime()),
00212                                              overlayCtrlInfo->getHopCount(),
00213                                              testMsg->getByteLength());
00214                 }
00215             } else if(lookupNodeIds) {
00216                 RECORD_STATS(numDropped++;
00217                              bytesDropped += testMsg->getByteLength());
00218                 EV << "[KBRTestApp::deliver() @ " << overlay->getThisNode().ip
00219                    << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00220                    << "    Error: Lookup of NodeIDs and KBRTestMessage"
00221                    << " received with different destKey!"
00222                    << endl;
00223             }
00224         }
00225     }
00226 
00227     EV << "[KBRTestApp::deliver() @ " << overlay->getThisNode().ip
00228        << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00229        << "    Received \"" << testMsg->getName() << "(seqNr: "
00230        << testMsg->getSeqNum() << ")\n"
00231        << "    with destination key: " << key.toString(16)
00232        << endl;
00233 
00234     delete overlayCtrlInfo;
00235     delete testMsg;
00236 }
00237 
00238 void KBRTestApp::forward(OverlayKey* key, cPacket** msg,
00239                          NodeHandle* nextHopNode)
00240 {
00241     KBRTestMessage* tempMsg = check_and_cast<KBRTestMessage*>(*msg);
00242 
00243     tempMsg->setVisitedNodesArraySize(tempMsg->getVisitedNodesArraySize() + 1);
00244     tempMsg->setVisitedNodes(tempMsg->getVisitedNodesArraySize() - 1,
00245                              overlay->getThisNode().ip);
00246 }
00247 
00248 bool KBRTestApp::checkSeen(const OverlayKey& key, int seqNum)
00249 {
00250     MsgHandle hdl(key, seqNum);
00251 
00252     for (MsgHandleBuf::iterator it = mhBufBegin; it != mhBufEnd; ++it) {
00253         if (it->key.isUnspecified()) {
00254             continue;
00255         }
00256 
00257         if (*it == hdl) {
00258             return true;
00259         }
00260     }
00261 
00262     *(mhBufNext++) = hdl;
00263     if (mhBufNext == mhBufEnd) {
00264         mhBufNext = mhBufBegin;
00265     }
00266     return false;
00267 }
00268 
00269 void KBRTestApp::evaluateData(simtime_t latency, int hopCount, long int bytes)
00270 {
00271     // count the number and size of successfully delivered messages
00272     RECORD_STATS(numDelivered++; bytesDelivered += bytes;
00273                  globalStatistics->deliveredKBRTestAppMessages++);
00274 
00275     if (numOverlaySent < numDelivered) {
00276         error("KBRTestApp::evaluateData(): numSent < numDelivered!");
00277     }
00278 
00279     RECORD_STATS(globalStatistics->recordOutVector("KBRTestApp: KBR Hop "
00280                                                    "Count", hopCount));
00281     RECORD_STATS(globalStatistics->recordOutVector("KBRTestApp: KBR Latency",
00282                                                    SIMTIME_DBL(latency)));
00283 }
00284 
00285 void KBRTestApp::finishApp()
00286 {
00287     simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00288 
00289     if (time != 0) {
00290         globalStatistics->addStdDev("KBRTestApp: Delivered Messages/s",
00291                                     numDelivered / time);
00292         globalStatistics->addStdDev("KBRTestApp: Delivered Bytes/s",
00293                                     bytesDelivered / time);
00294         globalStatistics->addStdDev("KBRTestApp: Dropped Messages/s",
00295                                     numDropped / time);
00296         globalStatistics->addStdDev("KBRTestApp: Dropped Bytes/s",
00297                                     bytesDropped / time);
00298         if (numOverlaySent != 0) {
00299             globalStatistics->addStdDev("KBRTestApp: KBR Delivery Ratio",
00300                                         (float)numDelivered /
00301                                         (float)numOverlaySent);
00302         } else {
00303             globalStatistics->addStdDev("KBRTestApp: KBR Delivery Ratio", 0);
00304         }
00305     }
00306 }
00307 

Generated on Wed Mar 25 16:32:49 2009 for OverSim by  doxygen 1.5.5