BaseOverlay.h

Go to the documentation of this file.
00001 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 //
00017 
00024 #ifndef __BASEOVERLAY_H_
00025 #define __BASEOVERLAY_H_
00026 
00027 #include <oversim_mapset.h>
00028 
00029 #include <omnetpp.h>
00030 
00031 #include <NodeVector.h>
00032 #include <TopologyVis.h>
00033 #include <INotifiable.h>
00034 #include <BaseRpc.h>
00035 #include <IterativeLookupConfiguration.h>
00036 #include <RecursiveLookup.h>
00037 #include <InitStages.h>
00038 
00039 class GlobalNodeList;
00040 class UnderlayConfigurator;
00041 class BaseApp;
00042 class NodeHandle;
00043 class OverlayKey;
00044 class NotificationBoard;
00045 class AbstractLookup;
00046 class BootstrapList;
00047 
00061 class BaseOverlay : public INotifiable,
00062                     public BaseRpc,
00063                     public TopologyVis
00064 {
00065 
00066     friend class IterativeLookup;
00067     friend class IterativePathLookup;
00068     friend class RecursiveLookup;
00069     friend class BootstrapList;
00070     friend class SendToKeyListener;
00071 
00072     //------------------------------------------------------------------------
00073     //--- Construction / Destruction -----------------------------------------
00074     //------------------------------------------------------------------------
00075 
00076 public:
00077 
00078     BaseOverlay();
00079 
00081     virtual ~BaseOverlay();
00082 
00083     enum States {
00084         INIT = 0,
00085         JOINING_1 = 1,
00086         JOINING_2 = 2,
00087         JOINING_3 = 3,
00088         READY = 4,
00089         REFRESH = 5,
00090         SHUTDOWN = 6,
00091         FAILED = 7,
00092 
00093         //some aliases for compatibility
00094         JOINING = JOINING_1,
00095         JOIN = JOINING_1,
00096         BOOTSTRAP = JOINING_1,
00097         RSET = JOINING_2,
00098         BSET = JOINING_3
00099     };
00100 
00101     States getState() { return state; };
00102 
00103     class BaseOverlayContext : public cObject
00104     {
00105     public:
00106         BaseOverlayContext(const OverlayKey& key, bool malicious) : key(key), malicious(malicious) {};
00107         OverlayKey key;
00108         bool malicious;
00109     };
00110 
00111 
00112     //------------------------------------------------------------------------
00113     //--- Statistics ---------------------------------------------------------
00114     //------------------------------------------------------------------------
00115 
00116 private://fields: statistics
00117 
00118     int numAppDataSent;   
00119     int bytesAppDataSent; 
00120     int numAppLookupSent;   
00121     int bytesAppLookupSent; 
00122     int numMaintenanceSent;   
00123     int bytesMaintenanceSent; 
00125     int numAppDataReceived;   
00126     int bytesAppDataReceived; 
00127     int numAppLookupReceived;   
00128     int bytesAppLookupReceived; 
00129     int numMaintenanceReceived;   
00130     int bytesMaintenanceReceived; 
00132     int numInternalSent;  
00133     int bytesInternalSent;  
00134     int numInternalReceived;  
00135     int bytesInternalReceived;  
00137     int bytesAuthBlockSent; 
00139     int joinRetries; 
00141 protected:
00142 
00146     struct HopDelayRecord
00147     {
00148         int count;
00149         simtime_t val;
00150         HopDelayRecord() : count(0), val(0) {};
00151     };
00152 
00153     int numAppDataForwarded;   
00154     int bytesAppDataForwarded; 
00155     int numAppLookupForwarded;   
00156     int bytesAppLookupForwarded; 
00157     int numMaintenanceForwarded;   
00158     int bytesMaintenanceForwarded; 
00160     int numFindNodeSent; 
00161     int bytesFindNodeSent; 
00162     int numFindNodeResponseSent; 
00163     int bytesFindNodeResponseSent; 
00164     int numFailedNodeSent; 
00165     int bytesFailedNodeSent; 
00166     int numFailedNodeResponseSent; 
00167     int bytesFailedNodeResponseSent; 
00168     std::vector<HopDelayRecord*> singleHopDelays; 
00170     simtime_t creationTime; 
00173     //------------------------------------------------------------------------
00174     //--- Common Overlay Attributes & Global Module References ---------------
00175     //------------------------------------------------------------------------
00176 
00177 protected://fields: overlay attributes
00178 
00179 
00180 
00181     // references to global modules
00182     GlobalNodeList* globalNodeList;           
00183     NotificationBoard* notificationBoard;       
00184     UnderlayConfigurator* underlayConfigurator; 
00185     BootstrapList* bootstrapList; 
00186     GlobalParameters* globalParameters; 
00188     // overlay common parameters
00189     bool debugOutput;           
00190     RoutingType defaultRoutingType;
00191     bool useCommonAPIforward;   
00192     bool collectPerHopDelay;    
00193     bool routeMsgAcks;          
00194     uint32_t recNumRedundantNodes;  
00195     bool recordRoute;   
00196     bool drawOverlayTopology;
00197     bool rejoinOnFailure;
00198     bool dropFindNodeAttack; 
00199     bool isSiblingAttack; 
00200     bool invalidNodesAttack; 
00201     bool dropRouteMessageAttack; 
00202     int localPort;              
00203     int hopCountMax;            
00204     bool measureAuthBlock; 
00205     bool restoreContext; 
00207     int numDropped;             
00208     int bytesDropped;           
00210     cOutVector delayVector;     
00211     cOutVector hopCountVector;  
00213     States state;
00214 
00215     //------------------------------------------------------------------------
00216     //--- Initialization & finishing -----------------------------------------
00217     //------------------------------------------------------------------------
00218 
00219 private://methods: cSimpleModule initialization
00220 
00226     void initialize(int stage);
00227 
00231     void finish();
00232 
00233 protected://methods: overlay initialization
00234 
00240     int numInitStages() const;
00241 
00252     virtual void initializeOverlay( int stage );
00253 
00257     virtual void finishOverlay();
00258 
00259 private:
00265     virtual void setOwnNodeID();
00266 
00267     //------------------------------------------------------------------------
00268     //--- General Overlay Parameters (getter and setters) --------------------
00269     //------------------------------------------------------------------------
00270 
00271 public://methods: getters and setters
00272 
00278     bool isMalicious();
00279 
00285     bool isInSimpleMultiOverlayHost();
00286 
00287     const simtime_t& getCreationTime() { return creationTime; };
00288 
00289 
00290     //------------------------------------------------------------------------
00291     //--- UDP functions copied from the INET framework .----------------------
00292     //------------------------------------------------------------------------
00293 
00294 protected:
00298     void bindToPort(int port);
00299 
00300 
00301     //------------------------------------------------------------------------
00302     //--- Overlay Common API: Key-based Routing ------------------------------
00303     //------------------------------------------------------------------------
00304 
00305 protected://methods: KBR
00306 
00323     virtual void route(const OverlayKey& key, CompType destComp,
00324                        CompType srcComp, cPacket* msg,
00325                        const std::vector<TransportAddress>& sourceRoute
00326                            = TransportAddress::UNSPECIFIED_NODES,
00327                        RoutingType routingType = DEFAULT_ROUTING);
00328 
00338     void callDeliver( BaseOverlayMessage* msg,
00339                       const OverlayKey& destKey);
00340 
00352     void callForward( const OverlayKey& key, BaseRouteMessage* msg,
00353                       const NodeHandle& nextHopNode);
00354 
00363     void callUpdate(const NodeHandle& node, bool joined);
00364 
00365 public:
00366 
00376     void join(const OverlayKey& nodeID = OverlayKey::UNSPECIFIED_KEY);
00377 
00389     virtual NodeVector* local_lookup(const OverlayKey& key, int num, bool safe);
00390 
00391 
00394     virtual NodeVector* neighborSet(int num);
00395 
00411     virtual bool isSiblingFor(const NodeHandle& node,
00412                               const OverlayKey& key, int numSiblings, bool* err);
00413 
00420     virtual int getMaxNumSiblings();
00421 
00428     virtual int getMaxNumRedundantNodes();
00429 
00430     //------------------------------------------------------------------------
00431     //--- Message Handlers ---------------------------------------------------
00432     //------------------------------------------------------------------------
00433 
00434 private://methods: message handling
00435 
00446     void handleMessage(cMessage* msg);
00447 
00448 
00458     void handleBaseOverlayMessage(BaseOverlayMessage* msg,
00459                                   const OverlayKey& destKey =
00460                                       OverlayKey::UNSPECIFIED_KEY);
00461 
00462 protected://methods: message handling
00463 
00469     virtual void handleUDPMessage(BaseOverlayMessage* msg);
00470 
00476     //virtual void handleTimerEvent(cMessage* msg);
00477 
00483     virtual void handleAppMessage(cMessage* msg);
00484 
00491     virtual void receiveChangeNotification(int category,
00492                                            const cPolymorphic* details);
00493 
00498     virtual void handleTransportAddressChangedNotification();
00499 
00503     virtual void handleNodeLeaveNotification();
00504 
00509     virtual void handleNodeGracefulLeaveNotification();
00510 
00511 
00522     virtual void recordOverlaySentStats(BaseOverlayMessage* msg);
00523 
00524 protected://methods: icons and ui support
00525 
00531     void setOverlayReady( bool ready );
00532 
00533     //------------------------------------------------------------------------
00534     //--- Messages -----------------------------------------------------------
00535     //------------------------------------------------------------------------
00536 
00537 private://methods: sending packets over udp
00538 
00539     void sendRouteMessage(const TransportAddress& dest,
00540                           BaseRouteMessage* msg,
00541                           bool ack);
00542 
00543     bool checkFindNode(BaseRouteMessage* routeMsg);
00544 
00545 public:
00552     void sendMessageToUDP(const TransportAddress& dest, cPacket* msg);
00553 
00554     //------------------------------------------------------------------------
00555     //--- Basic Routing ------------------------------------------------------
00556     //------------------------------------------------------------------------
00557 
00558 protected://fields: config
00559     IterativeLookupConfiguration iterativeLookupConfig;
00560     RecursiveLookupConfiguration recursiveLookupConfig;
00561 
00562     class lookupHashFcn
00563     {
00564     public:
00565         size_t operator()( const AbstractLookup* l1 ) const
00566         {
00567             return (size_t)l1;
00568         }
00569         bool operator()(const AbstractLookup* l1,
00570                         const AbstractLookup* l2) const
00571         {
00572             return (l1 == l2);
00573         }
00574     };
00575 
00576     typedef UNORDERED_SET<AbstractLookup*, lookupHashFcn, lookupHashFcn> LookupSet;
00577 
00578     LookupSet lookups;
00579 
00580 private://methods: internal routing
00581 
00585     void initLookups();
00586 
00590     void finishLookups();
00591 
00607     virtual bool recursiveRoutingHook(const TransportAddress& dest,
00608                                       BaseRouteMessage* msg);
00609 
00610 public://methods: basic message routing
00611 
00625     void sendToKey(const OverlayKey& key, BaseOverlayMessage* message,
00626                    int numSiblings = 1,
00627                    const std::vector<TransportAddress>& sourceRoute
00628                              = TransportAddress::UNSPECIFIED_NODES,
00629                    RoutingType routingType = DEFAULT_ROUTING);
00630 
00641     virtual OverlayKey distance(const OverlayKey& x,
00642                                 const OverlayKey& y,
00643                                 bool useAlternative = false) const;
00644 
00645 protected://methods: routing class factory
00646 
00659     virtual AbstractLookup* createLookup(RoutingType routingType = DEFAULT_ROUTING,
00660                                          const BaseOverlayMessage* msg = NULL,
00661                                          const cPacket* findNodeExt = NULL,
00662                                          bool appLookup = false);
00663 
00669     virtual void removeLookup( AbstractLookup* lookup );
00670 
00687     virtual NodeVector* findNode( const OverlayKey& key,
00688                                   int numRedundantNodes,
00689                                   int numSiblings,
00690                                   BaseOverlayMessage* msg = NULL);
00691 
00692 
00702     virtual void joinOverlay();
00703 
00713     virtual void joinForeignPartition(const NodeHandle& node);
00714 
00724     virtual bool handleFailedNode(const TransportAddress& failed);
00725 
00726     virtual void lookupRpc(LookupCall* call);
00727 
00728     virtual void nextHopRpc(NextHopCall* call);
00729 
00730 protected://methods: statistic helpers for IterativeLookup
00731 
00732     void countFindNodeCall(const FindNodeCall* call);
00733     void countFailedNodeCall(const FailedNodeCall* call);
00734 
00735 
00736     bool internalHandleRpcCall( BaseCallMessage* msg );
00737     void internalHandleRpcResponse(BaseResponseMessage* msg,
00738                                    cPolymorphic* context, int rpcId,
00739                                    simtime_t rtt);
00740     void internalHandleRpcTimeout(BaseCallMessage* msg,
00741                                   const TransportAddress& dest,
00742                                   cPolymorphic* context,
00743                                   int rpcId, const OverlayKey& destKey);
00744 
00745     // TODO rename to internalSendRouteRpcMessage()
00746     void internalSendRouteRpc(BaseRpcMessage* message,
00747                               const OverlayKey& destKey,
00748                               const std::vector<TransportAddress>&
00749                               sourceRoute,
00750                               RoutingType routingType);
00751 
00752     /*
00753      * Returns the component type of this module
00754      *
00755      * @return the component type
00756      */
00757     CompType getThisCompType();
00758 
00759     bool kbr; 
00761 private:
00762     void internalSendRpcResponse(BaseCallMessage* call,
00763                                  BaseResponseMessage* response);
00764 
00765     const cGate* udpGate;
00766     const cGate* appGate;
00767 
00768 public:
00769     /*
00770      * Register a new component at the overlay
00771      *
00772      * @param compType The compoment type (defined in CommonMessages.msg)
00773      * @param module The module pointer of the component
00774      */
00775     void registerComp(CompType compType, cModule *module);
00776 
00777     /*
00778      * Get the module pointer of a registered component
00779      *
00780      * @param compType The compoment type (defined in CommonMessages.msg)
00781      * @return The module pointer of the component
00782      */
00783     cModule* getCompModule(CompType compType);
00784 
00785     /*
00786      * Get the direct_in gate of a registered component
00787      *
00788      * @param compType The compoment type (defined in CommonMessages.msg)
00789      * @return The pointer to the direct_in gate of the component
00790      */
00791     cGate* getCompRpcGate(CompType compType);
00792 
00793     /*
00794      * Sends a message to all currently registered components, but the
00795      * source component
00796      *
00797      * @param msg The pointer to the message to send
00798      * @param srcComp The type of the originating component
00799      *
00800      */
00801     void sendMessageToAllComp(cMessage* msg, CompType srcComp);
00802 
00803     /*
00804      * Returns true, if the overlay is a structured overlay and
00805      * provides KBR services (e.g. route, lookup, ...).
00806      */
00807     bool providesKbr() { return kbr; };
00808 
00809     virtual uint8_t getBitsPerDigit() { return 1; };
00810 
00811     bool getMeasureAuthBlock() { return measureAuthBlock; }
00812 
00813 private:
00814     void findNodeRpc( FindNodeCall* call );
00815     void failedNodeRpc( FailedNodeCall* call );
00816 
00817     typedef std::map<CompType, std::pair<cModule*, cGate*> > CompModuleList;
00818     CompModuleList compModuleList;
00819     bool internalReadyState; 
00820 };
00821 
00822 #endif
Generated on Wed May 26 16:21:13 2010 for OverSim by  doxygen 1.6.3