00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <cassert>
00025
00026 #include <IPAddressResolver.h>
00027 #include <IPvXAddress.h>
00028 #include <IInterfaceTable.h>
00029 #include <IPv4InterfaceData.h>
00030 #include <RpcMacros.h>
00031 #include <InitStages.h>
00032 #include <GlobalStatistics.h>
00033 #include <LookupListener.h>
00034 #include <AbstractLookup.h>
00035
00036 #include "Bamboo.h"
00037
00038 Define_Module(Bamboo);
00039
00040 Bamboo::~Bamboo()
00041 {
00042
00043 cancelAndDelete(repairTaskTimeout);
00044 cancelAndDelete(leafsetMaintenanceTimeout);
00045 cancelAndDelete(globalTuningTimeout);
00046
00047
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 void Bamboo::initializeOverlay(int stage)
00070 {
00071 if ( stage != MIN_STAGE_OVERLAY )
00072 return;
00073
00074
00075 kbr = true;
00076
00077 baseInit();
00078
00079 repairTaskTimeoutAmount = par("repairTaskTimeoutAmount");
00080 leafsetMaintenanceTimeoutAmount = par("leafsetMaintenanceTimeoutAmount");
00081 globalTuningTimeoutAmount = par("globalTuningTimeoutAmount");
00082
00083 joinTimeout = new cMessage("joinTimeout");
00084
00085
00086 repairTaskTimeout = new cMessage("repairTaskTimeout");
00087 leafsetMaintenanceTimeout = new cMessage("leafsetMaintenanceTimeout");
00088 globalTuningTimeout = new cMessage("globalTuningTimeout");
00089
00090 sendPullFlag = true;
00091 }
00092
00093 void Bamboo::joinOverlay()
00094 {
00095 changeState(INIT);
00096
00097 if (bootstrapNode.isUnspecified()) {
00098
00099 changeState(READY);
00100 } else {
00101
00102 changeState(JOINING_2);
00103 }
00104 }
00105
00106 void Bamboo::changeState(int toState)
00107 {
00108 baseChangeState(toState);
00109
00110 switch (toState) {
00111 case INIT:
00112
00113 break;
00114
00115 case DISCOVERY:
00116
00117 break;
00118
00119 case JOINING_2:
00120
00121 {
00122 PastryLeafsetMessage* msg = new PastryLeafsetMessage("Leafset");
00123 msg->setPastryMsgType(PASTRY_MSG_LEAFSET_PULL);
00124 msg->setStatType(MAINTENANCE_STAT);
00125 msg->setSender(thisNode);
00126 msg->setSendStateTo(thisNode);
00127 leafSet->dumpToStateMessage(msg);
00128 msg->setBitLength(PASTRYLEAFSET_L(msg));
00129 RECORD_STATS(leafsetSent++; leafsetBytesSent += msg->getByteLength());
00130 std::vector<TransportAddress> sourceRoute;
00131 sourceRoute.push_back(bootstrapNode);
00132 sendToKey(thisNode.key, msg, 0, sourceRoute);
00133
00134 if (joinTimeout->isScheduled()) cancelEvent(joinTimeout);
00135 scheduleAt(simTime() + joinTimeoutAmount, joinTimeout);
00136 }
00137
00138 break;
00139
00140 case READY:
00141
00142
00143 cancelEvent(repairTaskTimeout);
00144 scheduleAt(simTime() + repairTaskTimeoutAmount, repairTaskTimeout);
00145
00146
00147
00148 cancelEvent(leafsetMaintenanceTimeout);
00149
00150 scheduleAt(simTime() + 0.2 , leafsetMaintenanceTimeout);
00151
00152 cancelEvent(globalTuningTimeout);
00153 scheduleAt(simTime() + globalTuningTimeoutAmount, globalTuningTimeout);
00154
00155 break;
00156 }
00157 }
00158
00159 void Bamboo::handleTimerEvent(cMessage* msg)
00160 {
00161
00162 if (msg == joinTimeout) {
00163 EV << "[Bamboo::handleTimerEvent() @ " << thisNode.ip
00164 << " (" << thisNode.key.toString(16) << ")]\n"
00165 << " join timeout expired, restarting..."
00166 << endl;
00167 joinOverlay();
00168 } else if (msg == repairTaskTimeout) {
00169 EV << "[Bamboo::handleTimerEvent() @ " << thisNode.ip
00170 << " (" << thisNode.key.toString(16) << ")]\n"
00171 << " starting routing table maintenance"
00172 << endl;
00173 doRoutingTableMaintenance();
00174 scheduleAt(simTime() + repairTaskTimeoutAmount, repairTaskTimeout);
00175 } else if (msg == leafsetMaintenanceTimeout) {
00176 EV << "[Bamboo::handleTimerEvent() @ " << thisNode.ip
00177 << " (" << thisNode.key.toString(16) << ")]\n"
00178 << " starting leafset maintenance"
00179 << endl;
00180 doLeafsetMaintenance();
00181 scheduleAt(simTime() + leafsetMaintenanceTimeoutAmount,
00182 leafsetMaintenanceTimeout);
00183 } else if (msg == globalTuningTimeout) {
00184 EV << "[Bamboo::handleTimerEvent() @ " << thisNode.ip
00185 << " (" << thisNode.key.toString(16) << ")]\n"
00186 << " starting global tuning"
00187 << endl;
00188 doGlobalTuning();
00189 scheduleAt(simTime() + globalTuningTimeoutAmount, globalTuningTimeout);
00190 }
00191 }
00192
00193 void Bamboo::handleUDPMessage(BaseOverlayMessage* msg)
00194 {
00195 PastryMessage* pastryMsg = check_and_cast<PastryMessage*>(msg);
00196 uint32_t type = pastryMsg->getPastryMsgType();
00197
00198 if (debugOutput) {
00199 EV << "[Bamboo::handleUDPMessage() @ " << thisNode.ip
00200 << " (" << thisNode.key.toString(16) << ")]\n"
00201 << " incoming message of type ";
00202 switch(type) {
00203 case PASTRY_MSG_STD:
00204 EV << "PASTRY_MSG_STD";
00205 break;
00206 case PASTRY_MSG_JOIN:
00207 EV << "PASTRY_MSG_JOIN";
00208 break;
00209 case PASTRY_MSG_STATE:
00210 EV << "PASTRY_MSG_STATE";
00211 break;
00212 case PASTRY_MSG_LEAFSET:
00213 EV << "PASTRY_MSG_LEAFSET";
00214 break;
00215 case PASTRY_MSG_LEAFSET_PULL:
00216 EV << "PASTRY_MSG_LEAFSET_PULL";
00217 break;
00218 case PASTRY_MSG_ROWREQ:
00219 EV << "PASTRY_MSG_ROWREQ";
00220 break;
00221 case PASTRY_MSG_RROW:
00222 EV << "PASTRY_MSG_RROW";
00223 break;
00224 case PASTRY_MSG_REQ:
00225 EV << "PASTRY_MSG_REQ";
00226 break;
00227 default:
00228 EV << "UNKNOWN (" << type <<")";
00229 break;
00230 }
00231 EV << endl;
00232 }
00233
00234 switch (type) {
00235 case PASTRY_MSG_STD:
00236 opp_error("Pastry received PastryMessage of unknown type!");
00237 break;
00238 case PASTRY_MSG_JOIN:
00239
00240 break;
00241
00242 case PASTRY_MSG_LEAFSET: {
00243 PastryLeafsetMessage* lmsg =
00244 check_and_cast<PastryLeafsetMessage*>(pastryMsg);
00245 RECORD_STATS(leafsetReceived++; leafsetBytesReceived +=
00246 lmsg->getByteLength());
00247
00248 if (state == JOINING_2) {
00249 cancelEvent(joinTimeout);
00250 }
00251
00252 if ((state == JOINING_2) || (state == READY)) {
00253 handleLeafsetMessage(lmsg, true);
00254 } else {
00255 delete lmsg;
00256 }
00257 }
00258 break;
00259
00260 case PASTRY_MSG_LEAFSET_PULL: {
00261 PastryLeafsetMessage* lmsg =
00262 check_and_cast<PastryLeafsetMessage*>(pastryMsg);
00263 RECORD_STATS(leafsetReceived++; leafsetBytesReceived +=
00264 lmsg->getByteLength());
00265
00266 if (state == READY) {
00267
00268 sendLeafset(lmsg->getSendStateTo());
00269 handleLeafsetMessage(lmsg, true);
00270
00271 } else {
00272 delete lmsg;
00273 }
00274 }
00275 break;
00276
00277 case PASTRY_MSG_ROWREQ: {
00278
00279 PastryRoutingRowRequestMessage* rtrmsg =
00280 check_and_cast<PastryRoutingRowRequestMessage*>(pastryMsg);
00281 RECORD_STATS(routingTableReqReceived++; routingTableReqBytesReceived +=
00282 rtrmsg->getByteLength());
00283 if (state == READY)
00284 if (rtrmsg->getRow() == -1)
00285 sendRoutingRow(rtrmsg->getSendStateTo(), routingTable->getLastRow());
00286 else if (rtrmsg->getRow() > routingTable->getLastRow())
00287 EV << "[Bamboo::handleUDPMessage() @ " << thisNode.ip
00288 << " (" << thisNode.key.toString(16) << ")]\n"
00289 << " received request for nonexistent routing"
00290 << "table row, dropping message!" << endl;
00291 else sendRoutingRow(rtrmsg->getSendStateTo(), rtrmsg->getRow());
00292 else
00293 EV << "[Bamboo::handleUDPMessage() @ " << thisNode.ip
00294 << " (" << thisNode.key.toString(16) << ")]\n"
00295 << " received routing table request before reaching "
00296 << "READY state, dropping message!" << endl;
00297 delete rtrmsg;
00298 }
00299 break;
00300
00301 case PASTRY_MSG_RROW: {
00302 PastryRoutingRowMessage* rtmsg =
00303 check_and_cast<PastryRoutingRowMessage*>(pastryMsg);
00304 RECORD_STATS(routingTableReceived++; routingTableBytesReceived +=
00305 rtmsg->getByteLength());
00306
00307 if (state == READY) {
00308
00309
00310 PastryStateMessage* stateMsg;
00311
00312 stateMsg = new PastryStateMessage("STATE");
00313 stateMsg->setTimestamp(rtmsg->getTimestamp());
00314 stateMsg->setPastryMsgType(PASTRY_MSG_STATE);
00315 stateMsg->setStatType(MAINTENANCE_STAT);
00316 stateMsg->setSender(rtmsg->getSender());
00317 stateMsg->setLeafSetArraySize(0);
00318 stateMsg->setNeighborhoodSetArraySize(0);
00319 stateMsg->setRoutingTableArraySize(rtmsg->getRoutingTableArraySize());
00320
00321 for (uint32_t i = 0; i < rtmsg->getRoutingTableArraySize(); i++) {
00322 stateMsg->setRoutingTable(i, rtmsg->getRoutingTable(i));
00323 }
00324
00325 handleStateMessage(stateMsg);
00326 }
00327
00328 delete rtmsg;
00329 }
00330 break;
00331
00332 case PASTRY_MSG_REQ: {
00333 PastryRequestMessage* lrmsg =
00334 check_and_cast<PastryRequestMessage*>(pastryMsg);
00335 handleRequestMessage(lrmsg);
00336 }
00337 break;
00338
00339 case PASTRY_MSG_STATE: {
00340 PastryStateMessage* stateMsg =
00341 check_and_cast<PastryStateMessage*>(msg);
00342 RECORD_STATS(stateReceived++; stateBytesReceived +=
00343 stateMsg->getByteLength());
00344 handleStateMessage(stateMsg);
00345 }
00346 break;
00347 }
00348 }
00349
00350 void Bamboo::doLeafsetMaintenance(void)
00351 {
00352
00353 const TransportAddress& ask = leafSet->getRandomNode();
00354 if (!ask.isUnspecified()) {
00355 sendLeafset(ask, true);
00356 EV << "[Bamboo::doLeafsetMaintenance()]\n"
00357 << " leafset maintenance: pulling leafset from "
00358 << ask << endl;
00359 }
00360
00361 }
00362
00363 void Bamboo::doGlobalTuning(void)
00364 {
00365
00366 OverlayKey newKey = OverlayKey::random();
00367
00368
00369 createLookup()->lookup(newKey, 1, 0, 0,
00370 NULL);
00371
00372 }
00373
00374 bool Bamboo::handleFailedNode(const TransportAddress& failed)
00375 {
00376 if (state != READY) {
00377 return false;
00378 }
00379
00380 if (failed.isUnspecified())
00381 opp_error("Bamboo::handleFailedNode(): failed is unspecified!");
00382
00383 const TransportAddress& lsAsk = leafSet->failedNode(failed);
00384 routingTable->failedNode(failed);
00385 neighborhoodSet->failedNode(failed);
00386
00387 if (lsAsk.isUnspecified() && (! leafSet->isValid())) {
00388 EV << "[Bamboo::handleFailedNode()]\n"
00389 << " lost connection to the network, trying to re-join."
00390 << endl;
00391 joinOverlay();
00392 return false;
00393 }
00394
00395 return true;
00396 }
00397
00398 void Bamboo::checkProxCache(void)
00399 {
00400 if (state == JOINING_2) {
00401 changeState(READY);
00402 return;
00403 }
00404
00405
00406 simtime_t now = simTime();
00407
00408
00409 if (!(stateCache.msg && stateCache.prox)) return;
00410
00411
00412 if (find(stateCache.prox->pr_rt.begin(), stateCache.prox->pr_rt.end(),
00413 PASTRY_PROX_PENDING) != stateCache.prox->pr_rt.end()) return;
00414 if (find(stateCache.prox->pr_ls.begin(), stateCache.prox->pr_ls.end(),
00415 PASTRY_PROX_PENDING) != stateCache.prox->pr_ls.end()) return;
00416 if (find(stateCache.prox->pr_ns.begin(), stateCache.prox->pr_ns.end(),
00417 PASTRY_PROX_PENDING) != stateCache.prox->pr_ns.end()) return;
00418
00419
00420
00421 if (neighborhoodSet->mergeState(stateCache.msg, stateCache.prox))
00422 lastStateChange = now;
00423 EV << "[Bamboo::checkProxCache()]\n"
00424 << " Merging nodes into routing table."
00425 << endl;
00426 if (routingTable->mergeState(stateCache.msg, stateCache.prox)) {
00427 lastStateChange = now;
00428 EV << "[Bamboo::checkProxCache()]\n"
00429 << " Merged nodes into routing table."
00430 << endl;
00431 }
00432
00433 updateTooltip();
00434
00435 delete stateCache.msg;
00436 stateCache.msg = NULL;
00437 delete stateCache.prox;
00438 stateCache.prox = NULL;
00439
00440
00441 if (! stateCacheQueue.empty()) {
00442 stateCache = stateCacheQueue.front();
00443 stateCacheQueue.pop();
00444 pingNodes();
00445 }
00446
00447 }
00448
00449 void Bamboo::handleStateMessage(PastryStateMessage* msg)
00450 {
00451 if (debugOutput) {
00452 EV << "[Bamboo::handleStateMessage() @ " << thisNode.ip
00453 << " (" << thisNode.key.toString(16) << ")]\n"
00454 << " new STATE message to process "
00455 << static_cast<void*>(msg) << " in state "
00456 << ((state == READY)?"READY":((state == JOINING_2)?"JOIN":"INIT"))
00457 << endl;
00458 }
00459
00460 if (state == INIT) {
00461 EV << "[Bamboo::handleStateMessage() @ " << thisNode.ip
00462 << " (" << thisNode.key.toString(16) << ")]\n"
00463 << " can't handle state messages until at least reaching JOIN state."
00464 << endl;
00465 delete msg;
00466 return;
00467 }
00468
00469 PastryStateMsgHandle handle(msg);
00470
00471 if (state == JOINING_2) {
00472 determineAliveTable(msg);
00473 leafSet->mergeState(msg, &aliveTable);
00474
00475 lastStateChange = simTime();
00476 newLeafs();
00477 updateTooltip();
00478
00479
00480 stateCache = handle;
00481 pingNodes();
00482
00483 return;
00484 }
00485
00486
00487
00488 determineAliveTable(msg);
00489 if (leafSet->mergeState(msg, &aliveTable)) {
00490
00491 lastStateChange = simTime();
00492 newLeafs();
00493 updateTooltip();
00494 }
00495
00496 if (!stateCache.msg) {
00497
00498 stateCache = handle;
00499 pingNodes();
00500 } else {
00501
00502 stateCacheQueue.push(handle);
00503 prePing(msg);
00504 }
00505 }
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517