00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #ifndef __KADEMLIA_H_
00025 #define __KADEMLIA_H_
00026
00027 #include <omnetpp.h>
00028
00029 #include <CommonMessages_m.h>
00030 #include <BaseOverlay.h>
00031 #include <GlobalStatistics.h>
00032
00033 #include "KademliaNodeHandle.h"
00034 #include "KademliaBucket.h"
00035
00041 class Kademlia : public BaseOverlay {
00042 protected:
00043
00044 uint32_t k;
00045 uint32_t b;
00046 uint32_t s;
00047
00048
00049 uint32_t maxStaleCount;
00050
00051
00052 bool pingNewSiblings;
00053
00054 simtime_t minSiblingTableRefreshInterval;
00055 simtime_t minBucketRefreshInterval;
00056
00057 cMessage* bucketRefreshTimer;
00058
00059 public:
00060 Kademlia();
00061
00062 ~Kademlia();
00063
00064 void initializeOverlay(int stage);
00065
00066 void finishOverlay();
00067
00068 void joinOverlay();
00069
00070 bool isSiblingFor( const NodeHandle& node,const OverlayKey& key,
00071 int numSiblings, bool* err );
00072
00073 int getMaxNumSiblings();
00074
00075 int getMaxNumRedundantNodes();
00076
00077 void handleTimerEvent(cMessage* msg);
00078
00079 bool handleRpc( BaseCallMessage* msg );
00080
00081 protected:
00082 NodeVector* findNode(const OverlayKey& key,
00083 int numRedundantNodes,
00084 int numSiblings,
00085 BaseOverlayMessage* msg);
00086
00087 void handleRpcResponse(BaseResponseMessage* msg,
00088 cPolymorphic* context,
00089 int rpcId, simtime_t rtt);
00090
00091 void handleRpcTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00092 cPolymorphic* context, int rpcId,
00093 const OverlayKey& destKey);
00094
00098
00099
00103 void handleBucketRefreshTimerExpired();
00104
00105 OverlayKey distance( const OverlayKey& x, const OverlayKey& y ) const;
00106
00110 void updateTooltip();
00111
00112 virtual void lookupFinished(bool isValid);
00113
00114 friend class KademliaLookupListener;
00115
00116 uint32_t nodesReplaced;
00117
00118 private:
00119 KeyDistanceComparator<KeyXorMetric>* comparator;
00120
00121 KademliaBucket* siblingTable;
00122 std::vector<KademliaBucket*> routingTable;
00123 int numBuckets;
00124
00125 std::map<NodeHandle, NodeHandle> replacementCache;
00126 void routingInit();
00127
00128 void routingDeinit();
00129
00139 int routingBucketIndex(const OverlayKey& key, bool firstOnLayer = false);
00140
00151 KademliaBucket* routingBucket( const OverlayKey& key, bool ensure );
00152
00161 bool routingAdd(const NodeHandle& handle, bool isAlive,
00162 simtime_t rtt = MAXTIME);
00163
00170 bool routingRemove( const OverlayKey& key );
00171
00180 bool routingTimeout( const OverlayKey& key, bool immediately = false );
00181
00182 void refillSiblingTable();
00183
00184 void setBucketUsage(const OverlayKey& key);
00185
00186
00187 bool handleFailedNode(const TransportAddress& failed);
00188 };
00189
00190 #endif