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 <NodeHandle.h>
00035
00036
00037 class Vivaldi;
00038 class GlobalStatistics;
00039 class TransportAddress;
00040 class RpcListener;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #define NEIGHBORSTATE_UNKNOWN -1.0
00052 #define NEIGHBORSTATE_TIMEOUT -2.0
00053 #define NEIGHBORSTATE_WAITING -3.0
00054
00055 struct WaitingContexts
00056 {
00057 std::vector<RpcListener*> pingListeners;
00058 std::vector<cPolymorphic*> pingContexts;
00059 std::vector<uint32_t> pingIds;
00060
00061 inline uint32_t size()
00062 {
00063 return pingContexts.size();
00064 };
00065 };
00066
00067 class NeighborCache : public cSimpleModule
00068 {
00069 private:
00070 bool enableNeighborCache;
00071 bool enableVivaldi;
00072
00073 simtime_t rttExpirationTime;
00074 uint32_t maxSize;
00075
00076 uint32_t misses;
00077 uint32_t hits;
00078
00079 bool cleanupCache();
00080
00081 void updateEntry(const TransportAddress& address,
00082 simtime_t insertTime);
00083
00084 Vivaldi* vivaldi;
00085
00086 static const std::vector<double> coordsDummy;
00087
00088 protected:
00089 GlobalStatistics* globalStatistics;
00090
00091 struct NeighborCacheEntry {
00092 NeighborCacheEntry()
00093 {
00094 waitingContexts = NULL;
00095 };
00096
00097 simtime_t insertTime;
00098 simtime_t rtt;
00099 NodeHandle nodeRef;
00100 WaitingContexts* waitingContexts;
00101 std::vector<double> coordinates;
00102 double coordErr;
00103 };
00104
00105
00106
00107
00108 std::map<IPvXAddress, NeighborCacheEntry> neighborCache;
00109 std::multimap<simtime_t, IPvXAddress> neighborCacheExpireMap;
00110
00111 void initialize();
00112
00113 void finish();
00114
00115 public:
00116 ~NeighborCache();
00117
00118 inline bool isEnabled()
00119 {
00120 return enableNeighborCache;
00121 };
00122
00123 inline bool vivaldiEnabled()
00124 {
00125 return enableVivaldi;
00126 };
00127
00128 bool insertNodeContext(const TransportAddress& handle,
00129 cPolymorphic* context,
00130 RpcListener* rpcListener,
00131 int rpcId);
00132
00133 inline WaitingContexts* getNodeContexts(const TransportAddress& handle)
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 };
00142
00143 simtime_t getNodeAge(const TransportAddress& handle);
00144
00145 void updateNode(const NodeHandle &add, simtime_t rtt,
00146 const std::vector<double>& coords = coordsDummy,
00147 double error = 1.0);
00148 void setNodeTimeout(const TransportAddress& handle);
00149 simtime_t getNodeRtt(const TransportAddress& add);
00150 simtime_t getNodePredictedRtt(const TransportAddress& add);
00151 NodeHandle getNodeHandle(const TransportAddress &add);
00152
00153 friend std::ostream& operator<<(std::ostream& os,
00154 const NeighborCacheEntry& entry);
00155
00156 const Vivaldi& getVivaldiAccess()
00157 {
00158 if (vivaldi == NULL)
00159 throw cRuntimeError("Vivaldi module uninitialized");
00160 else
00161 return *vivaldi;
00162 };
00163
00164 double getAvgAbsPredictionError(std::vector<double>& coord,
00165 uint32_t sampleSize);
00166 const std::vector<double>& getNodeCoords(const TransportAddress &node);
00167 double getNodeCoordError(const TransportAddress &node);
00168 };
00169
00170 #endif