GIASearchApp.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <IPAddressResolver.h>
00025
00026 #include <InitStages.h>
00027 #include <CommonMessages_m.h>
00028 #include <ExtAPIMessages_m.h>
00029 #include <GiaMessage_m.h>
00030 #include <GlobalStatistics.h>
00031
00032 #include "GIASearchApp.h"
00033
00034
00035 Define_Module(GIASearchApp);
00036
00037 GIASearchApp::GIASearchApp()
00038 {
00039 search_timer = keyList_timer = NULL;
00040 srMsgBook = NULL;
00041 }
00042
00043 GIASearchApp::~GIASearchApp()
00044 {
00045 cancelAndDelete(search_timer);
00046 cancelAndDelete(keyList_timer);
00047 if (srMsgBook != NULL) {
00048 delete srMsgBook;
00049 srMsgBook = NULL;
00050 }
00051 }
00052
00053 void GIASearchApp::initializeApp(int stage)
00054 {
00055 if (stage != MIN_STAGE_APP)
00056 return;
00057
00058
00059 mean = par("messageDelay");
00060 deviation = mean / 3;
00061 randomNodes = par("randomNodes");
00062 maxResponses = par("maxResponses");
00063
00064 srMsgBook = new SearchMsgBookkeeping();
00065
00066
00067 stat_keyListMessagesSent = 0;
00068 stat_keyListBytesSent = 0;
00069 stat_searchMessagesSent = 0;
00070 stat_searchBytesSent = 0;
00071 stat_searchResponseMessages = 0;
00072 stat_searchResponseBytes = 0;
00073
00074
00075 search_timer = new cMessage("search_timer");
00076 scheduleAt(simTime() + truncnormal(mean, deviation),
00077 search_timer);
00078
00079 keyList_timer = new cMessage("keyList_timer");
00080 scheduleAt(simTime() + uniform(0.0, 10.0), keyList_timer);
00081 }
00082
00083 void GIASearchApp::handleTimerEvent(cMessage *msg)
00084 {
00085 if(msg == keyList_timer) {
00086 keyList = globalNodeList->getKeyList(par("maximumKeys"));
00087 WATCH_VECTOR(*keyList);
00088
00089
00090 GIAput* putMsg = new GIAput("GIA-Keylist");
00091 putMsg->setCommand(GIA_PUT);
00092
00093 putMsg->setKeysArraySize(keyList->size());
00094
00095 std::vector<OverlayKey>::iterator it;
00096 int k;
00097 for(it = keyList->begin(), k = 0; it != keyList->end(); it++, k++)
00098 putMsg->setKeys(k, *it);
00099
00100 putMsg->setBitLength(GIAPUT_L(putMsg));
00101
00102 sendMessageToLowerTier(putMsg);
00103
00104 if (debugOutput)
00105 EV << "[GIASearchApp::handleTimerEvent() @ " << overlay->getThisNode().getAddress()<< "]\n"
00106 << " Node sent keylist to overlay."
00107 << endl;
00108
00109 stat_keyListMessagesSent++;
00110 stat_keyListBytesSent += putMsg->getByteLength();
00111 }
00112 else if(msg == search_timer) {
00113
00114 scheduleAt(simTime() + truncnormal(mean, deviation), msg);
00115
00116
00117 if((!par("activeNetwInitPhase")) && (underlayConfigurator->isInInitPhase()))
00118 return;
00119
00120 OverlayKey keyListItem;
00121 uint32_t maximumTries = 20;
00122
00123 do {
00124 if (maximumTries-- == 0)
00125 break;
00126 keyListItem = globalNodeList->getRandomKeyListItem();
00127 } while ((keyListItem.isUnspecified())
00128 && ((srMsgBook->contains(keyListItem))));
00129
00130 if (!keyListItem.isUnspecified()) {
00131
00132 GIAsearch* getMsg = new GIAsearch("GIA-Search");
00133 getMsg->setCommand(GIA_SEARCH);
00134 getMsg->setSearchKey(keyListItem);
00135 getMsg->setMaxResponses(maxResponses);
00136 getMsg->setBitLength(GIAGET_L(getMsg));
00137
00138 sendMessageToLowerTier(getMsg);
00139
00140
00141 srMsgBook->addMessage(keyListItem);
00142
00143 if (debugOutput)
00144 EV << "[GIASearchApp::handleTimerEvent() @ " << overlay->getThisNode().getAddress()<< "]\n"
00145 << " Node sent get-message to overlay."
00146 << endl;
00147
00148 stat_searchMessagesSent++;
00149 stat_searchBytesSent += getMsg->getByteLength();
00150 }
00151 }
00152 }
00153
00154 void GIASearchApp::handleLowerMessage(cMessage* msg)
00155 {
00156 GIAanswer* answer = check_and_cast<GIAanswer*>(msg);
00157 OverlayCtrlInfo* overlayCtrlInfo =
00158 check_and_cast<OverlayCtrlInfo*>(answer->removeControlInfo());
00159
00160 OverlayKey searchKey = answer->getSearchKey();
00161
00162 if (debugOutput)
00163 EV << "[GIASearchApp::handleLowerMessage() @ " << overlay->getThisNode().getAddress()<< "]\n"
00164 << " Node received answer-message from overlay:"
00165 << " (key: " << searchKey
00166 << " at node " << answer->getNode() << ")"
00167 << endl;
00168
00169 stat_searchResponseMessages++;
00170 stat_searchResponseBytes += answer->getByteLength();
00171
00172 if (srMsgBook->contains(searchKey))
00173 srMsgBook->updateItem(searchKey, overlayCtrlInfo->getHopCount());
00174
00175 delete answer;
00176 }
00177
00178 void GIASearchApp::finishApp()
00179 {
00180
00181 GiaSearchStats stats = srMsgBook->getStatisticalData();
00182
00183 if (stats.minDelay == 0 &&
00184 stats.maxDelay == 0 &&
00185 stats.minHopCount == 0 &&
00186 stats.maxHopCount == 0 &&
00187 stats.responseCount == 0) return;
00188
00189 globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. min delay",
00190 stats.minDelay);
00191 globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. max delay",
00192 stats.maxDelay);
00193 globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. min hops",
00194 stats.minHopCount);
00195 globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. max hops",
00196 stats.maxHopCount);
00197 globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. response count",
00198 stats.responseCount);
00199 }