00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #include <cassert>
00026
00027 #include <TransportAddress.h>
00028 #include <NodeHandle.h>
00029 #include <GlobalStatisticsAccess.h>
00030 #include <Vivaldi.h>
00031
00032 #include "NeighborCache.h"
00033
00034
00035 const std::vector<double> NeighborCache::coordsDummy;
00036
00037 std::ostream& operator<<(std::ostream& os,
00038 const NeighborCache::NeighborCacheEntry& entry)
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 }
00062
00063
00064 Define_Module(NeighborCache);
00065
00066 void NeighborCache::initialize()
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
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 }
00089
00090 void NeighborCache::finish() {
00091 if ((misses + hits) != 0) {
00092 globalStatistics
00093 ->addStdDev("NeighborCache: Ping hit rate",
00094 ((double)hits / (double)(misses + hits)));
00095 }
00096 }
00097
00098
00099 NeighborCache::~NeighborCache()
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 }
00116
00117 bool NeighborCache::insertNodeContext(const TransportAddress& handle,
00118 cPolymorphic* context,
00119 RpcListener* rpcListener,
00120 int rpcId)
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
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 }
00166
00167 void NeighborCache::setNodeTimeout(const TransportAddress& handle)
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 }
00191
00192
00193 void NeighborCache::updateNode(const NodeHandle &add, simtime_t rtt,
00194 const std::vector<double>& coords,
00195 double error)
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 }
00227
00228 simtime_t NeighborCache::getNodeRtt(const TransportAddress &add)
00229 {
00230
00231 if (!enableNeighborCache ||
00232 add.isUnspecified() ||
00233 (neighborCache.count(add.ip) == 0)) {
00234
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
00245 if ((simTime() - entry.insertTime) >= rttExpirationTime) {
00246 entry.rtt = NEIGHBORSTATE_UNKNOWN;
00247 return NEIGHBORSTATE_UNKNOWN;
00248 }
00249
00250 hits++;
00251 return entry.rtt;
00252 }
00253
00254 NodeHandle NeighborCache::getNodeHandle(const TransportAddress &add)
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 }
00262
00263 bool NeighborCache::cleanupCache()
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 }
00283
00284 void NeighborCache::updateEntry(const TransportAddress& address,
00285 simtime_t insertTime)
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 }
00294
00295
00296 double NeighborCache::getAvgAbsPredictionError(std::vector<double>& coord,
00297 uint32_t sampleSize)
00298 {
00299 if (neighborCache.size() < 2 || sampleSize == 0) return 1.0;
00300
00301 double absoluteDiff = 0;
00302 uint32_t numNeighbors = 0;
00303
00304
00305
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
00319 double predictionError = fabs(dist - SIMTIME_DBL(cacheEntry.rtt));
00320
00321
00322
00323
00324
00325
00326
00327
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 }
00339
00340 const std::vector<double>& NeighborCache::getNodeCoords(const TransportAddress &node)
00341 {
00342 if (neighborCache.count(node.ip) == 0) {
00343 return coordsDummy;
00344 }
00345 return neighborCache[node.ip].coordinates;
00346 }
00347
00348 simtime_t NeighborCache::getNodePredictedRtt(const TransportAddress& add)
00349 {
00350 const std::vector<double>& coords = getNodeCoords(add);
00351 const std::vector<double>& ownCoords = vivaldi->getOwnCoordinates();
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 }
00360
00361 double NeighborCache::getNodeCoordError(const TransportAddress& node)
00362 {
00363 if (neighborCache.count(node.ip) == 0) {
00364 return 1.0;
00365 }
00366 return neighborCache[node.ip].coordErr;
00367 }