IterativeLookup.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #ifndef __ITERATIVE_LOOKUP_H
00025 #define __ITERATIVE_LOOKUP_H
00026
00027 #include <vector>
00028 #include <oversim_mapset.h>
00029
00030 #include <IterativeLookupConfiguration.h>
00031 #include <AbstractLookup.h>
00032 #include <RpcListener.h>
00033
00034 #include <NodeVector.h>
00035 #include <Comparator.h>
00036
00037 class NodeHandle;
00038 class OverlayKey;
00039 class LookupListener;
00040 class IterativeLookup;
00041 class IterativePathLookup;
00042 class BaseOverlay;
00043
00044 static const double LOOKUP_TIMEOUT = 10.0;
00045
00046 class LookupEntry {
00047 public:
00048 NodeHandle handle;
00049 NodeHandle source;
00050 bool alreadyUsed;
00051
00052 LookupEntry(const NodeHandle& handle, const NodeHandle& source,
00053 bool alreadyUsed) : handle(handle), source(source),
00054 alreadyUsed(alreadyUsed) {};
00055
00056 LookupEntry() : handle(NodeHandle::UNSPECIFIED_NODE),
00057 source(NodeHandle::UNSPECIFIED_NODE), alreadyUsed(false) {};
00058
00059
00060 };
00061
00062 typedef BaseKeySortedVector< LookupEntry > LookupVector;
00063
00064 template <>
00065 struct KeyExtractor<LookupEntry> {
00066 static const OverlayKey& key(const LookupEntry& nodes)
00067 {
00068 return nodes.handle.getKey();
00069 };
00070 };
00071
00081 class IterativeLookup : public RpcListener,
00082 public AbstractLookup,
00083 public Comparator<OverlayKey>
00084 {
00085 friend class IterativePathLookup;
00086 friend class BaseOverlay;
00087
00088 protected:
00095 virtual IterativePathLookup* createPathLookup();
00096
00108 virtual FindNodeCall* createFindNodeCall(cPacket *findNodeExt = NULL);
00109
00110
00111
00112
00113 protected:
00114 OverlayKey key;
00115 BaseOverlay* overlay;
00116 LookupListener* listener;
00117 std::vector<IterativePathLookup*> paths;
00118 RoutingType routingType;
00119 IterativeLookupConfiguration config;
00120 cPacket* firstCallExt;
00121 uint32_t finishedPaths;
00122 uint32_t successfulPaths;
00123 uint32_t accumulatedHops;
00124 bool finished;
00125 bool success;
00126 bool running;
00127 int retries;
00128 bool appLookup;
00129 SimTime startTime;
00131 protected:
00141 int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const;
00142
00143
00144
00145
00146 protected:
00147 NodeVector siblings;
00148 TransportAddress::Set visited;
00149 TransportAddress::Set dead;
00150 TransportAddress::Set pinged;
00151 typedef std::map<int,int> PendingPings;
00152 typedef std::set<NodeHandle> MajoritySiblings;
00153 MajoritySiblings majoritySiblings;
00154 int numSiblings;
00155 int hopCountMax;
00156 PendingPings pendingPings;
00158 protected:
00166 bool addSibling(const NodeHandle& handle, bool assured = false);
00167
00174 void setVisited(const TransportAddress& addr, bool visitedFlag = true);
00175
00182 bool getVisited( const TransportAddress& addr);
00183
00189 void setPinged(const TransportAddress& addr);
00190
00197 bool getPinged(const TransportAddress& addr);
00198
00204 void setDead(const TransportAddress& addr);
00205
00212 bool getDead(const TransportAddress& addr);
00213
00214
00215
00216
00217 protected:
00218
00219 class RpcInfo
00220 {
00221 public:
00222 int vrpcId;
00223 IterativePathLookup* path;
00224 };
00225
00226 class RpcInfoVector : public std::vector<RpcInfo>
00227 {
00228 public:
00229 uint32_t nonce;
00230 };
00231
00232 typedef UNORDERED_MAP<TransportAddress, RpcInfoVector, TransportAddress::hashFcn> RpcInfoMap;
00233 RpcInfoMap rpcs;
00234
00235 protected:
00236 void handleRpcResponse(BaseResponseMessage* msg,
00237 cPolymorphic* context,
00238 int rpcId, simtime_t rtt);
00239
00240 void handleRpcTimeout(BaseCallMessage* msg,
00241 const TransportAddress& dest,
00242 cPolymorphic* context, int rpcId,
00243 const OverlayKey& destKey = OverlayKey::UNSPECIFIED_KEY);
00244
00245 protected:
00246
00247 void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00248 IterativePathLookup* listener, int rpcId);
00249
00250
00251
00252
00253 public:
00254 IterativeLookup(BaseOverlay* overlay, RoutingType routingType,
00255 const IterativeLookupConfiguration& config,
00256 const cPacket* findNodeExt = NULL, bool appLookup = false);
00257
00258 virtual ~IterativeLookup();
00259
00260 protected:
00261 void start();
00262 void stop();
00263 void checkStop();
00264
00265
00266
00267
00268 public:
00269 void lookup(const OverlayKey& key, int numSiblings = 1,
00270 int hopCountMax = 0, int retries = 0,
00271 LookupListener* listener = NULL);
00272
00273 const NodeVector& getResult() const;
00274
00275 bool isValid() const;
00276 void abortLookup();
00277
00278 uint32_t getAccumulatedHops() const;
00279 };
00280
00286 class IterativePathLookup
00287 {
00288 friend class IterativeLookup;
00289
00290 protected:
00291 IterativeLookup* lookup;
00292 BaseOverlay* overlay;
00293
00294 protected:
00295 int hops;
00296 int step;
00297 int pendingRpcs;
00298 bool finished;
00299 bool success;
00300 LookupVector nextHops;
00301 std::map<TransportAddress, NodeHandle> oldNextHops;
00302
00303 protected:
00304 bool accepts(int rpcId);
00305 void handleResponse(FindNodeResponse* msg);
00306 void handleTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00307 int rpcId);
00308 void handleFailedNodeResponse(const NodeHandle& src,
00309 cPacket* findNodeExt, bool retry);
00310
00311 private:
00312 void sendRpc(int num, cPacket* FindNodeExt = NULL);
00313
00314 void sendNewRpcAfterTimeout(cPacket* findNodeExt);
00315
00316 protected:
00317 IterativePathLookup(IterativeLookup* lookup);
00318 virtual ~IterativePathLookup();
00319
00323 int add(const NodeHandle& handle,
00324 const NodeHandle& source = NodeHandle::UNSPECIFIED_NODE);
00325 };
00326
00327 #endif
00328