RecursiveLookup.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2008 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 <CommonMessages_m.h>
00025 
00026 #include <BaseOverlay.h>
00027 #include <LookupListener.h>
00028 
00029 #include <RecursiveLookup.h>
00030 
00031 RecursiveLookup::RecursiveLookup(BaseOverlay* overlay,
00032                                  RoutingType routingType,
00033                                  const RecursiveLookupConfiguration& config,
00034                                  bool appLookup) :
00035     overlay(overlay),
00036     routingType(routingType),
00037     redundantNodes(config.redundantNodes),
00038     numRetries(config.numRetries),
00039     appLookup(appLookup)
00040 {
00041     valid = false;
00042 }
00043 
00044 RecursiveLookup::~RecursiveLookup()
00045 {
00046     if (listener != NULL) {
00047         delete listener;
00048         listener = NULL;
00049     }
00050 
00051     overlay->removeLookup(this);
00052 }
00053 
00054 void RecursiveLookup::lookup(const OverlayKey& key, int numSiblings,
00055                              int hopCountMax, int retries,
00056                              LookupListener* listener)
00057 {
00058     this->listener = listener;
00059 
00060     FindNodeCall* call = new FindNodeCall("FindNodeCall");
00061     if (appLookup) call->setStatType(APP_LOOKUP_STAT);
00062     else call->setStatType(MAINTENANCE_STAT);
00063     call->setLookupKey(key);
00064     call->setNumRedundantNodes(redundantNodes);
00065     call->setNumSiblings(numSiblings);
00066     call->setBitLength(FINDNODECALL_L(call));
00067 
00068     nonce = overlay->sendRouteRpcCall(OVERLAY_COMP, key, call, NULL,
00069                                       routingType, -1, retries, -1, this);
00070 }
00071 
00072 const NodeVector& RecursiveLookup::getResult() const
00073 {
00074     return siblings;
00075 }
00076 
00077 bool RecursiveLookup::isValid() const
00078 {
00079     return valid;
00080 }
00081 
00082 void RecursiveLookup::abortLookup()
00083 {
00084     overlay->cancelRpcMessage(nonce);
00085 
00086     delete this;
00087 }
00088 
00089 uint32_t RecursiveLookup::getAccumulatedHops() const
00090 {
00091     //throw new cRuntimeError("RecursiveLookup is asked for # accumulated hops!");
00092     return 0; //TODO hopCount in findNodeCall/Response ?
00093 }
00094 
00095 void RecursiveLookup::handleRpcTimeout(BaseCallMessage* msg,
00096                                        const TransportAddress& dest,
00097                                        cPolymorphic* context, int rpcId,
00098                                        const OverlayKey& destKey)
00099 {
00100     //TODO retry
00101     valid = false;
00102 
00103     // inform listener
00104     if (listener != NULL) {
00105         listener->lookupFinished(this);
00106         listener = NULL;
00107     }
00108 
00109     delete this;
00110 }
00111 
00112 void RecursiveLookup::handleRpcResponse(BaseResponseMessage* msg,
00113                                         cPolymorphic* context, int rpcId,
00114                                         simtime_t rtt)
00115 {
00116     FindNodeResponse* findNodeResponse = check_and_cast<FindNodeResponse*>(msg);
00117 
00118     if (findNodeResponse->getSiblings() &&
00119         findNodeResponse->getClosestNodesArraySize() > 0) {
00120         valid = true;
00121         for (uint32_t i = 0; i < findNodeResponse->getClosestNodesArraySize();
00122              i++) {
00123             siblings.push_back(findNodeResponse->getClosestNodes(i));
00124         }
00125     }
00126 
00127 //    std::cout << "RecursiveLookup::handleRpcResponse() "
00128 //              << findNodeResponse->getClosestNodesArraySize() << std::endl;
00129 
00130     // inform listener
00131     if (listener != NULL) {
00132         listener->lookupFinished(this);
00133         listener = NULL;
00134     }
00135     delete this;
00136 }
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3