BaseLookup Class Reference

#include <BaseLookup.h>

Inheritance diagram for BaseLookup:

RpcListener AbstractLookup Comparator< OverlayKey > List of all members.

Detailed Description

This class implements a basic greedy lookup strategy.

It uses the standard metric for greedy behaviour. If another metric is needed, the distance function can be replaced by overriding the distance method.

Author:
Sebastian Mies


Public Member Functions

void lookup (const OverlayKey &key, uint numNeighbors=0, LookupListener *listener=NULL)
 Lookup a neighborhood or a key.
const NodeVectorgetResult () 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 BasePathLookupcreatePathLookup ()
 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
BaseOverlayoverlay
 ptr to overlay
BaseLookupConfiguration config
 lookup configuration
LookupListenerlistener
 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
RpcInfoMap rpcs

Friends

class BasePathLookup
class BaseOverlay

Classes

class  RpcInfo
class  RpcInfoVector


Member Typedef Documentation

typedef hash_map<NodeHandle, RpcInfoVector, NodeHandle::hashFcn> BaseLookup::RpcInfoMap [protected]


Constructor & Destructor Documentation

BaseLookup::BaseLookup ( BaseOverlay overlay,
const BaseLookupConfiguration config 
) [protected]

00047 {
00048     this->config = config;
00049     if (config.secure)
00050         cout << "WARNING: Secure BaseLookup is still under development!!"
00051              << endl;
00052 
00053     this->overlay = overlay;
00054     this->running = false;
00055     this->finished = false;
00056     this->success = false;
00057 }

BaseLookup::~BaseLookup (  )  [protected, virtual]

00060 {
00061     stop();
00062     overlay->removeLookup(this);
00063 }


Member Function Documentation

bool BaseLookup::addNeighbor ( const NodeHandle handle  )  [protected]

00192 {
00193     bool result = false;
00194 
00195     if (numNeighbors == 0) {
00196         if (handle.key == key) {
00197             neighbors.clear();
00198             neighbors.push_back( handle );
00199             result = true;
00200         }
00201     } else {
00202         result = this->neighbors.add(handle);
00203     }
00204 
00205     return result;
00206 }

void BaseLookup::checkStop (  )  [inline, protected]

00153 {
00154     // check if there are rpcs pending or lookup finished
00155     if ( (successfulPaths >=1 && numNeighbors == 0) ||
00156             (finishedPaths ==
00157              (uint)config.parallelPaths && numNeighbors > 0) ) {
00158 
00159         for (uint i=0; i<paths.size();i++)
00160             success |= paths[i]->success;
00161         delete this;
00162 
00163     } else if (rpcs.size() == 0)
00164         delete this;
00165 
00166 }

int BaseLookup::compare ( const OverlayKey lhs,
const OverlayKey rhs 
) const [protected, virtual]

Reimplemented from Comparator< OverlayKey >.

00182 {
00183     return overlay->distance( key, lhs ).compareTo( overlay->distance(key,
00184                                                                       rhs));
00185 }

BasePathLookup * BaseLookup::createPathLookup (  )  [protected, virtual]

This method creates a new path lookup.

It may be overloaded to enhance BasePathLookup with some new information/features.

Returns:
The new path lookup
00172 {
00173     return new BasePathLookup(this);
00174 }

uint BaseLookup::getAccumulatedHops (  )  const [virtual]

Returns the total number of hops for all lookup paths.

Returns:
The accumulated number of hops.

Implements AbstractLookup.

00390 {
00391     return accumulatedHops;
00392 }

const NodeVector & BaseLookup::getResult (  )  const [virtual]

Returns the result of the lookup.

Returns:
The result node vector.

Implements AbstractLookup.

00379 {
00380     // return neighbor vector
00381     return neighbors;
00382 }

bool BaseLookup::getVisited ( const NodeHandle handle  )  [protected]

00217 {
00218     return this->visited.count(handle) != 0;
00219 }

void BaseLookup::handleRpcResponse ( BaseResponseMessage msg,
int  rpcId,
simtime_t  rtt 
) [protected, virtual]

This method is called if an RPC response has been received.

Parameters:
msg The response message.
rpcId The RPC id.
rtt The Round-Trip-Time of this RPC

