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 | |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 42 | #include "ns3/mobility-model.h" |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 43 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 44 | #include <regex.h> |
| 45 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 46 | #include <boost/foreach.hpp> |
| 47 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 48 | #include "../utils/spring-mobility-helper.h" |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 49 | |
| 50 | #include <iomanip> |
| 51 | #include <set> |
| 52 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 53 | using namespace std; |
| 54 | |
| 55 | NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader"); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 56 | |
| 57 | namespace ns3 { |
| 58 | |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 59 | RocketfuelWeightsReader::RocketfuelWeightsReader (const std::string &path/*=""*/) |
| 60 | : AnnotatedTopologyReader (path) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 61 | { |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 62 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 63 | SetMobilityModel ("ns3::SpringMobilityModel"); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | RocketfuelWeightsReader::~RocketfuelWeightsReader () |
| 67 | { |
| 68 | NS_LOG_FUNCTION (this); |
| 69 | } |
Ilya Moiseenko | 09ac899 | 2011-12-12 17:55:42 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 71 | void |
| 72 | RocketfuelWeightsReader::SetFileType (uint8_t inputType) |
| 73 | { |
| 74 | m_inputType = inputType; |
| 75 | } |
| 76 | |
| 77 | NodeContainer |
| 78 | RocketfuelWeightsReader::Read () |
| 79 | { |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 80 | if (m_inputType == POSITIONS) |
| 81 | return AnnotatedTopologyReader::Read (); |
| 82 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 83 | ifstream topgen; |
| 84 | topgen.open (GetFileName ().c_str ()); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 85 | |
| 86 | if ( !topgen.is_open () ) |
| 87 | { |
| 88 | NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading"); |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 89 | return m_nodes; |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 92 | 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 Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | Ptr<Node> toNode = Names::Find<Node> (m_path, to); |
| 124 | if (toNode == 0) |
| 125 | { |
| 126 | toNode = CreateNode (to); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 127 | } |
| 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 Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 144 | uint16_t metric = boost::lexical_cast<uint16_t> (attribute); |
| 145 | link->SetAttribute ("OSPF", boost::lexical_cast<string> (metric)); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 146 | break; |
| 147 | } |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 148 | case LATENCIES: |
Alexander Afanasyev | 1d2642a | 2012-01-02 16:20:05 -0800 | [diff] [blame^] | 149 | link->SetAttribute ("Delay", attribute+"ms"); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 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 Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 164 | |
| 165 | if (!repeatedRun) |
| 166 | { |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 167 | NS_LOG_INFO ("Rocketfuel topology created with " << m_nodes.GetN () << " nodes and " << LinksSize () << " links"); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 168 | } |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 169 | return m_nodes; |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 170 | } |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 171 | |
| 172 | void |
| 173 | RocketfuelWeightsReader::Commit () |
| 174 | { |
| 175 | ApplySettings (); |
| 176 | |
| 177 | SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ()); |
| 178 | } |
| 179 | |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 180 | void |
| 181 | RocketfuelWeightsReader::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 Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 197 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 198 | } /* namespace ns3 */ |