NeighborCache Class Reference

#include <NeighborCache.h>

List of all members.


Detailed Description

Definition at line 67 of file NeighborCache.h.


Public Member Functions

 ~NeighborCache ()
bool isEnabled ()
bool vivaldiEnabled ()
bool insertNodeContext (const TransportAddress &handle, cPolymorphic *context, RpcListener *rpcListener, int rpcId)
WaitingContexts * getNodeContexts (const TransportAddress &handle)
simtime_t getNodeAge (const TransportAddress &handle)
void updateNode (const NodeHandle &add, simtime_t rtt, const std::vector< double > &coords=coordsDummy, double error=1.0)
void setNodeTimeout (const TransportAddress &handle)
simtime_t getNodeRtt (const TransportAddress &add)
simtime_t getNodePredictedRtt (const TransportAddress &add)
NodeHandle getNodeHandle (const TransportAddress &add)
const Vivaldi & getVivaldiAccess ()
double getAvgAbsPredictionError (std::vector< double > &coord, uint32_t sampleSize)
const std::vector< double > & getNodeCoords (const TransportAddress &node)
double getNodeCoordError (const TransportAddress &node)

Protected Member Functions

void initialize ()
void finish ()

Protected Attributes

GlobalStatistics * globalStatistics
std::map< IPvXAddress,
NeighborCacheEntry > 
neighborCache
std::multimap< simtime_t,
IPvXAddress > 
neighborCacheExpireMap

Private Member Functions

bool cleanupCache ()
void updateEntry (const TransportAddress &address, simtime_t insertTime)

Private Attributes

bool enableNeighborCache
bool enableVivaldi
simtime_t rttExpirationTime
uint32_t maxSize
uint32_t misses
uint32_t hits
Vivaldi * vivaldi

Static Private Attributes

static const std::vector< double > coordsDummy

Friends

std::ostream & operator<< (std::ostream &os, const NeighborCacheEntry &entry)

Classes

struct  NeighborCacheEntry

Constructor & Destructor Documentation

NeighborCache::~NeighborCache (  ) 

Definition at line 99 of file NeighborCache.cc.

00100 {
00101     delete vivaldi;
00102 
00103     for (std::map<IPvXAddress, NeighborCacheEntry>::iterator it =
00104         neighborCache.begin(); it != neighborCache.end(); ++it) {
00105         if (it->second.waitingContexts) {
00106             for (uint32_t i = 0;
00107                  i < it->second.waitingContexts->pingContexts.size();
00108                  ++i) {
00109                 delete it->second.waitingContexts->pingContexts[i];
00110             }
00111 
00112             delete it->second.waitingContexts;
00113         }
00114     }
00115 }


Member Function Documentation

bool NeighborCache::cleanupCache (  )  [private]

Definition at line 263 of file NeighborCache.cc.

Referenced by insertNodeContext(), setNodeTimeout(), and updateNode().

00264 {
00265     bool result = false;
00266     uint32_t size = neighborCache.size();
00267     if (size > maxSize) {
00268         std::map<simtime_t, IPvXAddress>::iterator it;
00269         for (uint32_t i = 0; i < (size - (maxSize / 2)); ++i) {
00270             it = neighborCacheExpireMap.begin();
00271             if ((neighborCache[it->second].rtt == NEIGHBORSTATE_WAITING) ||
00272                 (neighborCache[it->second].insertTime == simTime())) {
00273                 break;
00274             }
00275             neighborCache.erase(it->second);
00276             neighborCacheExpireMap.erase(it);
00277             result = true;
00278         }
00279     }
00280     assert(neighborCache.size() == neighborCacheExpireMap.size());
00281     return result;
00282 }

void NeighborCache::updateEntry ( const TransportAddress &  address,
simtime_t  insertTime 
) [private]

Definition at line 284 of file NeighborCache.cc.

Referenced by insertNodeContext(), setNodeTimeout(), and updateNode().

