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 <BaseTcpSupport.h>
00036 #include <IterativeLookupConfiguration.h>
00037 #include <RecursiveLookup.h>
00038 #include <InitStages.h>
00039 
00040 class GlobalNodeList;
00041 class UnderlayConfigurator;
00042 class BaseApp;
00043 class NodeHandle;
00044 class OverlayKey;
00045 class NotificationBoard;
00046 class AbstractLookup;
00047 class BootstrapList;
00048 
00062 class BaseOverlay : public INotifiable,
00063                     public BaseRpc,
00064                     public BaseTcpSupport,
00065                     public TopologyVis
00066 {
00067 
00068     friend class IterativeLookup;
00069     friend class IterativePathLookup;
00070     friend class RecursiveLookup;
00071     friend class BootstrapList;
00072     friend class SendToKeyListener;
00073 
00074     //------------------------------------------------------------------------
00075     //--- Construction / Destruction -----------------------------------------
00076     //------------------------------------------------------------------------
00077 
00078 public:
00079 
00080     BaseOverlay();
00081 
00083     virtual ~BaseOverlay();
00084 
00085     enum States {
00086         INIT = 0,
00087         JOINING_1 = 1,
00088         JOINING_2 = 2,
00089         JOINING_3 = 3,
00090         READY = 4,
00091         REFRESH = 5,
00092         SHUTDOWN = 6,
00093         FAILED = 7,
00094 
00095         //some aliases for compatibility
00096         JOINING = JOINING_1,
00097         JOIN = JOINING_1,
00098         BOOTSTRAP = JOINING_1,
00099         RSET = JOINING_2,
00100         BSET = JOINING_3
00101     };
00102 
00103     States getState() { return state; };
00104 
00105     class BaseOverlayContext : public cObject
00106     {
00107     public:
00108         BaseOverlayContext(const OverlayKey& key, bool malicious) : key(key), malicious(malicious) {};
00109         OverlayKey key;
00110         bool malicious;
00111     };
00112 
00113 
00114     //------------------------------------------------------------------------
00115     //--- Statistics ---------------------------------------------------------
00116     //------------------------------------------------------------------------
00117 
00118 private://fields: statistics
00119 
00120     int numAppDataSent;   
00121     int bytesAppDataSent; 
00122     int numAppLookupSent;   
00123     int bytesAppLookupSent; 
00124     int numMaintenanceSent;   
00125     int bytesMaintenanceSent; 
00127     int numAppDataReceived;   
00128     int bytesAppDataReceived; 
00129     int numAppLookupReceived;   
00130     int bytesAppLookupReceived; 
00131     int numMaintenanceReceived;   
00132     int bytesMaintenanceReceived; 
00134     int numInternalSent;  
00135     int bytesInternalSent;  
00136     int numInternalReceived;  
00137     int bytesInternalReceived;  
00139     int bytesAuthBlockSent; 
00141     int joinRetries; 
00143 protected:
00144 
00148     struct HopDelayRecord
00149     {
00150         int count;
00151         simtime_t val;
00152         HopDelayRecord() : count(0), val(0) {};
00153     };
00154 
00155     int numAppDataForwarded;   
00156     int bytesAppDataForwarded; 
00157     int numAppLookupForwarded;   
00158     int bytesAppLookupForwarded; 
00159     int numMaintenanceForwarded;   
00160     int bytesMaintenanceForwarded; 
00162     int numFindNodeSent; 
00163     int bytesFindNodeSent; 
00164     int numFindNodeResponseSent; 
00165     int bytesFindNodeResponseSent; 
00166     int numFailedNodeSent; 
00167     int bytesFailedNodeSent; 
00168     int numFailedNodeResponseSent; 
00169     int bytesFailedNodeResponseSent; 
00170     std::vector<HopDelayRecord*> singleHopDelays; 
00172     simtime_t creationTime; 
00175     //------------------------------------------------------------------------
00176     //--- Common Overlay Attributes & Global Module References ---------------
00177     //------------------------------------------------------------------------
00178 
00179 protected://fields: overlay attributes
00180 
00181 
00182 
00183     // references to global modules
00184     GlobalNodeList* globalNodeList;           
00185     NotificationBoard* notificationBoard;       
00186     UnderlayConfigurator* underlayConfigurator; 
00187     BootstrapList* bootstrapList; 
00188     GlobalParameters* globalParameters; 
00190     // overlay common parameters
00191     bool debugOutput;           
00192     RoutingType defaultRoutingType;
00193     bool useCommonAPIforward;   
00194     bool collectPerHopDelay;    
00195     bool routeMsgAcks;          
00196     uint32_t recNumRedundantNodes;  
00197     bool recordRoute;   
00198     bool drawOverlayTopology;
00199     bool rejoinOnFailure;
00200     bool sendRpcResponseToLastHop; 
00201     bool dropFindNodeAttack; 
00202     bool isSiblingAttack; 
00203     bool invalidNodesAttack; 
00204     bool dropRouteMessageAttack; 
00205     int localPort;              
00206     int hopCountMax;            
00207     bool measureAuthBlock; 
00208     bool restoreContext; 
00210     int numDropped;             
00211     int bytesDropped;           
00213     cOutVector delayVector;     
00214     cOutVector hopCountVector;  
00216     States state;
00217 
00218     //------------------------------------------------------------------------
00219     //--- Initialization & finishing -----------------------------------------
00220     //------------------------------------------------------------------------
00221 
00222 private://methods: cSimpleModule initialization
00223 
00229     void initialize(int stage);
00230 
00234     void finish();
00235 
00236 protected://methods: overlay initialization
00237 
00243     int numInitStages() const;
00244 
00255     virtual void initializeOverlay( int stage );
00256 
00260     virtual void finishOverlay();
00261 
00262 private:
00268     virtual void setOwnNodeID();
00269 
00270     //------------------------------------------------------------------------
00271     //--- General Overlay Parameters (getter and setters) --------------------
00272     //------------------------------------------------------------------------
00273 
00274 public://methods: getters and setters
00275 
00281     bool isMalicious();
00282 
00288     bool isInSimpleMultiOverlayHost();
00289 
00290     const simtime_t& getCreationTime() { return creationTime; };
00291 
00292 
00293     //------------------------------------------------------------------------
00294     //--- UDP functions copied from the INET framework .----------------------
00295     //------------------------------------------------------------------------
00296 
00297 protected:
00301     void bindToPort(int port);
00302 
00303 
00304     //------------------------------------------------------------------------
00305     //--- Overlay Common API: Key-based Routing ------------------------------
00306     //------------------------------------------------------------------------
00307 
00308 protected://methods: KBR
00309 
00326     virtual void route(const OverlayKey& key, CompType destComp,
00327                        CompType srcComp, cPacket* msg,
00328                        const std::vector<TransportAddress>& sourceRoute
00329                            = TransportAddress::UNSPECIFIED_NODES,
00330                        RoutingType routingType = DEFAULT_ROUTING);
00331 
00341     void callDeliver( BaseOverlayMessage* msg,
00342                       const OverlayKey& destKey);
00343 
00355     void callForward( const OverlayKey& key, BaseRouteMessage* msg,
00356                       const NodeHandle& nextHopNode);
00357 
00366     void callUpdate(const NodeHandle& node, bool joined);
00367 
00368 public:
00369 
00379     void join(const OverlayKey& nodeID = OverlayKey::UNSPECIFIED_KEY);
00380 
00392     virtual NodeVector* local_lookup(const OverlayKey& key, int num, bool safe);
00393 
00394 
00397     virtual NodeVector* neighborSet(int num);
00398 
00414     virtual bool isSiblingFor(const NodeHandle& node,
00415                               const OverlayKey& key, int numSiblings, bool* err);
00416 
00423     virtual int getMaxNumSiblings();
00424 
00431     virtual int getMaxNumRedundantNodes();
00432 
00433     //------------------------------------------------------------------------
00434     //--- Message Handlers ---------------------------------------------------
00435     //------------------------------------------------------------------------
00436 
00437 protected://methods: message handling
00438 
00449     void handleMessage(cMessage* msg);
00450 
00451 
00461     void handleBaseOverlayMessage(BaseOverlayMessage* msg,
00462                                   const OverlayKey& destKey =
00463                                       OverlayKey::UNSPECIFIED_KEY);
00464 
00465 protected://methods: message handling
00466 
00472     virtual void handleUDPMessage(BaseOverlayMessage* msg);
00473 
00479     //virtual void handleTimerEvent(cMessage* msg);
00480 
00486     virtual void handleAppMessage(cMessage* msg);
00487 
00494     virtual void receiveChangeNotification(int category,
00495                                            const cPolymorphic* details);
00496 
00501     virtual void handleTransportAddressChangedNotification();
00502 
00506     virtual void handleNodeLeaveNotification();
00507 
00512     virtual void handleNodeGracefulLeaveNotification();
00513 
00514 
00525     virtual void recordOverlaySentStats(BaseOverlayMessage* msg);
00526 
00527 protected://methods: icons and ui support
00528 
00534     void setOverlayReady( bool ready );
00535 
00536     //------------------------------------------------------------------------
00537     //--- Messages -----------------------------------------------------------
00538     //------------------------------------------------------------------------
00539 
00540 private://methods: sending packets over udp
00541 
00542     void sendRouteMessage(const TransportAddress& dest,
00543                           BaseRouteMessage* msg,
00544                           bool ack);
00545 
00546     bool checkFindNode(BaseRouteMessage* routeMsg);
00547 
00548 public:
00555     void sendMessageToUDP(const TransportAddress& dest, cPacket* msg);
00556 
00557     //------------------------------------------------------------------------
00558     //--- Basic Routing ------------------------------------------------------
00559     //------------------------------------------------------------------------
00560 
00561 protected://fields: config
00562     IterativeLookupConfiguration iterativeLookupConfig;
00563     RecursiveLookupConfiguration recursiveLookupConfig;
00564 
00565     class lookupHashFcn
00566     {
00567     public:
00568         size_t operator()( const AbstractLookup* l1 ) const
00569         {
00570             return (size_t)l1;
00571         }
00572         bool operator()(const AbstractLookup* l1,
00573                         const AbstractLookup* l2) const
00574         {
00575             return (l1 == l2);
00576         }
00577     };
00578 
00579     typedef UNORDERED_SET<AbstractLookup*, lookupHashFcn, lookupHashFcn> LookupSet;
00580 
00581     LookupSet lookups;
00582 
00583 private://methods: internal routing
00584 
00588     void initLookups();
00589 
00593     void finishLookups();
00594 
00610     virtual bool recursiveRoutingHook(const TransportAddress& dest,
00611                                       BaseRouteMessage* msg);
00612 
00613 public://methods: basic message routing
00614 
00628     void sendToKey(const OverlayKey& key, BaseOverlayMessage* message,
00629                    int numSiblings = 1,
00630                    const std::vector<TransportAddress>& sourceRoute
00631                              = TransportAddress::UNSPECIFIED_NODES,
00632                    RoutingType routingType = DEFAULT_ROUTING);
00633 
00644     virtual OverlayKey distance(const OverlayKey& x,
00645                                 const OverlayKey& y,
00646                                 bool useAlternative = false) const;
00647 
00648 protected://methods: routing class factory
00649 
00662     virtual AbstractLookup* createLookup(RoutingType routingType = DEFAULT_ROUTING,
00663                                          const BaseOverlayMessage* msg = NULL,
00664                                          const cPacket* findNodeExt = NULL,
00665                                          bool appLookup = false);
00666 
00672     virtual void removeLookup( AbstractLookup* lookup );
00673 
00690     virtual NodeVector* findNode( const OverlayKey& key,
00691                                   int numRedundantNodes,
00692                                   int numSiblings,
00693                                   BaseOverlayMessage* msg = NULL);
00694 
00695 
00705     virtual void joinOverlay();
00706 
00716     virtual void joinForeignPartition(const NodeHandle& node);
00717 
00727     virtual bool handleFailedNode(const TransportAddress& failed);
00728 
00729     virtual void lookupRpc(LookupCall* call);
00730 
00731     virtual void nextHopRpc(NextHopCall* call);
00732 
00733 protected://methods: statistic helpers for IterativeLookup
00734 
00735     void countFindNodeCall(const FindNodeCall* call);
00736     void countFailedNodeCall(const FailedNodeCall* call);
00737 
00738 
00739     bool internalHandleRpcCall( BaseCallMessage* msg );
00740     void internalHandleRpcResponse(BaseResponseMessage* msg,
00741                                    cPolymorphic* context, int rpcId,
00742                                    simtime_t rtt);
00743     void internalHandleRpcTimeout(BaseCallMessage* msg,
00744                                   const TransportAddress& dest,
00745                                   cPolymorphic* context,
00746                                   int rpcId, const OverlayKey& destKey);
00747 
00748     // TODO rename to internalSendRouteRpcMessage()
00749     void internalSendRouteRpc(BaseRpcMessage* message,
00750                               const OverlayKey& destKey,
00751                               const std::vector<TransportAddress>&
00752                               sourceRoute,
00753                               RoutingType routingType);
00754 
00755     /*
00756      * Returns the component type of this module
00757      *
00758      * @return the component type
00759      */
00760     CompType getThisCompType();
00761 
00762     bool kbr; 
00764 private:
00765     void internalSendRpcResponse(BaseCallMessage* call,
00766                                  BaseResponseMessage* response);
00767 
00768     const cGate* udpGate;
00769     const cGate* appGate;
00770 
00771 public:
00772     /*
00773      * Register a new component at the overlay
00774      *
00775      * @param compType The compoment type (defined in CommonMessages.msg)
00776      * @param module The module pointer of the component
00777      */
00778     void registerComp(CompType compType, cModule *module);
00779 
00780     /*
00781      * Get the module pointer of a registered component
00782      *
00783      * @param compType The compoment type (defined in CommonMessages.msg)
00784      * @return The module pointer of the component
00785      */
00786     cModule* getCompModule(CompType compType);
00787 
00788     /*
00789      * Get the direct_in gate of a registered component
00790      *
00791      * @param compType The compoment type (defined in CommonMessages.msg)
00792      * @return The pointer to the direct_in gate of the component
00793      */
00794     cGate* getCompRpcGate(CompType compType);
00795 
00796     /*
00797      * Sends a message to all currently registered components, but the
00798      * source component
00799      *
00800      * @param msg The pointer to the message to send
00801      * @param srcComp The type of the originating component
00802      *
00803      */
00804     void sendMessageToAllComp(cMessage* msg, CompType srcComp);
00805 
00806     /*
00807      * Returns true, if the overlay is a structured overlay and
00808      * provides KBR services (e.g. route, lookup, ...).
00809      */
00810     bool providesKbr() { return kbr; };
00811 
00812     virtual uint8_t getBitsPerDigit() { return 1; };
00813 
00814     bool getMeasureAuthBlock() { return measureAuthBlock; }
00815 
00816     BootstrapList& getBootstrapList() const { return *bootstrapList;}
00817 
00818 private:
00819     void findNodeRpc( FindNodeCall* call );
00820     void failedNodeRpc( FailedNodeCall* call );
00821 
00822     typedef std::map<CompType, std::pair<cModule*, cGate*> > CompModuleList;
00823     CompModuleList compModuleList;
00824     bool internalReadyState; 
00825 };
00826 
00827 #endif