A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
26
#include "ns3/log.h"
27
28
#include "
inet-topology-reader.h
"
29
30
31
namespace
ns3 {
32
33
NS_LOG_COMPONENT_DEFINE
(
"InetTopologyReader"
)
34
;
35
36
NS_OBJECT_ENSURE_REGISTERED
(InetTopologyReader)
37
;
38
39
TypeId
InetTopologyReader::GetTypeId
(
void
)
40
{
41
static
TypeId
tid =
TypeId
(
"ns3::InetTopologyReader"
)
42
.
SetParent
<
Object
> ()
43
;
44
return
tid;
45
}
46
47
InetTopologyReader::InetTopologyReader
()
48
{
49
NS_LOG_FUNCTION
(
this
);
50
}
51
52
InetTopologyReader::~InetTopologyReader
()
53
{
54
NS_LOG_FUNCTION
(
this
);
55
}
56
57
NodeContainer
58
InetTopologyReader::Read
(
void
)
59
{
60
std::ifstream topgen;
61
topgen.open (
GetFileName
().c_str ());
62
std::map<std::string, Ptr<Node> > nodeMap;
63
NodeContainer
nodes
;
64
65
if
( !topgen.is_open () )
66
{
67
NS_LOG_WARN
(
"Inet topology file object is not open, check file name and permissions"
);
68
return
nodes
;
69
}
70
71
std::string from;
72
std::string to;
73
std::string linkAttr;
74
75
int
linksNumber = 0;
76
int
nodesNumber = 0;
77
78
int
totnode = 0;
79
int
totlink = 0;
80
81
std::istringstream lineBuffer;
82
std::string line;
83
84
getline (topgen,line);
85
lineBuffer.str (line);
86
87
lineBuffer >> totnode;
88
lineBuffer >> totlink;
89
NS_LOG_INFO
(
"Inet topology should have "
<< totnode <<
" nodes and "
<< totlink <<
" links"
);
90
91
for
(
int
i = 0; i < totnode && !topgen.eof (); i++)
92
{
93
getline (topgen,line);
94
}
95
96
for
(
int
i = 0; i < totlink && !topgen.eof (); i++)
97
{
98
getline (topgen,line);
99
lineBuffer.clear ();
100
lineBuffer.str (line);
101
102
lineBuffer >> from;
103
lineBuffer >> to;
104
lineBuffer >> linkAttr;
105
106
if
( (!from.empty ()) && (!to.empty ()) )
107
{
108
NS_LOG_INFO
(
"Link "
<< linksNumber <<
" from: "
<< from <<
" to: "
<< to);
109
110
if
( nodeMap[from] == 0 )
111
{
112
NS_LOG_INFO
(
"Node "
<< nodesNumber <<
" name: "
<< from);
113
Ptr<Node>
tmpNode = CreateObject<Node> ();
114
nodeMap[from] = tmpNode;
115
nodes.
Add
(tmpNode);
116
nodesNumber++;
117
}
118
119
if
(nodeMap[to] == 0)
120
{
121
NS_LOG_INFO
(
"Node "
<< nodesNumber <<
" name: "
<< to);
122
Ptr<Node>
tmpNode = CreateObject<Node> ();
123
nodeMap[to] = tmpNode;
124
nodes.
Add
(tmpNode);
125
nodesNumber++;
126
}
127
128
Link
link ( nodeMap[from], from, nodeMap[to], to );
129
if
( !linkAttr.empty () )
130
{
131
NS_LOG_INFO
(
"Link "
<< linksNumber <<
" weight: "
<< linkAttr);
132
link.
SetAttribute
(
"Weight"
, linkAttr);
133
}
134
AddLink
(link);
135
136
linksNumber++;
137
}
138
}
139
140
NS_LOG_INFO
(
"Inet topology created with "
<< nodesNumber <<
" nodes and "
<< linksNumber <<
" links"
);
141
topgen.close ();
142
143
return
nodes
;
144
}
145
146
}
/* namespace ns3 */
ns3::Ptr< Node >
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:345
ns3::InetTopologyReader::InetTopologyReader
InetTopologyReader()
Definition:
inet-topology-reader.cc:47
ns3::NS_LOG_COMPONENT_DEFINE
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
ns3::TopologyReader::GetFileName
std::string GetFileName(void) const
Returns the input file name.
Definition:
topology-reader.cc:60
ns3::InetTopologyReader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
inet-topology-reader.cc:39
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Definition:
log.h:298
ns3::TopologyReader::AddLink
void AddLink(Link link)
Adds a link to the topology.
Definition:
topology-reader.cc:92
first.nodes
tuple nodes
Definition:
first.py:25
ns3::TopologyReader::Link
Inner class holding the details about a link between two nodes.
Definition:
topology-reader.h:54
ns3::InetTopologyReader::Read
virtual NodeContainer Read(void)
Main topology reading function.
Definition:
inet-topology-reader.cc:58
ns3::NodeContainer
keep track of a set of node pointers.
Definition:
node-container.h:38
ns3::TopologyReader::Link::SetAttribute
void SetAttribute(const std::string &name, const std::string &value)
Sets an arbitrary link attribute.
Definition:
topology-reader.cc:154
ns3::InetTopologyReader::~InetTopologyReader
virtual ~InetTopologyReader()
Definition:
inet-topology-reader.cc:52
ns3::NodeContainer::Add
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Definition:
node-container.cc:109
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Definition:
log.h:280
ns3::Object
a base class which provides memory management and object aggregation
Definition:
object.h:63
inet-topology-reader.h
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:611
src
topology-read
model
inet-topology-reader.cc
Generated on Sat Apr 19 2014 14:07:09 for ns-3 by
1.8.6