00286 {
00287     std::multimap<simtime_t, IPvXAddress>::iterator it =
00288         neighborCacheExpireMap.lower_bound(insertTime);
00289     while (it->second != address.ip) ++it;
00290     neighborCacheExpireMap.erase(it);
00291     neighborCacheExpireMap.insert(std::make_pair(simTime(), address.ip));
00292     assert(neighborCache.size() == neighborCacheExpireMap.size());
00293 }

void NeighborCache::initialize (  )  [protected]

Definition at line 66 of file NeighborCache.cc.

00067 {
00068     neighborCache.clear();
00069     WATCH_MAP(neighborCache);
00070 
00071     enableNeighborCache = par("enableNeighborCache");
00072     enableVivaldi = par("enableVivaldi");
00073     rttExpirationTime = par("rttExpirationTime");
00074     maxSize = par("maxSize");
00075 
00076     globalStatistics = GlobalStatisticsAccess().get();
00077 
00078     // vivaldi init
00079     if (enableVivaldi) {
00080         vivaldi = new Vivaldi;
00081         vivaldi->initVivaldi(this);
00082     } else {
00083         vivaldi = NULL;
00084     }
00085 
00086     misses = 0;
00087     hits = 0;
00088 }

void NeighborCache::finish (  )  [protected]

Definition at line 90 of file NeighborCache.cc.

00090                            {
00091     if ((misses + hits) != 0) {
00092         globalStatistics
00093             ->addStdDev("NeighborCache: Ping hit rate",
00094                         ((double)hits / (double)(misses + hits)));
00095     }
00096 }

bool NeighborCache::isEnabled (  )  [inline]

Definition at line 118 of file NeighborCache.h.

Referenced by BasePastry::baseInit().

00119     {
00120         return enableNeighborCache;
00121     };

bool NeighborCache::vivaldiEnabled (  )  [inline]

Definition at line 123 of file NeighborCache.h.

Referenced by BaseRpc::internalHandleRpcMessage(), and BaseRpc::sendRpcResponse().

00124     {
00125         return enableVivaldi;
00126     };

bool NeighborCache::insertNodeContext ( const TransportAddress &  handle,
cPolymorphic *  context,
RpcListener *  rpcListener,
int  rpcId 
)

Definition at line 117 of file NeighborCache.cc.

Referenced by BaseRpc::pingNode().

00121 {
00122     if (!enableNeighborCache) return false;
00123     if (neighborCache.count(handle.ip) == 0) {
00124         NeighborCacheEntry entry;
00125 
00126         entry.insertTime = simTime();
00127         entry.rtt = NEIGHBORSTATE_WAITING;
00128         entry.nodeRef = NodeHandle::UNSPECIFIED_NODE;
00129 
00130         neighborCache[handle.ip] = entry;
00131         neighborCacheExpireMap.insert(std::make_pair(entry.insertTime, handle.ip));
00132 
00133         cleanupCache();
00134 
00135         assert(neighborCache.size() == neighborCacheExpireMap.size());
00136         return false;
00137     } else {
00138         NeighborCacheEntry& entry = neighborCache[handle.ip];
00139 
00140         // waiting?
00141         if (entry.rtt == NEIGHBORSTATE_WAITING) {
00142             if (entry.waitingContexts == NULL)
00143                 entry.waitingContexts = new WaitingContexts();
00144             entry.waitingContexts->pingContexts.push_back(context);
00145             entry.waitingContexts->pingListeners.push_back(rpcListener);
00146             entry.waitingContexts->pingIds.push_back(rpcId);
00147 
00148             return true;
00149         } else {
00150             if (entry.waitingContexts != NULL &&
00151                 entry.waitingContexts->pingContexts.size() != 0) {
00152                 throw cRuntimeError("not waiting for response,"
00153                                         " but additional contexts found!");
00154             }
00155 
00156             updateEntry(handle, entry.insertTime);
00157 
00158             entry.rtt = NEIGHBORSTATE_WAITING;
00159             entry.insertTime = simTime();
00160             entry.nodeRef = NodeHandle::UNSPECIFIED_NODE;
00161 
00162             return false;
00163         }
00164     }
00165 }

