OverSim
IterativeLookup.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 __ITERATIVE_LOOKUP_H
25 #define __ITERATIVE_LOOKUP_H
26 
27 #include <vector>
28 #include <oversim_mapset.h>
29 
31 #include <AbstractLookup.h>
32 #include <RpcListener.h>
33 
34 #include <NodeVector.h>
35 #include <Comparator.h>
36 
37 class NodeHandle;
38 class OverlayKey;
39 class LookupListener;
40 class IterativeLookup;
42 class BaseOverlay;
43 
44 static const double LOOKUP_TIMEOUT = 10.0;
45 
46 class LookupEntry {
47 public:
51 
53  bool alreadyUsed) : handle(handle), source(source),
54  alreadyUsed(alreadyUsed) {};
55 
56  LookupEntry() : handle(NodeHandle::UNSPECIFIED_NODE),
57  source(NodeHandle::UNSPECIFIED_NODE), alreadyUsed(false) {};
58 
59 
60 };
61 
63 
64 template <>
66  static const OverlayKey& key(const LookupEntry& nodes)
67  {
68  return nodes.handle.getKey();
69  };
70 };
71 
82  public AbstractLookup,
83  public Comparator<OverlayKey>
84 {
85  friend class IterativePathLookup;
86  friend class BaseOverlay;
87 
88 protected:
96 
108  virtual FindNodeCall* createFindNodeCall(cPacket *findNodeExt = NULL);
109 
110  //-------------------------------------------------------------------------
111  //- Base configuration and state ------------------------------------------
112  //-------------------------------------------------------------------------
113 protected:
117  std::vector<IterativePathLookup*> paths;
120  cPacket* firstCallExt;
121  uint32_t finishedPaths;
122  uint32_t successfulPaths;
123  uint32_t accumulatedHops;
124  bool finished;
125  bool success;
126  bool running;
127  int retries;
128  bool appLookup;
129  SimTime startTime;
131 public://virtual methods: comparator induced by distance in BaseOverlay
141  virtual int compare(const OverlayKey& lhs,
142  const OverlayKey& rhs/*,
143  bool useAlternativeMetric = false*/) const;
144 
145  //-------------------------------------------------------------------------
146  //- Siblings and visited nodes management---------------------------------
147  //-------------------------------------------------------------------------
148 protected://fields
153  typedef std::map<int,int> PendingPings;
154  typedef std::set<NodeHandle> MajoritySiblings;
160 protected://methods
168  bool addSibling(const NodeHandle& handle, bool assured = false);
169 
176  void setVisited(const TransportAddress& addr, bool visitedFlag = true);
177 
184  bool getVisited( const TransportAddress& addr);
185 
191  void setPinged(const TransportAddress& addr);
192 
199  bool getPinged(const TransportAddress& addr);
200 
206  void setDead(const TransportAddress& addr);
207 
214  bool getDead(const TransportAddress& addr);
215 
216  //-------------------------------------------------------------------------
217  //- Parallel RPC distribution ---------------------------------------------
218  //-------------------------------------------------------------------------
219 protected://fields and classes: rpc distribution
220 
221  class RpcInfo
222  {
223  public:
224  //RpcInfo() { vrpcId = 0; proxVectorId = 0; path = NULL; };
225  //~RpcInfo() { path = NULL;};
226  int vrpcId;
227  uint8_t proxVectorId;
229  };
230 
231  class RpcInfoVector : public std::vector<RpcInfo>
232  {
233  public:
234  uint32_t nonce;
235  };
236 
237  typedef UNORDERED_MAP<TransportAddress, RpcInfoVector, TransportAddress::hashFcn> RpcInfoMap;
239 
240 protected://methods: rpcListener
241  virtual void handleRpcResponse(BaseResponseMessage* msg,
242  cPolymorphic* context,
243  int rpcId, simtime_t rtt);
244 
245  virtual void handleRpcTimeout(BaseCallMessage* msg,
246  const TransportAddress& dest,
247  cPolymorphic* context, int rpcId,
248  const OverlayKey& destKey = OverlayKey::UNSPECIFIED_KEY);
249 
250 protected://methods: rpc distribution
251 
252  void sendRpc(const NodeHandle& handle, FindNodeCall* call,
253  IterativePathLookup* listener, int rpcId);
254 
255  //-------------------------------------------------------------------------
256  //- Construction & Destruction --------------------------------------------
257  //-------------------------------------------------------------------------
258 public://construction & destruction
261  const cPacket* findNodeExt = NULL, bool appLookup = false);
262 
263  virtual ~IterativeLookup();
264 
265 protected:
266  virtual void start();
267  void stop();
268  void checkStop();
269 
270  //-------------------------------------------------------------------------
271  //- AbstractLookup implementation -----------------------------------------
272  //-------------------------------------------------------------------------
273 public://methods
274  void lookup(const OverlayKey& key, int numSiblings = 1,
275  int hopCountMax = 0, int retries = 0,
276  LookupListener* listener = NULL);
277 
278  const NodeVector& getResult() const;
279 
280  bool isValid() const;
281  void abortLookup();
282 
283  uint32_t getAccumulatedHops() const;
284 };
285 
292 {
293  friend class IterativeLookup;
294 
295 protected://fields:
298 
299 protected://fields: state
300  int hops;
301  int step;
303  bool finished;
304  bool success;
306  std::map<TransportAddress, NodeHandle> oldNextHops;
307 
308 protected://methods: rpc handling
309  bool accepts(int rpcId);
310  void handleResponse(FindNodeResponse* msg);
311  void handleTimeout(BaseCallMessage* msg, const TransportAddress& dest,
312  int rpcId);
313  void handleFailedNodeResponse(const NodeHandle& src,
314  cPacket* findNodeExt, bool retry);
315 
316 private:
317  void sendRpc(int num, cPacket* FindNodeExt = NULL);
318 
319  void sendNewRpcAfterTimeout(cPacket* findNodeExt);
320 
321 protected:
323 
324  virtual ~IterativePathLookup();
325 
329  int add(const NodeHandle& handle,
330  const NodeHandle& source = NodeHandle::UNSPECIFIED_NODE);
331 };
332 
333 #endif
334