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 
21 #include "orbis-topology-reader.h"
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 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("OrbisTopologyReader");
41 
42 NS_OBJECT_ENSURE_REGISTERED(OrbisTopologyReader);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId("ns3::OrbisTopologyReader")
49  .SetGroupName("TopologyReader")
50  .AddConstructor<OrbisTopologyReader>();
51  return tid;
52 }
53 
55 {
56  NS_LOG_FUNCTION(this);
57 }
58 
60 {
61  NS_LOG_FUNCTION(this);
62 }
63 
66 {
67  std::ifstream topgen;
68  topgen.open(GetFileName());
69  std::map<std::string, Ptr<Node>> nodeMap;
71 
72  if (!topgen.is_open())
73  {
74  return nodes;
75  }
76 
77  std::string from;
78  std::string to;
79  std::istringstream lineBuffer;
80  std::string line;
81 
82  int linksNumber = 0;
83  int nodesNumber = 0;
84 
85  while (!topgen.eof())
86  {
87  line.clear();
88  lineBuffer.clear();
89  from.clear();
90  to.clear();
91 
92  getline(topgen, line);
93  lineBuffer.str(line);
94  lineBuffer >> from;
95  lineBuffer >> to;
96 
97  if ((!from.empty()) && (!to.empty()))
98  {
99  NS_LOG_INFO(linksNumber << " From: " << from << " to: " << to);
100  if (!nodeMap[from])
101  {
102  Ptr<Node> tmpNode = CreateObject<Node>();
103  std::string nodename = "OrbisTopology/NodeName/" + from;
104  Names::Add(nodename, tmpNode);
105  nodeMap[from] = tmpNode;
106  nodes.Add(tmpNode);
107  nodesNumber++;
108  }
109 
110  if (!nodeMap[to])
111  {
112  Ptr<Node> tmpNode = CreateObject<Node>();
113  std::string nodename = "OrbisTopology/NodeName/" + to;
114  Names::Add(nodename, tmpNode);
115  nodeMap[to] = tmpNode;
116  nodes.Add(tmpNode);
117  nodesNumber++;
118  }
119 
120  Link link(nodeMap[from], from, nodeMap[to], to);
121  AddLink(link);
122 
123  linksNumber++;
124  }
125  }
126  NS_LOG_INFO("Orbis topology created with " << nodesNumber << " nodes and " << linksNumber
127  << " links");
128  topgen.close();
129 
130  return nodes;
131 }
132 
133 } /* 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:775
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:46
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::OrbisTopologyReader declaration.