#include <BaseLookup.h>
Inheritance diagram for BaseLookup:
It uses the standard metric for greedy behaviour. If another metric is needed, the distance function can be replaced by overriding the distance method.
Public Member Functions | |
void | lookup (const OverlayKey &key, uint numNeighbors=0, int hopCountMax=0, LookupListener *listener=NULL) |
Lookup a neighborhood or a key. | |
const NodeVector & | getResult () const |
Returns the result of the lookup. | |
bool | isValid () const |
Returns true, if the lookup was successful. | |
uint | getAccumulatedHops () const |
Returns the total number of hops for all lookup paths. | |
Protected Types | |
typedef hash_map< NodeHandle, RpcInfoVector, NodeHandle::hashFcn > | RpcInfoMap |
Protected Member Functions | |
virtual BasePathLookup * | createPathLookup () |
This method creates a new path lookup. | |
int | compare (const OverlayKey &lhs, const OverlayKey &rhs) const |
bool | addNeighbor (const NodeHandle &handle) |
void | setVisited (const NodeHandle &handle, bool visited=true) |
bool | getVisited (const NodeHandle &handle) |
void | handleRpcResponse (BaseResponseMessage *msg, int rpcId, simtime_t rtt) |
This method is called if an RPC response has been received. | |
void | handleRpcTimeout (BaseCallMessage *msg, const NodeHandle &dest, int rpcId) |
This method is called if an RPC timeout has been reached. | |
void | sendRpc (const NodeHandle &handle, FindNodeCall *call, BasePathLookup *listener, int rpcId) |
BaseLookup (BaseOverlay *overlay, const BaseLookupConfiguration &config) | |
virtual | ~BaseLookup () |
void | start () |
void | stop () |
void | checkStop () |
Protected Attributes | |
OverlayKey | key |
key to lookup | |
BaseOverlay * | overlay |
ptr to overlay | |
BaseLookupConfiguration | config |
lookup configuration | |
LookupListener * | listener |
lookup listener | |
vector< BasePathLookup * > | paths |
parallel paths | |
bool | finished |
true, if lookup is finished | |
bool | success |
true, if lookup was successful | |
bool | running |
true, if lookup is running | |
uint | finishedPaths |
number of finished paths | |
uint | successfulPaths |
number of successful paths | |
uint | accumulatedHops |
total number of hops (for all paths) | |
NodeVector | neighbors |
closest nodes | |
NodeHandle::Set | visited |
nodes already visited | |
int | numNeighbors |
number of neighbors | |
int | hopCountMax |
maximum hop count | |
RpcInfoMap | rpcs |
Friends | |
class | BasePathLookup |
class | BaseOverlay |
Classes | |
class | RpcInfo |
class | RpcInfoVector |
typedef hash_map<NodeHandle, RpcInfoVector, NodeHandle::hashFcn> BaseLookup::RpcInfoMap [protected] |
BaseLookup::BaseLookup | ( | BaseOverlay * | overlay, | |
const BaseLookupConfiguration & | config | |||
) | [protected] |
BaseLookup::~BaseLookup | ( | ) | [protected, virtual] |
bool BaseLookup::addNeighbor | ( | const NodeHandle & | handle | ) | [protected] |
00190 { 00191 bool result = false; 00192 00193 if (numNeighbors == 0) { 00194 if (handle.key == key) { 00195 neighbors.clear(); 00196 neighbors.push_back( handle ); 00197 result = true; 00198 } 00199 } else { 00200 if (config.parallelPaths==1) { 00201 result = true; 00202 this->neighbors.push_back(handle); 00203 } else { 00204 result = this->neighbors.add(handle); 00205 } 00206 } 00207 00208 return result; 00209 }
void BaseLookup::checkStop | ( | ) | [inline, protected] |
00151 { 00152 // check if there are rpcs pending or lookup finished 00153 if ( (successfulPaths >=1 && numNeighbors == 0) || 00154 (finishedPaths == 00155 (uint)config.parallelPaths && numNeighbors > 0) ) { 00156 00157 for (uint i=0; i<paths.size();i++) 00158 success |= paths[i]->success; 00159 delete this; 00160 00161 } else if (rpcs.size() == 0) 00162 delete this; 00163 00164 }
int BaseLookup::compare | ( | const OverlayKey & | lhs, | |
const OverlayKey & | rhs | |||
) | const [protected, virtual] |
BasePathLookup * BaseLookup::createPathLookup | ( | ) | [protected, virtual] |
This method creates a new path lookup.
It may be overloaded to enhance BasePathLookup with some new information/features.
00170 { 00171 return new BasePathLookup(this); 00172 }
uint BaseLookup::getAccumulatedHops | ( | ) | const [virtual] |
Returns the total number of hops for all lookup paths.
Implements AbstractLookup.
00402 { 00403 return accumulatedHops; 00404 }
const NodeVector & BaseLookup::getResult | ( | ) | const [virtual] |
Returns the result of the lookup.
Implements AbstractLookup.
00391 { 00392 // return neighbor vector 00393 return neighbors; 00394 }
bool BaseLookup::getVisited | ( | const NodeHandle & | handle | ) | [protected] |
void BaseLookup::handleRpcResponse | ( | BaseResponseMessage * | msg, | |
int | rpcId, | |||
simtime_t | rtt | |||
) | [protected, virtual] |
This method is called if an RPC response has been received.
msg | The response message. | |
rpcId | The RPC id. | |
rtt | The Round-Trip-Time of this RPC |
Reimplemented from RpcListener.
00230 { 00231 // check flags 00232 if (finished || !running) 00233 return; 00234 00235 // get source, cast messages and mark node as visited 00236 const NodeHandle& src = msg->getSrcNode(); 00237 FindNodeResponse* findNodeResponse = dynamic_cast<FindNodeResponse*>(msg); 00238 PingResponse* pingResponse = dynamic_cast<PingResponse*>(msg); 00239 setVisited( src ); 00240 00241 if ( findNodeResponse != NULL || pingResponse != NULL) { 00242 // add authentificated neighbor 00243 // addNeighbor( src ); 00244 } 00245 00246 // handle find node response 00247 if ( findNodeResponse != NULL ) { 00248 00249 // check if rpc info is available, no -> exit 00250 if (rpcs.count(src)==0) 00251 return; 00252 00253 // get info 00254 RpcInfoVector infos = rpcs[src]; 00255 rpcs.erase(src); 00256 00257 // add to neighborlist if not secure 00258 // if (!config.secure) 00259 // for (uint i=0; i<findNodeResponse->getClosestNodesArraySize(); i++) 00260 // addNeighbor( findNodeResponse->getClosestNodes(i) ); 00261 00262 // iterate 00263 bool rpcHandled = false; 00264 for (uint i=0; i<infos.size(); i++) { 00265 00266 // get info 00267 const RpcInfo& info = infos[i]; 00268 00269 // do not handle finished paths 00270 if (info.path->finished) 00271 continue; 00272 00273 // check if path accepts the message 00274 if ( !rpcHandled && info.path->accepts( info.vrpcId ) ) { 00275 info.path->handleResponse( findNodeResponse ); 00276 rpcHandled = true; 00277 } else { 00278 info.path->handleTimeout( info.vrpcId ); 00279 } 00280 00281 // count finished and successful paths 00282 if (info.path->finished) { 00283 finishedPaths++; 00284 00285 // count total number of hops 00286 accumulatedHops += info.path->hops; 00287 00288 if (info.path->success) 00289 successfulPaths++; 00290 } 00291 00292 } 00293 } 00294 00295 checkStop(); 00296 }
void BaseLookup::handleRpcTimeout | ( | BaseCallMessage * | msg, | |
const NodeHandle & | dest, | |||
int | rpcId | |||
) | [protected, virtual] |
This method is called if an RPC timeout has been reached.
msg | The original RPC message. | |
dest | The destination node | |
rpcId | The RPC id. |
Reimplemented from RpcListener.
00301 { 00302 // check flags 00303 if (finished || !running) 00304 return; 00305 00306 // check if rpc info is available 00307 const NodeHandle& src = dest; 00308 if (rpcs.count(src)==0) 00309 return; 00310 RpcInfoVector& infos = rpcs[src]; 00311 00312 // iterate 00313 for (uint i=0; i < infos.size(); i++) { 00314 00315 const RpcInfo& info = infos[i]; 00316 00317 // do not handle finished paths 00318 if (info.path->finished) 00319 continue; 00320 00321 // delegate timeout 00322 info.path->handleTimeout( info.vrpcId ); 00323 00324 // count total number of hops 00325 accumulatedHops += info.path->hops; 00326 00327 // count finished and successful paths 00328 if (info.path->finished) { 00329 finishedPaths++; 00330 if (info.path->success) 00331 successfulPaths++; 00332 } 00333 } 00334 00335 checkStop(); 00336 }
bool BaseLookup::isValid | ( | ) | const [virtual] |
Returns true, if the lookup was successful.
Implements AbstractLookup.
void BaseLookup::lookup | ( | const OverlayKey & | key, | |
uint | numNeighbors = 0 , |
|||
int | hopCountMax = 0 , |
|||
LookupListener * | listener = NULL | |||
) | [virtual] |
Lookup a neighborhood or a key.
key | The key to lookup | |
numNeighbors | Number of Neighbors to lookup | |
hopCountMax | Maximum hop count | |
listener | Listener to inform, when the lookup is done |
Implements AbstractLookup.
00375 { 00376 // check flags 00377 if (finished || running) 00378 return; 00379 00380 // set params 00381 this->key = key; 00382 this->numNeighbors = numNeighbors; 00383 this->hopCountMax = hopCountMax; 00384 this->listener = listener; 00385 00386 // start lookup 00387 start(); 00388 }
void BaseLookup::sendRpc | ( | const NodeHandle & | handle, | |
FindNodeCall * | call, | |||
BasePathLookup * | listener, | |||
int | rpcId | |||
) | [protected] |
00340 { 00341 // check flags 00342 if (finished || !running) 00343 return; 00344 00345 // create rpc info 00346 RpcInfo info; 00347 info.path = listener; 00348 info.vrpcId = rpcId; 00349 00350 // send new message 00351 if ( rpcs.count(handle)==0 ) { 00352 RpcInfoVector newVector = RpcInfoVector(); 00353 rpcs[handle] = newVector; 00354 00355 if (overlay->measureNetwInitPhase || 00356 !overlay->underlayConfigurator->isInit()) { 00357 00358 overlay->numFindNodeSent++; 00359 overlay->bytesFindNodeSent += call->byteLength(); 00360 } 00361 00362 newVector.nonce = overlay->sendRpcMessage( handle, call, this ); 00363 } 00364 00365 // register info 00366 rpcs[handle].push_back(info); 00367 }
void BaseLookup::setVisited | ( | const NodeHandle & | handle, | |
bool | visited = true | |||
) | [protected] |
00212 { 00213 if (visited) 00214 this->visited.insert( handle ); 00215 else 00216 this->visited.erase( handle ); 00217 }
void BaseLookup::start | ( | ) | [protected] |
00065 { 00066 // init params 00067 this->successfulPaths = 0; 00068 this->finishedPaths = 0; 00069 this->accumulatedHops = 0; 00070 00071 // init flags 00072 this->finished = false; 00073 this->success = false; 00074 this->running = true; 00075 00076 // init neighborhood vector 00077 neighbors = NodeVector( numNeighbors == 0 ? 1 : numNeighbors, this ); 00078 00079 // get local closest nodes 00080 FindNodeCall* call = new FindNodeCall("FindNodeCall"); 00081 NodeVector* nextHops = overlay->findNode(key, call); 00082 00083 // if this node is new and no nodes are known -> stop lookup 00084 if (nextHops->size() == 0) { 00085 stop(); 00086 delete nextHops; 00087 delete call; 00088 return; 00089 } 00090 00091 // remove find node extensions 00092 cMessage* findNodeExt = NULL; 00093 if (call->hasObject("findNodeExt")) 00094 findNodeExt = (cMessage*)call->removeObject("findNodeExt"); 00095 delete call; 00096 00097 // distribution of nodes to paths 00098 uint n = nextHops->size() / config.parallelPaths; 00099 00100 // not enough nodes for all paths? -> reduce number of parallel paths 00101 if ( n == 0 ) { 00102 config.parallelPaths = nextHops->size(); 00103 n = 1; 00104 } 00105 00106 // create parallel paths 00107 int j=0; 00108 for (int i=0; i<config.parallelPaths; i++) { 00109 00110 // create state 00111 BasePathLookup* pathLookup = new BasePathLookup( this ); 00112 paths.push_back( pathLookup ); 00113 00114 // populate next hops 00115 for ( uint k=0; k<n; k++, j++ ) 00116 pathLookup->add( (*nextHops)[j] ); 00117 00118 // send initial rpcs 00119 pathLookup->sendRpc( config.parallelRpcs, findNodeExt ); 00120 } 00121 delete nextHops; 00122 delete findNodeExt; 00123 }
void BaseLookup::stop | ( | ) | [protected] |
00126 { 00127 // only stop if running 00128 if (!running) 00129 return; 00130 00131 // cancel pending rpcs 00132 for (RpcInfoMap::iterator i = rpcs.begin(); i != rpcs.end(); i++) 00133 overlay->cancelRpcMessage( i->second.nonce ); 00134 rpcs.clear(); 00135 00136 // delete path lookups 00137 for (uint i=0; i<paths.size(); i++) 00138 delete paths[i]; 00139 paths.clear(); 00140 00141 // reset running flag 00142 running = false; 00143 finished = true; 00144 00145 // inform listener 00146 if ( listener != NULL ) 00147 listener->lookupFinished(this); 00148 }
friend class BaseOverlay [friend] |
Reimplemented from RpcListener.
friend class BasePathLookup [friend] |
uint BaseLookup::accumulatedHops [protected] |
total number of hops (for all paths)
BaseLookupConfiguration BaseLookup::config [protected] |
lookup configuration
bool BaseLookup::finished [protected] |
true, if lookup is finished
uint BaseLookup::finishedPaths [protected] |
number of finished paths
int BaseLookup::hopCountMax [protected] |
maximum hop count
OverlayKey BaseLookup::key [protected] |
key to lookup
LookupListener* BaseLookup::listener [protected] |
lookup listener
NodeVector BaseLookup::neighbors [protected] |
closest nodes
int BaseLookup::numNeighbors [protected] |
number of neighbors
BaseOverlay* BaseLookup::overlay [protected] |
ptr to overlay
vector<BasePathLookup*> BaseLookup::paths [protected] |
parallel paths
RpcInfoMap BaseLookup::rpcs [protected] |
bool BaseLookup::running [protected] |
true, if lookup is running
bool BaseLookup::success [protected] |
true, if lookup was successful
uint BaseLookup::successfulPaths [protected] |
number of successful paths
NodeHandle::Set BaseLookup::visited [protected] |
nodes already visited