IterativeLookup.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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     //- Base configuration and state ------------------------------------------
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://virtual methods: comparator induced by distance in BaseOverlay
00141     int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const;
00142 
00143     //-------------------------------------------------------------------------
00144     //- Siblings and visited nodes management---------------------------------
00145     //-------------------------------------------------------------------------
00146 protected://fields
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://methods
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     //- Parallel RPC distribution ---------------------------------------------
00216     //-------------------------------------------------------------------------
00217 protected://fields and classes: rpc distribution
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://methods: rpcListener
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://methods: rpc distribution
00247 
00248     void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00249                  IterativePathLookup* listener, int rpcId);
00250 
00251     //-------------------------------------------------------------------------
00252     //- Construction & Destruction --------------------------------------------
00253     //-------------------------------------------------------------------------
00254 public://construction & destruction
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     //- AbstractLookup implementation -----------------------------------------
00268     //-------------------------------------------------------------------------
00269 public://methods
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://fields:
00292     IterativeLookup* lookup;
00293     BaseOverlay* overlay;
00294 
00295 protected://fields: state
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://methods: rpc handling
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