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 
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     //- Base configuration and state ------------------------------------------
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://virtual methods: comparator induced by distance in BaseOverlay
00136     int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const;
00137 
00138     //-------------------------------------------------------------------------
00139     //- Siblings and visited nodes management---------------------------------
00140     //-------------------------------------------------------------------------
00141 protected://fields
00142     NodeVector siblings;           
00143     TransportAddress::Set visited; 
00144     TransportAddress::Set dead;    
00145     int numSiblings;               
00146     int hopCountMax;               
00148 protected://methods
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     //- Parallel RPC distribution ---------------------------------------------
00190     //-------------------------------------------------------------------------
00191 protected://fields and classes: rpc distribution
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://methods: rpcListener
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://methods: rpc distribution
00220 
00221     void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00222                  IterativePathLookup* listener, int rpcId);
00223 
00224     //-------------------------------------------------------------------------
00225     //- Construction & Destruction --------------------------------------------
00226     //-------------------------------------------------------------------------
00227 public://construction & destruction
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     //- AbstractLookup implementation -----------------------------------------
00241     //-------------------------------------------------------------------------
00242 public://methods
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://fields:
00265     IterativeLookup* lookup;
00266 
00267 protected://fields: state
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://methods: rpc handling
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 

Generated on Wed Mar 25 16:32:49 2009 for OverSim by  doxygen 1.5.5