Reimplemented from RpcListener.

00227 {
00228     // check flags
00229     if (finished || !running)
00230         return;
00231 
00232     // get source, cast messages and mark node as visited
00233     const NodeHandle& src = msg->getSrcNode();
00234     FindNodeResponse* findNodeResponse = dynamic_cast<FindNodeResponse*>(msg);
00235     PingResponse* pingResponse = dynamic_cast<PingResponse*>(msg);
00236     setVisited( src );
00237 
00238     if ( findNodeResponse != NULL || pingResponse != NULL) {
00239         // add authentificated neighbor
00240         addNeighbor( src );
00241     }
00242 
00243     // handle find node response
00244     if ( findNodeResponse != NULL ) {
00245 
00246         // check if rpc info is available, no -> exit
00247         if (rpcs.count(src)==0)
00248             return;
00249 
00250         // get info
00251         RpcInfoVector infos = rpcs[src];
00252         rpcs.erase(src);
00253 
00254         // add to neighborlist if not secure
00255         if (!config.secure)
00256             for (uint i=0; i<findNodeResponse->getClosestNodesArraySize(); i++)
00257                 addNeighbor( findNodeResponse->getClosestNodes(i) );
00258 
00259         // iterate
00260         bool rpcHandled = false;
00261         for (uint i=0; i<infos.size(); i++) {
00262 
00263             // get info
00264             const RpcInfo& info = infos[i];
00265 
00266             // do not handle finished paths
00267             if (info.path->finished)
00268                 continue;
00269 
00270             // check if path accepts the message
00271             if ( !rpcHandled && info.path->accepts( info.vrpcId ) ) {
00272                 info.path->handleResponse( findNodeResponse );
00273                 rpcHandled = true;
00274             } else {
00275                 info.path->handleTimeout( info.vrpcId );
00276             }
00277 
00278             // count finished and successful paths
00279             if (info.path->finished) {
00280                 finishedPaths++;
00281 
00282                 // count total number of hops
00283                 accumulatedHops += info.path->hops;
00284 
00285                 if (info.path->success)
00286                     successfulPaths++;
00287             }
00288 
00289         }
00290     }
00291 
00292     checkStop();
00293 }

void BaseLookup::handleRpcTimeout ( BaseCallMessage msg,
const NodeHandle dest,
int  rpcId 
) [protected, virtual]

This method is called if an RPC timeout has been reached.

Parameters:
msg The original RPC message.
dest The destination node
rpcId The RPC id.

Reimplemented from RpcListener.

00298 {
00299     // check flags
00300     if (finished || !running)
00301         return;
00302 
00303     // check if rpc info is available
00304     const NodeHandle& src = dest;
00305     if (rpcs.count(src)==0)
00306         return;
00307     RpcInfoVector& infos = rpcs[src];
00308 
00309     // iterate
00310     for (uint i=0; i < infos.size(); i++) {
00311 
00312         const RpcInfo& info = infos[i];
00313 
00314         // do not handle finished paths
00315         if (info.path->finished)
00316             continue;
00317 
00318         // delegate timeout
00319         info.path->handleTimeout( info.vrpcId );
00320 
00321         // count total number of hops
00322         accumulatedHops += info.path->hops;
00323 
00324         // count finished and successful paths
00325         if (info.path->finished) {
00326             finishedPaths++;
00327             if (info.path->success)
00328                 successfulPaths++;
00329         }
00330     }
00331 
00332     checkStop();
00333 }

bool BaseLookup::isValid (  )  const [virtual]

Returns true, if the lookup was successful.

Returns:
true, if the lookup was successful.

Implements AbstractLookup.

00385 {
00386     return success && finished;
00387 }

void BaseLookup::lookup ( const OverlayKey key,
uint  numNeighbors = 0,
LookupListener listener = NULL 
) [virtual]

Lookup a neighborhood or a key.

Parameters:
key The key to lookup
numNeighbors Number of Neighbors to lookup
listener Listener to inform, when the lookup is done

Implements AbstractLookup.

00364 {
00365     // check flags
00366     if (finished || running)
00367         return;
00368 
00369     // set params
00370     this->key = key;
00371     this->numNeighbors = numNeighbors;
00372     this->listener = listener;
00373 
00374     // start lookup
00375     start();
00376 }

