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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella (tommaso.pecorella@unifi.it)
18 * Author: Valerio Sartini (Valesar@gmail.com)
19 */
20
22
23#include "ns3/log.h"
24#include "ns3/names.h"
25#include "ns3/node-container.h"
26
27#include <cstdlib>
28#include <fstream>
29#include <sstream>
30
37namespace ns3
38{
39NS_LOG_COMPONENT_DEFINE("InetTopologyReader");
40
41NS_OBJECT_ENSURE_REGISTERED(InetTopologyReader);
42
43TypeId
45{
46 static TypeId tid = TypeId("ns3::InetTopologyReader")
48 .SetGroupName("TopologyReader")
49 .AddConstructor<InetTopologyReader>();
50 return tid;
51}
52
54{
55 NS_LOG_FUNCTION(this);
56}
57
59{
60 NS_LOG_FUNCTION(this);
61}
62
65{
66 std::ifstream topgen;
67 topgen.open(GetFileName());
68 std::map<std::string, Ptr<Node>> nodeMap;
70
71 if (!topgen.is_open())
72 {
73 NS_LOG_WARN("Inet topology file object is not open, check file name and permissions");
74 return nodes;
75 }
76
77 std::string from;
78 std::string to;
79 std::string linkAttr;
80
81 int linksNumber = 0;
82 int nodesNumber = 0;
83
84 int totnode = 0;
85 int totlink = 0;
86
87 std::istringstream lineBuffer;
88 std::string line;
89
90 getline(topgen, line);
91 lineBuffer.str(line);
92
93 lineBuffer >> totnode;
94 lineBuffer >> totlink;
95 NS_LOG_INFO("Inet topology should have " << totnode << " nodes and " << totlink << " links");
96
97 for (int i = 0; i < totnode && !topgen.eof(); i++)
98 {
99 getline(topgen, line);
100 }
101
102 for (int i = 0; i < totlink && !topgen.eof(); i++)
103 {
104 getline(topgen, line);
105 lineBuffer.clear();
106 lineBuffer.str(line);
107
108 lineBuffer >> from;
109 lineBuffer >> to;
110 lineBuffer >> linkAttr;
111
112 if ((!from.empty()) && (!to.empty()))
113 {
114 NS_LOG_INFO("Link " << linksNumber << " from: " << from << " to: " << to);
115
116 if (!nodeMap[from])
117 {
118 NS_LOG_INFO("Node " << nodesNumber << " name: " << from);
119 Ptr<Node> tmpNode = CreateObject<Node>();
120 std::string nodeName = "InetTopology/NodeName/" + from;
121 Names::Add(from, tmpNode);
122 nodeMap[from] = tmpNode;
123 nodes.Add(tmpNode);
124 nodesNumber++;
125 }
126
127 if (!nodeMap[to])
128 {
129 NS_LOG_INFO("Node " << nodesNumber << " name: " << to);
130 Ptr<Node> tmpNode = CreateObject<Node>();
131 std::string nodename = "InetTopology/NodeName/" + to;
132 Names::Add(nodename, tmpNode);
133 nodeMap[to] = tmpNode;
134 nodes.Add(tmpNode);
135 nodesNumber++;
136 }
137
138 Link link(nodeMap[from], from, nodeMap[to], to);
139 if (!linkAttr.empty())
140 {
141 NS_LOG_INFO("Link " << linksNumber << " weight: " << linkAttr);
142 link.SetAttribute("Weight", linkAttr);
143 }
144 AddLink(link);
145
146 linksNumber++;
147 }
148 }
149
150 NS_LOG_INFO("Inet topology created with " << nodesNumber << " nodes and " << linksNumber
151 << " links");
152 topgen.close();
153
154 return nodes;
155}
156
157} /* 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:775
keep track of a set of node pointers.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:261
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
ns3::InetTopologyReader declaration.
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.