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 <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 // Prox stuff
00048 enum NeighborCacheQueryType {
00049     NEIGHBORCACHE_AVAILABLE,     //< RTT, timeout, or unknown (no query)
00050     NEIGHBORCACHE_EXACT,         //< RTT or query
00051     NEIGHBORCACHE_EXACT_TIMEOUT, //< RTT, timeout, or query
00052     NEIGHBORCACHE_ESTIMATED,     //< RTT or estimated
00053     NEIGHBORCACHE_QUERY,         //< only query, return unknown
00054     // default
00055     NEIGHBORCACHE_DEFAULT,       //< available, exact, exact_timeout or estimated
00056     NEIGHBORCACHE_DEFAULT_IMMEDIATELY, //< return a result immediately (available or estimated)
00057     NEIGHBORCACHE_DEFAULT_QUERY  //< do a query if needed (exact, exact_timeout, or 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     // parameters
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     // TODO roth
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     double countGetProxTotal;
00113     double countGetProxSuccessful;
00114     double relativeGetProx;
00115 
00116     double countCheckEntryTotal;
00117     double countCheckEntrySuccessful;
00118     double relativeCheckEntry;
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     // ping context stuff
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         //TODO roth
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; //TODO ptr->object
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     // getter for specific node information
00243     bool isEntry(const TransportAddress& node);
00244     simtime_t getNodeAge(const TransportAddress& handle);
00245     const NodeHandle& getNodeHandle(const TransportAddress &add);
00246 
00247     // getter for general node information
00248     TransportAddress getNearestNode(uint8_t maxLayer);
00249     double getAvgAbsPredictionError(std::vector<double>& coord,
00250                                     uint32_t sampleSize);
00251 
00252     // setter for specific node information
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     /***** NPS stuff *****/ //TODO
00261     //int8_t getAddressLayer(const IPvXAddress& add);
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; //TODO remove
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     //TODO roth
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

Generated on Tue Sep 8 17:26:53 2009 for OverSim by  doxygen 1.5.8