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
NS_OBJECT_ENSURE_REGISTERED
(InetTopologyReader);
36
37
TypeId
InetTopologyReader::GetTypeId
(
void
)
38
{
39
static
TypeId
tid =
TypeId
(
"ns3::InetTopologyReader"
)
40
.
SetParent
<
Object
> ()
41
;
42
return
tid;
43
}
44
45
InetTopologyReader::InetTopologyReader
()
46
{
47
NS_LOG_FUNCTION
(
this
);
48
}
49
50
InetTopologyReader::~InetTopologyReader
()
51
{
52
NS_LOG_FUNCTION
(
this
);
53
}
54
55
NodeContainer
56
InetTopologyReader::Read
(
void
)
57
{
58
std::ifstream topgen;
59
topgen.open (
GetFileName
().c_str ());
60
std::map<std::string, Ptr<Node> >
nodeMap
;
61
NodeContainer
nodes;
62
63
if
( !topgen.is_open () )
64
{
65
NS_LOG_WARN
(
"Inet topology file object is not open, check file name and permissions"
);
66
return
nodes;
67
}
68
69
std::string from;
70
std::string to;
71
std::string linkAttr;
72
73
int
linksNumber
= 0;
74
int
nodesNumber
= 0;
75
76
int
totnode = 0;
77
int
totlink = 0;
78
79
std::istringstream lineBuffer;
80
std::string line;
81
82
getline (topgen,line);
83
lineBuffer.str (line);
84
85
lineBuffer >> totnode;
86
lineBuffer >> totlink;
87
NS_LOG_INFO
(
"Inet topology should have "
<< totnode <<
" nodes and "
<< totlink <<
" links"
);
88
89
for
(
int
i = 0; i < totnode; i++)
90
{
91
getline (topgen,line);
92
}
93
94
for
(
int
i = 0; i < totlink && !topgen.eof (); i++)
95
{
96
getline (topgen,line);
97
lineBuffer.clear ();
98
lineBuffer.str (line);
99
100
lineBuffer >> from;
101
lineBuffer >> to;
102
lineBuffer >> linkAttr;
103
104
if
( (!from.empty ()) && (!to.empty ()) )
105
{
106
NS_LOG_INFO
(
"Link "
<< linksNumber <<
" from: "
<< from <<
" to: "
<< to);
107
108
if
( nodeMap[from] == 0 )
109
{
110
NS_LOG_INFO
(
"Node "
<< nodesNumber <<
" name: "
<< from);
111
Ptr<Node>
tmpNode = CreateObject<Node> ();
112
nodeMap[from] = tmpNode;
113
nodes.
Add
(tmpNode);
114
nodesNumber++;
115
}
116
117
if
(nodeMap[to] == 0)
118
{
119
NS_LOG_INFO
(
"Node "
<< nodesNumber <<
" name: "
<< to);
120
Ptr<Node>
tmpNode = CreateObject<Node> ();
121
nodeMap[to] = tmpNode;
122
nodes.
Add
(tmpNode);
123
nodesNumber++;
124
}
125
126
Link
link ( nodeMap[from], from, nodeMap[to], to );
127
if
( !linkAttr.empty () )
128
{
129
NS_LOG_INFO
(
"Link "
<< linksNumber <<
" weight: "
<< linkAttr);
130
link.
SetAttribute
(
"Weight"
, linkAttr);
131
}
132
AddLink
(link);
133
134
linksNumber++;
135
}
136
}
137
138
NS_LOG_INFO
(
"Inet topology created with "
<< nodesNumber <<
" nodes and "
<< linksNumber <<
" links"
);
139
topgen.close ();
140
141
return
nodes;
142
}
143
144
}
/* namespace ns3 */
src
topology-read
model
inet-topology-reader.cc
Generated on Tue May 14 2013 11:08:33 for ns-3 by
1.8.1.2