blob: f6895fadf1dee51f95f0f046906b91714c949b8e [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
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -080042#include "ns3/mobility-model.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080043
Ilya Moiseenko58d26672011-12-08 13:48:06 -080044#include <regex.h>
45
Alexander Afanasyev07827182011-12-13 01:07:32 -080046#include <boost/foreach.hpp>
47#include <boost/lexical_cast.hpp>
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080048#include "../utils/spring-mobility-helper.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080049
50#include <iomanip>
51#include <set>
52
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080053using namespace std;
54
55NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader");
Ilya Moiseenko58d26672011-12-08 13:48:06 -080056
57namespace ns3 {
58
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080059RocketfuelWeightsReader::RocketfuelWeightsReader (const std::string &path/*=""*/)
60 : AnnotatedTopologyReader (path)
Ilya Moiseenko58d26672011-12-08 13:48:06 -080061{
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080062 NS_LOG_FUNCTION (this);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080063 SetMobilityModel ("ns3::SpringMobilityModel");
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080064}
65
66RocketfuelWeightsReader::~RocketfuelWeightsReader ()
67{
68 NS_LOG_FUNCTION (this);
69}
Ilya Moiseenko09ac8992011-12-12 17:55:42 -080070
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080071void
72RocketfuelWeightsReader::SetFileType (uint8_t inputType)
73{
74 m_inputType = inputType;
75}
76
77NodeContainer
78RocketfuelWeightsReader::Read ()
79{
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -080080 if (m_inputType == POSITIONS)
81 return AnnotatedTopologyReader::Read ();
82
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080083 ifstream topgen;
84 topgen.open (GetFileName ().c_str ());
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080085
86 if ( !topgen.is_open () )
87 {
88 NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading");
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -080089 return m_nodes;
Ilya Moiseenko58d26672011-12-08 13:48:06 -080090 }
91
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080092 map<string, set<string> > processedLinks; // to eliminate duplications
93 bool repeatedRun = LinksSize () > 0;
94 std::list<Link>::iterator linkIterator = m_linksList.begin ();
95
96 while (!topgen.eof ())
97 {
98 string line;
99 getline (topgen,line);
100 if (line == "") continue;
101 if (line[0] == '#') continue; // comments
102
103 // NS_LOG_DEBUG ("Input: [" << line << "]");
104
105 istringstream lineBuffer (line);
106 string from, to, attribute;
107
108 lineBuffer >> from >> to >> attribute;
109
110 if (processedLinks[to].size () != 0 &&
111 processedLinks[to].find (from) != processedLinks[to].end ())
112 {
113 continue; // duplicated link
114 }
115 processedLinks[from].insert (to);
116
117 Ptr<Node> fromNode = Names::Find<Node> (m_path, from);
118 if (fromNode == 0)
119 {
120 fromNode = CreateNode (from);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800121 }
122
123 Ptr<Node> toNode = Names::Find<Node> (m_path, to);
124 if (toNode == 0)
125 {
126 toNode = CreateNode (to);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800127 }
128
129 Link *link;
130 if (!repeatedRun)
131 link = new Link (fromNode, from, toNode, to);
132 else
133 {
134 NS_ASSERT (linkIterator != m_linksList.end ());
135 link = &(*linkIterator);
136
137 linkIterator++;
138 }
139
140 switch (m_inputType)
141 {
142 case WEIGHTS:
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800143 {
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800144 uint16_t metric = boost::lexical_cast<uint16_t> (attribute);
145 link->SetAttribute ("OSPF", boost::lexical_cast<string> (metric));
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800146 break;
147 }
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800148 case LATENCIES:
149 link->SetAttribute ("Delay", attribute);
150 break;
151 default:
152 ; //
153 }
154
155 NS_LOG_DEBUG ("Link " << from << " <==> " << to << " / " << attribute);
156 if (!repeatedRun)
157 {
158 AddLink (*link);
159 delete link;
160 }
161 }
162
163 topgen.close ();
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800164
165 if (!repeatedRun)
166 {
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800167 NS_LOG_INFO ("Rocketfuel topology created with " << m_nodes.GetN () << " nodes and " << LinksSize () << " links");
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800168 }
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800169 return m_nodes;
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800170}
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800171
172void
173RocketfuelWeightsReader::Commit ()
174{
175 ApplySettings ();
176
177 SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ());
178}
179
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800180void
181RocketfuelWeightsReader::SavePositions (const std::string &file) const
182{
183 ofstream os (file.c_str (), ios::trunc);
184 os << "router\n";
185
186 for (NodeContainer::Iterator node = m_nodes.Begin ();
187 node != m_nodes.End ();
188 node++)
189 {
190 std::string name = Names::FindName (*node);
191 Ptr<MobilityModel> mobility = (*node)->GetObject<MobilityModel> ();
192 Vector position = mobility->GetPosition ();
193
194 os << name << "\t" << "unknown" << "\t" << -position.y << "\t" << position.x << "\n";
195 }
196}
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800197
198// void
199// RocketfuelWeightsReader::Cheat (NodeContainer &nodes)
200// {
201// double epsilon = 1;
202
203// for (NodeContainer::Iterator i = nodes.Begin ();
204// i != nodes.End ();
205// i++)
206// {
207
208// }
209// }
210
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800211} /* namespace ns3 */