Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 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/constant-position-mobility-model.h" |
| 43 | #include "ns3/random-variable.h" |
| 44 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 45 | #include <regex.h> |
| 46 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 47 | #include <boost/foreach.hpp> |
| 48 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 49 | #include "../utils/spring-mobility-helper.h" |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 50 | |
| 51 | #include <iomanip> |
| 52 | #include <set> |
| 53 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 54 | using namespace std; |
| 55 | |
| 56 | NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader"); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 57 | |
| 58 | namespace ns3 { |
| 59 | |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 60 | RocketfuelWeightsReader::RocketfuelWeightsReader (const std::string &path/*=""*/) |
| 61 | : AnnotatedTopologyReader (path) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 63 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 64 | SetMobilityModel ("ns3::SpringMobilityModel"); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | RocketfuelWeightsReader::~RocketfuelWeightsReader () |
| 68 | { |
| 69 | NS_LOG_FUNCTION (this); |
| 70 | } |
Ilya Moiseenko | 09ac899 | 2011-12-12 17:55:42 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 72 | void |
| 73 | RocketfuelWeightsReader::SetFileType (uint8_t inputType) |
| 74 | { |
| 75 | m_inputType = inputType; |
| 76 | } |
| 77 | |
| 78 | NodeContainer |
| 79 | RocketfuelWeightsReader::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 Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 91 | 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 Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 144 | { |
| 145 | double metric = boost::lexical_cast<double> (attribute); |
| 146 | link->SetAttribute ("OSPF", boost::lexical_cast<string> (metric*2)); |
| 147 | break; |
| 148 | } |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 149 | 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 Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 165 | |
| 166 | if (!repeatedRun) |
| 167 | { |
| 168 | NS_LOG_INFO ("Rocketfuel topology created with " << nodes.GetN () << " nodes and " << LinksSize () << " links"); |
| 169 | } |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 170 | return nodes; |
| 171 | } |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame^] | 172 | |
| 173 | void |
| 174 | RocketfuelWeightsReader::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 Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 195 | } /* namespace ns3 */ |