A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
orbis-topology-reader.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella (tommaso.pecorella@unifi.it)
7 * Author: Valerio Sartini (valesar@gmail.com)
8 */
9
11
12#include "ns3/log.h"
13#include "ns3/names.h"
14#include "ns3/node-container.h"
15
16#include <cstdlib>
17#include <fstream>
18#include <sstream>
19
20/**
21 * \file
22 * \ingroup topology
23 * ns3::OrbisTopologyReader implementation.
24 */
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("OrbisTopologyReader");
30
31NS_OBJECT_ENSURE_REGISTERED(OrbisTopologyReader);
32
33TypeId
35{
36 static TypeId tid = TypeId("ns3::OrbisTopologyReader")
38 .SetGroupName("TopologyReader")
39 .AddConstructor<OrbisTopologyReader>();
40 return tid;
41}
42
47
52
55{
56 std::ifstream topgen;
57 topgen.open(GetFileName());
58 std::map<std::string, Ptr<Node>> nodeMap;
60
61 if (!topgen.is_open())
62 {
63 return nodes;
64 }
65
66 std::string from;
67 std::string to;
68 std::istringstream lineBuffer;
69 std::string line;
70
71 int linksNumber = 0;
72 int nodesNumber = 0;
73
74 while (!topgen.eof())
75 {
76 line.clear();
77 lineBuffer.clear();
78 from.clear();
79 to.clear();
80
81 getline(topgen, line);
82 lineBuffer.str(line);
83 lineBuffer >> from;
84 lineBuffer >> to;
85
86 if ((!from.empty()) && (!to.empty()))
87 {
88 NS_LOG_INFO(linksNumber << " From: " << from << " to: " << to);
89 if (!nodeMap[from])
90 {
91 Ptr<Node> tmpNode = CreateObject<Node>();
92 std::string nodename = "OrbisTopology/NodeName/" + from;
93 Names::Add(nodename, tmpNode);
94 nodeMap[from] = tmpNode;
95 nodes.Add(tmpNode);
96 nodesNumber++;
97 }
98
99 if (!nodeMap[to])
100 {
101 Ptr<Node> tmpNode = CreateObject<Node>();
102 std::string nodename = "OrbisTopology/NodeName/" + to;
103 Names::Add(nodename, tmpNode);
104 nodeMap[to] = tmpNode;
105 nodes.Add(tmpNode);
106 nodesNumber++;
107 }
108
109 Link link(nodeMap[from], from, nodeMap[to], to);
110 AddLink(link);
111
112 linksNumber++;
113 }
114 }
115 NS_LOG_INFO("Orbis topology created with " << nodesNumber << " nodes and " << linksNumber
116 << " links");
117 topgen.close();
118
119 return nodes;
120}
121
122} /* namespace ns3 */
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
keep track of a set of node pointers.
Topology file reader (Orbis-format type).
static TypeId GetTypeId()
Get the type ID.
NodeContainer Read() override
Main topology reading function.
Smart pointer class similar to boost::intrusive_ptr.
Interface for input file readers management.
void AddLink(Link link)
Adds a link to the topology.
std::string GetFileName() const
Returns the input file name.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::OrbisTopologyReader declaration.