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.getKey();
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 
00107     virtual FindNodeCall* createFindNodeCall(cPacket *findNodeExt = NULL);
00108 
00109     //-------------------------------------------------------------------------
00110     //- Base configuration and state ------------------------------------------
00111     //-------------------------------------------------------------------------
00112 protected:
00113     OverlayKey key;                 
00114     BaseOverlay* overlay;           
00115     LookupListener* listener;       
00116     std::vector<IterativePathLookup*> paths;  
00117     RoutingType routingType;        
00118     IterativeLookupConfiguration config; 
00119     cPacket* firstCallExt;          
00120     uint32_t finishedPaths;             
00121     uint32_t successfulPaths;           
00122     uint32_t accumulatedHops;           
00123     bool finished;                  
00124     bool success;                   
00125     bool running;                   
00126     int retries;                    
00127     bool appLookup;
00128 
00129 protected://virtual methods: comparator induced by distance in BaseOverlay
00139     int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const;
00140 
00141     //-------------------------------------------------------------------------
00142     //- Siblings and visited nodes management---------------------------------
00143     //-------------------------------------------------------------------------
00144 protected://fields
00145     NodeVector siblings;           
00146     TransportAddress::Set visited; 
00147     TransportAddress::Set dead;    
00148     int numSiblings;               
00149     int hopCountMax;               
00151 protected://methods
00158     bool addSibling( const NodeHandle& handle );
00159 
00166     void setVisited(const TransportAddress& addr, bool visitedFlag = true);
00167 
00174     bool getVisited( const TransportAddress& addr );
00175 
00181     void setDead(const TransportAddress& addr);
00182 
00189     bool getDead(const TransportAddress& addr);
00190 
00191     //-------------------------------------------------------------------------
00192     //- Parallel RPC distribution ---------------------------------------------
00193     //-------------------------------------------------------------------------
00194 protected://fields and classes: rpc distribution
00195 
00196     class RpcInfo
00197     {
00198     public:
00199         int vrpcId;
00200         IterativePathLookup* path;
00201     };
00202 
00203     class RpcInfoVector : public std::vector<RpcInfo>
00204     {
00205     public:
00206         uint32_t nonce;
00207     };
00208 
00209     typedef UNORDERED_MAP<TransportAddress, RpcInfoVector, TransportAddress::hashFcn> RpcInfoMap;
00210     RpcInfoMap rpcs;
00211 
00212 protected://methods: rpcListener
00213     void handleRpcResponse(BaseResponseMessage* msg,
00214                            cPolymorphic* context,
00215                            int rpcId, simtime_t rtt);
00216 
00217     void handleRpcTimeout(BaseCallMessage* msg,
00218                           const TransportAddress& dest,
00219                           cPolymorphic* context, int rpcId,
00220                           const OverlayKey& destKey = OverlayKey::UNSPECIFIED_KEY);
00221 
00222 protected://methods: rpc distribution
00223 
00224     void sendRpc(const NodeHandle& handle, FindNodeCall* call,
00225                  IterativePathLookup* listener, int rpcId);
00226 
00227     //-------------------------------------------------------------------------
00228     //- Construction & Destruction --------------------------------------------
00229     //-------------------------------------------------------------------------
00230 public://construction & destruction
00231     IterativeLookup(BaseOverlay* overlay, RoutingType routingType,
00232                const IterativeLookupConfiguration& config,
00233                const cPacket* findNodeExt = NULL, bool appLookup = false);
00234 
00235     virtual ~IterativeLookup();
00236 
00237 protected:
00238     void start();
00239     void stop();
00240     void checkStop();
00241 
00242     //-------------------------------------------------------------------------
00243     //- AbstractLookup implementation -----------------------------------------
00244     //-------------------------------------------------------------------------
00245 public://methods
00246     void lookup(const OverlayKey& key, int numSiblings = 1,
00247                 int hopCountMax = 0, int retries = 0,
00248                 LookupListener* listener = NULL);
00249 
00250     const NodeVector& getResult() const;
00251 
00252     bool isValid() const;
00253     void abortLookup();
00254 
00255     uint32_t getAccumulatedHops() const;
00256 };
00257 
00263 class IterativePathLookup
00264 {
00265     friend class IterativeLookup;
00266 
00267 protected://fields:
00268     IterativeLookup* lookup;
00269 
00270 protected://fields: state
00271     int  hops;
00272     int  step;
00273     int  pendingRpcs;
00274     bool finished;
00275     bool success;
00276     LookupVector nextHops;
00277     std::map<TransportAddress, NodeHandle> oldNextHops;
00278 
00279 protected://methods: rpc handling
00280     bool accepts(int rpcId);
00281     void handleResponse(FindNodeResponse* msg);
00282     void handleTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00283                        int rpcId);
00284     void handleFailedNodeResponse(const NodeHandle& src,
00285                                   cPacket* findNodeExt, bool retry);
00286 
00287 private:
00288     void sendRpc(int num, cPacket* FindNodeExt = NULL);
00289 
00290     void sendNewRpcAfterTimeout(cPacket* findNodeExt);
00291 
00292 protected:
00293     IterativePathLookup(IterativeLookup* lookup);
00294     virtual ~IterativePathLookup();
00295 
00299     int add(const NodeHandle& handle,
00300             const NodeHandle& source = NodeHandle::UNSPECIFIED_NODE);
00301 };
00302 
00303 #endif
00304 

Generated on Tue Sep 8 17:26:53 2009 for OverSim by  doxygen 1.5.8