IterativeLookup.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 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 <iostream>
00025 #include <assert.h>
00026 
00027 #include <UnderlayConfigurator.h>
00028 #include <LookupListener.h>
00029 #include <BaseOverlay.h>
00030 #include <GlobalStatistics.h>
00031 
00032 #include "IterativeLookup.h"
00033 
00034 using namespace std;
00035 
00036 //----------------------------------------------------------------------------
00037 
00038 LookupListener::~LookupListener()
00039 {}
00040 
00041 //----------------------------------------------------------------------------
00042 
00043 AbstractLookup::~AbstractLookup()
00044 {}
00045 
00046 std::ostream& operator<<(std::ostream& os, const LookupEntry& n)
00047 {
00048     os << "handle: " << n.handle << " source: " << n.source
00049        << " alreadyUsed: " << n.alreadyUsed;
00050 
00051     return os;
00052 };
00053 
00054 std::ostream& operator<<(std::ostream& os, const LookupVector& n)
00055 {
00056     for (LookupVector::const_iterator i=n.begin(); i !=n.end(); i++) {
00057         os << *i;
00058         if (i+1 != n.end()) {
00059             os << endl;
00060         }
00061     }
00062 
00063     return os;
00064 };
00065 
00066 //----------------------------------------------------------------------------
00067 //- Construction & Destruction -----------------------------------------------
00068 //----------------------------------------------------------------------------
00069 IterativeLookup::IterativeLookup(BaseOverlay* overlay,
00070                                  RoutingType routingType,
00071                                  const IterativeLookupConfiguration& config,
00072                                  const cObject* findNodeExt,
00073                                  bool appLookup) :
00074 overlay(overlay),
00075 routingType(routingType),
00076 config(config),
00077 firstCallExt(NULL),
00078 finished(false),
00079 success(false),
00080 running(false),
00081 appLookup(appLookup)
00082 {
00083     if (findNodeExt) firstCallExt = static_cast<cObject*>(findNodeExt->dup());
00084 
00085     if (config.secure) {
00086         throw cRuntimeError("IterativeLookup::IterativeLookup(): "
00087                                 "Secure IterativeLookup not implemented yet!");
00088     }
00089 
00090     if ((config.parallelPaths > 1) && (!config.merge)) {
00091         throw cRuntimeError("IterativeLookup::IterativeLookup(): "
00092                                 "config.merge must be enabled for "
00093                                 "using parallel paths!");
00094     }
00095 
00096     if ((config.useAllParallelResponses) && (!config.merge)) {
00097         throw cRuntimeError("IterativeLookup::IterativeLookup(): "
00098                                 "config.merge must be enabled if "
00099                                 "config.useAllParallelResponses is true!");
00100     }
00101 }
00102 
00103 IterativeLookup::~IterativeLookup()
00104 {
00105     stop();
00106     delete firstCallExt;
00107     overlay->removeLookup(this);
00108 
00109 //    std::cout << "time: " << simTime() << "deleting " << this << endl;
00110 }
00111 
00112 void IterativeLookup::abortLookup()
00113 {
00114     if (listener != NULL) {
00115         delete listener;
00116         listener = NULL;
00117     }
00118     delete this;
00119 }
00120 
00121 void IterativeLookup::start()
00122 {
00123 //    std::cout << "time: " << simTime() << " start(): node: " << overlay->getThisNode() << " this: " << this  << " key: " << key << endl;
00124 
00125     // init params
00126     successfulPaths = 0;
00127     finishedPaths   = 0;
00128     accumulatedHops = 0;
00129 
00130     // init flags
00131     finished = false;
00132     success  = false;
00133     running  = true;
00134 
00135     // init siblings vector
00136     siblings = NodeVector(numSiblings == 0 ? 1 : numSiblings, this);
00137     visited.clear();
00138     dead.clear();
00139 
00140     // get local closest nodes
00141     FindNodeCall* call = createInitialRpcCall();
00142     NodeVector* nextHops = overlay->findNode(key, config.redundantNodes,
00143                                              numSiblings, call);
00144     bool err;
00145 
00146     setVisited(overlay->getThisNode());
00147 
00148     // if this node is new and no nodes are known -> stop lookup
00149     if (nextHops->size() == 0) {
00150         //std::cout << "IterativeLookup: No next hops known" << endl;
00151         finished = true;
00152         success = false;
00153     } else if ((numSiblings == 0)
00154             && overlay->isSiblingFor(overlay->getThisNode(),
00155                                      key, numSiblings,
00156                                      &err)) {
00157         if (overlay->getThisNode().key == key) {
00158             addSibling(overlay->getThisNode());
00159             success = true;
00160         } else {
00161             std::cout << "IterativeLookup: numSiblings==0 and no node with this id"
00162                       << endl;
00163             success = false;
00164         }
00165         finished = true;
00166     }
00167     // finish lookup if the key is local and siblings are needed
00168     else if (numSiblings != 0 &&
00169              overlay->isSiblingFor(overlay->getThisNode(), key,
00170                                    numSiblings, &err)
00171              && !config.secure) {
00172 
00173         for (uint32_t i=0; i<nextHops->size(); i++) {
00174             addSibling(nextHops->at(i));
00175         }
00176 
00177         success = finished = true;
00178     }
00179 
00180     // if the key was local or belongs to one of our siblings we are finished
00181     if (finished) {
00182         // calls stop and finishes the lookup
00183         delete nextHops;
00184         delete call;
00185         delete this;
00186         return;
00187     }
00188 
00189     // extract find node extensions
00190     cPacket* findNodeExt = NULL;
00191     if (call->hasObject("findNodeExt"))
00192         findNodeExt = (cPacket*)call->removeObject("findNodeExt");
00193     delete call;
00194 
00195     // not enough nodes for all paths? -> reduce number of parallel paths
00196     if ((uint32_t)config.parallelPaths > nextHops->size()) {
00197         config.parallelPaths = nextHops->size();
00198     }
00199 
00200     // create parallel paths and distribute nodes to paths
00201     for (int i = 0; i < config.parallelPaths; i++) {
00202 
00203         // create state
00204         IterativePathLookup* pathLookup = new IterativePathLookup(this);
00205         paths.push_back(pathLookup);
00206 
00207         // populate next hops
00208         for (uint32_t k=0; (k * config.parallelPaths + i) < nextHops->size(); k++) {
00209             pathLookup->add(nextHops->at(k * config.parallelPaths + i));
00210         }
00211 
00212         // send initial rpcs
00213         pathLookup->sendRpc(config.parallelRpcs, findNodeExt);
00214     }
00215 
00216 
00217     //std::cout << "nextHops size: " << nextHops->size()
00218     //<< " numSiblings: " << numSiblings
00219     //<< " redundantNodes: " << config.redundantNodes
00220     //<< " thisNode " << overlay->getThisNode().ip
00221     //<< " nextHop " << nextHops->at(0).ip << endl;
00222 
00223     delete nextHops;
00224     delete findNodeExt;
00225 
00226     checkStop();
00227 }
00228 
00229 void IterativeLookup::stop()
00230 {
00231     // only stop if running
00232     if (!running)
00233         return;
00234 
00235     // cancel pending rpcs
00236     for (RpcInfoMap::iterator i = rpcs.begin(); i != rpcs.end(); i++) {
00237 //      std::cout << "time: " << simTime()     << " node: " << overlay->thisNode          << " this: " << this << " first: " << i->first  << " nonce: " << i->second.nonce << endl;
00238         overlay->cancelRpcMessage(i->second.nonce);
00239     }
00240     rpcs.clear();
00241 
00242     // delete path lookups
00243     for (uint32_t i=0; i<paths.size(); i++) {
00244         delete paths[i];
00245     }
00246     paths.clear();
00247 
00248     // reset running flag
00249     running  = false;
00250     finished = true;
00251 
00252     // inform listener
00253     if (listener != NULL) {
00254         listener->lookupFinished(this);
00255         listener = NULL;
00256     }
00257 }
00258 
00259 inline void IterativeLookup::checkStop()
00260 {
00261     bool finishLookup = false;
00262 
00263     // check if there are rpcs pending or lookup finished
00264     if ((successfulPaths >= 1 && numSiblings == 0) ||
00265         (finishedPaths == (uint32_t)config.parallelPaths && numSiblings > 0)) {
00266         for (uint32_t i=0; i<paths.size(); i++)
00267             success |= paths[i]->success;
00268         finishLookup = true;
00269     } else if (rpcs.size() == 0) {
00270         finishLookup = true;
00271     }
00272 
00273     if (finishLookup == true) {
00274 
00275         if (success == false) {
00276             //cout << "failed: hops :" << accumulatedHops << endl;
00277         }
00278 
00279         if (success == false && retries > 0) {
00280             //      std::cout << "IterativeLookup::checkStop(): Retry..." << endl;
00281             retries--;
00282             LookupListener* oldListener = listener;
00283             listener = NULL;
00284             stop();
00285             listener = oldListener;
00286             start();
00287         } else {
00288             delete this;
00289         }
00290     }
00291 }
00292 
00293 //----------------------------------------------------------------------------
00294 //- Enhanceable methods ------------------------------------------------------
00295 //----------------------------------------------------------------------------
00296 IterativePathLookup* IterativeLookup::createPathLookup()
00297 {
00298     return new IterativePathLookup(this);
00299 }
00300 
00301 FindNodeCall* IterativeLookup::createInitialRpcCall()
00302 {
00303     FindNodeCall* call = new FindNodeCall("FindNodeCall");
00304     if (appLookup) {
00305         call->setStatType(APP_LOOKUP_STAT);
00306     } else {
00307         call->setStatType(MAINTENANCE_STAT);
00308     }
00309 
00310     if (firstCallExt) {
00311         call->addObject(static_cast<cObject*>(firstCallExt->dup()));
00312     }
00313 
00314     return call;
00315 }
00316 
00317 //----------------------------------------------------------------------------
00318 //- Base configuration and state ---------------------------------------------
00319 //----------------------------------------------------------------------------
00320 //virtual public
00321 int IterativeLookup::compare(const OverlayKey& lhs, const OverlayKey& rhs) const
00322 {
00323     return overlay->distance(lhs, key).compareTo(overlay->distance(rhs, key));
00324 }
00325 
00326 
00327 //----------------------------------------------------------------------------
00328 //- Siblings and visited nodes management -----------------------------------
00329 //----------------------------------------------------------------------------
00330 bool IterativeLookup::addSibling(const NodeHandle& handle)
00331 {
00332     bool result = false;
00333 
00334     if (numSiblings == 0) {
00335         if (handle.key == key) {
00336             siblings.clear();
00337             siblings.push_back( handle );
00338             result = true;
00339         }
00340     } else {
00341         if (config.parallelPaths == 1) {
00342             result = true;
00343             if (!siblings.isFull()) {
00344                 siblings.push_back(handle);
00345             }
00346         } else {
00347             if (this->siblings.add(handle) >= 0) {
00348                 result = true;
00349             }
00350         }
00351     }
00352 
00353     return result;
00354 }
00355 
00356 void IterativeLookup::setVisited(const TransportAddress& addr, bool visitedFlag)
00357 {
00358     if (visitedFlag)
00359         this->visited.insert(addr);
00360     else
00361         this->visited.erase(addr);
00362 }
00363 
00364 bool IterativeLookup::getVisited(const TransportAddress& addr)
00365 {
00366     return (visited.count(addr) != 0);
00367 }
00368 
00369 void IterativeLookup::setDead(const TransportAddress& addr)
00370 {
00371     dead.insert(addr);
00372 }
00373 
00374 bool IterativeLookup::getDead(const TransportAddress& addr)
00375 {
00376     return (dead.count(addr) != 0);
00377 }
00378 
00379 
00380 
00381 //----------------------------------------------------------------------------
00382 //- Parallel RPC distribution ------------------------------------------------
00383 //----------------------------------------------------------------------------
00384 void IterativeLookup::handleRpcResponse(BaseResponseMessage* msg,
00385                                    cPolymorphic* context,
00386                                    int rpcId, simtime_t rtt)
00387 {
00388     // check flags
00389     if (finished || !running)
00390         return;
00391 
00392     // get source, cast messages and mark node as visited
00393     const TransportAddress& src = msg->getSrcNode();
00394     FindNodeResponse* findNodeResponse = dynamic_cast<FindNodeResponse*>(msg);
00395     PingResponse* pingResponse = dynamic_cast<PingResponse*>(msg);
00396     FailedNodeResponse* failedNodeResponse =
00397     dynamic_cast<FailedNodeResponse*>(msg);
00398 
00399     if (findNodeResponse != NULL || pingResponse != NULL) {
00400         // add authentificated sibling
00401         // addSibling( src );
00402     }
00403 
00404     // handle find node response
00405     if (findNodeResponse != NULL) {
00406 //      std::cout << "time: " << simTime() << " node: " << overlay->thisNode << " this: " << this << " received rpc with nonce: " << findNodeResponse->getNonce() << " from: " << findNodeResponse->getSrcNode() << endl;
00407 
00408         // check if rpc info is available, no -> exit
00409         if (rpcs.count(src) == 0)
00410             return;
00411 
00412         // get info
00413         RpcInfoVector infos = rpcs[src];
00414         rpcs.erase(src);
00415 
00416         // add to siblinglist if not secure
00417         //      if (!config.secure)
00418         //          for (uint i=0; i<findNodeResponse->getClosestNodesArraySize(); i++)
00419         //              addSibling( findNodeResponse->getClosestNodes(i) );
00420 
00421         // iterate
00422         bool rpcHandled = false;
00423 
00424         for (uint32_t i=0; i<infos.size(); i++) {
00425             // get info
00426             const RpcInfo& info = infos[i];
00427 
00428             // do not handle finished paths
00429             if (info.path->finished)
00430                 continue;
00431 
00432             // check if path accepts the message
00433             // make an exception for responses with siblings==true
00434             if (!rpcHandled &&
00435                     (info.path->accepts(info.vrpcId) ||
00436                     ((routingType == EXHAUSTIVE_ITERATIVE_ROUTING)
00437                             || findNodeResponse->getSiblings()))) {
00438                 info.path->handleResponse(findNodeResponse);
00439                 rpcHandled = true;
00440             } else {
00441                 EV << "[IterativeLookup::handleRpcResponse()]\n"
00442                    << "    Path does not accept message with id " << info.vrpcId
00443                    << endl;
00444 
00445                 info.path->handleTimeout(NULL, findNodeResponse->getSrcNode(),
00446                                          info.vrpcId);
00447             }
00448 
00449             // count finished and successful paths
00450             if (info.path->finished) {
00451                 finishedPaths++;
00452 
00453                 // count total number of hops
00454                 accumulatedHops += info.path->hops;
00455 
00456                 if (info.path->success)
00457                     successfulPaths++;
00458             }
00459 
00460         }
00461     }
00462 
00463 
00464     // handle failed node response
00465     if (failedNodeResponse != NULL) {
00466         cPacket* findNodeExt = NULL;
00467         if (failedNodeResponse->hasObject("findNodeExt")) {
00468             findNodeExt =
00469                 (cPacket*)failedNodeResponse->removeObject("findNodeExt");
00470         }
00471 
00472         for (std::vector<IterativePathLookup*>::iterator i = paths.begin();
00473             i != paths.end(); i++) {
00474 
00475             (*i)->handleFailedNodeResponse(failedNodeResponse->getSrcNode(),
00476                                            findNodeExt,
00477                                            failedNodeResponse->getTryAgain());
00478         }
00479     }
00480 
00481     checkStop();
00482 }
00483 
00484 
00485 void IterativeLookup::handleRpcTimeout(BaseCallMessage* msg,
00486                                   const TransportAddress& dest,
00487                                   cPolymorphic* context, int rpcId,
00488                                   const OverlayKey& destKey)
00489 {
00490     // check flags
00491     if (finished || !running)
00492         return;
00493 
00494     // check if rpc info is available
00495     if (rpcs.count(dest)==0) {
00496         cout << "IterativeLookup::handleRpcTimeout(): RPC Timeout, but node"
00497                  << " is not in rpcs structure!" << endl;
00498         return;
00499     }
00500 
00501     // mark the node as dead
00502     setDead(dest);
00503 
00504     RpcInfoVector infos = rpcs[dest];
00505     rpcs.erase(dest);
00506 
00507     // iterate
00508     for (uint32_t i=0; i < infos.size(); i++) {
00509 
00510         const RpcInfo& info = infos[i];
00511 
00512         // do not handle finished paths
00513         if (info.path->finished)
00514             continue;
00515 
00516         // delegate timeout
00517         info.path->handleTimeout(msg, dest, info.vrpcId);
00518 
00519         // count total number of hops
00520         accumulatedHops += info.path->hops;
00521 
00522         // count finished and successful paths
00523         if (info.path->finished) {
00524             finishedPaths++;
00525             if (info.path->success)
00526                 successfulPaths++;
00527         }
00528     }
00529     checkStop();
00530 }
00531 
00532 void IterativeLookup::sendRpc(const NodeHandle& handle, FindNodeCall* call,
00533                          IterativePathLookup* listener, int rpcId)
00534 {
00535     // check flags
00536     if (finished || !running) {
00537         delete call;
00538         return;
00539     }
00540 
00541     // create rpc info
00542     RpcInfo info;
00543     info.path = listener;
00544     info.vrpcId = rpcId;
00545 
00546     // send new message
00547     if (rpcs.count(handle) == 0) {
00548         RpcInfoVector newVector;
00549 
00550         overlay->countFindNodeCall(call);
00551         newVector.nonce = overlay->sendUdpRpcCall(handle, call, NULL,
00552                                                   -1, 0, -1, this);
00553 
00554         //      std::cout << "time: " << simTime() << " node: " << overlay->thisNode << " new rpc with nonce: " << newVector.nonce << " to: " << handle << endl;
00555         rpcs[handle] = newVector;
00556     } else {
00557         EV << "[IterativeLookup::sendRpc()]\n"
00558            << "    RPC already sent...not sent again"
00559            << endl;
00560         delete call;
00561     }
00562 
00563     // register info
00564     rpcs[handle].push_back(info);
00565 }
00566 
00567 //----------------------------------------------------------------------------
00568 //- AbstractLookup implementation --------------------------------------------
00569 //----------------------------------------------------------------------------
00570 
00571 void IterativeLookup::lookup(const OverlayKey& key, int numSiblings,
00572                         int hopCountMax, int retries, LookupListener* listener)
00573 {
00574     EV << "[IterativeLookup::lookup() @ " << overlay->overlay->getThisNode().ip
00575        << " (" << overlay->overlay->getThisNode().key.toString(16) << ")]\n"
00576        << "    Lookup of key " << key
00577        << endl;
00578 
00579     // check flags
00580     if (finished || running)
00581         return;
00582 
00583     // set params
00584     this->key = key;
00585     this->numSiblings = numSiblings;
00586     this->hopCountMax = hopCountMax;
00587     this->listener = listener;
00588     this->retries = retries;
00589 
00590     if ((routingType == EXHAUSTIVE_ITERATIVE_ROUTING)
00591         && (numSiblings > config.redundantNodes)) {
00592         throw cRuntimeError("IterativeLookup::lookup(): "
00593                   "With EXHAUSTIVE_ITERATIVE_ROUTING numRedundantNodes "
00594                   "must be >= numSiblings!");
00595     }
00596 
00597     // start lookup
00598     start();
00599 }
00600 
00601 const NodeVector& IterativeLookup::getResult() const
00602 {
00603     // return sibling vector
00604     return siblings;
00605 }
00606 
00607 bool IterativeLookup::isValid() const
00608 {
00609     return success && finished;
00610 }
00611 
00612 uint32_t IterativeLookup::getAccumulatedHops() const
00613 {
00614     return accumulatedHops;
00615 }
00616 
00617 IterativePathLookup::IterativePathLookup(IterativeLookup* lookup)
00618 {
00619     this->lookup = lookup;
00620     this->hops = 0;
00621     this->step = 0;
00622     this->pendingRpcs = 0;
00623     this->finished = false;
00624     this->success = false;
00625 
00626     if (lookup->routingType == EXHAUSTIVE_ITERATIVE_ROUTING) {
00627         // need to add some extra space for backup nodes, if we have to
00628         // remove failed nodes from the nextHops vector
00629         this->nextHops = LookupVector(2*(lookup->config.redundantNodes),
00630                                       lookup);
00631     } else {
00632         this->nextHops = LookupVector((lookup->config.redundantNodes),
00633                                       lookup);
00634     }
00635 }
00636 
00637 IterativePathLookup::~IterativePathLookup()
00638 {}
00639 
00640 bool IterativePathLookup::accepts(int rpcId)
00641 {
00642     if (finished) {
00643         return false;
00644     }
00645 
00646     // shall we use all responses, or only
00647     // the first one (rpcId == step)?
00648     if (lookup->config.useAllParallelResponses
00649         && lookup->config.merge) {
00650 
00651         return true;
00652     }
00653 
00654     return (rpcId == step);
00655 }
00656 
00657 void IterativePathLookup::handleResponse(FindNodeResponse* msg)
00658 {
00659     if (finished)
00660         return;
00661 
00662     const NodeHandle& source = msg->getSrcNode();
00663     std::map<TransportAddress, NodeHandle>::iterator oldPos;
00664     oldPos = oldNextHops.find(source);
00665     if (oldPos != oldNextHops.end()) oldNextHops.erase(oldPos);
00666 
00667     // don't count local hops
00668     if (lookup->overlay->getThisNode() != source) {
00669         hops++;
00670     }
00671 
00672     lookup->setVisited(source);
00673 
00674     if (source.key == lookup->key) {
00675 
00676 //        cout << "received response from destination for key " << lookup->key
00677 //             << " with isSibling = " << msg->getSiblings() << endl;
00678     }
00679 
00680     step++;
00681 
00682     // decrease pending rpcs
00683     pendingRpcs--;
00684 
00685     if (msg->getClosestNodesArraySize() != 0) {
00686         // mode: merge or replace
00687         if (!lookup->config.merge) {
00688             nextHops.clear();
00689         }
00690     } else {
00691         //cout << "findNode() returned 0 nodes!" << endl;
00692     }
00693 
00694     int numNewRpcs = 0;
00695 
00696     // add new next hops
00697     for (uint32_t i=0; i < msg->getClosestNodesArraySize(); i++) {
00698         const NodeHandle& handle = msg->getClosestNodes(i);
00699 
00700         // add NodeHandle to next hops and siblings
00701         int pos = add(handle, source);
00702 
00703         // only send new rpcs if we've learned about new nodes
00704         if ((pos >= 0) && (pos < lookup->config.redundantNodes)) {
00705             numNewRpcs++;
00706         }
00707 
00708         // check if node was found
00709         if ((lookup->numSiblings == 0) && (handle.key == lookup->key)
00710                 && (!lookup->config.secure)) {
00711 
00712             lookup->addSibling(handle);
00713 
00714             finished = true;
00715             success = true;
00716             return;
00717         } else {
00718             if (lookup->numSiblings != 0 && !lookup->config.secure
00719                 && (lookup->routingType != EXHAUSTIVE_ITERATIVE_ROUTING)
00720                 && msg->getSiblings()) {
00721 
00722                 //cout << "adding sibling " << handle << endl;
00723                 lookup->addSibling(handle);
00724             }
00725         }
00726     }
00727 
00728 #if 0
00729     cout << "nextHops.size " << nextHops.size()
00730          << " find node response " << msg->getClosestNodesArraySize()
00731          << " config " << lookup->config.redundantNodes << endl;
00732 
00733     cout << "looking for " << lookup->key << endl;
00734 
00735     for (uint32_t i=0; i < msg->getClosestNodesArraySize(); i++) {
00736         cout << "find node " << msg->getClosestNodes(i) << endl;
00737     }
00738 
00739     cout << "next Hops " << nextHops << endl;
00740 #endif
00741 
00742     // check if sibling lookup is finished
00743     if ((lookup->routingType != EXHAUSTIVE_ITERATIVE_ROUTING)
00744             && msg->getSiblings()
00745             && msg->getClosestNodesArraySize() != 0 &&
00746             lookup->numSiblings != 0 && !lookup->config.secure) {
00747 
00748         finished = true;
00749         success = true;
00750         return;
00751     }
00752 
00753     // extract find node extension object
00754     cPacket* findNodeExt = NULL;
00755     if (msg->hasObject("findNodeExt")) {
00756         findNodeExt = (cPacket*)msg->removeObject("findNodeExt");
00757     }
00758 
00759     // If config.newRpcOnEveryResponse is true, send a new RPC
00760     // even if there was no lookup progress
00761     if ((numNewRpcs == 0) && lookup->config.newRpcOnEveryResponse) {
00762         numNewRpcs = 1;
00763     }
00764 
00765     // send next rpcs
00766     sendRpc(min(numNewRpcs, lookup->config.parallelRpcs), findNodeExt);
00767 
00768     delete findNodeExt;
00769 }
00770 
00771 void IterativePathLookup::sendNewRpcAfterTimeout(cPacket* findNodeExt)
00772 {
00773     // two alternatives to react on a timeout
00774     if (lookup->config.newRpcOnEveryTimeout) {
00775         // always send one new RPC for every timeout
00776         sendRpc(1, findNodeExt);
00777     } else if (pendingRpcs == 0) {
00778         // wait until all RPCs have timed out and then send alpha new RPCs
00779         sendRpc(lookup->config.parallelRpcs, findNodeExt);
00780     }
00781 }
00782 
00783 void IterativePathLookup::handleTimeout(BaseCallMessage* msg,
00784                                    const TransportAddress& dest, int rpcId)
00785 {
00786     if (finished)
00787         return;
00788 
00789     EV << "[IterativePathLookup::handleTimeout()]\n"
00790        << "    Timeout of RPC " << rpcId
00791        << endl;
00792 
00793     //std::cout << lookup->overlay->getThisNode() << ": Path timeout for node"
00794     //          << dest << endl;
00795 
00796     if ((lookup->routingType == EXHAUSTIVE_ITERATIVE_ROUTING)
00797             && lookup->getDead(dest)) {
00798         nextHops.erase(nextHops.findIterator((
00799                 dynamic_cast<const NodeHandle&>(dest)).key));
00800     }
00801 
00802     std::map<TransportAddress, NodeHandle>::iterator oldPos;
00803     oldPos = oldNextHops.find(dest);
00804 
00805     // decrease pending rpcs
00806     pendingRpcs--;
00807 
00808     cPacket* findNodeExt = NULL;
00809     if (msg && msg->hasObject("findNodeExt")) {
00810         findNodeExt = static_cast<cPacket*>(
00811                 msg->removeObject("findNodeExt"));
00812     }
00813 
00814     if (oldPos == oldNextHops.end() || (!lookup->config.failedNodeRpcs)) {
00815         sendNewRpcAfterTimeout(findNodeExt);
00816         delete findNodeExt;
00817     } else {
00818         if (oldPos->second.isUnspecified()) {
00819             FindNodeCall* findNodeCall = dynamic_cast<FindNodeCall*>(msg);
00820             // answer was from local findNode()
00821 
00822             if (findNodeCall && lookup->overlay->handleFailedNode(dest)) {
00823                 NodeVector* retry = lookup->overlay->findNode(
00824                    findNodeCall->getLookupKey(), -1, lookup->numSiblings, msg);
00825 
00826                 for (NodeVector::iterator i = retry->begin(); i != retry->end(); i++) {
00827                     nextHops.add(LookupEntry(*i, NodeHandle::UNSPECIFIED_NODE, false));
00828                 }
00829 
00830                 delete retry;
00831             }
00832 
00833             sendNewRpcAfterTimeout(findNodeExt);
00834             delete findNodeExt;
00835 
00836         } else {
00837             FailedNodeCall* call = new FailedNodeCall("FailedNodeCall");
00838             call->setFailedNode(dest);
00839             call->setBitLength(FAILEDNODECALL_L(call));
00840             if (findNodeExt) {
00841                 call->addObject(findNodeExt);
00842                 call->addBitLength(findNodeExt->getBitLength());
00843             }
00844             lookup->overlay->countFailedNodeCall(call);
00845             lookup->overlay->sendUdpRpcCall(oldPos->second, call, NULL,
00846                                             -1, 0, -1, lookup);
00847         }
00848     }
00849 }
00850 
00851 void IterativePathLookup::handleFailedNodeResponse(const NodeHandle& src,
00852                                               cPacket* findNodeExt, bool retry)
00853 {
00854     if (finished) {
00855         return;
00856     }
00857 
00858     std::map<TransportAddress, NodeHandle>::iterator oldPos;
00859     for (oldPos = oldNextHops.begin(); oldPos != oldNextHops.end(); oldPos++) {
00860         if ((! oldPos->second.isUnspecified()) &&
00861             (oldPos->second == src)) break;
00862     }
00863 
00864     if (oldPos == oldNextHops.end()) {
00865         return;
00866     }
00867 
00868     std::map<TransportAddress, NodeHandle>::iterator oldSrcPos =
00869         oldNextHops.find(src);
00870     const NodeHandle* oldSrc = &NodeHandle::UNSPECIFIED_NODE;
00871 
00872     if (oldSrcPos != oldNextHops.end()) {
00873         oldSrc = &(oldSrcPos->second);
00874     }
00875 
00876     if (retry) {
00877         // FIXME: This is needed for a node to be asked again when detecting
00878         // a failed node. It could pose problems when parallel lookups and
00879         // failed node recovery are both needed at the same time!
00880         lookup->setVisited(src, false);
00881 
00882         nextHops.add(LookupEntry(src, *oldSrc, false));
00883     }
00884 
00885     oldNextHops.erase(oldPos);
00886 
00887     sendNewRpcAfterTimeout(findNodeExt);
00888 }
00889 
00890 void IterativePathLookup::sendRpc(int num, cPacket* findNodeExt)
00891 {
00892     // path finished? yes -> quit
00893     if (finished)
00894         return;
00895 
00896     // check for maximum hop count
00897     if (lookup->hopCountMax && (hops >= lookup->hopCountMax)) {
00898         EV << "[IterativePathLookup::sendRpc()]\n"
00899            << "    Max hop count exceeded - lookup failed!"
00900            << endl;
00901         //cout << "[IterativePathLookup::sendRpc()]\n"
00902         //     << "    Max hop count exceeded - lookup failed!"
00903         //     << endl;
00904 
00905         finished = true;
00906         success = false;
00907 
00908         return;
00909     }
00910 
00911     // if strictParallelRpcs is true, limit concurrent in-flight requests
00912     // to config.parallelRpcs
00913     if (lookup->config.strictParallelRpcs) {
00914         num = min(num, lookup->config.parallelRpcs - pendingRpcs);
00915     }
00916 
00917     // try all remaining nodes
00918     if ((num == 0) && (pendingRpcs == 0)
00919             && !lookup->config.finishOnFirstUnchanged) {
00920         num = lookup->config.parallelRpcs;
00921         //cout << "trying all remaining nodes ("
00922         //     << lookup->numSiblings << ")" << endl;
00923     }
00924 
00925     // send rpc messages
00926     LookupVector::iterator it = nextHops.begin();
00927     int i = 0;
00928     for (LookupVector::iterator it = nextHops.begin();
00929          ((num > 0) && (i < lookup->config.redundantNodes)
00930           && (it != nextHops.end())); it++, i++)  {
00931 
00932         // ignore nodes to which we've already sent an RPC
00933         if (it->alreadyUsed || lookup->getDead(it->handle)) continue;
00934 
00935         // check if node has already been visited? no ->
00936         // TODO: doesn't work with Broose
00937         if (!lookup->getVisited(it->handle)) {
00938             // send rpc to node increase pending rpcs
00939             pendingRpcs++;
00940             num--;
00941             FindNodeCall* call = createRpcMessage(findNodeExt);
00942             lookup->sendRpc(it->handle, call, this, step);
00943             oldNextHops[it->handle] = it->source;
00944 
00945             //cout << "Sending RPC to " << it->handle
00946             //     << " ( " << num << " more )"
00947             //     << " thisNode = " << lookup->overlay->getThisNode().key << endl;
00948 
00949             // mark node as already used
00950             it->alreadyUsed = true;
00951         } else {
00952             //EV << "[IterativePathLookup::sendRpc()]\n"
00953             //   << "    Last next hop ("
00954             //   << it->handle
00955             //   << ") already visited."
00956             //   << endl;
00957 
00958 //            std::cout << "visited:" << std::endl;
00959 //            for (TransportAddress::Set::iterator it = lookup->visited.begin();
00960 //            it != lookup->visited.end(); it++)
00961 //                std::cout << *it << std::endl;
00962 //
00963 //            std::cout << "nextHops:" << std::endl;
00964 //            for (NodePairVector::iterator it = nextHops.begin();
00965 //                 it != nextHops.end(); it++)
00966 //                std::cout << it->first << std::endl;
00967         }
00968     }
00969 
00970     // no rpc sent, no pending rpcs?
00971     // -> failed for normal lookups
00972     // -> exhaustive lookups are always successful
00973     if (pendingRpcs == 0) {
00974         if (lookup->routingType == EXHAUSTIVE_ITERATIVE_ROUTING) {
00975             int i = 0;
00976             for (LookupVector::iterator it = nextHops.begin();
00977                  ((i < lookup->config.redundantNodes)
00978                          && (it != nextHops.end())); it++, i++)  {
00979                 lookup->addSibling(it->handle);
00980             }
00981 
00982             success = true;
00983         } else {
00984             success = false;
00985             //cout << "failed nextHops for key " << lookup->key << endl;
00986             //cout << nextHops << endl;
00987         }
00988 
00989         finished = true;
00990     }
00991     //cout << endl;
00992 }
00993 
00994 FindNodeCall* IterativePathLookup::createRpcMessage(cPacket* findNodeExt)
00995 {
00996     // create default find node call message
00997     FindNodeCall* call = new FindNodeCall( "FindNodeCall" );
00998 
00999     if (lookup->appLookup) {
01000         call->setStatType(APP_LOOKUP_STAT);
01001     } else {
01002         call->setStatType(MAINTENANCE_STAT);
01003     }
01004 
01005     call->setLookupKey(lookup->key);
01006     call->setNumRedundantNodes(lookup->config.redundantNodes);
01007     call->setNumSiblings(lookup->numSiblings);
01008     call->setBitLength(FINDNODECALL_L(call));
01009 
01010     // duplicate extension object
01011     if (findNodeExt != NULL) {
01012         call->addObject((cObject*)findNodeExt->dup());
01013         call->addBitLength(findNodeExt->getBitLength());
01014     }
01015 
01016     return call;
01017 }
01018 
01019 int IterativePathLookup::add(const NodeHandle& handle, const NodeHandle& source)
01020 {
01021     if (lookup->config.merge) {
01022         return nextHops.add(LookupEntry(handle, source, false));
01023     } else {
01024         nextHops.push_back(LookupEntry(handle, source, false));
01025         return (nextHops.size() - 1);
01026     }
01027 }
01028 

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