A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
43
TypeId
InetTopologyReader::GetTypeId
(
void
)
44
{
45
static
TypeId
tid =
TypeId
(
"ns3::InetTopologyReader"
)
46
.
SetParent
<
TopologyReader
> ()
47
.SetGroupName (
"TopologyReader"
)
48
.AddConstructor<
InetTopologyReader
> ()
49
;
50
return
tid;
51
}
52
53
InetTopologyReader::InetTopologyReader
()
54
{
55
NS_LOG_FUNCTION
(
this
);
56
}
57
58
InetTopologyReader::~InetTopologyReader
()
59
{
60
NS_LOG_FUNCTION
(
this
);
61
}
62
63
NodeContainer
64
InetTopologyReader::Read
(
void
)
65
{
66
std::ifstream topgen;
67
topgen.open (
GetFileName
().c_str ());
68
std::map<std::string, Ptr<Node> > nodeMap;
69
NodeContainer
nodes
;
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 */
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::TopologyReader
Interface for input file readers management.
Definition:
topology-reader.h:51
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:45
ns3::InetTopologyReader::InetTopologyReader
InetTopologyReader()
Definition:
inet-topology-reader.cc:53
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TopologyReader::Link
Inner class holding the details about a link between two nodes.
Definition:
topology-reader.h:62
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition:
log.h:265
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
first.nodes
nodes
Definition:
first.py:32
inet-topology-reader.h
ns3::InetTopologyReader declaration.
ns3::TopologyReader::AddLink
void AddLink(Link link)
Adds a link to the topology.
Definition:
topology-reader.cc:96
ns3::Ptr< Node >
ns3::TopologyReader::Link::SetAttribute
void SetAttribute(const std::string &name, const std::string &value)
Sets an arbitrary link attribute.
Definition:
topology-reader.cc:158
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition:
log.h:281
ns3::InetTopologyReader::~InetTopologyReader
virtual ~InetTopologyReader()
Definition:
inet-topology-reader.cc:58
ns3::InetTopologyReader
Topology file reader (Inet-format type).
Definition:
inet-topology-reader.h:53
ns3::InetTopologyReader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
inet-topology-reader.cc:43
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition:
log-macros-enabled.h:244
ns3::NodeContainer
keep track of a set of node pointers.
Definition:
node-container.h:39
ns3::TopologyReader::GetFileName
std::string GetFileName(void) const
Returns the input file name.
Definition:
topology-reader.cc:64
ns3::InetTopologyReader::Read
virtual NodeContainer Read(void)
Main topology reading function.
Definition:
inet-topology-reader.cc:64
src
topology-read
model
inet-topology-reader.cc
Generated on Fri Oct 1 2021 17:03:37 for ns-3 by
1.8.20