NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
rocketfuel-weights-reader.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hajime Tazaki
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: Hajime Tazaki (tazaki@sfc.wide.ad.jp)
19  * Ilya Moiseenko <iliamo@cs.ucla.edu>
20  */
21 
22 #include "rocketfuel-weights-reader.h"
23 
24 #include "ns3/nstime.h"
25 #include "ns3/log.h"
26 #include "ns3/assert.h"
27 #include "ns3/names.h"
28 #include "ns3/net-device-container.h"
29 #include "ns3/point-to-point-helper.h"
30 #include "ns3/point-to-point-net-device.h"
31 #include "ns3/internet-stack-helper.h"
32 #include "ns3/ipv4-address-helper.h"
33 #include "ns3/ipv4-global-routing-helper.h"
34 #include "ns3/drop-tail-queue.h"
35 #include "ns3/ipv4-interface.h"
36 #include "ns3/ipv4.h"
37 #include "ns3/string.h"
38 #include "ns3/pointer.h"
39 #include "ns3/uinteger.h"
40 #include "ns3/ipv4-address.h"
41 
42 #include "ns3/mobility-model.h"
43 
44 #include <regex.h>
45 
46 #include <boost/foreach.hpp>
47 #include <boost/lexical_cast.hpp>
48 
49 #include <iomanip>
50 #include <set>
51 
52 using namespace std;
53 
54 NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader");
55 
56 namespace ns3 {
57 
58 RocketfuelWeightsReader::RocketfuelWeightsReader (const std::string &path/*=""*/, double scale/*=1.0*/)
59  : AnnotatedTopologyReader (path, scale)
60  , m_defaultBandwidth ("100Mbps")
61 {
62  NS_LOG_FUNCTION (this);
63 
64  // TypeId tid;
65  // bool ok = TypeId::LookupByNameFailSafe ("ns3::SpringMobilityModel", &tid);
66  // if (ok)
67  // SetMobilityModel ("ns3::SpringMobilityModel");
68  // else
69  // Use default mobility model (supplied by AnnotatedTopologyReader)
70 }
71 
72 RocketfuelWeightsReader::~RocketfuelWeightsReader ()
73 {
74  NS_LOG_FUNCTION (this);
75 }
76 
77 void
78 RocketfuelWeightsReader::SetFileType (uint8_t inputType)
79 {
80  m_inputType = inputType;
81 }
82 
83 NodeContainer
85 {
86  if (m_inputType == POSITIONS)
88 
89  ifstream topgen;
90  topgen.open (GetFileName ().c_str ());
91 
92  if ( !topgen.is_open () )
93  {
94  NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading");
95  return m_nodes;
96  }
97 
98  map<string, set<string> > processedLinks; // to eliminate duplications
99  bool repeatedRun = LinksSize () > 0;
100  std::list<Link>::iterator linkIterator = m_linksList.begin ();
101 
102  while (!topgen.eof ())
103  {
104  string line;
105  getline (topgen,line);
106  if (line == "") continue;
107  if (line[0] == '#') continue; // comments
108 
109  // NS_LOG_DEBUG ("Input: [" << line << "]");
110 
111  istringstream lineBuffer (line);
112  string from, to, attribute;
113 
114  lineBuffer >> from >> to >> attribute;
115 
116  if (processedLinks[to].size () != 0 &&
117  processedLinks[to].find (from) != processedLinks[to].end ())
118  {
119  continue; // duplicated link
120  }
121  processedLinks[from].insert (to);
122 
123  Ptr<Node> fromNode = Names::Find<Node> (m_path, from);
124  if (fromNode == 0)
125  {
126  fromNode = CreateNode (from, 0);
127  }
128 
129  Ptr<Node> toNode = Names::Find<Node> (m_path, to);
130  if (toNode == 0)
131  {
132  toNode = CreateNode (to, 0);
133  }
134 
135  Link *link;
136  if (!repeatedRun)
137  link = new Link (fromNode, from, toNode, to);
138  else
139  {
140  NS_ASSERT (linkIterator != m_linksList.end ());
141  link = &(*linkIterator);
142 
143  linkIterator++;
144  }
145 
146  switch (m_inputType)
147  {
148  case LINKS:
149  {
150  // links only
151  // do nothing
152  break;
153  }
154  case WEIGHTS:
155  {
156  if (attribute == "")
157  attribute = "1";
158  uint16_t metric = boost::lexical_cast<uint16_t> (attribute);
159  link->SetAttribute ("OSPF", boost::lexical_cast<string> (metric));
160  break;
161  }
162  case LATENCIES:
163  if (attribute == "")
164  attribute = "1";
165 
166  link->SetAttribute ("DataRate", m_defaultBandwidth);
167  link->SetAttribute ("Delay", attribute+"ms");
168  if (!m_queue.empty ())
169  {
170  link->SetAttribute ("MaxPackets", m_queue);
171  }
172  break;
173  default:
174  ; //
175  }
176 
177  NS_LOG_DEBUG ("Link " << from << " <==> " << to << " / " << attribute);
178  if (!repeatedRun)
179  {
180  AddLink (*link);
181  delete link;
182  }
183  }
184 
185  topgen.close ();
186 
187  if (!repeatedRun)
188  {
189  NS_LOG_INFO ("Rocketfuel topology created with " << m_nodes.GetN () << " nodes and " << LinksSize () << " links");
190  }
191  return m_nodes;
192 }
193 
194 void
195 RocketfuelWeightsReader::Commit ()
196 {
197  ApplySettings ();
198 
199  // SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ());
200 }
201 
202 } /* namespace ns3 */
void ApplySettings()
This method applies setting to corresponding nodes and links NetDeviceContainer must be allocated Nod...
virtual NodeContainer Read()
Main annotated topology reading function.
virtual NodeContainer Read(void)
Main topology reading function.