A Discrete-Event Network Simulator
API
orbis-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/node-container.h"
25
26#include <cstdlib>
27#include <fstream>
28#include <sstream>
29
36namespace ns3
37{
38
39NS_LOG_COMPONENT_DEFINE("OrbisTopologyReader");
40
41NS_OBJECT_ENSURE_REGISTERED(OrbisTopologyReader);
42
43TypeId
45{
46 static TypeId tid = TypeId("ns3::OrbisTopologyReader")
48 .SetGroupName("TopologyReader")
49 .AddConstructor<OrbisTopologyReader>();
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 return nodes;
74 }
75
76 std::string from;
77 std::string to;
78 std::istringstream lineBuffer;
79 std::string line;
80
81 int linksNumber = 0;
82 int nodesNumber = 0;
83
84 while (!topgen.eof())
85 {
86 line.clear();
87 lineBuffer.clear();
88 from.clear();
89 to.clear();
90
91 getline(topgen, line);
92 lineBuffer.str(line);
93 lineBuffer >> from;
94 lineBuffer >> to;
95
96 if ((!from.empty()) && (!to.empty()))
97 {
98 NS_LOG_INFO(linksNumber << " From: " << from << " to: " << to);
99 if (!nodeMap[from])
100 {
101 Ptr<Node> tmpNode = CreateObject<Node>();
102 nodeMap[from] = tmpNode;
103 nodes.Add(tmpNode);
104 nodesNumber++;
105 }
106
107 if (!nodeMap[to])
108 {
109 Ptr<Node> tmpNode = CreateObject<Node>();
110 nodeMap[to] = tmpNode;
111 nodes.Add(tmpNode);
112 nodesNumber++;
113 }
114
115 Link link(nodeMap[from], from, nodeMap[to], to);
116 AddLink(link);
117
118 linksNumber++;
119 }
120 }
121 NS_LOG_INFO("Orbis topology created with " << nodesNumber << " nodes and " << linksNumber
122 << " links");
123 topgen.close();
124
125 return nodes;
126}
127
128} /* namespace ns3 */
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.
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:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#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_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:45
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::OrbisTopologyReader declaration.