OverSim
Kademlia.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __KADEMLIA_H_
25 #define __KADEMLIA_H_
26 
27 #include <deque>
28 #include <omnetpp.h>
29 
30 #include <CommonMessages_m.h>
31 #include <BaseOverlay.h>
32 #include <GlobalStatistics.h>
33 #include <NeighborCache.h>
34 
35 #include "KademliaNodeHandle.h"
36 #include "KademliaBucket.h"
37 
38 
61 class Kademlia : public BaseOverlay, public ProxListener
62 {
63 protected://fields: kademlia parameters
64 
65  uint32_t k; /*< number of redundant graphs */
66  uint32_t b; /*< number of bits to consider */
67  uint32_t s; /*< number of siblings */
68 
69  uint32_t maxStaleCount; /*< number of timouts until node is removed from
70  routingtable */
71 
72  bool exhaustiveRefresh; /*< if true, use exhaustive-iterative lookups to refresh buckets */
76 
77  bool enableReplacementCache; /*< enables the replacement cache to store nodes if a bucket is full */
78  bool replacementCachePing; /*< ping the least recently used node in a full bucket, when a node is added to the replacement cache */
79  uint replacementCandidates; /*< maximum number of candidates in the replacement cache for each bucket */
80  int siblingRefreshNodes; /*< number of redundant nodes for exhaustive sibling table refresh lookups (0 = numRedundantNodes) */
81  int bucketRefreshNodes; /*< number of redundant nodes for exhaustive bucket refresh lookups (0 = numRedundantNodes) */
82 
83  // R/Kademlia
84  bool activePing;
87  bool altRecMode;
88 
92 
93  cMessage* bucketRefreshTimer;
94  cMessage* siblingPingTimer;
95 
96 public:
97  Kademlia();
98 
99  ~Kademlia();
100 
101  void initializeOverlay(int stage);
102 
103  void finishOverlay();
104 
105  void joinOverlay();
106 
107  bool isSiblingFor(const NodeHandle& node,const OverlayKey& key,
108  int numSiblings, bool* err );
109 
110  int getMaxNumSiblings();
111 
113 
114  void handleTimerEvent(cMessage* msg);
115 
116  bool handleRpcCall(BaseCallMessage* msg);
117 
119 
120  virtual void proxCallback(const TransportAddress& node, int rpcId,
121  cPolymorphic *contextPointer, Prox prox);
122 
123 protected:
124  NodeVector* findNode(const OverlayKey& key,
125  int numRedundantNodes,
126  int numSiblings,
127  BaseOverlayMessage* msg);
128 
130  cPolymorphic* context,
131  int rpcId, simtime_t rtt);
132 
133  void handleRpcTimeout(BaseCallMessage* msg, const TransportAddress& dest,
134  cPolymorphic* context, int rpcId,
135  const OverlayKey& destKey);
136 
141 
142  OverlayKey distance(const OverlayKey& x,
143  const OverlayKey& y,
144  bool useAlternative = false) const;
145 
149  void updateTooltip();
150 
151  virtual void lookupFinished(bool isValid);
152 
154 
156 
157 #ifdef PAIL
158  AbstractProxKeyComparator* getProxKeyComparator(const OverlayKey& relativeKey,
159  uint8_t bitsPerDigit,
160  const AdjustableProxComparator* apc) {
161  return new KademliaPRComparator(relativeKey, bitsPerDigit);
162  };
163 #endif
164 private:
165  uint32_t bucketRefreshCount; /*< statistics: total number of bucket refreshes */
166  uint32_t siblingTableRefreshCount; /*< statistics: total number of sibling table refreshes */
167  uint32_t nodesReplaced;
168 
170 
172  std::vector<KademliaBucket*> routingTable;
174 
175  void routingInit();
176 
177  void routingDeinit();
178 
188  int routingBucketIndex(const OverlayKey& key, bool firstOnLayer = false);
189 
200  KademliaBucket* routingBucket(const OverlayKey& key, bool ensure);
201 
211  bool routingAdd(const NodeHandle& handle, bool isAlive,
212  simtime_t rtt = MAXTIME, bool maintenanceLookup = false);
213 
222  bool routingTimeout(const OverlayKey& key, bool immediately = false);
223 
224  void refillSiblingTable();
225 
226  void sendSiblingFindNodeCall(const TransportAddress& dest);
227 
228  void setBucketUsage(const OverlayKey& key);
229 
230  bool recursiveRoutingHook(const TransportAddress& dest,
231  BaseRouteMessage* msg);
232 
233  bool handleFailedNode(const TransportAddress& failed);
234 };
235 
236 #endif