DHTTestApp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 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 <GlobalNodeListAccess.h>
00026 #include <GlobalStatisticsAccess.h>
00027 #include <UnderlayConfiguratorAccess.h>
00028 #include <RpcMacros.h>
00029 #include "CommonMessages_m.h"
00030 
00031 #include <GlobalDhtTestMap.h>
00032 
00033 #include "DHTTestApp.h"
00034 
00035 Define_Module(DHTTestApp);
00036 
00037 using namespace std;
00038 
00039 DHTTestApp::~DHTTestApp()
00040 {
00041     cancelAndDelete(dhttestput_timer);
00042     cancelAndDelete(dhttestget_timer);
00043     cancelAndDelete(dhttestmod_timer);
00044 }
00045 
00046 DHTTestApp::DHTTestApp()
00047 {
00048     dhttestput_timer = NULL;
00049     dhttestget_timer = NULL;
00050     dhttestmod_timer = NULL;
00051 }
00052 
00053 void DHTTestApp::initializeApp(int stage)
00054 {
00055     if (stage != MIN_STAGE_APP)
00056         return;
00057 
00058     // fetch parameters
00059     debugOutput = par("debugOutput");
00060     activeNetwInitPhase = par("activeNetwInitPhase");
00061 
00062     mean = par("testInterval");
00063     deviation = mean / 10;
00064 
00065     ttl = par("testTtl");
00066 
00067     globalNodeList = GlobalNodeListAccess().get();
00068     underlayConfigurator = UnderlayConfiguratorAccess().get();
00069     globalStatistics = GlobalStatisticsAccess().get();
00070 
00071     globalDhtTestMap = dynamic_cast<GlobalDhtTestMap*>(simulation.getModuleByPath(
00072             "globalObserver.globalFunctions[0]"));
00073 
00074     if (globalDhtTestMap == NULL) {
00075         throw cRuntimeError("DHTTestApp::initializeApp(): "
00076                                 "GlobalDhtTestMap module not found!");
00077     }
00078 
00079     // statistics
00080     numSent = 0;
00081     numGetSent = 0;
00082     numGetError = 0;
00083     numGetSuccess = 0;
00084     numPutSent = 0;
00085     numPutError = 0;
00086     numPutSuccess = 0;
00087 
00088     initRpcs();
00089     WATCH(numSent);
00090     WATCH(numGetSent);
00091     WATCH(numGetError);
00092     WATCH(numGetSuccess);
00093     WATCH(numPutSent);
00094     WATCH(numPutError);
00095     WATCH(numPutSuccess);
00096 
00097     nodeIsLeavingSoon = false;
00098 
00099     // initiate test message transmission
00100     dhttestput_timer = new cMessage("dhttest_put_timer");
00101     dhttestget_timer = new cMessage("dhttest_get_timer");
00102     dhttestmod_timer = new cMessage("dhttest_mod_timer");
00103 
00104     if (mean > 0) {
00105         scheduleAt(simTime() + truncnormal(mean, deviation),
00106                    dhttestput_timer);
00107         scheduleAt(simTime() + truncnormal(mean + mean / 3,
00108                                                       deviation),
00109                                                       dhttestget_timer);
00110         scheduleAt(simTime() + truncnormal(mean + 2 * mean / 3,
00111                                                       deviation),
00112                                                       dhttestmod_timer);
00113     }
00114 }
00115 
00116 void DHTTestApp::handleRpcResponse(BaseResponseMessage* msg,
00117                                    cPolymorphic* context, int rpcId,
00118                                    simtime_t rtt)
00119 {
00120     RPC_SWITCH_START(msg)
00121     RPC_ON_RESPONSE( DHTputCAPI ) {
00122         handlePutResponse(_DHTputCAPIResponse,
00123                           check_and_cast<DHTStatsContext*>(context));
00124         EV << "[DHTTestApp::handleRpcResponse()]\n"
00125            << "    DHT Put RPC Response received: id=" << rpcId
00126            << " msg=" << *_DHTputCAPIResponse << " rtt=" << rtt
00127            << endl;
00128         break;
00129     }
00130     RPC_ON_RESPONSE(DHTgetCAPI)
00131     {
00132         handleGetResponse(_DHTgetCAPIResponse,
00133                           check_and_cast<DHTStatsContext*>(context));
00134         EV << "[DHTTestApp::handleRpcResponse()]\n"
00135            << "    DHT Get RPC Response received: id=" << rpcId
00136            << " msg=" << *_DHTgetCAPIResponse << " rtt=" << rtt
00137            << endl;
00138         break;
00139     }
00140     RPC_SWITCH_END()
00141 }
00142 
00143 void DHTTestApp::handlePutResponse(DHTputCAPIResponse* msg,
00144                                    DHTStatsContext* context)
00145 {
00146     if (!(msg->getIsSuccess())) {
00147         if (context->measurementPhase == false) {
00148             // don't count response, if the request was not sent
00149             // in the measurement phase
00150             RECORD_STATS(numPutError++);
00151         }
00152         return;
00153     }
00154 
00155     OverlayKey key = msg->getKey();
00156     DHTEntry entry = {BinaryValue(msg->getValue()),
00157             simTime() + ttl};
00158 
00159     globalDhtTestMap->insertEntry(msg->getKey(), entry);
00160 
00161     if (context->measurementPhase == false) {
00162         // don't count response, if the request was not sent
00163         // in the measurement phase
00164         return;
00165     }
00166 
00167     RECORD_STATS(numPutSuccess++);
00168 
00169     RECORD_STATS(globalStatistics->addStdDev("DHTTestApp: PUT delay (s)",
00170                                              SIMTIME_DBL(simTime() - context->requestTime)));
00171 }
00172 
00173 void DHTTestApp::handleGetResponse(DHTgetCAPIResponse* msg,
00174                                    DHTStatsContext* context)
00175 {
00176     if (context->measurementPhase == false) {
00177         // don't count response, if the request was not sent
00178         // in the measurement phase
00179         return;
00180     }
00181 
00182     RECORD_STATS(globalStatistics->addStdDev("DHTTestApp: GET delay (s)",
00183                                              SIMTIME_DBL(simTime() - context->requestTime)));
00184 
00185     if (!(msg->getIsSuccess())) {
00186         //cout << "DHTTestApp: success == false" << endl;
00187         RECORD_STATS(numGetError++);
00188         return;
00189     }
00190 
00191     const DHTEntry* entry = globalDhtTestMap->findEntry(msg->getKey());
00192 
00193     if (entry == NULL) {
00194         //unexpected key
00195         RECORD_STATS(numGetError++);
00196         //cout << "DHTTestApp: unexpected key" << endl;
00197         return;
00198     }
00199 
00200     if (simTime() > entry->endtime) {
00201         //this key doesn't exist anymore in the DHT, delete it in our hashtable
00202 
00203         globalDhtTestMap->eraseEntry(msg->getKey());
00204 
00205         if (!(msg->getValue().isUnspecified())) {
00206             RECORD_STATS(numGetError++);
00207             //cout << "DHTTestApp: deleted key still available" << endl;
00208             return;
00209         } else {
00210             RECORD_STATS(numGetSuccess++);
00211             return;
00212         }
00213     } else {
00214         if (msg->getValue() != entry->value) {
00215             RECORD_STATS(numGetError++);
00216             //cout << "DHTTestApp: wrong value" << endl;
00217             //cout << "size: " << msg->getValue().size() << endl;
00218             return;
00219         } else {
00220             RECORD_STATS(numGetSuccess++);
00221             return;
00222         }
00223     }
00224 
00225 }
00226 
00227 void DHTTestApp::handleTraceMessage(cMessage* msg)
00228 {
00229     char* cmd = new char[strlen(msg->getName()) + 1];
00230     strcpy(cmd, msg->getName());
00231 
00232     if (strlen(msg->getName()) < 5) {
00233         delete[] cmd;
00234         delete msg;
00235         return;
00236     }
00237 
00238     if (strncmp(cmd, "PUT ", 4) == 0) {
00239         // Generate key
00240         char* buf = cmd + 4;
00241 
00242         while (!isspace(buf[0])) {
00243             if (buf[0] == '\0')
00244                 throw cRuntimeError("Error parsing PUT command");
00245             buf++;
00246         }
00247 
00248         buf[0] = '\0';
00249         BinaryValue b(cmd + 4);
00250         OverlayKey destKey(OverlayKey::sha1(b));
00251 
00252         // get value
00253         buf++;
00254 
00255         // build putMsg
00256         DHTputCAPICall* dhtPutMsg = new DHTputCAPICall();
00257         dhtPutMsg->setKey(destKey);
00258         dhtPutMsg->setValue(buf);
00259         dhtPutMsg->setTtl(ttl);
00260         dhtPutMsg->setIsModifiable(true);
00261         RECORD_STATS(numSent++; numPutSent++);
00262         sendInternalRpcCall(TIER1_COMP, dhtPutMsg,
00263                 new DHTStatsContext(globalStatistics->isMeasuring(),
00264                                     simTime()));
00265     } else if (strncmp(cmd, "GET ", 4) == 0) {
00266         // Get key
00267         BinaryValue b(cmd + 4);
00268         OverlayKey key(OverlayKey::sha1(b));
00269 
00270         DHTgetCAPICall* dhtGetMsg = new DHTgetCAPICall();
00271         dhtGetMsg->setKey(key);
00272         RECORD_STATS(numSent++; numGetSent++);
00273         sendInternalRpcCall(TIER1_COMP, dhtGetMsg,
00274                 new DHTStatsContext(globalStatistics->isMeasuring(),
00275                                     simTime()));
00276     } else {
00277         throw cRuntimeError("Unknown trace command; "
00278                                 "only GET and PUT are allowed");
00279     }
00280 
00281     delete[] cmd;
00282     delete msg;
00283 }
00284 
00285 void DHTTestApp::handleTimerEvent(cMessage* msg)
00286 {
00287     if (msg->isName("dhttest_put_timer")) {
00288         // schedule next timer event
00289         scheduleAt(simTime() + truncnormal(mean, deviation), msg);
00290 
00291         // do nothing if the network is still in the initialization phase
00292         if ((!activeNetwInitPhase) && (underlayConfigurator->isInInitPhase())
00293                 || underlayConfigurator->isSimulationEndingSoon()
00294                 || nodeIsLeavingSoon)
00295             return;
00296 
00297         // create a put test message with random destination key
00298         OverlayKey destKey = OverlayKey::random();
00299         DHTputCAPICall* dhtPutMsg = new DHTputCAPICall();
00300         dhtPutMsg->setKey(destKey);
00301         dhtPutMsg->setValue(generateRandomValue());
00302         dhtPutMsg->setTtl(ttl);
00303         dhtPutMsg->setIsModifiable(true);
00304 
00305         RECORD_STATS(numSent++; numPutSent++);
00306         sendInternalRpcCall(TIER1_COMP, dhtPutMsg,
00307                 new DHTStatsContext(globalStatistics->isMeasuring(),
00308                                     simTime()));
00309     } else if (msg->isName("dhttest_get_timer")) {
00310         scheduleAt(simTime() + truncnormal(mean, deviation), msg);
00311 
00312         // do nothing if the network is still in the initialization phase
00313         if ((!activeNetwInitPhase) && (underlayConfigurator->isInInitPhase())
00314                 || underlayConfigurator->isSimulationEndingSoon()
00315                 || nodeIsLeavingSoon) {
00316             return;
00317         }
00318 
00319         const OverlayKey& key = globalDhtTestMap->getRandomKey();
00320 
00321         if (key.isUnspecified())
00322             return;
00323 
00324         DHTgetCAPICall* dhtGetMsg = new DHTgetCAPICall();
00325         dhtGetMsg->setKey(key);
00326         RECORD_STATS(numSent++; numGetSent++);
00327 
00328         sendInternalRpcCall(TIER1_COMP, dhtGetMsg,
00329                 new DHTStatsContext(globalStatistics->isMeasuring(),
00330                                     simTime()));
00331     } else if (msg->isName("dhttest_mod_timer")) {
00332         scheduleAt(simTime() + truncnormal(mean, deviation), msg);
00333 
00334         // do nothing if the network is still in the initialization phase
00335         if ((!activeNetwInitPhase) && (underlayConfigurator->isInInitPhase())
00336                 || underlayConfigurator->isSimulationEndingSoon()
00337                 || nodeIsLeavingSoon) {
00338             return;
00339         }
00340 
00341         const OverlayKey& key = globalDhtTestMap->getRandomKey();
00342 
00343         if (key.isUnspecified())
00344             return;
00345 
00346         DHTputCAPICall* dhtPutMsg = new DHTputCAPICall();
00347         dhtPutMsg->setKey(key);
00348         dhtPutMsg->setValue(generateRandomValue());
00349         dhtPutMsg->setTtl(ttl);
00350         dhtPutMsg->setIsModifiable(true);
00351 
00352         RECORD_STATS(numSent++; numPutSent++);
00353         sendInternalRpcCall(TIER1_COMP, dhtPutMsg,
00354                 new DHTStatsContext(globalStatistics->isMeasuring(),
00355                                     simTime()));
00356     }
00357 }
00358 
00359 
00360 BinaryValue DHTTestApp::generateRandomValue()
00361 {
00362     char value[DHTTESTAPP_VALUE_LEN + 1];
00363 
00364     for (int i = 0; i < DHTTESTAPP_VALUE_LEN; i++) {
00365         value[i] = intuniform(0, 25) + 'a';
00366     }
00367 
00368     value[DHTTESTAPP_VALUE_LEN] = '\0';
00369     return BinaryValue(value);
00370 }
00371 
00372 void DHTTestApp::handleNodeLeaveNotification()
00373 {
00374     nodeIsLeavingSoon = true;
00375 }
00376 
00377 void DHTTestApp::finishApp()
00378 {
00379     simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00380 
00381     // record scalar data
00382     globalStatistics->addStdDev("DHTTestApp: Sent Total Messages/s",
00383                                 numSent / time);
00384     globalStatistics->addStdDev("DHTTestApp: Sent GET Messages/s",
00385                                 numGetSent / time);
00386     globalStatistics->addStdDev("DHTTestApp: Failed GET Requests/s",
00387                                 numGetError / time);
00388     globalStatistics->addStdDev("DHTTestApp: Successful GET Requests/s",
00389                                 numGetSuccess / time);
00390 
00391     globalStatistics->addStdDev("DHTTestApp: Sent PUT Messages/s",
00392                                 numPutSent / time);
00393     globalStatistics->addStdDev("DHTTestApp: Failed PUT Requests/s",
00394                                 numPutError / time);
00395     globalStatistics->addStdDev("DHTTestApp: Successful PUT Requests/s",
00396                                 numPutSuccess / time);
00397 
00398     if (numGetSent > 0) {
00399             globalStatistics->addStdDev("DHTTestApp: GET Success Ratio",
00400                                         (double) numGetSuccess
00401                                         / (double) numGetSent);
00402     }
00403 }
00404 

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