WaitingContexts* NeighborCache::getNodeContexts ( const TransportAddress &  handle  )  [inline]

Definition at line 133 of file NeighborCache.h.

Referenced by BaseRpc::pingRpcResponse(), and BaseRpc::pingRpcTimeout().

00134     {
00135         if (!enableNeighborCache) return NULL;
00136         if (neighborCache.count(handle.ip) == 0)
00137             throw cRuntimeError("NeighborCache error!");
00138         WaitingContexts* temp = neighborCache[handle.ip].waitingContexts;
00139         neighborCache[handle.ip].waitingContexts = NULL;
00140         return temp;
00141     };

simtime_t NeighborCache::getNodeAge ( const TransportAddress &  handle  ) 

void NeighborCache::updateNode ( const NodeHandle &  add,
simtime_t  rtt,
const std::vector< double > &  coords = coordsDummy,
double  error = 1.0 
)

Definition at line 193 of file NeighborCache.cc.

Referenced by BaseRpc::internalHandleRpcMessage().

00196 {
00197     if (enableNeighborCache) {
00198         TransportAddress ta = add;
00199         if (neighborCache.count(ta.ip) == 0) {
00200             NeighborCacheEntry entry;
00201 
00202         entry.insertTime = simTime();
00203             entry.rtt = rtt;
00204             entry.nodeRef = add;
00205             entry.coordinates = coords;
00206             entry.coordErr = error;
00207 
00208             neighborCache[ta.ip] = entry;
00209             neighborCacheExpireMap.insert(std::make_pair(entry.insertTime,
00210                                                          add.ip));
00211 
00212             cleanupCache();
00213         } else {
00214             updateEntry(add, neighborCache[ta.ip].insertTime);
00215 
00216             neighborCache[ta.ip].insertTime = simTime();
00217             if (neighborCache[ta.ip].rtt < 0 || neighborCache[ta.ip].rtt > rtt)
00218                 neighborCache[ta.ip].rtt = rtt;
00219             neighborCache[ta.ip].nodeRef = add;
00220             neighborCache[ta.ip].coordinates = coords;
00221             neighborCache[ta.ip].coordErr = error;
00222         }
00223         assert(neighborCache.size() == neighborCacheExpireMap.size());
00224     }
00225     if (enableVivaldi) vivaldi->processCoordinates(rtt, coords, error);
00226 }

void NeighborCache::setNodeTimeout ( const TransportAddress &  handle  ) 

Definition at line 167 of file NeighborCache.cc.

Referenced by BaseRpc::pingRpcTimeout().

00168 {
00169     if (!enableNeighborCache) return;
00170     if (neighborCache.count(handle.ip) == 0) {
00171         NeighborCacheEntry entry;
00172 
00173         entry.insertTime = simTime();
00174         entry.rtt = NEIGHBORSTATE_TIMEOUT;
00175         entry.nodeRef = NodeHandle::UNSPECIFIED_NODE;
00176 
00177         neighborCache[handle.ip] = entry;
00178         neighborCacheExpireMap.insert(std::make_pair(entry.insertTime, handle.ip));
00179 
00180         cleanupCache();
00181     } else {
00182         NeighborCacheEntry& entry = neighborCache[handle.ip];
00183 
00184         updateEntry(handle, entry.insertTime);
00185 
00186         entry.insertTime = simTime();
00187         entry.rtt = NEIGHBORSTATE_TIMEOUT;
00188     }
00189     assert(neighborCache.size() == neighborCacheExpireMap.size());
00190 }

