00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026 #include <cassert>
00027
00028 #include <IPAddressResolver.h>
00029 #include <IPvXAddress.h>
00030 #include <IInterfaceTable.h>
00031 #include <IPv4InterfaceData.h>
00032 #include <RpcMacros.h>
00033 #include <InitStages.h>
00034 #include <GlobalStatistics.h>
00035
00036 #include "Pastry.h"
00037
00038
00039 Define_Module(Pastry);
00040
00041 Pastry::~Pastry()
00042 {
00043
00044 cancelAndDelete(readyWait);
00045 cancelAndDelete(joinUpdateWait);
00046 cancelAndDelete(secondStageWait);
00047 cancelAndDelete(ringCheck);
00048 if (useDiscovery) cancelAndDelete(discoveryTimeout);
00049 if (repairTaskTimeoutAmount > 0) cancelAndDelete(repairTaskTimeout);
00050 }
00051
00052 void Pastry::initializeOverlay(int stage)
00053 {
00054 if ( stage != MIN_STAGE_OVERLAY )
00055 return;
00056
00057
00058 kbr = true;
00059
00060 baseInit();
00061
00062 useDiscovery = par("useDiscovery");
00063 pingBeforeSecondStage = par("pingBeforeSecondStage");
00064 secondStageWaitAmount = par("secondStageWait");
00065 discoveryTimeoutAmount = par("discoveryTimeoutAmount");
00066 repairTaskTimeoutAmount = par("repairTaskTimeoutAmount");
00067 sendStateAtLeafsetRepair = par("sendStateAtLeafsetRepair");
00068 ringCheckInterval = par("ringCheckInterval");
00069 partialJoinPath = par("partialJoinPath");
00070 readyWaitAmount = par("readyWait");
00071
00072 overrideOldPastry = par("overrideOldPastry");
00073 overrideNewPastry = par("overrideNewPastry");
00074
00075 if (overrideOldPastry) {
00076
00077
00078 useDiscovery = false;
00079 sendStateAtLeafsetRepair = true;
00080 repairTaskTimeoutAmount = 0;
00081 }
00082
00083 if (overrideNewPastry) {
00084
00085 secondStageWaitAmount = 0;
00086 useDiscovery = true;
00087 discoveryTimeoutAmount = 0.4;
00088 repairTaskTimeoutAmount = 60;
00089 sendStateAtLeafsetRepair = false;
00090 }
00091
00092 joinTimeout = new cMessage("joinTimeout");
00093 readyWait = new cMessage("readyWait");
00094 secondStageWait = new cMessage("secondStageWait");
00095 joinUpdateWait = new cMessage("joinUpdateWait");
00096 ringCheck = new cMessage("ringCheck");
00097
00098 if (useDiscovery) discoveryTimeout = new cMessage("discoveryTimeout");
00099 if (repairTaskTimeoutAmount > 0) repairTaskTimeout =
00100 new cMessage("repairTaskTimeout");
00101
00102 sendPullFlag = false;
00103
00104 }
00105
00106 void Pastry::joinOverlay()
00107 {
00108 changeState(INIT);
00109
00110 if (bootstrapNode.isUnspecified()) {
00111
00112 changeState(READY);
00113 } else {
00114
00115 nearNode = bootstrapNode;
00116 if (useDiscovery) changeState(DISCOVERY);
00117 else changeState(JOINING_2);
00118 }
00119 }
00120
00121 void Pastry::changeState(int toState)
00122 {
00123 if (ringCheck->isScheduled()) cancelEvent(ringCheck);
00124 if (readyWait->isScheduled()) cancelEvent(readyWait);
00125 baseChangeState(toState);
00126
00127 switch (toState) {
00128 case INIT:
00129 cancelAllRpcs();
00130 purgeVectors();
00131 break;
00132
00133 case DISCOVERY:
00134 state = DISCOVERY;
00135
00136 nearNodeRtt = MAXTIME;
00137 pingNode(bootstrapNode, discoveryTimeoutAmount, 0,
00138 NULL, "PING bootstrapNode in discovery mode",
00139 NULL, -1, UDP_TRANSPORT, false);
00140 sendRequest(bootstrapNode, PASTRY_REQ_LEAFSET);
00141 depth = -1;
00142
00143 break;
00144
00145 case JOINING_2:
00146
00147 joinHopCount = 0;
00148
00149 {
00150 PastryJoinMessage* msg = new PastryJoinMessage("JOIN-Request");
00151
00152 msg->setPastryMsgType(PASTRY_MSG_JOIN);
00153 msg->setStatType(MAINTENANCE_STAT);
00154
00155 msg->setSendStateTo(thisNode);
00156 msg->setBitLength(PASTRYJOIN_L(msg));
00157 RECORD_STATS(joinSent++; joinBytesSent += msg->getByteLength());
00158 std::vector<TransportAddress> sourceRoute;
00159 sourceRoute.push_back(nearNode);
00160 sendToKey(thisNode.key, msg, 0, sourceRoute);
00161
00162 }
00163
00164
00165 break;
00166
00167 case READY:
00168 if (ringCheckInterval > 0) {
00169 scheduleAt(simTime()+ringCheckInterval, ringCheck);
00170 }
00171
00172
00173 notifyList.clear();
00174 leafSet->dumpToVector(notifyList);
00175 routingTable->dumpToVector(notifyList);
00176 sort(notifyList.begin(), notifyList.end());
00177 notifyList.erase(unique(notifyList.begin(), notifyList.end()),
00178 notifyList.end());
00179
00180
00181 cancelEvent(joinUpdateWait);
00182 scheduleAt(simTime() + sendStateWaitAmount, joinUpdateWait);
00183
00184
00185 if (secondStageWaitAmount > 0) {
00186 cancelEvent(secondStageWait);
00187 scheduleAt(simTime() + secondStageWaitAmount, secondStageWait);
00188 }
00189
00190
00191 if (repairTaskTimeoutAmount > 0) {
00192 cancelEvent(repairTaskTimeout);
00193 scheduleAt(simTime() + repairTaskTimeoutAmount, repairTaskTimeout);
00194
00195 }
00196
00197 break;
00198 }
00199 }
00200
00201 void Pastry::handleTimerEvent(cMessage* msg)
00202 {
00203
00204 if (msg->isName("joinTimeout")) {
00205 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00206 << " (" << thisNode.key.toString(16) << ")]\n"
00207 << " timeout expired, restarting..."
00208 << endl;
00209
00210 joinOverlay();
00211 } else if (msg->isName("readyWait")) {
00212 if (partialJoinPath) {
00213 RECORD_STATS(joinPartial++);
00214 sort(stReceived.begin(), stReceived.end(), stateMsgIsSmaller);
00215
00216
00217 stReceivedPos = stReceived.begin();
00218 stateCache = *stReceivedPos;
00219 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00220 << " (" << thisNode.key.toString(16) << ")]\n"
00221 << " joining despite some missing STATE messages."
00222 << endl;
00223 processState();
00224
00225
00226
00227
00228
00229
00230
00231
00232 } else {
00233 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00234 << " (" << thisNode.key.toString(16) << ")]\n"
00235 << " timeout waiting for missing state messages in JOIN state, restarting..."
00236 << endl;
00237
00238
00239 joinOverlay();
00240 }
00241 } else if (msg->isName("joinUpdateWait")) {
00242 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00243 << " (" << thisNode.key.toString(16) << ")]\n"
00244 << " sending state updates to all nodes."
00245 << endl;
00246 doJoinUpdate();
00247 } else if (msg->isName("secondStageWait")) {
00248 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00249 << " (" << thisNode.key.toString(16) << ")]\n"
00250 << " sending STATE requests to all nodes in second stage of initialization."
00251 << endl;
00252 doSecondStage();
00253 } else if (msg->isName("ringCheck")) {
00254 if (state == READY) {
00255
00256 const NodeHandle& pred = leafSet->getPredecessor();
00257 const NodeHandle& succ = leafSet->getSuccessor();
00258 if (! pred.isUnspecified()) {
00259 pingNode(pred, timeoutPing, pingRetries,
00260 NULL, "PING ring check");
00261 }
00262 if (! succ.isUnspecified()) {
00263 pingNode(succ, timeoutPing, pingRetries,
00264 NULL, "PING ring check");
00265 }
00266 }
00267 scheduleAt(simTime() + ringCheckInterval, ringCheck);
00268 } else if (msg->isName("sendStateWait")) {
00269 PastrySendState* sendStateMsg = check_and_cast<PastrySendState*>(msg);
00270
00271 std::vector<PastrySendState*>::iterator pos =
00272 std::find(sendStateWait.begin(), sendStateWait.end(),
00273 sendStateMsg);
00274 if (pos != sendStateWait.end()) sendStateWait.erase(pos);
00275
00276 sendStateTables(sendStateMsg->getDest());
00277 delete sendStateMsg;
00278 } else if (msg->isName("discoveryTimeout")) {
00279 if ((depth == 0) && (nearNodeImproved)) {
00280 depth++;
00281 }
00282 if ((depth == 0) || (pingedNodes < 1)) {
00283 changeState(JOINING_2);
00284 } else {
00285 PastryRoutingRowRequestMessage* msg = new PastryRoutingRowRequestMessage("ROWREQ");
00286 msg->setPastryMsgType(PASTRY_MSG_ROWREQ);
00287 msg->setStatType(MAINTENANCE_STAT);
00288 msg->setSendStateTo(thisNode);
00289 msg->setRow(depth);
00290 msg->setBitLength(PASTRYRTREQ_L(msg));
00291 RECORD_STATS(routingTableReqSent++; routingTableReqBytesSent += msg->getByteLength());
00292 sendMessageToUDP(nearNode, msg);
00293 }
00294 } else if (msg->isName("repairTaskTimeout")) {
00295 EV << "[Pastry::handleTimerEvent() @ " << thisNode.ip
00296 << " (" << thisNode.key.toString(16) << ")]\n"
00297 << " starting routing table maintenance"
00298 << endl;
00299 doRoutingTableMaintenance();
00300 scheduleAt(simTime() + repairTaskTimeoutAmount, repairTaskTimeout);
00301 }
00302 }
00303
00304 void Pastry::handleUDPMessage(BaseOverlayMessage* msg)
00305 {
00306 PastryMessage* pastryMsg = check_and_cast<PastryMessage*>(msg);
00307 uint32_t type = pastryMsg->getPastryMsgType();
00308
00309 if (debugOutput) {
00310 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00311 << " (" << thisNode.key.toString(16) << ")]\n"
00312 << " incoming message of type ";
00313 switch(type) {
00314 case PASTRY_MSG_STD:
00315 EV << "PASTRY_MSG_STD";
00316 break;
00317 case PASTRY_MSG_JOIN:
00318 EV << "PASTRY_MSG_JOIN";
00319 break;
00320 case PASTRY_MSG_STATE:
00321 EV << "PASTRY_MSG_STATE";
00322 break;
00323 case PASTRY_MSG_LEAFSET:
00324 EV << "PASTRY_MSG_LEAFSET";
00325 break;
00326 case PASTRY_MSG_ROWREQ:
00327 EV << "PASTRY_MSG_ROWREQ";
00328 break;
00329 case PASTRY_MSG_RROW:
00330 EV << "PASTRY_MSG_RROW";
00331 break;
00332 case PASTRY_MSG_REQ:
00333 EV << "PASTRY_MSG_REQ";
00334 break;
00335 default:
00336 EV << "UNKNOWN (" << type <<")";
00337 break;
00338 }
00339 EV << endl;
00340 }
00341
00342 switch (type) {
00343 case PASTRY_MSG_STD:
00344 opp_error("Pastry received PastryMessage of unknown type!");
00345 break;
00346
00347 case PASTRY_MSG_JOIN: {
00348 PastryJoinMessage* jmsg =
00349 check_and_cast<PastryJoinMessage*>(pastryMsg);
00350 RECORD_STATS(joinReceived++; joinBytesReceived +=
00351 jmsg->getByteLength());
00352 if (state != READY) {
00353 if (jmsg->getSendStateTo() == thisNode) {
00354 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00355 << " (" << thisNode.key.toString(16) << ")]\n"
00356 << " PastryJoinMessage received by originator!"
00357 << endl;
00358 } else {
00359 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00360 << " (" << thisNode.key.toString(16) << ")]\n"
00361 << " received join message before reaching "
00362 << "READY state, dropping message!"
00363 << endl;
00364 }
00365 }
00366 else if (jmsg->getSendStateTo() == thisNode) {
00367 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00368 << " (" << thisNode.key.toString(16) << ")]\n"
00369 << " PastryJoinMessage gets dropped because it is "
00370 << "outdated and has been received by originator!"
00371 << endl;
00372 } else {
00373 OverlayCtrlInfo* overlayCtrlInfo
00374 = check_and_cast<OverlayCtrlInfo*>(jmsg->getControlInfo());
00375
00376 uint32_t joinHopCount = overlayCtrlInfo->getHopCount();
00377 if ((joinHopCount > 1) &&
00378 ((defaultRoutingType == ITERATIVE_ROUTING) ||
00379 (defaultRoutingType == EXHAUSTIVE_ITERATIVE_ROUTING)))
00380 joinHopCount--;
00381
00382
00383 handleFailedNode(jmsg->getSendStateTo());
00384
00385 sendStateTables(jmsg->getSendStateTo(),
00386 PASTRY_STATE_JOIN, joinHopCount, true);
00387 }
00388
00389 delete jmsg;
00390 }
00391 break;
00392
00393 case PASTRY_MSG_LEAFSET: {
00394 PastryLeafsetMessage* lmsg =
00395 check_and_cast<PastryLeafsetMessage*>(pastryMsg);
00396 RECORD_STATS(leafsetReceived++; leafsetBytesReceived +=
00397 lmsg->getByteLength());
00398
00399 if (state == DISCOVERY) {
00400 uint32_t lsSize = lmsg->getLeafSetArraySize();
00401 const NodeHandle* node;
00402 pingedNodes = 0;
00403
00404 for (uint32_t i = 0; i < lsSize; i++) {
00405 node = &(lmsg->getLeafSet(i));
00406
00407 if ( !(node->isUnspecified()) ) {
00408 pingNode(*node, discoveryTimeoutAmount, 0,
00409 NULL, "PING received leafs for nearest node",
00410 NULL, -1, UDP_TRANSPORT, false);
00411 pingedNodes++;
00412 }
00413 }
00414
00415 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00416 << " (" << thisNode.key.toString(16) << ")]\n"
00417 << " received leafset, waiting for pings"
00418 << endl;
00419 if (discoveryTimeout->isScheduled()) cancelEvent(discoveryTimeout);
00420 scheduleAt(simTime() + discoveryTimeoutAmount, discoveryTimeout);
00421 delete lmsg;
00422 }
00423
00424 else if (state == READY) {
00425 handleLeafsetMessage(lmsg, false);
00426
00427 } else {
00428 delete lmsg;
00429 }
00430 }
00431 break;
00432
00433 case PASTRY_MSG_ROWREQ: {
00434 PastryRoutingRowRequestMessage* rtrmsg =
00435 check_and_cast<PastryRoutingRowRequestMessage*>(pastryMsg);
00436 RECORD_STATS(routingTableReqReceived++; routingTableReqBytesReceived +=
00437 rtrmsg->getByteLength());
00438 if (state == READY)
00439 if (rtrmsg->getRow() == -1)
00440 sendRoutingRow(rtrmsg->getSendStateTo(), routingTable->getLastRow());
00441 else if (rtrmsg->getRow() > routingTable->getLastRow())
00442 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00443 << " (" << thisNode.key.toString(16) << ")]\n"
00444 << " received request for nonexistent routing"
00445 << "table row, dropping message!"
00446 << endl;
00447 else sendRoutingRow(rtrmsg->getSendStateTo(), rtrmsg->getRow());
00448 else
00449 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00450 << " (" << thisNode.key.toString(16) << ")]\n"
00451 << " received routing table request before reaching "
00452 << "READY state, dropping message!"
00453 << endl;
00454 delete rtrmsg;
00455 }
00456 break;
00457
00458 case PASTRY_MSG_RROW: {
00459 PastryRoutingRowMessage* rtmsg =
00460 check_and_cast<PastryRoutingRowMessage*>(pastryMsg);
00461 RECORD_STATS(routingTableReceived++; routingTableBytesReceived +=
00462 rtmsg->getByteLength());
00463
00464 if (state == DISCOVERY) {
00465 uint32_t nodesPerRow = rtmsg->getRoutingTableArraySize();
00466 const NodeHandle* node;
00467 if (depth == -1) {
00468 depth = rtmsg->getRow();
00469 }
00470 pingedNodes = 0;
00471 nearNodeImproved = false;
00472
00473 if (depth > 0) {
00474 for (uint32_t i = 0; i < nodesPerRow; i++) {
00475 node = &(rtmsg->getRoutingTable(i));
00476
00477 if ( !(node->isUnspecified()) ) {
00478
00479 pingNode(*node, discoveryTimeoutAmount, 0, NULL,
00480 "PING received routing table for nearest node",
00481 NULL, -1, UDP_TRANSPORT, false);
00482 pingedNodes++;
00483 }
00484 }
00485 depth--;
00486 }
00487 EV << "[Pastry::handleUDPMessage() @ " << thisNode.ip
00488 << " (" << thisNode.key.toString(16) << ")]\n"
00489 << " received routing table, waiting for pings"
00490 << endl;
00491 if (discoveryTimeout->isScheduled()) {
00492 cancelEvent(discoveryTimeout);
00493 }
00494 scheduleAt(simTime() + discoveryTimeoutAmount, discoveryTimeout);
00495 }
00496
00497 else if (state == READY) {
00498
00499 uint32_t nodesPerRow = rtmsg->getRoutingTableArraySize();
00500 PastryStateMessage* stateMsg;
00501
00502 stateMsg = new PastryStateMessage("STATE");
00503 stateMsg->setTimestamp(rtmsg->getTimestamp());
00504 stateMsg->setPastryMsgType(PASTRY_MSG_STATE);
00505 stateMsg->setStatType(MAINTENANCE_STAT);
00506 stateMsg->setSender(rtmsg->getSender());
00507 stateMsg->setLeafSetArraySize(0);
00508 stateMsg->setNeighborhoodSetArraySize(0);
00509 stateMsg->setRoutingTableArraySize(nodesPerRow);
00510
00511 for (uint32_t i = 0; i < nodesPerRow; i++) {
00512 stateMsg->setRoutingTable(i, rtmsg->getRoutingTable(i));
00513 }
00514
00515 handleStateMessage(stateMsg);
00516 }
00517
00518 delete rtmsg;
00519 }
00520 break;
00521
00522 case PASTRY_MSG_REQ: {
00523 PastryRequestMessage* lrmsg =
00524 check_and_cast<PastryRequestMessage*>(pastryMsg);
00525 handleRequestMessage(lrmsg);
00526 }
00527 break;
00528
00529 case PASTRY_MSG_STATE: {
00530 PastryStateMessage* stateMsg =
00531 check_and_cast<PastryStateMessage*>(msg);
00532 RECORD_STATS(stateReceived++; stateBytesReceived +=
00533 stateMsg->getByteLength());
00534 handleStateMessage(stateMsg);
00535 }
00536 break;
00537 }
00538 }
00539
00540 void Pastry::doJoinUpdate(void)
00541 {
00542
00543
00544
00545 std::vector<TransportAddress>::iterator nListPos;
00546 if (!stReceived.empty()) {
00547 for (std::vector<PastryStateMsgHandle>::iterator it =
00548 stReceived.begin(); it != stReceived.end(); ++it) {
00549 simtime_t timestamp = it->msg->getTimestamp();
00550 sendStateTables(it->msg->getSender(), PASTRY_STATE_UPDATE,
00551 ×tamp);
00552 nListPos = find(notifyList.begin(), notifyList.end(),
00553 it->msg->getSender());
00554 if (nListPos != notifyList.end()) {
00555 notifyList.erase(nListPos);
00556 }
00557 delete it->msg;
00558 delete it->prox;
00559 }
00560 stReceived.clear();
00561 }
00562
00563
00564 for (std::vector<TransportAddress>::iterator it =
00565 notifyList.begin(); it != notifyList.end(); it++) {
00566 if (*it != thisNode) sendStateTables(*it, PASTRY_STATE_JOINUPDATE);
00567 }
00568 notifyList.clear();
00569
00570 updateTooltip();
00571 }
00572
00573 void Pastry::doSecondStage(void)
00574 {
00575 getParentModule()->getParentModule()->bubble("entering SECOND STAGE");
00576
00577
00578 if (leafSet->isValid()) {
00579 PastryStateMessage* stateMsg = new PastryStateMessage("STATE");
00580 stateMsg->setPastryMsgType(PASTRY_MSG_STATE);
00581 stateMsg->setStatType(MAINTENANCE_STAT);
00582 stateMsg->setPastryStateMsgType(PASTRY_STATE_STD);
00583 stateMsg->setSender(thisNode);
00584 routingTable->dumpToStateMessage(stateMsg);
00585 leafSet->dumpToStateMessage(stateMsg);
00586 neighborhoodSet->dumpToStateMessage(stateMsg);
00587
00588 PastryStateMsgHandle handle(stateMsg);
00589
00590 if (!stateCache.msg) {
00591 stateCache = handle;
00592 processState();
00593 } else {
00594 stateCacheQueue.push(handle);
00595 prePing(stateMsg);
00596 }
00597 }
00598
00599
00600 notifyList.clear();
00601 routingTable->dumpToVector(notifyList);
00602 neighborhoodSet->dumpToVector(notifyList);
00603 sort(notifyList.begin(), notifyList.end());
00604 notifyList.erase(unique(notifyList.begin(), notifyList.end()),
00605 notifyList.end());
00606 for (std::vector<TransportAddress>::iterator it = notifyList.begin();
00607 it != notifyList.end(); it++) {
00608 if (*it == thisNode) continue;
00609 EV << "[Pastry::doSecondStage() @ " << thisNode.ip
00610 << " (" << thisNode.key.toString(16) << ")]\n"
00611 << " second stage: requesting state from " << *it
00612 << endl;
00613 sendRequest(*it, PASTRY_REQ_STATE);
00614 }
00615 notifyList.clear();
00616 }
00617
00618 bool Pastry::handleFailedNode(const TransportAddress& failed)
00619 {
00620 if (state != READY) {
00621 return false;
00622 }
00623 bool wasValid = leafSet->isValid();
00624
00625
00626 if (failed.isUnspecified())
00627 opp_error("Pastry::handleFailedNode(): failed is unspecified!");
00628
00629 const TransportAddress& lsAsk = leafSet->failedNode(failed);
00630 const TransportAddress& rtAsk = routingTable->failedNode(failed);
00631 neighborhoodSet->failedNode(failed);
00632
00633 if (! lsAsk.isUnspecified()) {
00634 newLeafs();
00635 if (sendStateAtLeafsetRepair) sendRequest(lsAsk, PASTRY_REQ_REPAIR);
00636 else sendRequest(lsAsk, PASTRY_REQ_LEAFSET);
00637 }
00638 if (! rtAsk.isUnspecified() &&
00639 (lsAsk.isUnspecified() ||
00640 lsAsk != rtAsk)) sendRequest(rtAsk, PASTRY_REQ_REPAIR);
00641
00642 if (wasValid && lsAsk.isUnspecified() && (! leafSet->isValid())) {
00643 EV << "[Pastry::handleFailedNode() @ " << thisNode.ip
00644 << " (" << thisNode.key.toString(16) << ")]\n"
00645 << " lost connection to the network, trying to re-join."
00646 << endl;
00647
00648
00649 joinOverlay();
00650 return false;
00651 }
00652
00653 return true;
00654 }
00655
00656 void Pastry::checkProxCache(void)
00657 {
00658 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00659 << " (" << thisNode.key.toString(16) << ")]"
00660 << endl;
00661
00662
00663 if (!(stateCache.msg && stateCache.prox)) return;
00664
00665
00666 if (stateCache.prox->pr_rt.empty() &&
00667 stateCache.prox->pr_ls.empty() &&
00668 stateCache.prox->pr_ns.empty())
00669 throw new cRuntimeError("ERROR in Pastry: stateCache.prox empty!");
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689 if ((find(stateCache.prox->pr_rt.begin(), stateCache.prox->pr_rt.end(),
00690 PASTRY_PROX_PENDING) != stateCache.prox->pr_rt.end()) ||
00691 (find(stateCache.prox->pr_ls.begin(), stateCache.prox->pr_ls.end(),
00692 PASTRY_PROX_PENDING) != stateCache.prox->pr_ls.end()) ||
00693 (find(stateCache.prox->pr_ns.begin(), stateCache.prox->pr_ns.end(),
00694 PASTRY_PROX_PENDING) != stateCache.prox->pr_ns.end())) {
00695 return;
00696 }
00697
00698 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00699 << " (" << thisNode.key.toString(16) << ")]\n"
00700 << " all proximities for current STATE message from "
00701 << stateCache.msg->getSender().ip
00702 << " collected!"
00703 << endl;
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 simtime_t now = simTime();
00729
00730 if (state == JOINING_2) {
00731
00732 stReceivedPos->prox = stateCache.prox;
00733
00734
00735 if (++stReceivedPos == stReceived.end()) {
00736 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00737 << " (" << thisNode.key.toString(16) << ")]\n"
00738 << " proximities for all STATE messages collected!"
00739 << endl;
00740 stateCache.msg = NULL;
00741 stateCache.prox = NULL;
00742 if (debugOutput) {
00743 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00744 << " (" << thisNode.key.toString(16) << ")]\n"
00745 << " [JOIN] starting to build own state from "
00746 << stReceived.size() << " received state messages..."
00747 << endl;
00748 }
00749 if (mergeState()) {
00750
00751
00752 changeState(READY);
00753 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00754 << " (" << thisNode.key.toString(16) << ")]\n"
00755 << " changeState(READY) called"
00756 << endl;
00757 } else {
00758 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00759 << " (" << thisNode.key.toString(16) << ")]\n"
00760 << " Error initializing while joining! Restarting ..."
00761 << endl;
00762 joinOverlay();
00763 }
00764
00765 } else {
00766 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00767 << " (" << thisNode.key.toString(16) << ")]\n"
00768 << " NOT all proximities for all STATE messages collected!"
00769 << endl;
00770 for (uint32_t i = 0; i < stReceived.size(); ++i) {
00771 if (stReceived[i].msg == stReceivedPos->msg) EV << " *";
00772 EV << stReceived[i].msg;
00773 }
00774 EV << endl;
00775
00776
00777 if (stReceivedPos->msg == NULL)
00778 throw cRuntimeError("stReceivedPos->msg = NULL");
00779 stateCache = *stReceivedPos;
00780 if (stateCache.msg == NULL)
00781 throw cRuntimeError("msg = NULL");
00782 processState();
00783
00784
00785
00786
00787
00788
00789 }
00790 } else {
00791
00792 if (stateCache.msg->getPastryStateMsgType() == PASTRY_STATE_REPAIR) {
00793
00794 const TransportAddress& askRt =
00795 routingTable->repair(stateCache.msg, stateCache.prox);
00796 if (! askRt.isUnspecified()) {
00797 sendRequest(askRt, PASTRY_REQ_REPAIR);
00798 }
00799
00800
00801
00802 lastStateChange = now;
00803 } else {
00804 if (stateCache.outdatedUpdate) {
00805
00806 sendStateDelayed(stateCache.msg->getSender());
00807
00808 } else {
00809
00810
00811
00812 if (neighborhoodSet->mergeState(stateCache.msg, stateCache.prox))
00813 lastStateChange = now;
00814 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00815 << " (" << thisNode.key.toString(16) << ")]\n"
00816 << " Merging nodes into routing table"
00817 << endl;
00818 if (routingTable->mergeState(stateCache.msg, stateCache.prox)) {
00819 lastStateChange = now;
00820 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00821 << " (" << thisNode.key.toString(16) << ")]\n"
00822 << " Merged nodes into routing table"
00823 << endl;
00824 }
00825 }
00826 }
00827 updateTooltip();
00828
00829 endProcessingState();
00830 }
00831 }
00832
00833 void Pastry::endProcessingState(void)
00834 {
00835
00836 if (stateCache.msg &&
00837 stateCache.msg->getPastryStateMsgType() != PASTRY_STATE_UPDATE &&
00838 (alwaysSendUpdate || lastStateChange == simTime()) &&
00839 thisNode != stateCache.msg->getSender()) {
00840 simtime_t timestamp = stateCache.msg->getTimestamp();
00841 sendStateTables(stateCache.msg->getSender(), PASTRY_STATE_UPDATE,
00842 ×tamp);
00843 }
00844
00845 delete stateCache.msg;
00846 stateCache.msg = NULL;
00847 delete stateCache.prox;
00848 stateCache.prox = NULL;
00849
00850
00851 if (! stateCacheQueue.empty()) {
00852 stateCache = stateCacheQueue.front();
00853 stateCacheQueue.pop();
00854 processState();
00855
00856
00857
00858
00859
00860
00861
00862 }
00863 }
00864
00865 bool Pastry::mergeState(void)
00866 {
00867 bool ret = true;
00868
00869 if (state == JOINING_2) {
00870
00871 if (debugOutput) {
00872 EV << "[Pastry::mergeState() @ " << thisNode.ip
00873 << " (" << thisNode.key.toString(16) << ")]\n"
00874 << " [JOIN] starting to build own state from "
00875 << stReceived.size() << " received state messages..."
00876 << endl;
00877 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00878 << " (" << thisNode.key.toString(16) << ")]\n"
00879 << " [JOIN] initializing NeighborhoodSet from "
00880 << stReceived.front().msg->getJoinHopCount() << ". hop"
00881 << endl;
00882 }
00883 if (!neighborhoodSet->mergeState(stReceived.front().msg,
00884 stReceived.front().prox )) {
00885 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00886 << " (" << thisNode.key.toString(16) << ")]\n"
00887 << " Error initializing own neighborhoodSet while joining! Restarting ..."
00888 << endl;
00889
00890
00891 ret = false;
00892 }
00893 if (debugOutput) {
00894 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00895 << " (" << thisNode.key.toString(16) << ")]\n"
00896 << " [JOIN] initializing LeafSet from "
00897 << stReceived.back().msg->getJoinHopCount() << ". hop"
00898 << endl;
00899 }
00900
00901 if (!leafSet->mergeState(stReceived.back().msg,
00902 stReceived.back().prox )) {
00903 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00904 << " (" << thisNode.key.toString(16) << ")]\n"
00905 << " Error initializing own leafSet while joining! Restarting ..."
00906 << endl;
00907
00908
00909 ret = false;
00910 } else {
00911 newLeafs();
00912 }
00913 if (debugOutput) {
00914 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00915 << " (" << thisNode.key.toString(16) << ")]\n"
00916 << " [JOIN] initializing RoutingTable from all hops"
00917 << endl;
00918 }
00919
00920 if (!routingTable->initStateFromHandleVector(stReceived)) {
00921 EV << "[Pastry::checkProxCache() @ " << thisNode.ip
00922 << " (" << thisNode.key.toString(16) << ")]\n"
00923 << " Error initializing own routingTable while joining! Restarting ..."
00924 << endl;
00925
00926
00927
00928 ret = false;
00929 }
00930 stateCache.msg = NULL;
00931 stateCache.prox = NULL;
00932 } else if (state == READY) {
00933
00934 if (!neighborhoodSet->mergeState(stateCache.msg, NULL)) {
00935 ret = false;
00936 }
00937 if (!leafSet->mergeState(stateCache.msg, NULL)) {
00938 ret = false;
00939 } else {
00940 newLeafs();
00941 }
00942 if (!routingTable->mergeState(stateCache.msg, NULL)) {
00943 ret = false;
00944 }
00945 }
00946
00947 if (ret) lastStateChange = simTime();
00948 return ret;
00949 }
00950
00951 void Pastry::handleStateMessage(PastryStateMessage* msg)
00952 {
00953 if (debugOutput) {
00954 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
00955 << " (" << thisNode.key.toString(16) << ")]\n"
00956 << " new STATE message to process "
00957 << static_cast<void*>(msg) << " in state " <<
00958 ((state == READY)?"READY":((state == JOINING_2)?"JOIN":"INIT"))
00959 << endl;
00960 if (state == JOINING_2) {
00961 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
00962 << " (" << thisNode.key.toString(16) << ")]\n"
00963 << " *** own joinHopCount: " << joinHopCount << endl
00964 << " *** already received: " << stReceived.size() << endl
00965 << " *** last-hop flag: " << msg->getLastHop() << endl
00966 << " *** msg joinHopCount: "
00967 << msg->getJoinHopCount() << endl;
00968 }
00969 }
00970 if (state == INIT || state == DISCOVERY) {
00971 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
00972 << " (" << thisNode.key.toString(16) << ")]\n"
00973 << " can't handle state messages until at least reaching JOIN state."
00974 << endl;
00975 delete msg;
00976 return;
00977 }
00978
00979 PastryStateMsgHandle handle(msg);
00980
00981
00982 if (state == JOINING_2) {
00983
00984
00985
00986
00987 if (msg->getPastryStateMsgType() != PASTRY_STATE_JOIN) {
00988 delete msg;
00989 return;
00990 }
00991
00992 if (joinHopCount && stReceived.size() == joinHopCount) {
00993 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
00994 << " (" << thisNode.key.toString(16) << ")]\n"
00995 << " Warning: dropping state message received after "
00996 << "all needed state messages were collected in JOIN state."
00997 << endl;
00998 delete msg;
00999 return;
01000 }
01001
01002 stReceived.push_back(handle);
01003 if (pingBeforeSecondStage) prePing(msg);
01004
01005 if (msg->getLastHop()) {
01006 if (joinTimeout->isScheduled()) {
01007
01008
01009
01010 cancelEvent(joinTimeout);
01011 }
01012 if (msg->getSender().key == thisNode.key) {
01013 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01014 << " (" << thisNode.key.toString(16) << ")]\n"
01015 << " Error: OverlayKey already in use, restarting!"
01016 << endl;
01017
01018
01019 joinOverlay();
01020 return;
01021 }
01022
01023 if (joinHopCount) {
01024 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01025 << " (" << thisNode.key.toString(16) << ")]\n"
01026 << " Error: received a second `last' state message! Restarting ..."
01027 << endl;
01028
01029
01030 joinOverlay();
01031 return;
01032 }
01033
01034 joinHopCount = msg->getJoinHopCount();
01035
01036 if (stReceived.size() < joinHopCount) {
01037
01038 cancelEvent(readyWait);
01039 scheduleAt(simTime() + readyWaitAmount, readyWait);
01040
01041 }
01042 }
01043
01044 if (joinHopCount) {
01045 if (stReceived.size() > joinHopCount) {
01046 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01047 << " (" << thisNode.key.toString(16) << ")]\n"
01048 << " Error: too many state messages received in JOIN state! ("
01049 << stReceived.size() << " > " << joinHopCount << ") Restarting ..."
01050 << endl;
01051
01052 joinOverlay();
01053 return;
01054 }
01055 if (stReceived.size() == joinHopCount) {
01056
01057 sort(stReceived.begin(), stReceived.end(),
01058 stateMsgIsSmaller);
01059
01060
01061 stReceivedPos = stReceived.begin();
01062 stateCache = *stReceivedPos;
01063 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01064 << " (" << thisNode.key.toString(16) << ")]\n"
01065 << " have all STATE messages, now pinging nodes."
01066 << endl;
01067 if (pingBeforeSecondStage) pingNodes();
01068 else {
01069 mergeState();
01070
01071
01072 changeState(READY);
01073 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01074 << " (" << thisNode.key.toString(16) << ")]\n"
01075 << " changeState(READY) called"
01076 << endl;
01077 }
01078
01079
01080 if (readyWait->isScheduled()) cancelEvent(readyWait);
01081 } else {
01082
01083
01084
01085 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01086 << " (" << thisNode.key.toString(16) << ")]\n"
01087 << " Still need some STATE messages."
01088 << endl;
01089 }
01090
01091 }
01092 return;
01093 }
01094
01095 if (debugOutput) {
01096 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01097 << " (" << thisNode.key.toString(16) << ")]\n"
01098 << " handling STATE message"
01099 << endl;
01100 EV << " type: " << ((msg->getPastryStateMsgType()
01101 == PASTRY_STATE_UPDATE) ? "update"
01102 :"standard")
01103 << endl;
01104 if (msg->getPastryStateMsgType() == PASTRY_STATE_UPDATE) {
01105 EV << " msg timestamp: " <<
01106 msg->getTimestamp() << endl;
01107 EV << " last state change: " <<
01108 lastStateChange << endl;
01109 }
01110 }
01111
01112 if (((msg->getPastryStateMsgType() == PASTRY_STATE_UPDATE))
01113 && (msg->getTimestamp() <= lastStateChange)) {
01114
01115
01116 EV << "[Pastry::handleStateMessage() @ " << thisNode.ip
01117 << " (" << thisNode.key.toString(16) << ")]\n"
01118 << " outdated state from " << msg->getSender()
01119 << endl;
01120 handle.outdatedUpdate = true;
01121 }
01122
01123
01124
01125 determineAliveTable(msg);
01126
01127 if (msg->getPastryStateMsgType() == PASTRY_STATE_REPAIR) {
01128
01129 const TransportAddress& askLs = leafSet->repair(msg, &aliveTable);
01130 if (! askLs.isUnspecified()) {
01131 sendRequest(askLs, PASTRY_REQ_REPAIR);
01132 }
01133
01134
01135
01136 lastStateChange = simTime();
01137 newLeafs();
01138 } else if (leafSet->mergeState(msg, &aliveTable)) {
01139
01140 lastStateChange = simTime();
01141 newLeafs();
01142 updateTooltip();
01143 }
01144
01145 if (!stateCache.msg) {
01146
01147 stateCache = handle;
01148 processState();
01149 } else {
01150 if (pingBeforeSecondStage ||
01151 msg->getPastryStateMsgType() == PASTRY_STATE_STD) {
01152
01153 stateCacheQueue.push(handle);
01154 prePing(msg);
01155 } else {
01156 bool temp = true;
01157 if (!neighborhoodSet->mergeState(msg, NULL)) {
01158 temp = false;
01159 }
01160 if (!leafSet->mergeState(msg, NULL)) {
01161 temp = false;
01162 } else {
01163 newLeafs();
01164 }
01165 if (!routingTable->mergeState(msg, NULL)) {
01166 temp = false;
01167 }
01168 if (temp) lastStateChange = simTime();
01169 delete msg;
01170 }
01171 }
01172 }
01173
01174 void Pastry::processState(void)
01175 {
01176 if (pingBeforeSecondStage ||
01177 stateCache.msg->getPastryStateMsgType() == PASTRY_STATE_STD) {
01178 pingNodes();
01179 } else {
01180 mergeState();
01181 endProcessingState();
01182 }
01183 }