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 
00061 class Kademlia : public BaseOverlay, public ProxListener
00062 {
00063 protected://fields: kademlia parameters
00064 
00065     uint32_t k; /*< number of redundant graphs */
00066     uint32_t b; /*< number of bits to consider */
00067     uint32_t s; /*< number of siblings         */
00068 
00069     uint32_t maxStaleCount; /*< number of timouts until node is removed from
00070                             routingtable */
00071 
00072     bool exhaustiveRefresh; /*< if true, use exhaustive-iterative lookups to refresh buckets */
00073     bool pingNewSiblings;
00074     bool secureMaintenance; 
00075     bool newMaintenance;
00076 
00077     bool enableReplacementCache; /*< enables the replacement cache to store nodes if a bucket is full */
00078     bool replacementCachePing; /*< ping the least recently used node in a full bucket, when a node is added to the replacement cache */
00079     uint replacementCandidates; /*< maximum number of candidates in the replacement cache for each bucket */
00080     int siblingRefreshNodes; /*< number of redundant nodes for exhaustive sibling table refresh lookups (0 = numRedundantNodes) */
00081     int bucketRefreshNodes; /*< number of redundant nodes for exhaustive bucket refresh lookups (0 = numRedundantNodes) */
00082 
00083     // R/Kademlia
00084     bool activePing;
00085     bool proximityRouting;
00086     bool proximityNeighborSelection;
00087     bool altRecMode;
00088 
00089     simtime_t minSiblingTableRefreshInterval;
00090     simtime_t minBucketRefreshInterval;
00091     simtime_t siblingPingInterval;
00092 
00093     cMessage* bucketRefreshTimer;
00094     cMessage* siblingPingTimer;
00095 
00096 public:
00097     Kademlia();
00098 
00099     ~Kademlia();
00100 
00101     void initializeOverlay(int stage);
00102 
00103     void finishOverlay();
00104 
00105     void joinOverlay();
00106 
00107     bool isSiblingFor(const NodeHandle& node,const OverlayKey& key,
00108                       int numSiblings, bool* err );
00109 
00110     int getMaxNumSiblings();
00111 
00112     int getMaxNumRedundantNodes();
00113 
00114     void handleTimerEvent(cMessage* msg);
00115 
00116     bool handleRpcCall(BaseCallMessage* msg);
00117 
00118     void handleUDPMessage(BaseOverlayMessage* msg);
00119 
00120     virtual void proxCallback(const TransportAddress& node, int rpcId,
00121                               cPolymorphic *contextPointer, Prox prox);
00122 
00123 protected:
00124     NodeVector* findNode(const OverlayKey& key,
00125                          int numRedundantNodes,
00126                          int numSiblings,
00127                          BaseOverlayMessage* msg);
00128 
00129     void handleRpcResponse(BaseResponseMessage* msg,
00130                            cPolymorphic* context,
00131                            int rpcId, simtime_t rtt);
00132 
00133     void handleRpcTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00134                           cPolymorphic* context, int rpcId,
00135                           const OverlayKey& destKey);
00136 
00140     void handleBucketRefreshTimerExpired();
00141 
00142     OverlayKey distance(const OverlayKey& x,
00143                         const OverlayKey& y,
00144                         bool useAlternative = false) const;
00145 
00149     void updateTooltip();
00150 
00151     virtual void lookupFinished(bool isValid);
00152 
00153     virtual void handleNodeGracefulLeaveNotification();
00154 
00155     friend class KademliaLookupListener;
00156 
00157 
00158 private:
00159     uint32_t bucketRefreshCount; /*< statistics: total number of bucket refreshes */
00160     uint32_t siblingTableRefreshCount; /*< statistics: total number of sibling table refreshes */
00161     uint32_t nodesReplaced;
00162 
00163     KeyDistanceComparator<KeyXorMetric>* comparator;
00164 
00165     KademliaBucket*  siblingTable;
00166     std::vector<KademliaBucket*> routingTable;
00167     int numBuckets;
00168 
00169     void routingInit();
00170 
00171     void routingDeinit();
00172 
00182     int routingBucketIndex(const OverlayKey& key, bool firstOnLayer = false);
00183 
00194     KademliaBucket* routingBucket(const OverlayKey& key, bool ensure);
00195 
00205     bool routingAdd(const NodeHandle& handle, bool isAlive,
00206                     simtime_t rtt = MAXTIME, bool maintenanceLookup = false);
00207 
00216     bool routingTimeout(const OverlayKey& key, bool immediately = false);
00217 
00218     void refillSiblingTable();
00219 
00220     void sendSiblingFindNodeCall(const TransportAddress& dest);
00221 
00222     void setBucketUsage(const OverlayKey& key);
00223 
00224     bool recursiveRoutingHook(const TransportAddress& dest,
00225                               BaseRouteMessage* msg);
00226 
00227     bool handleFailedNode(const TransportAddress& failed);
00228 };
00229 
00230 #endif