INET Framework for OMNeT++/OMNEST
inet::Topology Class Reference

Routing support. More...

#include <Topology.h>

Inheritance diagram for inet::Topology:
inet::L2NetworkConfigurator::L2Topology inet::NetworkConfiguratorBase::Topology inet::IPv4NetworkConfigurator::Topology

Classes

class  Link
 Supporting class for Topology, represents a link in the graph. More...
 
class  LinkIn
 Supporting class for Topology. More...
 
class  LinkOut
 Supporting class for Topology. More...
 
class  Node
 Supporting class for Topology, represents a node in the graph. More...
 
class  Predicate
 Base class for selector objects used in extract...() methods of Topology. More...
 

Public Member Functions

Constructors, destructor, assignment
 Topology (const char *name=nullptr)
 Constructor. More...
 
 Topology (const Topology &topo)
 Copy constructor. More...
 
virtual ~Topology ()
 Destructor. More...
 
Topologyoperator= (const Topology &topo)
 Assignment operator. More...
 
Redefined cObject member functions.
virtual Topologydup () const override
 Creates and returns an exact copy of this object. More...
 
virtual std::string info () const override
 Produces a one-line description of the object's contents. More...
 
virtual void parsimPack (cCommBuffer *buffer) const override
 Serializes the object into an MPI send buffer. More...
 
virtual void parsimUnpack (cCommBuffer *buffer) override
 Deserializes the object from an MPI receive buffer Used by the simulation kernel for parallel execution. More...
 
Extracting the topology from a network.

extract...() functions build topology from the model.

User can select which modules to include. All connections between those modules will be in the topology. Connections can cross compound module boundaries.

