A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
inet-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::InetTopologyReader implementation.
24 */
25
26namespace ns3
27{
28NS_LOG_COMPONENT_DEFINE("InetTopologyReader");
29
30NS_OBJECT_ENSURE_REGISTERED(InetTopologyReader);
31
32TypeId
34{
35 static TypeId tid = TypeId("ns3::InetTopologyReader")
37 .SetGroupName("TopologyReader")
38 .AddConstructor<InetTopologyReader>();
39 return tid;
40}
41
46
51
54{
55 std::ifstream topgen;
56 topgen.open(GetFileName());
57 std::map<std::string, Ptr<Node>> nodeMap;
59
60 if (!topgen.is_open())
61 {
62 NS_LOG_WARN("Inet topology file object is not open, check file name and permissions");
63 return nodes;
64 }
65
66 std::string from;
67 std::string to;
68 std::string linkAttr;
69
70 int linksNumber = 0;
71 int nodesNumber = 0;
72
73 int totnode = 0;
74 int totlink = 0;
75
76 std::istringstream lineBuffer;
77 std::string line;
78
79 getline(topgen, line);
80 lineBuffer.str(line);
81
82 lineBuffer >> totnode;
83 lineBuffer >> totlink;
84 NS_LOG_INFO("Inet topology should have " << totnode << " nodes and " << totlink << " links");
85
86 for (int i = 0; i < totnode && !topgen.eof(); i++)
87 {
88 getline(topgen, line);
89 }
90
91 for (int i = 0; i < totlink && !topgen.eof(); i++)
92 {
93 getline(topgen, line);
94 lineBuffer.clear();
95 lineBuffer.str(line);
96
97 lineBuffer >> from;
98 lineBuffer >> to;
99 lineBuffer >> linkAttr;
100
101 if ((!from.empty()) && (!to.empty()))
102 {
103 NS_LOG_INFO("Link " << linksNumber << " from: " << from << " to: " << to);
104
105 if (!nodeMap[from])
106 {
107 NS_LOG_INFO("Node " << nodesNumber << " name: " << from);
108 Ptr<Node> tmpNode = CreateObject<Node>();
109 std::string nodeName = "InetTopology/NodeName/" + from;
110 Names::Add(from, tmpNode);
111 nodeMap[from] = tmpNode;
112 nodes.Add(tmpNode);
113 nodesNumber++;
114 }
115
116 if (!nodeMap[to])
117 {
118 NS_LOG_INFO("Node " << nodesNumber << " name: " << to);
119 Ptr<Node> tmpNode = CreateObject<Node>();
120 std::string nodename = "InetTopology/NodeName/" + to;
121 Names::Add(nodename, tmpNode);
122 nodeMap[to] = tmpNode;
123 nodes.Add(tmpNode);
124 nodesNumber++;
125 }
126
127 Link link(nodeMap[from], from, nodeMap[to], to);
128 if (!linkAttr.empty())
129 {
130 NS_LOG_INFO("Link " << linksNumber << " weight: " << linkAttr);
131 link.SetAttribute("Weight", linkAttr);
132 }
133 AddLink(link);
134
135 linksNumber++;
136 }
137 }
138
139 NS_LOG_INFO("Inet topology created with " << nodesNumber << " nodes and " << linksNumber
140 << " links");
141 topgen.close();
142
143 return nodes;
144}
145
146} /* namespace ns3 */
Topology file reader (Inet-format type).
NodeContainer Read() override
Main topology reading function.
static TypeId GetTypeId()
Get the type ID.
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.
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_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#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
ns3::InetTopologyReader declaration.
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.