blob: 448464e648df862c68784a1e339f8dcc06052a40 [file] [log] [blame]
Ilya Moiseenko58d26672011-12-08 13:48:06 -08001/* -*- 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
Alexander Afanasyev07827182011-12-13 01:07:32 -080022#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/constant-position-mobility-model.h"
43#include "ns3/random-variable.h"
44
Ilya Moiseenko58d26672011-12-08 13:48:06 -080045#include <regex.h>
46
Alexander Afanasyev07827182011-12-13 01:07:32 -080047#include <boost/foreach.hpp>
48#include <boost/lexical_cast.hpp>
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080049#include "../utils/spring-mobility-helper.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080050
51#include <iomanip>
52#include <set>
53
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080054using namespace std;
55
56NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader");
Ilya Moiseenko58d26672011-12-08 13:48:06 -080057
58namespace ns3 {
59
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080060RocketfuelWeightsReader::RocketfuelWeightsReader (const std::string &path/*=""*/)
61 : AnnotatedTopologyReader (path)
Ilya Moiseenko58d26672011-12-08 13:48:06 -080062{
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080063 NS_LOG_FUNCTION (this);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080064 SetMobilityModel ("ns3::SpringMobilityModel");
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080065}
66
67RocketfuelWeightsReader::~RocketfuelWeightsReader ()
68{
69 NS_LOG_FUNCTION (this);
70}
Ilya Moiseenko09ac8992011-12-12 17:55:42 -080071
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080072void
73RocketfuelWeightsReader::SetFileType (uint8_t inputType)
74{
75 m_inputType = inputType;
76}
77
78NodeContainer
79RocketfuelWeightsReader::Read ()
80{
81 ifstream topgen;
82 topgen.open (GetFileName ().c_str ());
83 NodeContainer nodes;
84
85 if ( !topgen.is_open () )
86 {
87 NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading");
88 return nodes;
Ilya Moiseenko58d26672011-12-08 13:48:06 -080089 }
90
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080091 map<string, set<string> > processedLinks; // to eliminate duplications
92 bool repeatedRun = LinksSize () > 0;
93 std::list<Link>::iterator linkIterator = m_linksList.begin ();
94
95 while (!topgen.eof ())
96 {
97 string line;
98 getline (topgen,line);
99 if (line == "") continue;
100 if (line[0] == '#') continue; // comments
101
102 // NS_LOG_DEBUG ("Input: [" << line << "]");
103
104 istringstream lineBuffer (line);
105 string from, to, attribute;
106
107 lineBuffer >> from >> to >> attribute;
108
109 if (processedLinks[to].size () != 0 &&
110 processedLinks[to].find (from) != processedLinks[to].end ())
111 {
112 continue; // duplicated link
113 }
114 processedLinks[from].insert (to);
115
116 Ptr<Node> fromNode = Names::Find<Node> (m_path, from);
117 if (fromNode == 0)
118 {
119 fromNode = CreateNode (from);
120 nodes.Add (fromNode);
121 }
122
123 Ptr<Node> toNode = Names::Find<Node> (m_path, to);
124 if (toNode == 0)
125 {
126 toNode = CreateNode (to);
127 nodes.Add (toNode);
128 }
129
130 Link *link;
131 if (!repeatedRun)
132 link = new Link (fromNode, from, toNode, to);
133 else
134 {
135 NS_ASSERT (linkIterator != m_linksList.end ());
136 link = &(*linkIterator);
137
138 linkIterator++;
139 }
140
141 switch (m_inputType)
142 {
143 case WEIGHTS:
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800144 {
145 double metric = boost::lexical_cast<double> (attribute);
146 link->SetAttribute ("OSPF", boost::lexical_cast<string> (metric*2));
147 break;
148 }
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800149 case LATENCIES:
150 link->SetAttribute ("Delay", attribute);
151 break;
152 default:
153 ; //
154 }
155
156 NS_LOG_DEBUG ("Link " << from << " <==> " << to << " / " << attribute);
157 if (!repeatedRun)
158 {
159 AddLink (*link);
160 delete link;
161 }
162 }
163
164 topgen.close ();
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800165
166 if (!repeatedRun)
167 {
168 NS_LOG_INFO ("Rocketfuel topology created with " << nodes.GetN () << " nodes and " << LinksSize () << " links");
169 }
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800170 return nodes;
171}
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800172
173void
174RocketfuelWeightsReader::Commit ()
175{
176 ApplySettings ();
177
178 SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ());
179}
180
181
182// void
183// RocketfuelWeightsReader::Cheat (NodeContainer &nodes)
184// {
185// double epsilon = 1;
186
187// for (NodeContainer::Iterator i = nodes.Begin ();
188// i != nodes.End ();
189// i++)
190// {
191
192// }
193// }
194
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800195} /* namespace ns3 */