OverSim
BaseOverlay.h
Go to the documentation of this file.
1 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
24 #ifndef __BASEOVERLAY_H_
25 #define __BASEOVERLAY_H_
26 
27 #include <oversim_mapset.h>
28 
29 #include <omnetpp.h>
30 
31 #include <NodeVector.h>
32 #include <TopologyVis.h>
33 #include <INotifiable.h>
34 #include <BaseRpc.h>
35 #include <BaseTcpSupport.h>
37 #include <RecursiveLookup.h>
38 #include <InitStages.h>
39 
40 class GlobalNodeList;
42 class BaseApp;
43 class NodeHandle;
44 class OverlayKey;
45 class NotificationBoard;
46 class AbstractLookup;
47 class BootstrapList;
48 
62 class BaseOverlay : public INotifiable,
63  public BaseRpc,
64  public BaseTcpSupport,
65  public TopologyVis
66 {
67 
68  friend class IterativeLookup;
69  friend class IterativePathLookup;
70  friend class RecursiveLookup;
71  friend class BootstrapList;
72  friend class SendToKeyListener;
73 
74  //------------------------------------------------------------------------
75  //--- Construction / Destruction -----------------------------------------
76  //------------------------------------------------------------------------
77 
78 public:
79 
80  BaseOverlay();
81 
83  virtual ~BaseOverlay();
84 
85  enum States {
86  INIT = 0,
87  BOOTSTRAP = 1,
88  DISCOVERY = 2,
89  PREJOIN = 3,
90  JOIN = 4,
91  POSTJOIN = 5,
92  READY = 6,
93  REFRESH = 7,
94  SHUTDOWN = 8,
95  FAILED = 9,
96 
97  //some aliases for compatibility
100  };
101 
102  States getState() { return state; };
103 
104  class BaseOverlayContext : public cObject
105  {
106  public:
107  BaseOverlayContext(const OverlayKey& key, bool malicious) : key(key), malicious(malicious) {};
108  OverlayKey key;
109  bool malicious;
110  };
111 
112 
113  //------------------------------------------------------------------------
114  //--- Statistics ---------------------------------------------------------
115  //------------------------------------------------------------------------
116 
117 private://fields: statistics
118 
142 protected:
143 
148  {
149  int count;
150  simtime_t val;
151  HopDelayRecord() : count(0), val(0) {};
152  };
153 
169  std::vector<HopDelayRecord*> singleHopDelays;
171  simtime_t creationTime;
174  //------------------------------------------------------------------------
175  //--- Common Overlay Attributes & Global Module References ---------------
176  //------------------------------------------------------------------------
177 
178 protected://fields: overlay attributes
179 
180 
181 
182  // references to global modules
184  NotificationBoard* notificationBoard;
189  // overlay common parameters
190  uint32_t overlayId;
191  bool debugOutput;
197  bool recordRoute;
205  int localPort;
213  cOutVector delayVector;
214  cOutVector hopCountVector;
217 
218  //------------------------------------------------------------------------
219  //--- Initialization & finishing -----------------------------------------
220  //------------------------------------------------------------------------
221 
222 private://methods: cSimpleModule initialization
223 
229  void initialize(int stage);
230 
234  void finish();
235 
236 protected://methods: overlay initialization
237 
243  int numInitStages() const;
244 
255  virtual void initializeOverlay( int stage );
256 
260  virtual void finishOverlay();
261 
262 private:
268  virtual void setOwnNodeID();
269 
270  //------------------------------------------------------------------------
271  //--- General Overlay Parameters (getter and setters) --------------------
272  //------------------------------------------------------------------------
273 
274 public://methods: getters and setters
275 
281  bool isMalicious();
282 
289 
290  const simtime_t& getCreationTime() { return creationTime; };
291 
292 
293  //------------------------------------------------------------------------
294  //--- UDP functions copied from the INET framework .----------------------
295  //------------------------------------------------------------------------
296 
297 protected:
301  void bindToPort(int port);
302 
303 
304  //------------------------------------------------------------------------
305  //--- Overlay Common API: Key-based Routing ------------------------------
306  //------------------------------------------------------------------------
307 
308 protected://methods: KBR
309 
326  virtual void route(const OverlayKey& key, CompType destComp,
327  CompType srcComp, cPacket* msg,
328  const std::vector<TransportAddress>& sourceRoute
330  RoutingType routingType = DEFAULT_ROUTING);
331 
341  void callDeliver( BaseOverlayMessage* msg,
342  const OverlayKey& destKey);
343 
355  void callForward( const OverlayKey& key, BaseRouteMessage* msg,
356  const NodeHandle& nextHopNode);
357 
366  void callUpdate(const NodeHandle& node, bool joined);
367 
368 public:
369 
379  void join(const OverlayKey& nodeID = OverlayKey::UNSPECIFIED_KEY);
380 
392  virtual NodeVector* local_lookup(const OverlayKey& key, int num, bool safe);
393 
394 
397  virtual NodeVector* neighborSet(int num);
398 
414  virtual bool isSiblingFor(const NodeHandle& node,
415  const OverlayKey& key, int numSiblings, bool* err);
416 
423  virtual int getMaxNumSiblings();
424 
431  virtual int getMaxNumRedundantNodes();
432 
433  //------------------------------------------------------------------------
434  //--- Message Handlers ---------------------------------------------------
435  //------------------------------------------------------------------------
436 
437 protected://methods: message handling
438 
449  void handleMessage(cMessage* msg);
450 
451 
462  const OverlayKey& destKey =
464 
465 protected://methods: message handling
466 
472  virtual void handleUDPMessage(BaseOverlayMessage* msg);
473 
479  //virtual void handleTimerEvent(cMessage* msg);
480 
486  virtual void handleAppMessage(cMessage* msg);
487 
494  virtual void receiveChangeNotification(int category,
495  const cPolymorphic* details);
496 
502 
506  virtual void handleNodeLeaveNotification();
507 
513 
514 
525  virtual void recordOverlaySentStats(BaseOverlayMessage* msg);
526 
527 protected://methods: icons and ui support
528 
534  void setOverlayReady( bool ready );
535 
536  //------------------------------------------------------------------------
537  //--- Messages -----------------------------------------------------------
538  //------------------------------------------------------------------------
539 
540 private://methods: sending packets over udp
541 
542  void sendRouteMessage(const TransportAddress& dest,
543  BaseRouteMessage* msg,
544  bool ack);
545 
546  bool checkFindNode(BaseRouteMessage* routeMsg);
547 
548 public:
555  void sendMessageToUDP(const TransportAddress& dest, cPacket* msg,
556  simtime_t delay = SIMTIME_ZERO);
557 
558  //------------------------------------------------------------------------
559  //--- Basic Routing ------------------------------------------------------
560  //------------------------------------------------------------------------
561 
562 protected://fields: config
565 
567  {
568  public:
569  size_t operator()( const AbstractLookup* l1 ) const
570  {
571  return (size_t)l1;
572  }
573  bool operator()(const AbstractLookup* l1,
574  const AbstractLookup* l2) const
575  {
576  return (l1 == l2);
577  }
578  };
579 
580  typedef UNORDERED_SET<AbstractLookup*, lookupHashFcn, lookupHashFcn> LookupSet;
581 
583 
584 private://methods: internal routing
585 
589  void initLookups();
590 
594  void finishLookups();
595 
611  virtual bool recursiveRoutingHook(const TransportAddress& dest,
612  BaseRouteMessage* msg);
613 
614 public://methods: basic message routing
615 
629  void sendToKey(const OverlayKey& key, BaseOverlayMessage* message,
630  int numSiblings = 1,
631  const std::vector<TransportAddress>& sourceRoute
633  RoutingType routingType = DEFAULT_ROUTING);
634 
645  virtual OverlayKey distance(const OverlayKey& x,
646  const OverlayKey& y,
647  bool useAlternative = false) const;
648 
649 protected://methods: routing class factory
650 
664  const BaseOverlayMessage* msg = NULL,
665  const cPacket* findNodeExt = NULL,
666  bool appLookup = false);
667 
673  virtual void removeLookup( AbstractLookup* lookup );
674 
691  virtual NodeVector* findNode( const OverlayKey& key,
692  int numRedundantNodes,
693  int numSiblings,
694  BaseOverlayMessage* msg = NULL);
695 
696 
706  virtual void joinOverlay();
707 
717  virtual void joinForeignPartition(const NodeHandle& node);
718 
728  virtual bool handleFailedNode(const TransportAddress& failed);
729 
730  virtual void lookupRpc(LookupCall* call);
731 
732  virtual void nextHopRpc(NextHopCall* call);
733 
734 protected://methods: statistic helpers for IterativeLookup
735 
736  void countFindNodeCall(const FindNodeCall* call);
737  void countFailedNodeCall(const FailedNodeCall* call);
738 
739 
742  cPolymorphic* context, int rpcId,
743  simtime_t rtt);
745  const TransportAddress& dest,
746  cPolymorphic* context,
747  int rpcId, const OverlayKey& destKey);
748 
749  // TODO rename to internalSendRouteRpcMessage()
750  void internalSendRouteRpc(BaseRpcMessage* message,
751  const OverlayKey& destKey,
752  const std::vector<TransportAddress>&
753  sourceRoute,
754  RoutingType routingType);
755 
756  /*
757  * Returns the component type of this module
758  *
759  * @return the component type
760  */
762 
763  bool kbr;
765 private:
767  BaseResponseMessage* response);
768 
769  const cGate* udpGate;
770  const cGate* appGate;
771 
772 public:
773  /*
774  * Register a new component at the overlay
775  *
776  * @param compType The compoment type (defined in CommonMessages.msg)
777  * @param module The module pointer of the component
778  */
779  void registerComp(CompType compType, cModule *module);
780 
781  /*
782  * Get the module pointer of a registered component
783  *
784  * @param compType The compoment type (defined in CommonMessages.msg)
785  * @return The module pointer of the component
786  */
787  cModule* getCompModule(CompType compType);
788 
789  /*
790  * Get the direct_in gate of a registered component
791  *
792  * @param compType The compoment type (defined in CommonMessages.msg)
793  * @return The pointer to the direct_in gate of the component
794  */
795  cGate* getCompRpcGate(CompType compType);
796 
797  /*
798  * Sends a message to all currently registered components, but the
799  * source component
800  *
801  * @param msg The pointer to the message to send
802  * @param srcComp The type of the originating component
803  *
804  */
805  void sendMessageToAllComp(cMessage* msg, CompType srcComp);
806 
807  /*
808  * Returns true, if the overlay is a structured overlay and
809  * provides KBR services (e.g. route, lookup, ...).
810  */
811  bool providesKbr() { return kbr; };
812 
813  virtual uint8_t getBitsPerDigit() { return 1; };
814 
816 
818 
825 
831  virtual uint32_t estimateOverlaySize();
832 
833 private:
834  void findNodeRpc( FindNodeCall* call );
835  void failedNodeRpc( FailedNodeCall* call );
836 
837  typedef std::map<CompType, std::pair<cModule*, cGate*> > CompModuleList;
841  int socketId;
842 };
843 
844 #endif