simtime_t NeighborCache::getNodeRtt ( const TransportAddress &  add  ) 

Definition at line 228 of file NeighborCache.cc.

Referenced by BasePastry::determineAliveTable(), and BaseRpc::pingNode().

00229 {
00230     // cache disabled or entry not there
00231     if (!enableNeighborCache ||
00232         add.isUnspecified() ||
00233         (neighborCache.count(add.ip) == 0)) {
00234         //std::cout << "NeighobrCache: MISS!" << std::endl;
00235         misses++;
00236         return NEIGHBORSTATE_UNKNOWN;
00237     }
00238 
00239     NeighborCacheEntry entry = neighborCache[add.ip];
00240 
00241     if (entry.rtt == NEIGHBORSTATE_WAITING ||
00242         entry.rtt == NEIGHBORSTATE_UNKNOWN)
00243         return entry.rtt;
00244     // entry expired
00245     if ((simTime() - entry.insertTime) >= rttExpirationTime) {
00246         entry.rtt = NEIGHBORSTATE_UNKNOWN;
00247         return NEIGHBORSTATE_UNKNOWN;
00248     }
00249     //std::cout << "NeighborCache: HIT!" << std::endl;
00250     hits++;
00251     return entry.rtt;
00252 }

simtime_t NeighborCache::getNodePredictedRtt ( const TransportAddress &  add  ) 

Definition at line 348 of file NeighborCache.cc.

00349 {
00350     const std::vector<double>& coords = getNodeCoords(add);
00351     const std::vector<double>& ownCoords = vivaldi->getOwnCoordinates(); //TODO
00352 
00353     double dist = 0.0;
00354 
00355     for (uint32_t i = 0; i < coords.size(); i++) {
00356         dist += pow(ownCoords[i] - coords[i], 2);
00357     }
00358     return sqrt(dist);
00359 }

NodeHandle NeighborCache::getNodeHandle ( const TransportAddress &  add  ) 

Definition at line 254 of file NeighborCache.cc.

Referenced by BaseRpc::pingNode(), and BaseRpc::pingRpcResponse().

00255 {
00256     if (neighborCache.count(add.ip) == 0) {
00257         throw cRuntimeError("NeighborCache.cc: getNodeHandle was asked for "
00258                             "a non-existent node reference.");
00259     }
00260     return neighborCache[add.ip].nodeRef;
00261 }

const Vivaldi& NeighborCache::getVivaldiAccess (  )  [inline]

Definition at line 156 of file NeighborCache.h.

Referenced by BaseRpc::sendRpcResponse().

00157     {
00158         if (vivaldi == NULL)
00159             throw cRuntimeError("Vivaldi module uninitialized");
00160         else
00161             return *vivaldi;
00162     };

double NeighborCache::getAvgAbsPredictionError ( std::vector< double > &  coord,
uint32_t  sampleSize 
)

Definition at line 296 of file NeighborCache.cc.

Referenced by Vivaldi::processCoordinates().

00298 {
00299     if (neighborCache.size() < 2 || sampleSize == 0) return 1.0;
00300 
00301     double absoluteDiff = 0;
00302     uint32_t numNeighbors = 0;
00303 
00304     //for (std::map<IPvXAddress, NeighborCacheEntry>::iterator it =
00305     //     neighborCache.begin(); it != neighborCache.end(); ++it) {
00306     for (std::map<simtime_t, IPvXAddress>::reverse_iterator it =
00307          neighborCacheExpireMap.rbegin();
00308          it != neighborCacheExpireMap.rend() &&
00309          numNeighbors < sampleSize; ++it) {
00310         NeighborCacheEntry& cacheEntry = neighborCache[it->second];
00311         double dist = 0;
00312         for (uint32_t i = 0; i < cacheEntry.coordinates.size(); i++) {
00313             dist += pow(coord[i] - cacheEntry.coordinates[i], 2);
00314         }
00315         dist = sqrt(dist);
00316 
00317         if (dist != 0 && cacheEntry.rtt > 0) {
00318             //double predictionError = fabs(dist - cacheEntry.rtt);
00319             double predictionError = fabs(dist - SIMTIME_DBL(cacheEntry.rtt));
00320 
00321             //test: error weighted
00322             //if (it->second.coordErr < 1) {
00323             //    predictionError /= it->second.coordErr;
00324             //}
00325             //test: age weighted
00326             //if ((simTime() - it->second.insertTime) > 1) {
00327             //    predictionError /= (simTime() - it->second.insertTime);
00328             //}
00329 
00330             numNeighbors++;
00331             absoluteDiff += predictionError;
00332         }
00333     }
00334     assert(numNeighbors != 0);
00335     absoluteDiff /= numNeighbors;
00336 
00337     return (absoluteDiff > 1.0) ? 1.0 : absoluteDiff;
00338 }

