NeighborCache.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 // does not compile on Mac OS X
00044 enum {
00045     NEIGHBORSTATE_UNKNOWN = -1,
00046     NEIGHBORSTATE_TIMEOUT = -2,
00047     NEIGHBORSTATE_WAITING = -3
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; //TODO ptr->object
00101         std::vector<double> coordinates;
00102         double coordErr;
00103     };
00104 
00105     //std::map<TransportAddress, NeighborCacheEntry> neighborCache;
00106     //std::multimap<simtime_t, TransportAddress> neighborCacheExpireMap;
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     // vivaldi stuff
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

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