Kademlia.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 
00024 #ifndef __KADEMLIA_H_
00025 #define __KADEMLIA_H_
00026 
00027 #include <deque>
00028 #include <omnetpp.h>
00029 
00030 #include <CommonMessages_m.h>
00031 #include <BaseOverlay.h>
00032 #include <GlobalStatistics.h>
00033 #include <NeighborCache.h>
00034 
00035 #include "KademliaNodeHandle.h"
00036 #include "KademliaBucket.h"
00037 
00038 
00044 class Kademlia : public BaseOverlay
00045 {
00046 protected://fields: kademlia parameters
00047 
00048     uint32_t k; /*< number of redundant graphs */
00049     uint32_t b; /*< number of bits to consider */
00050     uint32_t s; /*< number of siblings         */
00051 
00052     uint32_t maxStaleCount; /*< number of timouts until node is removed from
00053                             routingtable */
00054 
00055     bool exhaustiveRefresh; /*< if true, use exhaustive-iterative lookups to refresh buckets */
00056     bool pingNewSiblings;
00057     bool secureMaintenance; 
00058     bool newMaintenance;
00059 
00060     bool enableReplacementCache; /*< enables the replacement cache to store nodes if a bucket is full */
00061     bool replacementCachePing; /*< ping the least recently used node in a full bucket, when a node is added to the replacement cache */
00062     uint replacementCandidates; /*< maximum number of candidates in the replacement cache for each bucket */
00063     int siblingRefreshNodes; /*< number of redundant nodes for exhaustive sibling table refresh lookups (0 = numRedundantNodes) */
00064     int bucketRefreshNodes; /*< number of redundant nodes for exhaustive bucket refresh lookups (0 = numRedundantNodes) */
00065 
00066     simtime_t minSiblingTableRefreshInterval;
00067     simtime_t minBucketRefreshInterval;
00068     simtime_t siblingPingInterval;
00069 
00070     cMessage* bucketRefreshTimer;
00071     cMessage* siblingPingTimer;
00072 
00073 public:
00074     Kademlia();
00075 
00076     ~Kademlia();
00077 
00078     void initializeOverlay(int stage);
00079 
00080     void finishOverlay();
00081 
00082     void joinOverlay();
00083 
00084     bool isSiblingFor(const NodeHandle& node,const OverlayKey& key,
00085                       int numSiblings, bool* err );
00086 
00087     int getMaxNumSiblings();
00088 
00089     int getMaxNumRedundantNodes();
00090 
00091     void handleTimerEvent(cMessage* msg);
00092 
00093     bool handleRpcCall(BaseCallMessage* msg);
00094 
00095 protected:
00096     NodeVector* findNode(const OverlayKey& key,
00097                          int numRedundantNodes,
00098                          int numSiblings,
00099                          BaseOverlayMessage* msg);
00100 
00101     void handleRpcResponse(BaseResponseMessage* msg,
00102                            cPolymorphic* context,
00103                            int rpcId, simtime_t rtt);
00104 
00105     void handleRpcTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00106                           cPolymorphic* context, int rpcId,
00107                           const OverlayKey& destKey);
00108 
00112     void handleBucketRefreshTimerExpired();
00113 
00114     OverlayKey distance(const OverlayKey& x,
00115                         const OverlayKey& y,
00116                         bool useAlternative = false) const;
00117 
00121     void updateTooltip();
00122 
00123     virtual void lookupFinished(bool isValid);
00124 
00125     virtual void handleNodeGracefulLeaveNotification();
00126 
00127     friend class KademliaLookupListener;
00128 
00129 
00130 private:
00131     uint32_t bucketRefreshCount; /*< statistics: total number of bucket refreshes */
00132     uint32_t siblingTableRefreshCount; /*< statistics: total number of sibling table refreshes */
00133     uint32_t nodesReplaced;
00134 
00135     KeyDistanceComparator<KeyXorMetric>* comparator;
00136 
00137     KademliaBucket*  siblingTable;
00138     std::vector<KademliaBucket*> routingTable;
00139     int numBuckets;
00140 
00141     void routingInit();
00142 
00143     void routingDeinit();
00144 
00154     int routingBucketIndex(const OverlayKey& key, bool firstOnLayer = false);
00155 
00166     KademliaBucket* routingBucket(const OverlayKey& key, bool ensure);
00167 
00177     bool routingAdd(const NodeHandle& handle, bool isAlive,
00178                     simtime_t rtt = MAXTIME, bool maintenanceLookup = false);
00179 
00188     bool routingTimeout(const OverlayKey& key, bool immediately = false);
00189 
00190     void refillSiblingTable();
00191 
00192     void sendSiblingFindNodeCall(const TransportAddress& dest);
00193 
00194     void setBucketUsage(const OverlayKey& key);
00195 
00196     bool handleFailedNode(const TransportAddress& failed);
00197 };
00198 
00199 #endif
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3