00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include "Kademlia.h"
00024
00025 #include <assert.h>
00026 #include <algorithm>
00027
00028 #include <IPAddressResolver.h>
00029 #include <IPvXAddress.h>
00030 #include <IInterfaceTable.h>
00031 #include <IPv4InterfaceData.h>
00032 #include "TopologyVis.h"
00033 #include <AbstractLookup.h>
00034 #include <LookupListener.h>
00035 #include <RpcMacros.h>
00036 #include <BootstrapList.h>
00037
00038 #if 0
00039 #define BUCKET_CONSISTENCY(msg) \
00040 do {\
00041 bool stFull = false;\
00042 int z = 0;\
00043 if (siblingTable != NULL && siblingTable->size() > 0) {\
00044 stFull = siblingTable->isFull();\
00045 z = routingBucketIndex(siblingTable->back().key) - 1;\
00046 if (routingTable[z - 1] != NULL && routingTable[z - 1]->size())\
00047 breakpoint(#msg);\
00048 }\
00049 for (int y = 0; y < (z - 2); ++y) {\
00050 if ((routingTable[y] != NULL && routingTable[y]->size() > k) ||\
00051 (routingTable[y] != NULL && routingTable[y]->size() && !stFull))\
00052 breakpoint(#msg);\
00053 }\
00054 } while (false)
00055 #else
00056 #define BUCKET_CONSISTENCY(msg)
00057 #endif
00058
00059 Define_Module(Kademlia);
00060
00061 std::ostream& operator<<(std::ostream& os, const KademliaBucket* n)
00062 {
00063 if (n == NULL)
00064 os << "NULL";
00065 else {
00066 for (KademliaBucket::const_iterator i=n->begin(); i !=n->end(); i++) {
00067 os << *i << endl;
00068 }
00069 os << "last usage = " << n->getLastUsage()
00070 << ", last update = " << n->getLastUpdate();
00071 }
00072 return os;
00073 };
00074
00075 class KademliaLookupListener : public LookupListener
00076 {
00077 private:
00078 Kademlia* overlay;
00079 public:
00080 KademliaLookupListener(Kademlia* overlay)
00081 {
00082 this->overlay = overlay;
00083 }
00084
00085 virtual void lookupFinished(AbstractLookup *lookup)
00086 {
00087 overlay->lookupFinished(lookup->isValid());
00088 delete this;
00089 }
00090 };
00091
00092
00093
00094 void Kademlia::initializeOverlay(int stage)
00095 {
00096 if (stage != MIN_STAGE_OVERLAY)
00097 return;
00098
00099
00100 kbr = true;
00101
00102
00103 minSiblingTableRefreshInterval = par("minSiblingTableRefreshInterval");
00104 minBucketRefreshInterval = par("minBucketRefreshInterval");
00105 maxStaleCount = par("maxStaleCount");
00106 pingNewSiblings = par("pingNewSiblings");
00107
00108
00109 k = par("k");
00110 b = par("b");
00111 s = par("s");
00112
00113
00114 numBuckets = ((1L << b) - 1L) * (OverlayKey::getLength() / b);
00115
00116
00117 siblingTable = new KademliaBucket(s * 5, NULL);
00118
00119
00120 routingTable.assign(numBuckets, (KademliaBucket*)NULL);
00121
00122 WATCH_VECTOR(*siblingTable);
00123 WATCH_VECTOR(routingTable);
00124
00125
00126 bucketRefreshTimer = new cMessage("bucketRefreshTimer");
00127
00128 nodesReplaced = 0;
00129
00130 comparator = NULL;
00131 }
00132
00133 Kademlia::Kademlia()
00134 {
00135 siblingTable = NULL;
00136 comparator = NULL;
00137 }
00138
00139 Kademlia::~Kademlia()
00140 {
00141 routingDeinit();
00142
00143 replacementCache.clear();
00144 delete siblingTable;
00145 delete comparator;
00146 cancelAndDelete(bucketRefreshTimer);
00147 }
00148
00149 void Kademlia::finishOverlay()
00150 {
00151
00152 cancelEvent(bucketRefreshTimer);
00153
00154 simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00155 if(time == 0) return;
00156
00157 globalStatistics->addStdDev("Kademlia: Nodes replaced in buckets/s",
00158 nodesReplaced / time);
00159 }
00160
00161 void Kademlia::joinOverlay()
00162 {
00163
00164 if (!thisNode.key.isUnspecified()) {
00165 bootstrapList->removeBootstrapNode(thisNode);
00166 }
00167
00168
00169 routingDeinit();
00170 routingInit();
00171
00172 TransportAddress handle = bootstrapList->getBootstrapNode();
00173
00174 if (!handle.isUnspecified()) {
00175
00176 pingNode(handle);
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 } else {
00193
00194 state = READY;
00195 setOverlayReady(true);
00196
00197
00198 cancelEvent(bucketRefreshTimer);
00199 scheduleAt(simTime(), bucketRefreshTimer);
00200 }
00201 }
00202
00203
00204
00205 void Kademlia::routingInit()
00206 {
00207
00208 state = INIT;
00209
00210 setOverlayReady(false);
00211
00212
00213 comparator = new KeyDistanceComparator<KeyXorMetric>( thisNode.getKey() );
00214
00215 siblingTable->setComparator(comparator);
00216
00217 updateTooltip();
00218 BUCKET_CONSISTENCY(routingInit: end);
00219 }
00220
00221 void Kademlia::routingDeinit()
00222 {
00223
00224 for (uint32_t i = 0; i < routingTable.size(); i++) {
00225 if (routingTable[i] != NULL) {
00226 delete routingTable[i];
00227 routingTable[i] = NULL;
00228 }
00229 }
00230
00231 if (siblingTable != NULL) {
00232 siblingTable->clear();
00233 }
00234
00235 if (comparator != NULL) {
00236 delete comparator;
00237 comparator = NULL;
00238 }
00239 }
00240
00241 int Kademlia::getMaxNumSiblings()
00242 {
00243 return s;
00244 }
00245
00246 int Kademlia::getMaxNumRedundantNodes()
00247 {
00248 return k;
00249 }
00250
00251 int Kademlia::routingBucketIndex(const OverlayKey& key, bool firstOnLayer)
00252 {
00253
00254 OverlayKey delta = key ^ getThisNode().getKey();
00255
00256
00257 int i;
00258 for (i = key.getLength() - b; i >= 0 && delta.getBitRange(i, b) == 0;
00259 i -= b);
00260
00261 if (i < 0)
00262 return -1;
00263
00264 if (!firstOnLayer)
00265 return (i / b) * ((1 << b) - 1) + (delta.getBitRange(i, b) - 1);
00266 else
00267 return (i / b) * ((1 << b) - 1) + (pow(2, b) - 2);
00268 }
00269
00270 KademliaBucket* Kademlia::routingBucket(const OverlayKey& key, bool ensure)
00271 {
00272
00273 int num = routingBucketIndex(key);
00274 if (num < 0)
00275 return NULL;
00276
00277
00278 KademliaBucket* bucket = routingTable[ num ];
00279 if (bucket == NULL && ensure)
00280 bucket = routingTable[ num ] = new KademliaBucket( k, comparator );
00281
00282
00283 return bucket;
00284 }
00285
00286 bool Kademlia::routingAdd(const NodeHandle& handle, bool isAlive, simtime_t rtt)
00287 {
00288 BUCKET_CONSISTENCY(routingAdd: start);
00289
00290 if (handle.isUnspecified() || handle.key == getThisNode().getKey() )
00291 return false;
00292
00293
00294 KademliaBucket::iterator i;
00295 bool result = false;
00296
00297
00298 KademliaBucketEntry kadHandle = handle;
00299 kadHandle.setRtt(rtt);
00300 kadHandle.setLastSeen(simTime());
00301
00302
00303 if ((i = siblingTable->findIterator(handle.getKey()))
00304 != siblingTable->end()) {
00305
00306 if (isAlive) {
00307 if (kadHandle.getRtt() != i->getRtt()) {
00308 siblingTable->setLastUpdate(simTime());
00309 if (kadHandle.getRtt() == MAXTIME)
00310 kadHandle.setRtt(i->getRtt());
00311 }
00312
00313 (*i) = kadHandle;
00314 }
00315 BUCKET_CONSISTENCY(routingAdd: node is sibling);
00316 return true;
00317 }
00318
00319
00320 KademliaBucket* bucket = routingBucket(handle.getKey(), false);
00321 if (bucket != NULL && (i = bucket->findIterator(handle.getKey() ) )
00322 != bucket->end() ) {
00323
00324 if (isAlive) {
00325 if (kadHandle.getRtt() == MAXTIME) {
00326 kadHandle.setRtt(i->getRtt());
00327 }
00328
00329 bucket->erase(i);
00330
00331 bucket->push_back(kadHandle);
00332
00333 bucket->setLastUpdate(simTime());
00334 }
00335 BUCKET_CONSISTENCY(routingAdd: node is in bucket);
00336 return true;
00337 }
00338
00339
00340 if (siblingTable->isAddable(handle) ) {
00341
00342 bool finished = false;
00343 int siblingPos = -1;
00344
00345
00346 if (siblingTable->isFull()) {
00347
00348 KademliaBucketEntry oldHandle = siblingTable->back();
00349 assert(oldHandle.key != kadHandle.key);
00350
00351 siblingPos = siblingTable->add(kadHandle);
00352
00353
00354 kadHandle = oldHandle;
00355
00356
00357 result = true;
00358 } else {
00359
00360 siblingPos = siblingTable->add(kadHandle);
00361
00362
00363 finished = true;
00364 }
00365 assert(siblingPos > -1);
00366
00367
00368 if (pingNewSiblings && !isAlive) {
00369 pingNode(handle);
00370 }
00371
00372 siblingTable->setLastUpdate(simTime());
00373
00374 updateTooltip();
00375
00376
00377 if (siblingPos < getMaxNumSiblings()) {
00378 if (siblingTable->size() > (uint32_t)getMaxNumSiblings()) {
00379
00380 NodeHandle& removedSibling = siblingTable->at(getMaxNumSiblings());
00381 deleteOverlayNeighborArrow(removedSibling);
00382 callUpdate(removedSibling, false);
00383 }
00384
00385 showOverlayNeighborArrow(handle, false,
00386 "m=m,50,100,50,100;ls=green,1");
00387 callUpdate(handle, true);
00388 }
00389
00390 if (finished) {
00391 BUCKET_CONSISTENCY(routingAdd: node is now sibling);
00392 return true;
00393 }
00394 }
00395
00396
00397 bucket = routingBucket(kadHandle.getKey(), true);
00398 if (!bucket->isFull()) {
00399 EV << "[Kademlia::routingAdd()]\n"
00400 << " Adding new node " << kadHandle
00401 << " to bucket " << routingBucketIndex(kadHandle.getKey())
00402 << endl;
00403 bucket->push_back(kadHandle);
00404 bucket->setLastUpdate(simTime());
00405 result = true;
00406 } else if (isAlive) {
00407
00408
00409 KademliaBucket::iterator it = bucket->begin();
00410 while (it != bucket->end() &&
00411 replacementCache.find(*it) != replacementCache.end()) {
00412 ++it;
00413 }
00414 if (it != bucket->end()) {
00415 replacementCache.insert(std::make_pair(*it, kadHandle));
00416
00417
00418 pingNode(*it);
00419 }
00420 }
00421
00422 BUCKET_CONSISTENCY(routingAdd: end);
00423 return result;
00424 }
00425
00426 bool Kademlia::routingRemove(const OverlayKey& key)
00427 {
00428 return routingTimeout(key, true);
00429 }
00430
00431 bool Kademlia::routingTimeout(const OverlayKey& key, bool immediately)
00432 {
00433 BUCKET_CONSISTENCY(routingTimeout: start);
00434
00435 if (key.isUnspecified())
00436 return false;
00437
00438
00439 KademliaBucket::iterator i;
00440
00441
00442 if ((i = siblingTable->findIterator(key)) != siblingTable->end()) {
00443
00444 i->incStaleCount();
00445
00446 if (i->getStaleCount() > maxStaleCount || immediately) {
00447
00448 NodeHandle oldSibling = *i;
00449 siblingTable->erase(i);
00450
00451 if (siblingTable->size() < (uint32_t)getMaxNumSiblings()) {
00452
00453 deleteOverlayNeighborArrow(oldSibling);
00454 callUpdate(oldSibling, false);
00455 } else if (comparator->compare(oldSibling.key,
00456 siblingTable->at(getMaxNumSiblings() - 1).key) < 0) {
00457
00458 deleteOverlayNeighborArrow(oldSibling);
00459 callUpdate(oldSibling, false);
00460
00461 showOverlayNeighborArrow(siblingTable->at(getMaxNumSiblings() - 1),
00462 false, "m=m,50,100,50,100;ls=green,1");
00463 callUpdate(siblingTable->at(getMaxNumSiblings() - 1), true);
00464 }
00465
00466 updateTooltip();
00467
00468
00469 if (siblingTable->size() == 0) {
00470 joinOverlay();
00471 return true;
00472 }
00473
00474 BUCKET_CONSISTENCY(routingTimeout: is sibling);
00475
00476
00477 refillSiblingTable();
00478
00479 return true;
00480 }
00481 }
00482
00483
00484 KademliaBucket* bucket = routingBucket(key, false);
00485 if (bucket != NULL && (i = bucket->findIterator(key) ) != bucket->end() ) {
00486
00487 i->incStaleCount();
00488 std::map<NodeHandle, NodeHandle>::iterator it
00489 = replacementCache.find(*i);
00490 if (i->getStaleCount() > maxStaleCount ||
00491 it != replacementCache.end() || immediately) {
00492
00493 bucket->erase(i);
00494 }
00495 if (it != replacementCache.end()) {
00496 routingAdd(it->second, true);
00497 nodesReplaced++;
00498
00499 }
00500 BUCKET_CONSISTENCY(routingTimeout: is in bucket);
00501 return true;
00502 }
00503 BUCKET_CONSISTENCY(routingTimeout: end);
00504 return false;
00505 }
00506
00507 void Kademlia::refillSiblingTable()
00508 {
00509 if (siblingTable->size() == 0 ||
00510 siblingTable->isFull())
00511 return;
00512
00513 int index = routingBucketIndex(siblingTable->back().key) - 1;
00514 assert(index > 0);
00515
00516 while ((routingTable[index] == NULL ||
00517 routingTable[index]->empty()) &&
00518 index < (int)(OverlayKey::getLength() - 1)) {
00519 index++;
00520 }
00521 if (index < (int)OverlayKey::getLength() &&
00522 routingTable[index] != NULL && routingTable[index]->size()) {
00523 KademliaBucket sortedBucket(k, comparator);
00524 for (uint32_t i = 0; i < routingTable[index]->size(); ++i)
00525 sortedBucket.add(routingTable[index]->at(i));
00526 siblingTable->add(sortedBucket.front());
00527
00528
00529 routingTable[index]->
00530 erase(routingTable[index]->
00531 findIterator(sortedBucket.front().key));
00532 assert(siblingTable->isFull());
00533 BUCKET_CONSISTENCY(routingTimeout: end refillSiblingTable());
00534 }
00535 }
00536
00537 void Kademlia::setBucketUsage(const OverlayKey& key)
00538 {
00539 KademliaBucket* bucket = routingBucket(key, true);
00540
00541 if (bucket)
00542 bucket->setLastUsage(simTime());
00543
00544
00545
00546
00547
00548
00549
00550 if (((siblingTable->size() + 1) < (uint32_t)getMaxNumSiblings())
00551 || ((siblingTable->at(getMaxNumSiblings() - 2).key ^ thisNode.key)
00552 >= (key ^ thisNode.key))) {
00553 siblingTable->setLastUsage(simTime());
00554 }
00555
00556 }
00557
00558 bool Kademlia::isSiblingFor(const NodeHandle& node, const OverlayKey& key,
00559 int numSiblings, bool* err)
00560 {
00561 if (key.isUnspecified())
00562 error("Kademlia::isSiblingFor(): key is unspecified!");
00563
00564 if (state != READY) {
00565 *err = true;
00566 return false;
00567 }
00568
00569 if (numSiblings > getMaxNumSiblings()) {
00570 opp_error("Kademlia::isSiblingFor(): numSiblings too big!");
00571 }
00572
00573
00574 if (numSiblings == -1) {
00575 numSiblings = getMaxNumSiblings();
00576 }
00577
00578 if (numSiblings == 0) {
00579 *err = false;
00580 return (node.getKey() == key);
00581 }
00582
00583 if ((siblingTable->size() + 1) < (uint32_t)getMaxNumSiblings()) {
00584 *err = false;
00585 return true;
00586 }
00587
00588 if ((thisNode.key ^ key) > (thisNode.key ^ siblingTable->back().key)) {
00589 *err = true;
00590 return false;
00591 }
00592
00593 KeyDistanceComparator<KeyXorMetric>* comp =
00594 new KeyDistanceComparator<KeyXorMetric>(key);
00595
00596
00597 NodeVector* result = new NodeVector(numSiblings, comp);
00598
00599 for (KademliaBucket::iterator i=siblingTable->begin();
00600 i != siblingTable->end(); i++) {
00601 result->add( *i);
00602 }
00603
00604
00605 result->add(thisNode);
00606
00607 *err = false;
00608 delete comp;
00609
00610 if (result->contains(node.key)) {
00611 delete result;
00612 return true;
00613 } else {
00614 delete result;
00615 assert(!(numSiblings == 1 && key == node.key));
00616 return false;
00617 }
00618 }
00619
00620 bool Kademlia::handleFailedNode(const TransportAddress& failed)
00621 {
00622 assert(!failed.isUnspecified());
00623
00624 KademliaBucket::iterator i;
00625
00626 for (i = siblingTable->begin(); i != siblingTable->end(); ++i) {
00627 if (failed == *i) break;
00628 }
00629
00630 if (i != siblingTable->end()) {
00631
00632 NodeHandle oldSibling = *i;
00633 siblingTable->erase(i);
00634
00635 if (siblingTable->size() < (uint32_t)getMaxNumSiblings()) {
00636
00637 deleteOverlayNeighborArrow(oldSibling);
00638 callUpdate(oldSibling, false);
00639 } else if (comparator->compare(oldSibling.key,
00640 siblingTable->at(getMaxNumSiblings() - 1).key) < 0) {
00641
00642 deleteOverlayNeighborArrow(oldSibling);
00643 callUpdate(oldSibling, false);
00644
00645 showOverlayNeighborArrow(siblingTable->at(getMaxNumSiblings() - 1),
00646 false, "m=m,50,100,50,100;ls=green,1");
00647 callUpdate(siblingTable->at(getMaxNumSiblings() - 1), true);
00648 }
00649
00650 updateTooltip();
00651
00652
00653 refillSiblingTable();
00654 } else {
00655
00656 uint32_t m;
00657 for (m = 0; m < routingTable.size(); ++m) {
00658 if (routingTable[m] != NULL) {
00659 for (i = routingTable[m]->begin(); i != routingTable[m]->end();
00660 ++i) {
00661 if (failed == *i) {
00662
00663 routingTable[m]->erase(i);
00664 return (siblingTable->size() != 0);
00665 }
00666 }
00667 }
00668 }
00669 }
00670 return (siblingTable->size() != 0);
00671 }
00672
00673 NodeVector* Kademlia::findNode(const OverlayKey& key, int numRedundantNodes,
00674 int numSiblings, BaseOverlayMessage* msg)
00675 {
00676 if ((numRedundantNodes > getMaxNumRedundantNodes()) || (numSiblings
00677 > getMaxNumSiblings())) {
00678
00679 opp_error("(Kademlia::findNode()) numRedundantNodes or numSiblings "
00680 "too big!");
00681 }
00682
00683 if (numRedundantNodes < 2) {
00684 throw cRuntimeError("Kademlia::findNode(): For Kademlia "
00685 "redundantNodes must be at least 2 "
00686 "and lookupMerge should be true!");
00687 }
00688
00689
00690 KeyDistanceComparator<KeyXorMetric>* comp =
00691 new KeyDistanceComparator<KeyXorMetric>( key );
00692
00693
00694 bool err;
00695 int resultSize = isSiblingFor(thisNode, key, numSiblings, &err) ?
00696 (numSiblings ? numSiblings : 1) : numRedundantNodes;
00697
00698 assert(numSiblings || numRedundantNodes);
00699
00700 NodeVector* result = new NodeVector(resultSize, comp);
00701
00702 if (siblingTable->isEmpty()) {
00703 result->add(thisNode);
00704 delete comp;
00705 return result;
00706 }
00707
00708
00709 int index;
00710 int mainIndex = routingBucketIndex(key);
00711 int startIndex = routingBucketIndex(key, true);
00712 int endIndex = routingBucketIndex(siblingTable->back().key);
00713
00714
00715 if (mainIndex != -1) {
00716 KademliaBucket* bucket = routingTable[mainIndex];
00717 if (bucket != NULL && bucket->size()) {
00718 for (KademliaBucket::iterator i=bucket->begin(); i!=bucket->end(); i++) {
00719 result->add(*i);
00720
00721
00722 }
00723 }
00724 }
00725
00726
00727 if (startIndex >= endIndex || !result->isFull()) {
00728 for (index = startIndex; index >= endIndex; --index) {
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 if (index == mainIndex) continue;
00739 KademliaBucket* bucket = routingTable[index];
00740 if (bucket != NULL && bucket->size()) {
00741 for (KademliaBucket::iterator i=bucket->begin(); i!=bucket->end(); i++) {
00742 result->add(*i);
00743
00744
00745 }
00746 }
00747 }
00748
00749
00750 for (KademliaBucket::iterator i = siblingTable->begin();
00751 i != siblingTable->end(); i++) {
00752 result->add(*i);
00753 }
00754
00755 result->add(thisNode);
00756 }
00757
00758
00759 for (index = mainIndex + 1; !result->isFull() && index < numBuckets;
00760 ++index) {
00761
00762 KademliaBucket* bucket = routingTable[index];
00763 if (bucket != NULL && bucket->size()) {
00764 for (KademliaBucket::iterator i=bucket->begin(); i!=bucket->end(); i++) {
00765 result->add(*i);
00766
00767
00768 }
00769 }
00770 }
00771
00772 delete comp;
00773
00774 return result;
00775 }
00776
00777
00778
00779
00780 void Kademlia::handleTimerEvent(cMessage* msg)
00781 {
00782 if (msg == bucketRefreshTimer) {
00783 handleBucketRefreshTimerExpired();
00784 }
00785 }
00786
00787
00788 bool Kademlia::handleRpc(BaseCallMessage* msg)
00789 {
00790 RPC_SWITCH_START( msg )
00791 RPC_ON_CALL( Ping ) {
00792
00793 routingAdd( _PingCall->getSrcNode(), true );
00794 break;
00795 }
00796 RPC_ON_CALL(FindNode)
00797 {
00798
00799 routingAdd(_FindNodeCall->getSrcNode(), true);
00800 break;
00801 }
00802 RPC_SWITCH_END( )
00803 return false;
00804 }
00805
00806
00807 void Kademlia::handleRpcResponse(BaseResponseMessage* msg,
00808 cPolymorphic* context, int rpcId,
00809 simtime_t rtt)
00810 {
00811 RPC_SWITCH_START(msg)
00812 RPC_ON_RESPONSE(Ping) {
00813 if (state == INIT) {
00814
00815 cancelEvent(bucketRefreshTimer);
00816 scheduleAt(simTime(), bucketRefreshTimer);
00817 state = JOIN;
00818 }
00819
00820
00821 replacementCache.erase(_PingResponse->getSrcNode());
00822 }
00823 RPC_ON_RESPONSE(FindNode)
00824 {
00825
00826 if (defaultRoutingType == SEMI_RECURSIVE_ROUTING ||
00827 defaultRoutingType == FULL_RECURSIVE_ROUTING ||
00828 defaultRoutingType == RECURSIVE_SOURCE_ROUTING)
00829 rtt = MAXTIME;
00830 setBucketUsage(_FindNodeResponse->getSrcNode().key);
00831
00832
00833 for (uint32_t i=0; i<_FindNodeResponse->getClosestNodesArraySize(); i++)
00834 routingAdd(_FindNodeResponse->getClosestNodes(i), false);
00835 break;
00836 }
00837 RPC_SWITCH_END()
00838
00839
00840 routingAdd(msg->getSrcNode(), true, rtt);
00841 }
00842
00843
00844 void Kademlia::handleRpcTimeout(BaseCallMessage* msg,
00845 const TransportAddress& dest,
00846 cPolymorphic* context, int rpcId,
00847 const OverlayKey& destKey)
00848 {
00849 if (dest.isUnspecified()) return;
00850 try {
00851 RPC_SWITCH_START(msg)
00852 if (state == JOIN || state == INIT) {
00853 joinOverlay();
00854 return;
00855 }
00856
00857 const NodeHandle& handle = dynamic_cast<const NodeHandle&>(dest);
00858 RPC_ON_CALL(Ping) {
00859 routingTimeout(handle.key);
00860
00861
00862 std::map<NodeHandle, NodeHandle>::iterator it
00863 = replacementCache.find(handle);
00864 if (it != replacementCache.end()) {
00865 replacementCache.erase(it);
00866 }
00867
00868 break;
00869 }
00870 RPC_ON_CALL(FindNode) {
00871 routingTimeout(handle.key);
00872 setBucketUsage(handle.key);
00873 break;
00874 }
00875 RPC_SWITCH_END()
00876 } catch (...) {
00877 EV << "[Kademlia:handleRpcTimout() @ " << thisNode.ip
00878 << " (" << thisNode.key.toString(16) << ")]\n"
00879 << " ERROR: RPC timeout without key ("
00880 << msg << " -> " << dest << ")" << endl;
00881 return;
00882 }
00883 }
00884
00885 void Kademlia::lookupFinished(bool isValid)
00886 {
00887 if (state == JOIN) {
00888 cancelEvent(bucketRefreshTimer);
00889 scheduleAt(simTime(), bucketRefreshTimer);
00890
00891 state = READY;
00892 setOverlayReady(true);
00893 }
00894 }
00895
00896
00897 void Kademlia::handleBucketRefreshTimerExpired()
00898 {
00899
00900 if (state != READY || (((simTime() - siblingTable->getLastUsage()) >
00901 minSiblingTableRefreshInterval))) {
00902
00903 createLookup()->lookup(getThisNode().getKey() +
00904 OverlayKey::ONE, 0, 0, 0,
00905 new KademliaLookupListener(this));
00906 }
00907
00908 if (state == READY) {
00909 if (siblingTable->size()) {
00910 int32_t diff = OverlayKey::getLength() - getThisNode().getKey().
00911 sharedPrefixLength(siblingTable->front().key);
00912 for (int32_t i = OverlayKey::getLength() - 1; i >= (diff - 1); i--) {
00913 if ((routingTable[i] == NULL) ||
00914 ((simTime() - routingTable[i]->getLastUsage()) >
00915 minBucketRefreshInterval)) {
00916 createLookup()->lookup(getThisNode().getKey() ^
00917 (OverlayKey::ONE << i), 0, 0, 0,
00918 new KademliaLookupListener(this));
00919 setBucketUsage(getThisNode().getKey() ^
00920 (OverlayKey::ONE << i));
00921 }
00922 }
00923 }
00924
00925 cancelEvent(bucketRefreshTimer);
00926 scheduleAt(simTime() +
00927 (std::min(minSiblingTableRefreshInterval,
00928 minBucketRefreshInterval) / 10.0),
00929 bucketRefreshTimer);
00930 }
00931 }
00932
00933
00934 OverlayKey Kademlia::distance(const OverlayKey& x, const OverlayKey& y) const
00935 {
00936 return x^y;
00937 }
00938
00939 void Kademlia::updateTooltip()
00940 {
00941 if (ev.isGUI()) {
00942 std::stringstream ttString;
00943
00944
00945 ttString << "This: " << thisNode << endl << "Siblings: "
00946 << siblingTable->size();
00947
00948 getParentModule()->getParentModule()->getDisplayString().
00949 setTagArg("tt", 0, ttString.str().c_str());
00950 getParentModule()->getDisplayString().
00951 setTagArg("tt", 0, ttString.str().c_str());
00952 getDisplayString().setTagArg("tt", 0, ttString.str().c_str());
00953 }
00954 }
00955