void BaseLookup::sendRpc ( const NodeHandle handle,
FindNodeCall call,
BasePathLookup listener,
int  rpcId 
) [protected]

00337 {
00338     // check flags
00339     if (finished || !running)
00340         return;
00341 
00342     // create rpc info
00343     RpcInfo info;
00344     info.path = listener;
00345     info.vrpcId = rpcId;
00346 
00347     // send new message
00348     if ( rpcs.count(handle)==0 ) {
00349         RpcInfoVector newVector = RpcInfoVector();
00350         rpcs[handle] = newVector;
00351         newVector.nonce = overlay->sendRpcMessage( handle, call, this );
00352     }
00353 
00354     // register info
00355     rpcs[handle].push_back(info);
00356 }

void BaseLookup::setVisited ( const NodeHandle handle,
bool  visited = true 
) [protected]

00209 {
00210     if (visited)
00211         this->visited.insert( handle );
00212     else
00213         this->visited.erase( handle );
00214 }

void BaseLookup::start (  )  [protected]

00066 {
00067     // init params
00068     this->successfulPaths = 0;
00069     this->finishedPaths   = 0;
00070     this->accumulatedHops = 0;
00071 
00072     // init flags
00073     this->finished = false;
00074     this->success  = false;
00075     this->running  = true;
00076 
00077     // init neighborhood vector
00078     neighbors = NodeVector( numNeighbors == 0 ? 1 : numNeighbors, this );
00079 
00080     // get local closest nodes
00081     FindNodeCall* call = new FindNodeCall("FindNodeCall");
00082     NodeVector* nextHops = overlay->findNode(key, call);
00083 
00084     // if this node is new and no nodes are known -> stop lookup
00085     if (nextHops->size() == 0) {
00086         stop();
00087         delete nextHops;
00088         delete call;
00089         return;
00090     }
00091 
00092     // remove find node extensions
00093     cObject* findNodeExt = NULL;
00094     if (call->hasObject("findNodeExt"))
00095         findNodeExt = call->removeObject("findNodeExt");
00096     delete call;
00097 
00098     // distribution of nodes to paths
00099     uint n = nextHops->size() / config.parallelPaths;
00100 
00101     // not enough nodes for all paths? -> reduce number of parallel paths
00102     if ( n == 0 ) {
00103         config.parallelPaths = nextHops->size();
00104         n = 1;
00105     }
00106 
00107     // create parallel paths
00108     int j=0;
00109     for (int i=0; i<config.parallelPaths; i++) {
00110 
00111         // create state
00112         BasePathLookup* pathLookup = new BasePathLookup( this );
00113         paths.push_back( pathLookup );
00114 
00115         // populate next hops
00116         for ( uint k=0; k<n; k++, j++ )
00117             pathLookup->add
00118             ( (*nextHops)[j] );
00119 
00120         // send initial rpcs
00121         pathLookup->sendRpc( config.parallelRpcs, findNodeExt );
00122     }
00123     delete nextHops;
00124     delete findNodeExt;
00125 }

void BaseLookup::stop (  )  [protected]

00128 {
00129     // only stop if running
00130     if (!running)
00131         return;
00132 
00133     // cancel pending rpcs
00134     for (RpcInfoMap::iterator i = rpcs.begin(); i != rpcs.end(); i++)
00135         overlay->cancelRpcMessage( i->second.nonce );
00136     rpcs.clear();
00137 
00138     // delete path lookups
00139     for (uint i=0; i<paths.size(); i++)
00140         delete paths[i];
00141     paths.clear();
00142 
00143     // reset running flag
00144     running  = false;
00145     finished = true;
00146 
00147     // inform listener
00148     if ( listener != NULL )
00149         listener->lookupFinished(this);
00150 }


Friends And Related Function Documentation

friend class BaseOverlay [friend]

Reimplemented from RpcListener.

friend class BasePathLookup [friend]


Member Data Documentation

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

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


The documentation for this class was generated from the following files:
Generated on Fri Dec 15 17:50:30 2006 for ITM OverSim by  doxygen 1.4.7