A Discrete-Event Network Simulator
API
inet-topology-reader.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella (tommaso.pecorella@unifi.it)
19  * Author: Valerio Sartini (Valesar@gmail.com)
20  */
21 
22 #include <fstream>
23 #include <cstdlib>
24 #include <sstream>
25 #include "ns3/node-container.h"
26 #include "ns3/log.h"
27 
28 #include "inet-topology-reader.h"
29 
30 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("InetTopologyReader");
40 
41 NS_OBJECT_ENSURE_REGISTERED (InetTopologyReader);
42 
44 {
45  static TypeId tid = TypeId ("ns3::InetTopologyReader")
47  .SetGroupName ("TopologyReader")
48  .AddConstructor<InetTopologyReader> ()
49  ;
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 ().c_str ());
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] == 0 )
117  {
118  NS_LOG_INFO ( "Node " << nodesNumber << " name: " << from);
119  Ptr<Node> tmpNode = CreateObject<Node> ();
120  nodeMap[from] = tmpNode;
121  nodes.Add (tmpNode);
122  nodesNumber++;
123  }
124 
125  if (nodeMap[to] == 0)
126  {
127  NS_LOG_INFO ( "Node " << nodesNumber << " name: " << to);
128  Ptr<Node> tmpNode = CreateObject<Node> ();
129  nodeMap[to] = tmpNode;
130  nodes.Add (tmpNode);
131  nodesNumber++;
132  }
133 
134  Link link ( nodeMap[from], from, nodeMap[to], to );
135  if ( !linkAttr.empty () )
136  {
137  NS_LOG_INFO ( "Link " << linksNumber << " weight: " << linkAttr);
138  link.SetAttribute ("Weight", linkAttr);
139  }
140  AddLink (link);
141 
142  linksNumber++;
143  }
144  }
145 
146  NS_LOG_INFO ("Inet topology created with " << nodesNumber << " nodes and " << linksNumber << " links");
147  topgen.close ();
148 
149  return nodes;
150 }
151 
152 } /* namespace ns3 */
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
static TypeId GetTypeId(void)
Get the type ID.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
void AddLink(Link link)
Adds a link to the topology.
Interface for input file readers management.
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual NodeContainer Read(void)
Main topology reading function.
keep track of a set of node pointers.
std::string GetFileName(void) const
Returns the input file name.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
Topology file reader (Inet-format type).
ns3::InetTopologyReader declaration.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923