NeighborCache.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026 #ifndef __NEIGHBORCACHE_H_
00027 #define __NEIGHBORCACHE_H_
00028
00029 #include <omnetpp.h>
00030
00031 #include <map>
00032 #include <cfloat>
00033
00034 #include <BaseApp.h>
00035 #include <NodeHandle.h>
00036 #include <Nps.h>
00037 #include <CoordinateSystem.h>
00038 #include <ProxNodeHandle.h>
00039 #include <HashFunc.h>
00040
00041 class Vivaldi;
00042 class GlobalStatistics;
00043 class TransportAddress;
00044 class RpcListener;
00045
00046
00047
00048 enum NeighborCacheQueryType {
00049 NEIGHBORCACHE_AVAILABLE,
00050 NEIGHBORCACHE_EXACT,
00051 NEIGHBORCACHE_EXACT_TIMEOUT,
00052 NEIGHBORCACHE_ESTIMATED,
00053 NEIGHBORCACHE_QUERY,
00054
00055 NEIGHBORCACHE_DEFAULT,
00056 NEIGHBORCACHE_DEFAULT_IMMEDIATELY,
00057 NEIGHBORCACHE_DEFAULT_QUERY
00058 };
00059
00060 class ProxListener {
00061 public:
00062 virtual void proxCallback(const TransportAddress& node, int rpcId,
00063 cPolymorphic *contextPointer, Prox prox) = 0;
00064 };
00065
00066 class NeighborCache : public BaseApp
00067 {
00068 friend class Nps;
00069
00070 private:
00071
00072 bool enableNeighborCache;
00073 simtime_t rttExpirationTime;
00074 uint32_t maxSize;
00075
00076 bool enableNps;
00077 bool enableVivaldi;
00078
00079 uint32_t misses;
00080 uint32_t hits;
00081
00082 bool cleanupCache();
00083
00084 void updateEntry(const TransportAddress& address,
00085 simtime_t insertTime);
00086
00087 Vivaldi* vivaldi;
00088 Nps* nps;
00089
00090 NeighborCacheQueryType defaultQueryType;
00091 NeighborCacheQueryType defaultQueryTypeI;
00092 NeighborCacheQueryType defaultQueryTypeQ;
00093
00094 Prox getCoordinateBasedProx(const TransportAddress& node);
00095
00096 cMessage *landmarkTimer;
00097
00098 static const std::vector<double> coordsDummy;
00099
00100
00101
00102 void calcRttError(const NodeHandle &handle, simtime_t rtt);
00103 uint32_t numMsg;
00104 double absoluteError;
00105 double relativeError;
00106 uint32_t numRttErrorToHigh;
00107 uint32_t numRttErrorToLow;
00108 uint32_t rttHistory;
00109 uint32_t rttErrorHistory;
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 std::map<TransportAddress, std::vector<double> > lastAbsoluteErrorPerNode;
00121
00122
00123 struct WaitingContexts
00124 {
00125 std::vector<ProxListener*> pingListeners;
00126 std::vector<cPolymorphic*> pingContexts;
00127 std::vector<uint32_t> pingIds;
00128
00129 inline uint32_t size() { return pingContexts.size(); };
00130 };
00131
00132
00133 bool insertNodeContext(const TransportAddress& handle,
00134 cPolymorphic* context,
00135 ProxListener* rpcListener,
00136 int rpcId);
00137
00138 NeighborCache::WaitingContexts* getNodeContexts(const TransportAddress& handle);
00139
00140 enum NeighborCacheRttState {
00141 RTTSTATE_VALID,
00142 RTTSTATE_UNKNOWN,
00143 RTTSTATE_TIMEOUT,
00144 RTTSTATE_WAITING
00145 };
00146
00147 typedef std::pair<simtime_t, NeighborCacheRttState> Rtt;
00148
00149 Rtt getNodeRtt(const TransportAddress& add);
00150
00151 protected:
00152 GlobalStatistics* globalStatistics;
00153
00154 struct ProxContext {
00155 int rpcId;
00156 ProxListener *listener;
00157 cPolymorphic *contextPointer;
00158 };
00159
00160
00161
00162 struct NeighborCacheEntry {
00163 NeighborCacheEntry() { waitingContexts = NULL;
00164 proxContext = NULL;
00165 rttState = RTTSTATE_UNKNOWN; };
00166
00167 ~NeighborCacheEntry() { };
00168
00169 simtime_t insertTime;
00170 simtime_t rtt;
00171 NeighborCacheRttState rttState;
00172
00173
00174 std::vector<simtime_t> lastRtts;
00175 uint32_t rttCounter;
00176
00177 NodeHandle nodeRef;
00178 NodeHandle srcRoute;
00179 NodeCoordsInfo coordsInfo;
00180
00181 ProxContext* proxContext;
00182 WaitingContexts* waitingContexts;
00183 };
00184
00185 UNORDERED_MAP<TransportAddress, NeighborCacheEntry> neighborCache;
00186 typedef UNORDERED_MAP<TransportAddress, NeighborCacheEntry>::iterator NeighborCacheIterator;
00187 typedef UNORDERED_MAP<TransportAddress, NeighborCacheEntry>::const_iterator NeighborCacheConstIterator;
00188
00189 std::multimap<simtime_t, TransportAddress> neighborCacheExpireMap;
00190 typedef std::multimap<simtime_t, TransportAddress>::iterator neighborCacheExpireMapIterator;
00191
00192 void initializeApp(int stage);
00193
00194 void finishApp();
00195
00196 virtual CompType getThisCompType() { return NEIGHBORCACHE_COMP; };
00197
00198 void handleTimerEvent(cMessage* msg);
00199
00206 void queryProx(const TransportAddress &node,
00207 int rpcId,
00208 ProxListener *listener,
00209 cPolymorphic *contextPointer);
00210
00214 bool handleRpcCall(BaseCallMessage* msg);
00215
00216 public:
00217 ~NeighborCache();
00218
00219
00220 inline bool isEnabled() { return enableNeighborCache; };
00221
00222 inline bool npsEnabled() { return enableNps; };
00223 Nps& getNpsAccess()
00224 {
00225 if (nps == NULL)
00226 throw cRuntimeError("NPS module uninitialized");
00227 else
00228 return *nps;
00229 };
00230
00231 inline bool vivaldiEnabled() { return enableVivaldi; };
00232 const Vivaldi& getVivaldiAccess()
00233 {
00234 if (vivaldi == NULL)
00235 throw cRuntimeError("Vivaldi module uninitialized");
00236 else
00237 return *vivaldi;
00238 };
00239
00240 uint16_t getNeighborCacheSize() { return neighborCache.size(); };
00241
00242
00243 bool isEntry(const TransportAddress& node);
00244 simtime_t getNodeAge(const TransportAddress& handle);
00245 const NodeHandle& getNodeHandle(const TransportAddress &add);
00246
00247
00248 TransportAddress getNearestNode(uint8_t maxLayer);
00249 double getAvgAbsPredictionError(std::vector<double>& coord,
00250 uint32_t sampleSize);
00251
00252
00253 void updateNode(const NodeHandle &add, simtime_t rtt,
00254 const NodeHandle& srcRoute = NodeHandle::UNSPECIFIED_NODE,
00255 const std::vector<double>& coords = coordsDummy,
00256 double error = 1.0);
00257 void setNodeTimeout(const TransportAddress& handle);
00258 void setNodeLayer(const NodeHandle& add, int8_t layer);
00259
00260
00261
00262 std::vector<LandmarkDataEntry> getLandmarkData(const std::vector<TransportAddress>& landmarkSet) const;
00263 bool setLandmarkSet(uint8_t dim, uint8_t maxLayer, std::vector<TransportAddress>* landmarkSet);
00264 double getOwnEuclidianDistanceToKey(const OverlayKey& destKey) const;
00265
00285 Prox getProx(const TransportAddress &node,
00286 NeighborCacheQueryType type = NEIGHBORCACHE_AVAILABLE,
00287 int rpcId = -1,
00288 ProxListener *listener = NULL,
00289 cPolymorphic *contextPointer = NULL);
00290
00296 Prox estimateProx(const TransportAddress &node);
00297
00302 const NodeCoordsInfo& getNodeCoordsInfo(const TransportAddress &node);
00303
00308 void setNodeCoordsInfo(const TransportAddress &node, const NodeCoordsInfo &coords);
00309
00310
00311
00312 bool checkEntry(const TransportAddress &node);
00313 simtime_t getMeanRtt(const TransportAddress &node);
00314 double getVarRtt(const TransportAddress &node, simtime_t &meanRtt);
00315
00316
00317 friend std::ostream& operator<<(std::ostream& os,
00318 const NeighborCacheEntry& entry);
00319 };
00320
00321 #endif