OMNeT++ API 6.2.0
Discrete Event Simulation Library
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 <functional>
22 #include "cownedobject.h"
23 #include "csimulation.h"
24 #include "cmodule.h"
25 #include "cgate.h"
26 
27 namespace omnetpp {
28 
29 class cPar;
30 
31 // not all compilers define INFINITY (gcc does)
32 #ifndef INFINITY
33 #define INFINITY HUGE_VAL
34 #endif
35 
55 class SIM_API cTopology : public cOwnedObject
56 {
57  public:
58  class Link;
59  class LinkIn;
60  class LinkOut;
61 
65  class SIM_API Node
66  {
67  friend class cTopology;
68 
69  protected:
70  int moduleId;
71  double weight;
72  bool enabled;
73  std::vector<Link*> inLinks;
74  std::vector<Link*> outLinks;
75 
76  // variables used by the shortest-path algorithms
77  double dist;
78  Link *outPath;
79 
80  public:
84  Node(int moduleId=-1) {this->moduleId=moduleId; weight=0; enabled=true; dist=INFINITY; outPath=nullptr;}
85  virtual ~Node() {}
86 
89 
93  int getModuleId() const {return moduleId;}
94 
98  cModule *getModule() const {return getSimulation()->getModule(moduleId);}
99 
104  double getWeight() const {return weight;}
105 
110  void setWeight(double d) {weight=d;}
111 
116  bool isEnabled() const {return enabled;}
117 
122  void enable() {enabled=true;}
123 
128  void disable() {enabled=false;}
130 
133 
137  int getNumInLinks() const {return inLinks.size();}
138 
142  LinkIn *getLinkIn(int i);
143 
147  int getNumOutLinks() const {return outLinks.size();}
148 
152  LinkOut *getLinkOut(int i);
154 
157 
161  double getDistanceToTarget() const {return dist;}
162 
167  int getNumPaths() const {return outPath?1:0;}
168 
174  LinkOut *getPath(int) const {return (LinkOut *)outPath;}
176  };
177 
178 
182  class SIM_API Link
183  {
184  friend class cTopology;
185 
186  protected:
187  Node *srcNode;
188  int srcGateId;
189  Node *destNode;
190  int destGateId;
191  double weight;
192  bool enabled;
193 
194  public:
198  Link(double weight=1) {srcNode=destNode=nullptr; srcGateId=destGateId=-1; this->weight=weight; enabled=true;}
199  virtual ~Link() {}
200 
205  double getWeight() const {return weight;}
206 
211  void setWeight(double d) {weight=d;}
212 
217  bool isEnabled() const {return enabled;}
218 
223  void enable() {enabled=true;}
224 
229  void disable() {enabled=false;}
230  };
231 
232 
241  class SIM_API LinkIn : public Link
242  {
243  private:
244  LinkIn(double weight=1) : Link(weight) {}
245 
246  public:
250  Node *getRemoteNode() const {return srcNode;}
251 
255  Node *getLocalNode() const {return destNode;}
256 
260  int getRemoteGateId() const {return srcGateId;}
261 
265  int getLocalGateId() const {return destGateId;}
266 
270  cGate *getRemoteGate() const {return srcNode->getModule()->gate(srcGateId);}
271 
275  cGate *getLocalGate() const {return destNode->getModule()->gate(destGateId);}
276  };
277 
278 
287  class SIM_API LinkOut : public Link
288  {
289  private:
290  LinkOut(double weight=1) : Link(weight) {}
291 
292  public:
296  Node *getRemoteNode() const {return destNode;}
297 
301  Node *getLocalNode() const {return srcNode;}
302 
306  int getRemoteGateId() const {return destGateId;}
307 
311  int getLocalGateId() const {return srcGateId;}
312 
316  cGate *getRemoteGate() const {return destNode->getModule()->gate(destGateId);}
317 
321  cGate *getLocalGate() const {return srcNode->getModule()->gate(srcGateId);}
322  };
323 
330  class SIM_API Predicate
331  {
332  public:
333  virtual ~Predicate() {}
334  virtual bool matches(cModule *module) = 0;
335  };
336 
337  protected:
338  std::vector<Node*> nodes;
339  Node *target = nullptr;
340 
341  // note: the purpose of the (unsigned int) cast is that nodes with moduleId==-1 are inserted at the end of the vector
342  static bool lessByModuleId(Node *a, Node *b) { return (unsigned int)a->moduleId < (unsigned int)b->moduleId; }
343  static bool isModuleIdLess(Node *a, int moduleId) { return (unsigned int)a->moduleId < (unsigned int)moduleId; }
344 
345  void unlinkFromSourceNode(Link *link);
346  void unlinkFromDestNode(Link *link);
347 
348  private:
349  virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
350  virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
351 
352  public:
355 
359  explicit cTopology(const char *name=nullptr);
360 
364  cTopology(const cTopology& topo);
365 
369  virtual ~cTopology();
370 
374  cTopology& operator=(const cTopology& topo);
376 
379 
384  virtual cTopology *dup() const override {return new cTopology(*this);}
385 
390  virtual std::string str() const override;
392 
401 
408  virtual void extractFromNetwork(bool (*selfunc)(cModule *,void *), void *userdata=nullptr);
409 
413  virtual void extractFromNetwork(Predicate *predicate);
414 
418  virtual void extractFromNetwork(std::function<bool(cModule *)> predicate);
419 
429  virtual void extractByModulePath(const std::vector<std::string>& fullPathPatterns);
430 
441  virtual void extractByNedTypeName(const std::vector<std::string>& nedTypeNames);
442 
461  virtual void extractByProperty(const char *propertyName, const char *value=nullptr);
462 
469  virtual void extractByParameter(const char *paramName, const char *paramValue=nullptr);
470 
474  virtual void clear();
476 
484  virtual int addNode(Node *node);
485 
490  virtual void deleteNode(Node *node);
491 
497  virtual void addLink(Link *link, Node *srcNode, Node *destNode);
498 
506  virtual void addLink(Link *link, cGate *srcGate, cGate *destGate);
507 
512  virtual void deleteLink(Link *link);
514 
515 
522 
526  virtual int getNumNodes() const {return nodes.size();}
527 
532  virtual Node *getNode(int i) const;
533 
541  virtual Node *getNodeFor(cModule *mod) const;
543 
545  /*
546  * To be implemented:
547  * - void unweightedMultiShortestPathsTo(Node *target);
548  * - void weightedMultiShortestPathsTo(Node *target);
549  */
551 
556  virtual void calculateUnweightedSingleShortestPathsTo(Node *target);
557 
563  virtual void calculateWeightedSingleShortestPathsTo(Node *target);
564 
569  virtual Node *getTargetNode() const {return target;}
571 
572  protected:
576  virtual Node *createNode(cModule *module) { return new Node(module->getId()); }
577 
581  virtual Link *createLink() { return new Link(); }
582 };
583 
584 } // namespace omnetpp
585 
586 #endif
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cTopology::createLink
virtual Link * createLink()
Definition: ctopology.h:581
omnetpp::cTopology::Node::getNumOutLinks
int getNumOutLinks() const
Definition: ctopology.h:147
omnetpp::cTopology::Node::getNumPaths
int getNumPaths() const
Definition: ctopology.h:167
omnetpp::cTopology::LinkOut
Supporting class for cTopology.
Definition: ctopology.h:287
omnetpp::cTopology::LinkOut::getRemoteGateId
int getRemoteGateId() const
Definition: ctopology.h:306
omnetpp::cTopology::LinkIn
Supporting class for cTopology.
Definition: ctopology.h:241
omnetpp::cTopology::LinkOut::getRemoteGate
cGate * getRemoteGate() const
Definition: ctopology.h:316
omnetpp::cTopology::Node::getWeight
double getWeight() const
Definition: ctopology.h:104
omnetpp::cTopology::Node::getNumInLinks
int getNumInLinks() const
Definition: ctopology.h:137
omnetpp::cTopology::Node::getModule
cModule * getModule() const
Definition: ctopology.h:98
omnetpp::cTopology::Node
Supporting class for cTopology, represents a node in the graph.
Definition: ctopology.h:65
omnetpp::cTopology::getNumNodes
virtual int getNumNodes() const
Definition: ctopology.h:526
omnetpp::cTopology::Node::getPath
LinkOut * getPath(int) const
Definition: ctopology.h:174
omnetpp::cTopology::dup
virtual cTopology * dup() const override
Definition: ctopology.h:384
omnetpp::cSimulation::getModule
cModule * getModule(int id) const
Definition: csimulation.h:240
omnetpp::cTopology::LinkIn::getRemoteGateId
int getRemoteGateId() const
Definition: ctopology.h:260
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:608
omnetpp::cGate
Represents a module gate.
Definition: cgate.h:62
omnetpp::cTopology::LinkOut::getLocalGateId
int getLocalGateId() const
Definition: ctopology.h:311
omnetpp::cTopology::Node::disable
void disable()
Definition: ctopology.h:128
omnetpp::cTopology::Node::Node
Node(int moduleId=-1)
Definition: ctopology.h:84
omnetpp::cTopology::LinkOut::getRemoteNode
Node * getRemoteNode() const
Definition: ctopology.h:296
omnetpp::cTopology::Node::enable
void enable()
Definition: ctopology.h:122
omnetpp::cTopology::createNode
virtual Node * createNode(cModule *module)
Definition: ctopology.h:576
omnetpp::cComponent::getId
int getId() const
Definition: ccomponent.h:433
omnetpp::cTopology::LinkOut::getLocalGate
cGate * getLocalGate() const
Definition: ctopology.h:321
omnetpp::cTopology::getTargetNode
virtual Node * getTargetNode() const
Definition: ctopology.h:569
omnetpp::cTopology::Predicate
Base class for selector objects used in extract...() methods of cTopology.
Definition: ctopology.h:330
omnetpp::cTopology::Node::getDistanceToTarget
double getDistanceToTarget() const
Definition: ctopology.h:161
omnetpp::cTopology
Routing support. The cTopology class was designed primarily to support routing in telecommunication o...
Definition: ctopology.h:55
omnetpp::cTopology::LinkIn::getRemoteNode
Node * getRemoteNode() const
Definition: ctopology.h:250
omnetpp::cTopology::LinkIn::getLocalNode
Node * getLocalNode() const
Definition: ctopology.h:255
omnetpp::cTopology::LinkIn::getLocalGateId
int getLocalGateId() const
Definition: ctopology.h:265
omnetpp::cTopology::LinkIn::getRemoteGate
cGate * getRemoteGate() const
Definition: ctopology.h:270
omnetpp::cTopology::LinkIn::getLocalGate
cGate * getLocalGate() const
Definition: ctopology.h:275
omnetpp::cTopology::Node::isEnabled
bool isEnabled() const
Definition: ctopology.h:116
omnetpp::cTopology::Node::getModuleId
int getModuleId() const
Definition: ctopology.h:93
omnetpp::cTopology::LinkOut::getLocalNode
Node * getLocalNode() const
Definition: ctopology.h:301
omnetpp::cTopology::Node::setWeight
void setWeight(double d)
Definition: ctopology.h:110
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