const std::vector< double > & NeighborCache::getNodeCoords ( const TransportAddress &  node  ) 

Definition at line 340 of file NeighborCache.cc.

Referenced by getNodePredictedRtt().

00341 {
00342     if (neighborCache.count(node.ip) == 0) {
00343         return coordsDummy;
00344     }
00345     return neighborCache[node.ip].coordinates;
00346 }

double NeighborCache::getNodeCoordError ( const TransportAddress &  node  ) 

Definition at line 361 of file NeighborCache.cc.

00362 {
00363     if (neighborCache.count(node.ip) == 0) {
00364         return 1.0;
00365     }
00366     return neighborCache[node.ip].coordErr;
00367 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const NeighborCacheEntry &  entry 
) [friend]

Definition at line 37 of file NeighborCache.cc.

00039 {
00040     os << entry.rtt << " (inserted: " << entry.insertTime
00041        << ", #contexts: ";
00042     if (entry.waitingContexts == NULL) os << "0";
00043     else os << entry.waitingContexts->pingContexts.size();
00044     os << ", key: "
00045        << (entry.nodeRef.isUnspecified() ? "<unspec>"
00046                                          : entry.nodeRef.key.toString(16))
00047        << ", coordinates: <";
00048     if (entry.coordinates.size() == 0) {
00049         os << "unspec";
00050     } else {
00051         for (uint32_t i = 0; i < entry.coordinates.size(); ++i) {
00052             if (i != 0) os << ", ";
00053             os << entry.coordinates[i];
00054         }
00055     }
00056     os << ">";
00057     if (entry.coordErr >= 0) os << ", error: " << entry.coordErr;
00058     os << ")";
00059 
00060     return os;
00061 }


Member Data Documentation

Definition at line 71 of file NeighborCache.h.

Referenced by initialize(), updateNode(), and vivaldiEnabled().

simtime_t NeighborCache::rttExpirationTime [private]

Definition at line 73 of file NeighborCache.h.

Referenced by getNodeRtt(), and initialize().

uint32_t NeighborCache::maxSize [private]

Definition at line 74 of file NeighborCache.h.

Referenced by cleanupCache(), and initialize().

uint32_t NeighborCache::misses [private]

Definition at line 76 of file NeighborCache.h.

Referenced by finish(), getNodeRtt(), and initialize().

uint32_t NeighborCache::hits [private]

Definition at line 77 of file NeighborCache.h.

Referenced by finish(), getNodeRtt(), and initialize().

const std::vector< double > NeighborCache::coordsDummy [static, private]

Definition at line 86 of file NeighborCache.h.

Referenced by getNodeCoords().

Definition at line 89 of file NeighborCache.h.

Referenced by finish(), and initialize().

std::map<IPvXAddress, NeighborCacheEntry> NeighborCache::neighborCache [protected]

std::multimap<simtime_t, IPvXAddress> NeighborCache::neighborCacheExpireMap [protected]


The documentation for this class was generated from the following files:

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