A Discrete-Event Network Simulator
API
 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 #include "inet-topology-reader.h"
28 
29 using namespace std;
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 
56 InetTopologyReader::Read (void)
57 {
58  ifstream topgen;
59  topgen.open (GetFileName ().c_str ());
60  map<string, Ptr<Node> > nodeMap;
61  NodeContainer nodes;
62 
63  if ( !topgen.is_open () )
64  {
65  return nodes;
66  }
67 
68  string from;
69  string to;
70  string linkAttr;
71 
72  int linksNumber = 0;
73  int nodesNumber = 0;
74 
75  int totnode = 0;
76  int totlink = 0;
77 
78  istringstream lineBuffer;
79  string line;
80 
81  getline (topgen,line);
82  lineBuffer.str (line);
83 
84  lineBuffer >> totnode;
85  lineBuffer >> totlink;
86  NS_LOG_INFO ("Inet topology should have " << totnode << " nodes and " << totlink << " links");
87 
88  for (int i = 0; i <= totnode; i++)
89  {
90  getline (topgen,line);
91  }
92 
93  for (int i = 0; i < totlink && !topgen.eof (); i++)
94  {
95  getline (topgen,line);
96  lineBuffer.clear ();
97  lineBuffer.str (line);
98 
99  lineBuffer >> from;
100  lineBuffer >> to;
101  lineBuffer >> linkAttr;
102 
103  if ( (!from.empty ()) && (!to.empty ()) )
104  {
105  NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to );
106 
107  if ( nodeMap[from] == 0 )
108  {
109  Ptr<Node> tmpNode = CreateObject<Node> ();
110  nodeMap[from] = tmpNode;
111  nodes.Add (tmpNode);
112  nodesNumber++;
113  }
114 
115  if (nodeMap[to] == 0)
116  {
117  Ptr<Node> tmpNode = CreateObject<Node> ();
118  nodeMap[to] = tmpNode;
119  nodes.Add (tmpNode);
120  nodesNumber++;
121  }
122 
123  Link link ( nodeMap[from], from, nodeMap[to], to );
124  if ( !linkAttr.empty () )
125  {
126  link.SetAttribute ("Weight", linkAttr);
127  }
128  AddLink (link);
129 
130  linksNumber++;
131  }
132  }
133 
134  NS_LOG_INFO ("Inet topology created with " << nodesNumber << " nodes and " << linksNumber << " links");
135  topgen.close ();
136 
137  return nodes;
138 }
139 
140 } /* namespace ns3 */