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
00045 class LookupEntry {
00046 public:
00047 NodeHandle handle;
00048 NodeHandle source;
00049 bool alreadyUsed;
00050
00051 LookupEntry(const NodeHandle& handle, const NodeHandle& source,
00052 bool alreadyUsed) : handle(handle), source(source),
00053 alreadyUsed(alreadyUsed) {};
00054
00055 LookupEntry() : handle(NodeHandle::UNSPECIFIED_NODE),
00056 source(NodeHandle::UNSPECIFIED_NODE), alreadyUsed(false) {};
00057
00058
00059 };
00060
00061 typedef BaseKeySortedVector< LookupEntry > LookupVector;
00062
00063 template <>
00064 struct KeyExtractor<LookupEntry> {
00065 static const OverlayKey& key(const LookupEntry& nodes)
00066 {
00067 return nodes.handle.key;
00068 };
00069 };
00070
00080 class IterativeLookup : public RpcListener,
00081 public AbstractLookup,
00082 public Comparator<OverlayKey>
00083 {
00084 friend class IterativePathLookup;
00085 friend class BaseOverlay;
00086
00087 protected:
00094 virtual IterativePathLookup* createPathLookup();
00095
00104 virtual FindNodeCall* createInitialRpcCall();
00105
00106
00107
00108
00109 protected:
00110 OverlayKey key;
00111 BaseOverlay* overlay;
00112 LookupListener* listener;
00113 std::vector<IterativePathLookup*> paths;
00114 RoutingType routingType;
00115 IterativeLookupConfiguration config;
00116 cObject* firstCallExt;
00117 uint32_t finishedPaths;
00118 uint32_t successfulPaths;
00119 uint32_t accumulatedHops;
00120 bool finished;
00121 bool success;
00122 bool running;
00123 int retries;
00124 bool appLookup;
00125
00126 protected:
00136 int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const;
00137
00138
00139
00140
00141 protected:
00142 NodeVector siblings;
00143 TransportAddress::Set visited;
00144 TransportAddress::Set dead;
00145 int numSiblings;
00146 int hopCountMax;
00148 protected:
00155 bool addSibling( const NodeHandle& handle );
00156
00163 void setVisited(const TransportAddress& addr, bool visitedFlag = true);
00164
00171 bool getVisited( const TransportAddress& addr );
00172
00178 void setDead(const TransportAddress& addr);
00179
00186 bool getDead(const TransportAddress& addr);
00187
00188
00189
00190
00191 protected:
00192
00193 class RpcInfo
00194 {
00195 public:
00196 int vrpcId;
00197 IterativePathLookup* path;
00198 };
00199
00200 class RpcInfoVector : public std::vector<RpcInfo>
00201 {
00202 public:
00203 uint32_t nonce;
00204 };
00205
00206 typedef UNORDERED_MAP<TransportAddress, RpcInfoVector, TransportAddress::hashFcn> RpcInfoMap;
00207 RpcInfoMap rpcs;
00208
00209 protected:
00210 void handleRpcResponse(BaseResponseMessage* msg,
00211 cPolymorphic* context,
00212 int rpcId, simtime_t rtt);
00213
00214 void handleRpcTimeout(BaseCallMessage* msg,
00215 const TransportAddress& dest,
00216 cPolymorphic* context, int rpcId,
00217 const OverlayKey& destKey = OverlayKey::UNSPECIFIED_KEY);
00218
00219 protected:
00220
00221 void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00222 IterativePathLookup* listener, int rpcId);
00223
00224
00225
00226
00227 public:
00228 IterativeLookup(BaseOverlay* overlay, RoutingType routingType,
00229 const IterativeLookupConfiguration& config,
00230 const cObject* findNodeExt = NULL, bool appLookup = false);
00231
00232 virtual ~IterativeLookup();
00233
00234 protected:
00235 void start();
00236 void stop();
00237 void checkStop();
00238
00239
00240
00241
00242 public:
00243 void lookup(const OverlayKey& key, int numSiblings = 1,
00244 int hopCountMax = 0, int retries = 0,
00245 LookupListener* listener = NULL);
00246
00247 const NodeVector& getResult() const;
00248
00249 bool isValid() const;
00250 void abortLookup();
00251
00252 uint32_t getAccumulatedHops() const;
00253 };
00254
00260 class IterativePathLookup
00261 {
00262 friend class IterativeLookup;
00263
00264 protected:
00265 IterativeLookup* lookup;
00266
00267 protected:
00268 int hops;
00269 int step;
00270 int pendingRpcs;
00271 bool finished;
00272 bool success;
00273 LookupVector nextHops;
00274 std::map<TransportAddress, NodeHandle> oldNextHops;
00275
00276 protected:
00277 bool accepts(int rpcId);
00278 void handleResponse(FindNodeResponse* msg);
00279 void handleTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00280 int rpcId);
00281 void handleFailedNodeResponse(const NodeHandle& src,
00282 cPacket* findNodeExt, bool retry);
00283
00284 private:
00285 void sendRpc(int num, cPacket* FindNodeExt = NULL);
00286
00287 void sendNewRpcAfterTimeout(cPacket* findNodeExt);
00288
00289 protected:
00290 IterativePathLookup(IterativeLookup* lookup);
00291 virtual ~IterativePathLookup();
00292
00304 virtual FindNodeCall* createRpcMessage(cPacket *findNodeExt = NULL);
00305
00309 int add(const NodeHandle& handle,
00310 const NodeHandle& source = NodeHandle::UNSPECIFIED_NODE);
00311 };
00312
00313 #endif
00314