void extractFromNetwork (bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
 Extracts model topology by a user-defined criteria. More...
 
void extractFromNetwork (Predicate *predicate)
 The type safe, object-oriented equivalent of extractFromNetwork(selfunc, userdata). More...
 
void extractByModulePath (const std::vector< std::string > &fullPathPatterns)
 Extracts model topology by module full path. More...
 
void extractByNedTypeName (const std::vector< std::string > &nedTypeNames)
 Extracts model topology by the fully qualified NED type name of the modules. More...
 
void extractByProperty (const char *propertyName, const char *value=nullptr)
 Extracts model topology by a module property. More...
 
void extractByParameter (const char *paramName, const char *paramValue=nullptr)
 Extracts model topology by a module parameter. More...
 
void clear ()
 Deletes the topology stored in the object. More...
 
Manipulating the graph.
int addNode (Node *node)
 Adds the given node to the graph. More...
 
void deleteNode (Node *node)
 Removes the given node from the graph, together with all of its links. More...
 
void addLink (Link *link, Node *srcNode, Node *destNode)
 TODO Note: also serves as reconnectLink() More...
 
void addLink (Link *link, cGate *srcGate, cGate *destGate)
 TODO Note: also serves as reconnectLink() More...
 
void deleteLink (Link *link)
 Removes the given link from the graph. More...
 
Functions to examine topology by hand.

Users also need to rely on Node and Link member functions to explore the graph stored in the object.

int getNumNodes () const
 Returns the number of nodes in the graph. More...
 
NodegetNode (int i)
 Returns pointer to the ith node in the graph. More...
 
NodegetNodeFor (cModule *mod)
 Returns the graph node which corresponds to the given module in the network. More...
 
Algorithms to find shortest paths.
void calculateUnweightedSingleShortestPathsTo (Node *target)
 Apply the Dijkstra algorithm to find all shortest paths to the given graph node. More...
 
void calculateWeightedSingleShortestPathsTo (Node *target)
 Apply the Dijkstra algorithm to find all shortest paths to the given graph node. More...
 
NodegetTargetNode () const
 Returns the node that was passed to the most recently called shortest path finding function. More...
 

Protected Member Functions

void unlinkFromSourceNode (Link *link)
 
void unlinkFromDestNode (Link *link)
 
virtual NodecreateNode (cModule *module)
 Node factory. More...
 
virtual LinkcreateLink ()
 Link factory. More...
 

Static Protected Member Functions

static bool lessByModuleId (Node *a, Node *b)
 
static bool isModuleIdLess (Node *a, int moduleId)
 

Protected Attributes

std::vector< Node * > nodes
 
Nodetarget
 

Detailed Description

Routing support.

The Topology class was designed primarily to support routing in telecommunication or multiprocessor networks.

A Topology object stores an abstract representation of the network in graph form:

  • each Topology node corresponds to a module (simple or compound), and
  • each Topology edge corresponds to a link or series of connecting links.

You can specify which modules (either simple or compound) you want to include in the graph. The graph will include all connections among the selected modules. In the graph, all nodes are at the same level, there is no submodule nesting. Connections which span across compound module boundaries are also represented as one graph edge. Graph edges are directed, just as module gates are.

See also
Topology::Node, Topology::Link, Topology::LinkIn, Topology::LinkOut

Constructor & Destructor Documentation

inet::Topology::Topology ( const char *  name = nullptr)
explicit

Constructor.

49  : cOwnedObject(name)
50 {
51  target = nullptr;
52 }
Node * target
Definition: Topology.h:334
inet::Topology::Topology ( const Topology topo)

Copy constructor.

54  : cOwnedObject(topo)
55 {
56  throw cRuntimeError(this, "copy ctor not implemented yet");
57 }
inet::Topology::~Topology ( )
virtual

Destructor.

Reimplemented in inet::NetworkConfiguratorBase::Topology.

60 {
61  clear();
62 }
void clear()
Deletes the topology stored in the object.
Definition: Topology.cc:86

Member Function Documentation

void inet::Topology::addLink ( Link link,
Node srcNode,
Node destNode 
)

TODO Note: also serves as reconnectLink()

Referenced by inet::NetworkConfiguratorBase::extractTopology().

292 {
293  // remove from graph if it's already in
294  if (link->srcNode)
295  unlinkFromSourceNode(link);
296  if (link->destNode)
297  unlinkFromDestNode(link);
298 
299  // insert
300  if (link->srcNode != srcNode)
301  link->srcGateId = -1;
302  if (link->destNode != destNode)
303  link->destGateId = -1;
304  link->srcNode = srcNode;
305  link->destNode = destNode;
306  srcNode->outLinks.push_back(link);
307  destNode->inLinks.push_back(link);
308 }
void unlinkFromSourceNode(Link *link)
Definition: Topology.cc:340
void unlinkFromDestNode(Link *link)
Definition: Topology.cc:348
void inet::Topology::addLink ( Link link,
cGate *  srcGate,
cGate *  destGate 
)

TODO Note: also serves as reconnectLink()

311 {
312  // remove from graph if it's already in
313  if (link->srcNode)
314  unlinkFromSourceNode(link);
315  if (link->destNode)
316  unlinkFromDestNode(link);
317 
318  // insert
319  Node *srcNode = getNodeFor(srcGate->getOwnerModule());
320  Node *destNode = getNodeFor(destGate->getOwnerModule());
321  if (!srcNode)
322  throw cRuntimeError("cTopology::addLink: module of source gate \"%s\" is not in the graph", srcGate->getFullPath().c_str());
323  if (!destNode)
324  throw cRuntimeError("cTopology::addLink: module of destination gate \"%s\" is not in the graph", destGate->getFullPath().c_str());
325  link->srcNode = srcNode;
326  link->destNode = destNode;
327  link->srcGateId = srcGate->getId();
328  link->destGateId = destGate->getId();
329  srcNode->outLinks.push_back(link);
330  destNode->inLinks.push_back(link);
331 }
std::vector< Link * > outLinks
Definition: Topology.h:79
void unlinkFromSourceNode(Link *link)
Definition: Topology.cc:340
Node * getNodeFor(cModule *mod)
Returns the graph node which corresponds to the given module in the network.
Definition: Topology.cc:363
void unlinkFromDestNode(Link *link)
Definition: Topology.cc:348
int inet::Topology::addNode ( Node node)

Adds the given node to the graph.

Returns the index of the new graph node (see getNode(int)). Indices of existing graph nodes may change.

251 {
252  if (node->moduleId == -1) {
253  // elements without module ID are stored at the end
254  nodes.push_back(node);
255  return nodes.size() - 1;
256  }
257  else {
258  // must find an insertion point because nodes[] is ordered by module ID
259  auto it = std::lower_bound(nodes.begin(), nodes.end(), node, lessByModuleId);
260  it = nodes.insert(it, node);
261  return it - nodes.begin();
262  }
263 }
std::vector< Node * > nodes
Definition: Topology.h:333
static bool lessByModuleId(Node *a, Node *b)
Definition: Topology.h:337
void inet::Topology::calculateUnweightedSingleShortestPathsTo ( Node target)

Apply the Dijkstra algorithm to find all shortest paths to the given graph node.

The paths found can be extracted via Node's methods.

373 {
374  // multiple paths not supported :-(
375 
376  if (!_target)
377  throw cRuntimeError(this, "..ShortestPathTo(): target node is nullptr");
378  target = _target;
379 
380  for (auto & elem : nodes) {
381  elem->dist = INFINITY;
382  elem->outPath = nullptr;
383  }
384  target->dist = 0;
385 
386  std::deque<Node *> q;
387 
388  q.push_back(target);
389 
390  while (!q.empty()) {
391  Node *v = q.front();
392  q.pop_front();
393 
394  // for each w adjacent to v...
395  for (int i = 0; i < (int)v->inLinks.size(); i++) {
396  if (!v->inLinks[i]->enabled)
397  continue;
398 
399  Node *w = v->inLinks[i]->srcNode;
400  if (!w->enabled)
401  continue;
402 
403  if (w->dist == INFINITY) {
404  w->dist = v->dist + 1;
405  w->outPath = v->inLinks[i];
406  q.push_back(w);
407  }
408  }
409  }
410 }
#define INFINITY
Definition: Topology.h:29
double dist
Definition: Topology.h:82
Node * target
Definition: Topology.h:334
std::vector< Node * > nodes
Definition: Topology.h:333
void inet::Topology::calculateWeightedSingleShortestPathsTo ( Node target)

Apply the Dijkstra algorithm to find all shortest paths to the given graph node.

The paths found can be extracted via Node's methods. Uses weights in nodes and links.

Referenced by inet::GenericNetworkConfigurator::addStaticRoutes(), and inet::IPv4NetworkConfigurator::addStaticRoutes().

413 {
414  if (!_target)
415  throw cRuntimeError(this, "..ShortestPathTo(): target node is nullptr");
416  target = _target;
417 
418  // clean path infos
419  for (auto & elem : nodes) {
420  elem->dist = INFINITY;
421  elem->outPath = nullptr;
422  }
423 
424  target->dist = 0;
425 
426  std::list<Node *> q;
427 
428  q.push_back(target);
429 
430  while (!q.empty()) {
431  Node *dest = q.front();
432  q.pop_front();
433 
434  ASSERT(dest->getWeight() >= 0.0);
435 
436  // for each w adjacent to v...
437  for (int i = 0; i < dest->getNumInLinks(); i++) {
438  if (!(dest->getLinkIn(i)->isEnabled()))
439  continue;
440 
441  Node *src = dest->getLinkIn(i)->getRemoteNode();
442  if (!src->isEnabled())
443  continue;
444 
445  double linkWeight = dest->getLinkIn(i)->getWeight();
446 
447  // links with linkWeight == 0 might induce circles
448  ASSERT(linkWeight > 0.0);
449 
450  double newdist = dest->dist + linkWeight;
451  if (dest != target)
452  newdist += dest->getWeight(); // dest is not the target, uses weight of dest node as price of routing (infinity means dest node doesn't route between interfaces)
453  if (newdist != INFINITY && src->dist > newdist) { // it's a valid shorter path from src to target node
454  if (src->dist != INFINITY)
455  q.remove(src); // src is in the queue
456  src->dist = newdist;
457  src->outPath = dest->inLinks[i];
458 
459  // insert src node to ordered list
460  auto it = q.begin();
461  for ( ; it != q.end(); ++it)
462  if ((*it)->dist > newdist)
463  break;
464 
465  q.insert(it, src);
466  }
467  }
468  }
469 }
#define INFINITY
Definition: Topology.h:29
double dist
Definition: Topology.h:82
Node * target
Definition: Topology.h:334
std::vector< Node * > nodes
Definition: Topology.h:333
void inet::Topology::clear ( )

Deletes the topology stored in the object.

Referenced by inet::IPv4NetworkConfigurator::computeConfiguration(), extractFromNetwork(), and ~Topology().

87 {
88  for (auto & elem : nodes) {
89  for (auto & _j : elem->outLinks)
90  delete _j; // delete links from their source side
91  delete elem;
92  }
93  nodes.clear();
94 }
std::vector< Node * > nodes
Definition: Topology.h:333
virtual Link* inet::Topology::createLink ( )
inlineprotectedvirtual

Link factory.

Reimplemented in inet::NetworkConfiguratorBase::Topology, and inet::L2NetworkConfigurator::L2Topology.

Referenced by extractFromNetwork().

577 { return new Link(); }
virtual Node* inet::Topology::createNode ( cModule *  module)
inlineprotectedvirtual

Node factory.

Reimplemented in inet::NetworkConfiguratorBase::Topology, inet::L2NetworkConfigurator::L2Topology, and inet::IPv4NetworkConfigurator::Topology.

Referenced by extractFromNetwork().

572 { return new Node(module->getId()); }
void inet::Topology::deleteLink ( Link link)

Removes the given link from the graph.

Indices of existing links in the source and destination nodes may change.

334 {
335  unlinkFromSourceNode(link);
336  unlinkFromDestNode(link);
337  delete link;
338 }
void unlinkFromSourceNode(Link *link)
Definition: Topology.cc:340
void unlinkFromDestNode(Link *link)
Definition: Topology.cc:348
void inet::Topology::deleteNode ( Node node)

Removes the given node from the graph, together with all of its links.

Indices of existing graph nodes may change.

266 {
267  // remove outgoing links
268  for (auto & elem : node->outLinks) {
269  Link *link = elem;
270  unlinkFromDestNode(link);
271  delete link;
272  }
273  node->outLinks.clear();
274 
275  // remove incoming links
276  for (auto & elem : node->inLinks) {
277  Link *link = elem;
278  unlinkFromSourceNode(link);
279  delete link;
280  }
281  node->inLinks.clear();
282 
283  // remove from nodes[]
284  auto it = find(nodes, node);
285  ASSERT(it != nodes.end());
286  nodes.erase(it);
287 
288  delete node;
289 }
void unlinkFromSourceNode(Link *link)
Definition: Topology.cc:340
void unlinkFromDestNode(Link *link)
Definition: Topology.cc:348
std::vector< Node * > nodes
Definition: Topology.h:333
std::vector< T >::iterator find(std::vector< T > &v, const T &a)
Definition: stlutils.h:48
virtual Topology* inet::Topology::dup ( ) const
inlineoverridevirtual

Creates and returns an exact copy of this object.

See cObject for more details.

375 { return new Topology(*this); }
Topology(const char *name=nullptr)
Constructor.
Definition: Topology.cc:49
void inet::Topology::extractByModulePath ( const std::vector< std::string > &  fullPathPatterns)

Extracts model topology by module full path.

All modules whole getFullPath() matches one of the patterns in given string vector will get included. The patterns may contain wilcards in the same syntax as in ini files.

An example:

topo.extractByModulePath(cStringTokenizer("**.host[*] **.router*").asVector());

150 {
151  extractFromNetwork(selectByModulePath, (void *)&fullPathPatterns);
152 }
void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
Extracts model topology by a user-defined criteria.
Definition: Topology.cc:196
void inet::Topology::extractByNedTypeName ( const std::vector< std::string > &  nedTypeNames)

Extracts model topology by the fully qualified NED type name of the modules.

All modules whose getNedTypeName() is listed in the given string vector will get included.

Note: If you have all class names as a single, space-separated string, you can use cStringTokenizer to turn it into a string vector:

topo.extractByNedTypeName(cStringTokenizer("some.package.Host other.package.Router").asVector());

155 {
156  extractFromNetwork(selectByNedTypeName, (void *)&nedTypeNames);
157 }
void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
Extracts model topology by a user-defined criteria.
Definition: Topology.cc:196
void inet::Topology::extractByParameter ( const char *  paramName,
const char *  paramValue = nullptr 
)

Extracts model topology by a module parameter.

All modules get included that have a parameter with the given name, and the parameter's str() method returns the paramValue string. If paramValue is nullptr, only the parameter's existence is checked but not its value.

172 {
173  struct
174  {
175  const char *name;
176  const char *value;
177  } data = {
178  paramName, paramValue
179  };
180  extractFromNetwork(selectByParameter, (void *)&data);
181 }
void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
Extracts model topology by a user-defined criteria.
Definition: Topology.cc:196
void inet::Topology::extractByProperty ( const char *  propertyName,
const char *  value = nullptr 
)

Extracts model topology by a module property.

All modules get included that have a property with the given name and the given value (more precisely, the first value of its default key being the specified value). If value is nullptr, the property's value may be anything except "false" (i.e. the first value of the default key may not be "false").

For example, topo.extractByProperty("networkNode"); would extract all modules that contain the @networkNode property, like the following one:

module X {
    @networkNode;
}

Referenced by inet::STPTester::depthFirstSearch(), inet::L2NetworkConfigurator::extractTopology(), and inet::NetworkConfiguratorBase::extractTopology().

160 {
161  struct
162  {
163  const char *name;
164  const char *value;
165  } data = {
166  propertyName, value
167  };
168  extractFromNetwork(selectByProperty, (void *)&data);
169 }
void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
Extracts model topology by a user-defined criteria.
Definition: Topology.cc:196
void inet::Topology::extractFromNetwork ( bool(*)(cModule *, void *)  selfunc,
void *  userdata = nullptr 
)

Extracts model topology by a user-defined criteria.

Includes into the graph modules for which the passed selfunc() returns nonzero. The userdata parameter may take any value you like, and it is passed back to selfunc() in its second argument.

Referenced by extractByModulePath(), extractByNedTypeName(), extractByParameter(), extractByProperty(), and extractFromNetwork().

197 {
198  clear();
199 
200  // Loop through all modules and find those that satisfy the criteria
201  for (int modId = 0; modId <= getSimulation()->getLastComponentId(); modId++)
202  {
203  cModule *module = getSimulation()->getModule(modId);
204  if (module && predicate(module, data)) {
205  Node *node = createNode(module);
206  nodes.push_back(node);
207  }
208  }
209 
210  // Discover out neighbors too.
211  for (auto & elem : nodes) {
212  // Loop through all its gates and find those which come
213  // from or go to modules included in the topology.
214 
215  Node *node = elem;
216  cModule *mod = getSimulation()->getModule(node->moduleId);
217 
218  for (cModule::GateIterator i(mod); !i.end(); i++) {
219  cGate *gate = *i;
220  if (gate->getType() != cGate::OUTPUT)
221  continue;
222 
223  // follow path
224  cGate *srcGate = gate;
225  do {
226  gate = gate->getNextGate();
227  } while (gate && !predicate(gate->getOwnerModule(), data));
228 
229  // if we arrived at a module in the topology, record it.
230  if (gate) {
231  Link *link = createLink();
232  link->srcNode = node;
233  link->srcGateId = srcGate->getId();
234  link->destNode = getNodeFor(gate->getOwnerModule());
235  link->destGateId = gate->getId();
236  node->outLinks.push_back(link);
237  }
238  }
239  }
240 
241  // fill inLinks vectors
242  for (auto & elem : nodes) {
243  for (auto & _l : elem->outLinks) {
244  Topology::Link *link = _l;
245  link->destNode->inLinks.push_back(link);
246  }
247  }
248 }
std::vector< Link * > outLinks
Definition: Topology.h:79
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
Node * getNodeFor(cModule *mod)
Returns the graph node which corresponds to the given module in the network.
Definition: Topology.cc:363
std::vector< Node * > nodes
Definition: Topology.h:333
virtual Link * createLink()
Link factory.
Definition: Topology.h:577
virtual Node * createNode(cModule *module)
Node factory.
Definition: Topology.h:572
void clear()
Deletes the topology stored in the object.
Definition: Topology.cc:86
void inet::Topology::extractFromNetwork ( Predicate predicate)

The type safe, object-oriented equivalent of extractFromNetwork(selfunc, userdata).

192 {
193  extractFromNetwork(selectByPredicate, (void *)predicate);
194 }
void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
Extracts model topology by a user-defined criteria.
Definition: Topology.cc:196
Topology::Node * inet::Topology::getNodeFor ( cModule *  mod)

Returns the graph node which corresponds to the given module in the network.

If no graph node corresponds to the module, the method returns nullptr. This method assumes that the topology corresponds to the network, that is, it was probably created with one of the extract...() functions.

Referenced by addLink(), and extractFromNetwork().

364 {
365  // binary search because nodes[] is ordered by module ID
366  Node tmpNode(mod->getId());
367  auto it = std::lower_bound(nodes.begin(), nodes.end(), &tmpNode, lessByModuleId);
368 //TODO: this does not compile with VC9 (VC10 is OK): auto it = std::lower_bound(nodes.begin(), nodes.end(), mod->getId(), isModuleIdLess);
369  return it == nodes.end() || (*it)->moduleId != mod->getId() ? nullptr : *it;
370 }
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
std::vector< Node * > nodes
Definition: Topology.h:333
static bool lessByModuleId(Node *a, Node *b)
Definition: Topology.h:337
Node* inet::Topology::getTargetNode ( ) const
inline

Returns the node that was passed to the most recently called shortest path finding function.

565 { return target; }
Node * target
Definition: Topology.h:334
std::string inet::Topology::info ( ) const
overridevirtual

Produces a one-line description of the object's contents.

See cObject for more details.

65 {
66  std::stringstream out;
67  out << "n=" << nodes.size();
68  return out.str();
69 }
std::vector< Node * > nodes
Definition: Topology.h:333
static bool inet::Topology::isModuleIdLess ( Node a,
int  moduleId 
)
inlinestaticprotected
338 { return (unsigned int)a->moduleId < (unsigned int)moduleId; }
static bool inet::Topology::lessByModuleId ( Node a,
Node b 
)
inlinestaticprotected

Referenced by addNode(), and getNodeFor().

337 { return (unsigned int)a->moduleId < (unsigned int)b->moduleId; }
value< double, units::m > b
Definition: Units.h:1054
Topology & inet::Topology::operator= ( const Topology topo)

Assignment operator.

The name member is not copied; see cNamedObject's operator=() for more details.

82 {
83  throw cRuntimeError(this, "operator= not implemented yet");
84 }
void inet::Topology::parsimPack ( cCommBuffer *  buffer) const
overridevirtual

Serializes the object into an MPI send buffer.

Used by the simulation kernel for parallel execution. See cObject for more details.

72 {
73  throw cRuntimeError(this, "parsimPack() not implemented");
74 }
void inet::Topology::parsimUnpack ( cCommBuffer *  buffer)
overridevirtual

Deserializes the object from an MPI receive buffer Used by the simulation kernel for parallel execution.

See cObject for more details.

77 {
78  throw cRuntimeError(this, "parsimUnpack() not implemented");
79 }
void inet::Topology::unlinkFromDestNode ( Link link)
protected

Referenced by addLink(), deleteLink(), and deleteNode().

349 {
350  std::vector<Link *>& destInLinks = link->destNode->inLinks;
351  auto it = find(destInLinks, link);
352  ASSERT(it != destInLinks.end());
353  destInLinks.erase(it);
354 }
std::vector< T >::iterator find(std::vector< T > &v, const T &a)
Definition: stlutils.h:48
void inet::Topology::unlinkFromSourceNode ( Link link)
protected

Referenced by addLink(), deleteLink(), and deleteNode().

341 {
342  std::vector<Link *>& srcOutLinks = link->srcNode->outLinks;
343  auto it = find(srcOutLinks, link);
344  ASSERT(it != srcOutLinks.end());
345  srcOutLinks.erase(it);
346 }
std::vector< T >::iterator find(std::vector< T > &v, const T &a)
Definition: stlutils.h:48

Member Data Documentation


The documentation for this class was generated from the following files: