OMNeT++ Simulation Library  5.6.1
ctopology.h
1 //==========================================================================
2 // CTOPOLOGY.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CTOPOLOGY_H
17 #define __OMNETPP_CTOPOLOGY_H
18 
19 #include <string>
20 #include <vector>
21 #include "cownedobject.h"
22 #include "csimulation.h"
23 #include "cmodule.h"
24 #include "cgate.h"
25 
26 namespace omnetpp {
27 
28 class cPar;
29 
30 // not all compilers define INFINITY (gcc does)
31 #ifndef INFINITY
32 #define INFINITY HUGE_VAL
33 #endif
34 
54 class SIM_API cTopology : public cOwnedObject
55 {
56  public:
57  class Link;
58  class LinkIn;
59  class LinkOut;
60 
64  class SIM_API Node
65  {
66  friend class cTopology;
67 
68  protected:
69  int moduleId;
70  double weight;
71  bool enabled;
72  std::vector<Link*> inLinks;
73  std::vector<Link*> outLinks;
74 
75  // variables used by the shortest-path algorithms
76  double dist;
77  Link *outPath;
78 
79  public:
83  Node(int moduleId=-1) {this->moduleId=moduleId; weight=0; enabled=true; dist=INFINITY; outPath=nullptr;}
84  virtual ~Node() {}
85 
88 
92  int getModuleId() const {return moduleId;}
93 
97  cModule *getModule() const {return getSimulation()->getModule(moduleId);}
98 
103  double getWeight() const {return weight;}
104 
109  void setWeight(double d) {weight=d;}
110 
115  bool isEnabled() const {return enabled;}
116 
121  void enable() {enabled=true;}
122 
127  void disable() {enabled=false;}
129 
132 
136  int getNumInLinks() const {return inLinks.size();}
137 
141  LinkIn *getLinkIn(int i);
142 
146  int getNumOutLinks() const {return outLinks.size();}
147 
151  LinkOut *getLinkOut(int i);
153 
156 
160  double getDistanceToTarget() const {return dist;}
161 
166  int getNumPaths() const {return outPath?1:0;}
167 
173  LinkOut *getPath(int) const {return (LinkOut *)outPath;}
175  };
176 
177 
181  class SIM_API Link
182  {
183  friend class cTopology;
184 
185  protected:
186  Node *srcNode;
187  int srcGateId;
188  Node *destNode;
189  int destGateId;
190  double weight;
191  bool enabled;
192 
193  public:
197  Link(double weight=1) {srcNode=destNode=nullptr; srcGateId=destGateId=-1; this->weight=weight; enabled=true;}
198  virtual ~Link() {}
199 
204  double getWeight() const {return weight;}
205 
210  void setWeight(double d) {weight=d;}
211 
216  bool isEnabled() const {return enabled;}
217 
222  void enable() {enabled=true;}
223 
228  void disable() {enabled=false;}
229  };
230 
231 
240  class SIM_API LinkIn : public Link
241  {
242  private:
243  LinkIn(double weight=1) : Link(weight) {}
244 
245  public:
249  Node *getRemoteNode() const {return srcNode;}
250 
254  Node *getLocalNode() const {return destNode;}
255 
259  int getRemoteGateId() const {return srcGateId;}
260 
264  int getLocalGateId() const {return destGateId;}
265 
269  cGate *getRemoteGate() const {return srcNode->getModule()->gate(srcGateId);}
270 
274  cGate *getLocalGate() const {return destNode->getModule()->gate(destGateId);}
275  };
276 
277 
286  class SIM_API LinkOut : public Link
287  {
288  private:
289  LinkOut(double weight=1) : Link(weight) {}
290 
291  public:
295  Node *getRemoteNode() const {return destNode;}
296 
300  Node *getLocalNode() const {return srcNode;}
301 
305  int getRemoteGateId() const {return destGateId;}
306 
310  int getLocalGateId() const {return srcGateId;}
311 
315  cGate *getRemoteGate() const {return destNode->getModule()->gate(destGateId);}
316 
320  cGate *getLocalGate() const {return srcNode->getModule()->gate(srcGateId);}
321  };
322 
329  class SIM_API Predicate
330  {
331  public:
332  virtual ~Predicate() {}
333  virtual bool matches(cModule *module) = 0;
334  };
335 
336  protected:
337  std::vector<Node*> nodes;
338  Node *target;
339 
340  // note: the purpose of the (unsigned int) cast is that nodes with moduleId==-1 are inserted at the end of the vector
341  static bool lessByModuleId(Node *a, Node *b) { return (unsigned int)a->moduleId < (unsigned int)b->moduleId; }
342  static bool isModuleIdLess(Node *a, int moduleId) { return (unsigned int)a->moduleId < (unsigned int)moduleId; }
343 
344  void unlinkFromSourceNode(Link *link);
345  void unlinkFromDestNode(Link *link);
346 
347  public:
350 
354  explicit cTopology(const char *name=nullptr);
355 
359  cTopology(const cTopology& topo);
360 
364  virtual ~cTopology();
365 
369  cTopology& operator=(const cTopology& topo);
371 
374 
379  virtual cTopology *dup() const override {return new cTopology(*this);}
380 
385  virtual std::string str() const override;
386 
392  virtual void parsimPack(cCommBuffer *buffer) const override;
393 
399  virtual void parsimUnpack(cCommBuffer *buffer) override;
401 
410 
417  virtual void extractFromNetwork(bool (*selfunc)(cModule *,void *), void *userdata=nullptr);
418 
422  virtual void extractFromNetwork(Predicate *predicate);
423 
433  virtual void extractByModulePath(const std::vector<std::string>& fullPathPatterns);
434 
445  virtual void extractByNedTypeName(const std::vector<std::string>& nedTypeNames);
446 
465  virtual void extractByProperty(const char *propertyName, const char *value=nullptr);
466 
473  virtual void extractByParameter(const char *paramName, const char *paramValue=nullptr);
474 
478  virtual void clear();
480 
488  virtual int addNode(Node *node);
489 
494  virtual void deleteNode(Node *node);
495 
501  virtual void addLink(Link *link, Node *srcNode, Node *destNode);
502 
510  virtual void addLink(Link *link, cGate *srcGate, cGate *destGate);
511 
516  virtual void deleteLink(Link *link);
518 
519 
526 
530  virtual int getNumNodes() const {return nodes.size();}
531 
536  virtual Node *getNode(int i);
537 
545  virtual Node *getNodeFor(cModule *mod);
547 
549  /*
550  * To be implemented:
551  * - void unweightedMultiShortestPathsTo(Node *target);
552  * - void weightedMultiShortestPathsTo(Node *target);
553  */
555 
560  virtual void calculateUnweightedSingleShortestPathsTo(Node *target);
561 
567  virtual void calculateWeightedSingleShortestPathsTo(Node *target);
568 
573  virtual Node *getTargetNode() const {return target;}
575 
576  protected:
580  virtual Node *createNode(cModule *module) { return new Node(module->getId()); }
581 
585  virtual Link *createLink() { return new Link(); }
586 };
587 
588 } // namespace omnetpp
589 
590 #endif
Supporting class for cTopology, represents a node in the graph.
Definition: ctopology.h:64
Node * getLocalNode() const
Definition: ctopology.h:300
bool isEnabled() const
Definition: ctopology.h:115
Represents a module gate.
Definition: cgate.h:63
cModule * getModule() const
Definition: ctopology.h:97
int getNumInLinks() const
Definition: ctopology.h:136
cModule * getModule(int id) const
Definition: csimulation.h:215
int getRemoteGateId() const
Definition: ctopology.h:259
void setWeight(double d)
Definition: ctopology.h:109
virtual cTopology * dup() const override
Definition: ctopology.h:379
virtual Link * createLink()
Definition: ctopology.h:585
virtual Node * getTargetNode() const
Definition: ctopology.h:573
int getId() const
Definition: ccomponent.h:363
LinkOut * getPath(int) const
Definition: ctopology.h:173
double getDistanceToTarget() const
Definition: ctopology.h:160
int getLocalGateId() const
Definition: ctopology.h:310
This class represents modules in the simulation.
Definition: cmodule.h:47
cGate * getRemoteGate() const
Definition: ctopology.h:269
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
virtual Node * createNode(cModule *module)
Definition: ctopology.h:580
Routing support. The cTopology class was designed primarily to support routing in telecommunication o...
Definition: ctopology.h:54
Base class for selector objects used in extract...() methods of cTopology.
Definition: ctopology.h:329
virtual int getNumNodes() const
Definition: ctopology.h:530
cGate * getLocalGate() const
Definition: ctopology.h:320
Definition: cabstracthistogram.h:21
Node * getRemoteNode() const
Definition: ctopology.h:249
int getModuleId() const
Definition: ctopology.h:92
Node(int moduleId=-1)
Definition: ctopology.h:83
Supporting class for cTopology.
Definition: ctopology.h:286
Supporting class for cTopology.
Definition: ctopology.h:240
Node * getRemoteNode() const
Definition: ctopology.h:295
cGate * getLocalGate() const
Definition: ctopology.h:274
void enable()
Definition: ctopology.h:121
int getRemoteGateId() const
Definition: ctopology.h:305
int getNumOutLinks() const
Definition: ctopology.h:146
Node * getLocalNode() const
Definition: ctopology.h:254
cGate * getRemoteGate() const
Definition: ctopology.h:315
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:575
int getNumPaths() const
Definition: ctopology.h:166
void disable()
Definition: ctopology.h:127
int getLocalGateId() const
Definition: ctopology.h:264
double getWeight() const
Definition: ctopology.h:103