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 public:
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 uint8_t proxVectorId;
00224 IterativePathLookup* path;
00225 };
00226
00227 class RpcInfoVector : public std::vector<RpcInfo>
00228 {
00229 public:
00230 uint32_t nonce;
00231 };
00232
00233 typedef UNORDERED_MAP<TransportAddress, RpcInfoVector, TransportAddress::hashFcn> RpcInfoMap;
00234 RpcInfoMap rpcs;
00235
00236 protected:
00237 virtual void handleRpcResponse(BaseResponseMessage* msg,
00238 cPolymorphic* context,
00239 int rpcId, simtime_t rtt);
00240
00241 virtual void handleRpcTimeout(BaseCallMessage* msg,
00242 const TransportAddress& dest,
00243 cPolymorphic* context, int rpcId,
00244 const OverlayKey& destKey = OverlayKey::UNSPECIFIED_KEY);
00245
00246 protected:
00247
00248 void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00249 IterativePathLookup* listener, int rpcId);
00250
00251
00252
00253
00254 public:
00255 IterativeLookup(BaseOverlay* overlay, RoutingType routingType,
00256 const IterativeLookupConfiguration& config,
00257 const cPacket* findNodeExt = NULL, bool appLookup = false);
00258
00259 virtual ~IterativeLookup();
00260
00261 protected:
00262 void start();
00263 void stop();
00264 void checkStop();
00265
00266
00267
00268
00269 public:
00270 void lookup(const OverlayKey& key, int numSiblings = 1,
00271 int hopCountMax = 0, int retries = 0,
00272 LookupListener* listener = NULL);
00273
00274 const NodeVector& getResult() const;
00275
00276 bool isValid() const;
00277 void abortLookup();
00278
00279 uint32_t getAccumulatedHops() const;
00280 };
00281
00287 class IterativePathLookup
00288 {
00289 friend class IterativeLookup;
00290
00291 protected:
00292 IterativeLookup* lookup;
00293 BaseOverlay* overlay;
00294
00295 protected:
00296 int hops;
00297 int step;
00298 int pendingRpcs;
00299 bool finished;
00300 bool success;
00301 LookupVector nextHops;
00302 std::map<TransportAddress, NodeHandle> oldNextHops;
00303
00304 protected:
00305 bool accepts(int rpcId);
00306 void handleResponse(FindNodeResponse* msg);
00307 void handleTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00308 int rpcId);
00309 void handleFailedNodeResponse(const NodeHandle& src,
00310 cPacket* findNodeExt, bool retry);
00311
00312 private:
00313 void sendRpc(int num, cPacket* FindNodeExt = NULL);
00314
00315 void sendNewRpcAfterTimeout(cPacket* findNodeExt);
00316
00317 protected:
00318 IterativePathLookup(IterativeLookup* lookup);
00319 virtual ~IterativePathLookup();
00320
00324 int add(const NodeHandle& handle,
00325 const NodeHandle& source = NodeHandle::UNSPECIFIED_NODE);
00326 };
00327
00328 #endif
00329