OMNeT++ Simulation Library  6.0.3
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 = nullptr;
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  private:
348  virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
349  virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
350 
351  public:
354 
358  explicit cTopology(const char *name=nullptr);
359 
363  cTopology(const cTopology& topo);
364 
368  virtual ~cTopology();
369 
373  cTopology& operator=(const cTopology& topo);
375 
378 
383  virtual cTopology *dup() const override {return new cTopology(*this);}
384 
389  virtual std::string str() const override;
391 
400 
407  virtual void extractFromNetwork(bool (*selfunc)(cModule *,void *), void *userdata=nullptr);
408 
412  virtual void extractFromNetwork(Predicate *predicate);
413 
423  virtual void extractByModulePath(const std::vector<std::string>& fullPathPatterns);
424 
435  virtual void extractByNedTypeName(const std::vector<std::string>& nedTypeNames);
436 
455  virtual void extractByProperty(const char *propertyName, const char *value=nullptr);
456 
463  virtual void extractByParameter(const char *paramName, const char *paramValue=nullptr);
464 
468  virtual void clear();
470 
478  virtual int addNode(Node *node);
479 
484  virtual void deleteNode(Node *node);
485 
491  virtual void addLink(Link *link, Node *srcNode, Node *destNode);
492 
500  virtual void addLink(Link *link, cGate *srcGate, cGate *destGate);
501 
506  virtual void deleteLink(Link *link);
508 
509 
516 
520  virtual int getNumNodes() const {return nodes.size();}
521 
526  virtual Node *getNode(int i) const;
527 
535  virtual Node *getNodeFor(cModule *mod) const;
537 
539  /*
540  * To be implemented:
541  * - void unweightedMultiShortestPathsTo(Node *target);
542  * - void weightedMultiShortestPathsTo(Node *target);
543  */
545 
550  virtual void calculateUnweightedSingleShortestPathsTo(Node *target);
551 
557  virtual void calculateWeightedSingleShortestPathsTo(Node *target);
558 
563  virtual Node *getTargetNode() const {return target;}
565 
566  protected:
570  virtual Node *createNode(cModule *module) { return new Node(module->getId()); }
571 
575  virtual Link *createLink() { return new Link(); }
576 };
577 
578 } // namespace omnetpp
579 
580 #endif
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cTopology::createLink
virtual Link * createLink()
Definition: ctopology.h:575
omnetpp::cTopology::Node::getNumOutLinks
int getNumOutLinks() const
Definition: ctopology.h:146
omnetpp::cTopology::Node::getNumPaths
int getNumPaths() const
Definition: ctopology.h:166
omnetpp::cTopology::LinkOut
Supporting class for cTopology.
Definition: ctopology.h:286
omnetpp::cTopology::LinkOut::getRemoteGateId
int getRemoteGateId() const
Definition: ctopology.h:305
omnetpp::cTopology::LinkIn
Supporting class for cTopology.
Definition: ctopology.h:240
omnetpp::cTopology::LinkOut::getRemoteGate
cGate * getRemoteGate() const
Definition: ctopology.h:315
omnetpp::cTopology::Node::getWeight
double getWeight() const
Definition: ctopology.h:103
omnetpp::cTopology::Node::getNumInLinks
int getNumInLinks() const
Definition: ctopology.h:136
omnetpp::cTopology::Node::getModule
cModule * getModule() const
Definition: ctopology.h:97
omnetpp::cTopology::Node
Supporting class for cTopology, represents a node in the graph.
Definition: ctopology.h:64
omnetpp::cTopology::getNumNodes
virtual int getNumNodes() const
Definition: ctopology.h:520
omnetpp::cTopology::Node::getPath
LinkOut * getPath(int) const
Definition: ctopology.h:173
omnetpp::cTopology::dup
virtual cTopology * dup() const override
Definition: ctopology.h:383
omnetpp::cSimulation::getModule
cModule * getModule(int id) const
Definition: csimulation.h:235
omnetpp::cTopology::LinkIn::getRemoteGateId
int getRemoteGateId() const
Definition: ctopology.h:259
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:603
omnetpp::cGate
Represents a module gate.
Definition: cgate.h:62
omnetpp::cTopology::LinkOut::getLocalGateId
int getLocalGateId() const
Definition: ctopology.h:310
omnetpp::cTopology::Node::disable
void disable()
Definition: ctopology.h:127
omnetpp::cTopology::Node::Node
Node(int moduleId=-1)
Definition: ctopology.h:83
omnetpp::cTopology::LinkOut::getRemoteNode
Node * getRemoteNode() const
Definition: ctopology.h:295
omnetpp::cTopology::Node::enable
void enable()
Definition: ctopology.h:121
omnetpp::cTopology::createNode
virtual Node * createNode(cModule *module)
Definition: ctopology.h:570
omnetpp::cComponent::getId
int getId() const
Definition: ccomponent.h:433
omnetpp::cTopology::LinkOut::getLocalGate
cGate * getLocalGate() const
Definition: ctopology.h:320
omnetpp::cTopology::getTargetNode
virtual Node * getTargetNode() const
Definition: ctopology.h:563
omnetpp::cTopology::Predicate
Base class for selector objects used in extract...() methods of cTopology.
Definition: ctopology.h:329
omnetpp::cTopology::Node::getDistanceToTarget
double getDistanceToTarget() const
Definition: ctopology.h:160
omnetpp::cTopology
Routing support. The cTopology class was designed primarily to support routing in telecommunication o...
Definition: ctopology.h:54
omnetpp::cTopology::LinkIn::getRemoteNode
Node * getRemoteNode() const
Definition: ctopology.h:249
omnetpp::cTopology::LinkIn::getLocalNode
Node * getLocalNode() const
Definition: ctopology.h:254
omnetpp::cTopology::LinkIn::getLocalGateId
int getLocalGateId() const
Definition: ctopology.h:264
omnetpp::cTopology::LinkIn::getRemoteGate
cGate * getRemoteGate() const
Definition: ctopology.h:269
omnetpp::cTopology::LinkIn::getLocalGate
cGate * getLocalGate() const
Definition: ctopology.h:274
omnetpp::cTopology::Node::isEnabled
bool isEnabled() const
Definition: ctopology.h:115
omnetpp::cTopology::Node::getModuleId
int getModuleId() const
Definition: ctopology.h:92
omnetpp::cTopology::LinkOut::getLocalNode
Node * getLocalNode() const
Definition: ctopology.h:300
omnetpp::cTopology::Node::setWeight
void setWeight(double d)
Definition: ctopology.h:109
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105