Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame^] | 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame^] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame^] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame^] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame^] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | // Based on the code by Hajime Tazaki <tazaki@sfc.wide.ad.jp> |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "rocketfuel-map-reader.hpp" |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 23 | |
| 24 | #include "ns3/nstime.h" |
| 25 | #include "ns3/log.h" |
| 26 | #include "ns3/fatal-error.h" |
| 27 | #include "ns3/assert.h" |
| 28 | #include "ns3/names.h" |
| 29 | #include "ns3/net-device-container.h" |
| 30 | #include "ns3/point-to-point-helper.h" |
| 31 | #include "ns3/point-to-point-net-device.h" |
| 32 | #include "ns3/internet-stack-helper.h" |
| 33 | #include "ns3/ipv4-address-helper.h" |
| 34 | #include "ns3/ipv4-global-routing-helper.h" |
| 35 | #include "ns3/drop-tail-queue.h" |
| 36 | #include "ns3/ipv4-interface.h" |
| 37 | #include "ns3/ipv4.h" |
| 38 | #include "ns3/string.h" |
| 39 | #include "ns3/pointer.h" |
| 40 | #include "ns3/uinteger.h" |
| 41 | #include "ns3/ipv4-address.h" |
| 42 | #include "ns3/node-list.h" |
| 43 | #include "ns3/random-variable.h" |
| 44 | |
| 45 | #include "ns3/mobility-model.h" |
| 46 | |
| 47 | #include <regex.h> |
| 48 | |
| 49 | #include <boost/foreach.hpp> |
| 50 | #include <boost/lexical_cast.hpp> |
| 51 | |
| 52 | #include <boost/graph/graphviz.hpp> |
| 53 | #include <boost/graph/connected_components.hpp> |
| 54 | |
| 55 | #include <iomanip> |
| 56 | |
| 57 | using namespace std; |
| 58 | using namespace boost; |
| 59 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 60 | NS_LOG_COMPONENT_DEFINE("RocketfuelMapReader"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 61 | |
| 62 | namespace ns3 { |
| 63 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | RocketfuelMapReader::RocketfuelMapReader(const std::string& path /*=""*/, double scale /*=1.0*/, |
| 65 | const std::string& referenceOspfRate) |
| 66 | : AnnotatedTopologyReader(path, scale) |
| 67 | , m_referenceOspfRate(boost::lexical_cast<DataRate>(referenceOspfRate)) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | RocketfuelMapReader::~RocketfuelMapReader() |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
| 75 | NodeContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | RocketfuelMapReader::Read() |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 78 | NS_FATAL_ERROR("Deprecated call. Use the other overloaded method instead"); |
| 79 | return NodeContainer(); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 82 | /* uid @loc [+] [bb] (num_neigh) [&ext] -> <nuid-1> <nuid-2> ... {-euid} ... =name[!] rn */ |
| 83 | |
| 84 | #define REGMATCH_MAX 16 |
| 85 | |
| 86 | #define START "^" |
| 87 | #define END "$" |
| 88 | #define SPACE "[ \t]+" |
| 89 | #define MAYSPACE "[ \t]*" |
| 90 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | #define ROCKETFUEL_MAPS_LINE \ |
| 92 | START "(-*[0-9]+)" SPACE "(@[?A-Za-z0-9,+]+)" SPACE "(\\+)*" MAYSPACE "(bb)*" MAYSPACE \ |
| 93 | "\\(([0-9]+)\\)" SPACE "(&[0-9]+)*" MAYSPACE "->" MAYSPACE "(<[0-9 \t<>]+>)*" MAYSPACE \ |
| 94 | "(\\{-[0-9\\{\\} \t-]+\\})*" SPACE "=([A-Za-z0-9.!-]+)" SPACE "r([0-9])" MAYSPACE END |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 95 | |
| 96 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 97 | RocketfuelMapReader::CreateLink(string nodeName1, string nodeName2, double averageRtt, |
| 98 | const string& minBw, const string& maxBw, const string& minDelay, |
| 99 | const string& maxDelay) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | Ptr<Node> node1 = Names::Find<Node>(m_path, nodeName1); |
| 102 | Ptr<Node> node2 = Names::Find<Node>(m_path, nodeName2); |
| 103 | Link link(node1, nodeName1, node2, nodeName2); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | DataRate randBandwidth( |
| 106 | m_randVar.GetInteger(static_cast<uint32_t>(lexical_cast<DataRate>(minBw).GetBitRate()), |
| 107 | static_cast<uint32_t>(lexical_cast<DataRate>(maxBw).GetBitRate()))); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | int32_t metric = std::max(1, static_cast<int32_t>(1.0 * m_referenceOspfRate.GetBitRate() |
| 110 | / randBandwidth.GetBitRate())); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 111 | |
| 112 | Time randDelay = |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 113 | Time::FromDouble((m_randVar.GetValue(lexical_cast<Time>(minDelay).ToDouble(Time::US), |
| 114 | lexical_cast<Time>(maxDelay).ToDouble(Time::US))), |
| 115 | Time::US); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 117 | uint32_t queue = ceil(averageRtt * (randBandwidth.GetBitRate() / 8.0 / 1100.0)); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | link.SetAttribute("DataRate", boost::lexical_cast<string>(randBandwidth)); |
| 120 | link.SetAttribute("OSPF", boost::lexical_cast<string>(metric)); |
| 121 | link.SetAttribute("Delay", |
| 122 | boost::lexical_cast<string>(ceil(randDelay.ToDouble(Time::US))) + "us"); |
| 123 | link.SetAttribute("MaxPackets", boost::lexical_cast<string>(queue)); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | AddLink(link); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // NodeContainer |
| 129 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 130 | RocketfuelMapReader::GenerateFromMapsFile(int argc, char* argv[]) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 131 | { |
| 132 | string uid; |
| 133 | string loc; |
| 134 | string ptr; |
| 135 | string name; |
| 136 | string nuid; |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 137 | // bool dns = false; |
| 138 | // bool bb = false; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 139 | int num_neigh_s = 0; |
| 140 | unsigned int num_neigh = 0; |
| 141 | int radius = 0; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 142 | vector<string> neigh_list; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 143 | |
| 144 | uid = argv[0]; |
| 145 | loc = argv[1]; |
| 146 | |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 147 | // if (argv[2]) |
| 148 | // { |
| 149 | // dns = true; |
| 150 | // } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 152 | // if (argv[3]) |
| 153 | // { |
| 154 | // bb = true; |
| 155 | // } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 157 | num_neigh_s = ::atoi(argv[4]); |
| 158 | if (num_neigh_s < 0) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 159 | num_neigh = 0; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | NS_LOG_WARN("Negative number of neighbors given"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 161 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | else { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 163 | num_neigh = num_neigh_s; |
| 164 | } |
| 165 | |
| 166 | /* neighbors */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 167 | if (argv[6]) { |
| 168 | char* nbr; |
| 169 | char* stringp = argv[6]; |
| 170 | while ((nbr = strsep(&stringp, " \t")) != NULL) { |
| 171 | nbr[strlen(nbr) - 1] = '\0'; |
| 172 | neigh_list.push_back(nbr + 1); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 176 | if (num_neigh != neigh_list.size()) { |
| 177 | NS_LOG_WARN("Given number of neighbors = " << num_neigh << " != size of neighbors list = " |
| 178 | << neigh_list.size()); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* externs */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 182 | if (argv[7]) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 183 | // euid = argv[7]; |
| 184 | } |
| 185 | |
| 186 | /* name */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | if (argv[8]) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 188 | name = argv[8]; |
| 189 | } |
| 190 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 191 | radius = ::atoi(&argv[9][1]); |
| 192 | if (radius > 0) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Create node and link |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 197 | if (uid.empty()) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 198 | return; |
| 199 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 200 | node_map_t::iterator node = m_graphNodes.find(uid); |
| 201 | if (node == m_graphNodes.end()) { |
| 202 | bool ok; |
| 203 | tie(node, ok) = m_graphNodes.insert(make_pair(uid, add_vertex(nodeProperty(uid), m_graph))); |
| 204 | NS_ASSERT(ok == true); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 206 | put(vertex_index, m_graph, node->second, m_maxNodeId); |
| 207 | m_maxNodeId++; |
| 208 | } |
| 209 | |
| 210 | for (uint32_t i = 0; i < neigh_list.size(); ++i) { |
| 211 | nuid = neigh_list[i]; |
| 212 | |
| 213 | if (nuid.empty()) { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | node_map_t::iterator otherNode = m_graphNodes.find(nuid); |
| 218 | if (otherNode == m_graphNodes.end()) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 219 | bool ok; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 220 | tie(otherNode, ok) = |
| 221 | m_graphNodes.insert(make_pair(nuid, add_vertex(nodeProperty(nuid), m_graph))); |
| 222 | NS_ASSERT(ok == true); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 224 | put(vertex_index, m_graph, otherNode->second, m_maxNodeId); |
| 225 | m_maxNodeId++; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 228 | // cout << node->second << " <-> " << otherNode->second << endl; |
| 229 | // parallel edges are disabled in the graph, so no need to worry |
| 230 | add_edge(node->second, otherNode->second, m_graph); |
| 231 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 234 | void |
| 235 | RocketfuelMapReader::assignGw(Traits::vertex_descriptor vertex, uint32_t degree, |
| 236 | node_type_t nodeType) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 237 | { |
| 238 | graph_traits<Graph>::adjacency_iterator u, endu; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 239 | for (tie(u, endu) = adjacent_vertices(vertex, m_graph); u != endu; u++) { |
| 240 | if (get(vertex_rank, m_graph, *u) != UNKNOWN) |
| 241 | continue; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 242 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 243 | put(vertex_rank, m_graph, *u, nodeType); |
| 244 | put(vertex_color, m_graph, *u, "green"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 246 | uint32_t u_degree = out_degree(*u, m_graph); |
| 247 | if (u_degree < degree) |
| 248 | assignGw(*u, degree, BACKBONE); |
| 249 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 253 | RocketfuelMapReader::AssignClients(uint32_t clientDegree, uint32_t gwDegree) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 254 | { |
| 255 | graph_traits<Graph>::vertex_iterator v, endv; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 256 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 257 | uint32_t degree = out_degree(*v, m_graph); |
| 258 | if (degree == clientDegree) { |
| 259 | put(vertex_rank, m_graph, *v, CLIENT); |
| 260 | put(vertex_color, m_graph, *v, "red"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 261 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 262 | assignGw(*v, gwDegree + 1, GATEWAY); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 263 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 264 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | NodeContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 268 | RocketfuelMapReader::Read(RocketfuelParams params, bool keepOneComponent /*=true*/, |
| 269 | bool connectBackbones /*=true*/) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 270 | { |
| 271 | m_maxNodeId = 0; |
| 272 | |
| 273 | ifstream topgen; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 274 | topgen.open(GetFileName().c_str()); |
| 275 | // NodeContainer nodes; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 276 | UniformVariable var; |
| 277 | |
| 278 | istringstream lineBuffer; |
| 279 | string line; |
| 280 | int lineNumber = 0; |
| 281 | char errbuf[512]; |
| 282 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 283 | if (!topgen.is_open()) { |
| 284 | NS_LOG_WARN("Couldn't open the file " << GetFileName()); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 285 | return m_nodes; |
| 286 | } |
| 287 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 288 | while (!topgen.eof()) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 289 | int ret; |
| 290 | int argc; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 291 | char* argv[REGMATCH_MAX]; |
| 292 | char* buf; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 293 | |
| 294 | lineNumber++; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 295 | line.clear(); |
| 296 | lineBuffer.clear(); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 298 | getline(topgen, line); |
| 299 | buf = (char*)line.c_str(); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 300 | |
| 301 | regmatch_t regmatch[REGMATCH_MAX]; |
| 302 | regex_t regex; |
| 303 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 304 | ret = regcomp(®ex, ROCKETFUEL_MAPS_LINE, REG_EXTENDED | REG_NEWLINE); |
| 305 | if (ret != 0) { |
| 306 | regerror(ret, ®ex, errbuf, sizeof(errbuf)); |
| 307 | regfree(®ex); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 308 | continue; |
| 309 | } |
| 310 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 311 | ret = regexec(®ex, buf, REGMATCH_MAX, regmatch, 0); |
| 312 | if (ret == REG_NOMATCH) { |
| 313 | NS_LOG_WARN("match failed (maps file): %s" << buf); |
| 314 | regfree(®ex); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 315 | continue; |
| 316 | } |
| 317 | |
| 318 | line = buf; |
| 319 | argc = 0; |
| 320 | |
| 321 | /* regmatch[0] is the entire strings that matched */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 322 | for (int i = 1; i < REGMATCH_MAX; i++) { |
| 323 | if (regmatch[i].rm_so == -1) { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 324 | argv[i - 1] = NULL; |
| 325 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 326 | else { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 327 | line[regmatch[i].rm_eo] = '\0'; |
| 328 | argv[i - 1] = &line[regmatch[i].rm_so]; |
| 329 | argc = i; |
| 330 | } |
| 331 | } |
| 332 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 333 | GenerateFromMapsFile(argc, argv); |
| 334 | regfree(®ex); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 337 | if (keepOneComponent) { |
| 338 | NS_LOG_DEBUG("Before eliminating disconnected nodes: " << num_vertices(m_graph)); |
| 339 | KeepOnlyBiggestConnectedComponent(); |
| 340 | NS_LOG_DEBUG("After eliminating disconnected nodes: " << num_vertices(m_graph)); |
| 341 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 342 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 343 | for (int clientDegree = 1; clientDegree <= params.clientNodeDegrees; clientDegree++) { |
| 344 | AssignClients(clientDegree, std::min(clientDegree, 3)); |
| 345 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 346 | |
| 347 | graph_traits<Graph>::vertex_iterator v, endv; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 348 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 349 | node_type_t type = get(vertex_rank, m_graph, *v); |
| 350 | if (type == UNKNOWN) { |
| 351 | put(vertex_rank, m_graph, *v, BACKBONE); |
| 352 | put(vertex_color, m_graph, *v, "blue"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 353 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 354 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 355 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 356 | if (connectBackbones) { |
| 357 | ConnectBackboneRouters(); |
| 358 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 359 | |
| 360 | graph_traits<Graph>::edge_iterator e, ende; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 361 | for (tie(e, ende) = edges(m_graph); e != ende;) { |
| 362 | Traits::vertex_descriptor u = source(*e, m_graph), v = target(*e, m_graph); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 363 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 364 | node_type_t u_type = get(vertex_rank, m_graph, u), v_type = get(vertex_rank, m_graph, v); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 365 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 366 | if (u_type == BACKBONE && v_type == BACKBONE) { |
| 367 | // ok |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 368 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 369 | else if ((u_type == GATEWAY && v_type == BACKBONE) |
| 370 | || (u_type == BACKBONE && v_type == GATEWAY)) { |
| 371 | // ok |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 372 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 373 | else if (u_type == GATEWAY && v_type == GATEWAY) { |
| 374 | // ok |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 375 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 376 | else if ((u_type == GATEWAY && v_type == CLIENT) || (u_type == CLIENT && v_type == GATEWAY)) { |
| 377 | // ok |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 378 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 379 | else { |
| 380 | // not ok |
| 381 | NS_LOG_DEBUG("Wrong link type between nodes: " << u_type << " <-> " << v_type |
| 382 | << " (deleting the link)"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 383 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 384 | graph_traits<Graph>::edge_iterator tmp = e; |
| 385 | tmp++; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 386 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 387 | remove_edge(*e, m_graph); |
| 388 | e = tmp; |
| 389 | continue; |
| 390 | } |
| 391 | e++; |
| 392 | } |
| 393 | |
| 394 | if (keepOneComponent) { |
| 395 | NS_LOG_DEBUG("Before 2 eliminating disconnected nodes: " << num_vertices(m_graph)); |
| 396 | KeepOnlyBiggestConnectedComponent(); |
| 397 | NS_LOG_DEBUG("After 2 eliminating disconnected nodes: " << num_vertices(m_graph)); |
| 398 | } |
| 399 | |
| 400 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 401 | string nodeName = get(vertex_name, m_graph, *v); |
| 402 | Ptr<Node> node = CreateNode(nodeName, 0); |
| 403 | |
| 404 | node_type_t type = get(vertex_rank, m_graph, *v); |
| 405 | switch (type) { |
| 406 | case BACKBONE: |
| 407 | Names::Rename(nodeName, "bb-" + nodeName); |
| 408 | put(vertex_name, m_graph, *v, "bb-" + nodeName); |
| 409 | m_backboneRouters.Add(node); |
| 410 | break; |
| 411 | case CLIENT: |
| 412 | Names::Rename(nodeName, "leaf-" + nodeName); |
| 413 | put(vertex_name, m_graph, *v, "leaf-" + nodeName); |
| 414 | m_customerRouters.Add(node); |
| 415 | break; |
| 416 | case GATEWAY: |
| 417 | Names::Rename(nodeName, "gw-" + nodeName); |
| 418 | put(vertex_name, m_graph, *v, "gw-" + nodeName); |
| 419 | m_gatewayRouters.Add(node); |
| 420 | break; |
| 421 | case UNKNOWN: |
| 422 | NS_FATAL_ERROR("Should not happen"); |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | for (tie(e, ende) = edges(m_graph); e != ende; e++) { |
| 428 | Traits::vertex_descriptor u = source(*e, m_graph), v = target(*e, m_graph); |
| 429 | |
| 430 | node_type_t u_type = get(vertex_rank, m_graph, u), v_type = get(vertex_rank, m_graph, v); |
| 431 | |
| 432 | string u_name = get(vertex_name, m_graph, u), v_name = get(vertex_name, m_graph, v); |
| 433 | |
| 434 | if (u_type == BACKBONE && v_type == BACKBONE) { |
| 435 | CreateLink(u_name, v_name, params.averageRtt, params.minb2bBandwidth, params.maxb2bBandwidth, |
| 436 | params.minb2bDelay, params.maxb2bDelay); |
| 437 | } |
| 438 | else if ((u_type == GATEWAY && v_type == BACKBONE) |
| 439 | || (u_type == BACKBONE && v_type == GATEWAY)) { |
| 440 | CreateLink(u_name, v_name, params.averageRtt, params.minb2gBandwidth, params.maxb2gBandwidth, |
| 441 | params.minb2gDelay, params.maxb2gDelay); |
| 442 | } |
| 443 | else if (u_type == GATEWAY && v_type == GATEWAY) { |
| 444 | CreateLink(u_name, v_name, params.averageRtt, params.minb2gBandwidth, params.maxb2gBandwidth, |
| 445 | params.minb2gDelay, params.maxb2gDelay); |
| 446 | } |
| 447 | else if ((u_type == GATEWAY && v_type == CLIENT) || (u_type == CLIENT && v_type == GATEWAY)) { |
| 448 | CreateLink(u_name, v_name, params.averageRtt, params.ming2cBandwidth, params.maxg2cBandwidth, |
| 449 | params.ming2cDelay, params.maxg2cDelay); |
| 450 | } |
| 451 | else { |
| 452 | NS_FATAL_ERROR("Wrong link type between nodes: " << u_type << " <-> " << v_type); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | ApplySettings(); |
| 457 | |
| 458 | NS_LOG_INFO("Clients: " << m_customerRouters.GetN()); |
| 459 | NS_LOG_INFO("Gateways: " << m_gatewayRouters.GetN()); |
| 460 | NS_LOG_INFO("Backbones: " << m_backboneRouters.GetN()); |
| 461 | NS_LOG_INFO("Links: " << GetLinks().size()); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 462 | |
| 463 | return m_nodes; |
| 464 | } |
| 465 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 466 | const NodeContainer& |
| 467 | RocketfuelMapReader::GetBackboneRouters() const |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 468 | { |
| 469 | return m_backboneRouters; |
| 470 | } |
| 471 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 472 | const NodeContainer& |
| 473 | RocketfuelMapReader::GetGatewayRouters() const |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 474 | { |
| 475 | return m_gatewayRouters; |
| 476 | } |
| 477 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 478 | const NodeContainer& |
| 479 | RocketfuelMapReader::GetCustomerRouters() const |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 480 | { |
| 481 | return m_customerRouters; |
| 482 | } |
| 483 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 484 | static void |
| 485 | nodeWriter(std::ostream& os, NodeContainer& m) |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 486 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 487 | for (NodeContainer::Iterator node = m.Begin(); node != m.End(); node++) { |
| 488 | std::string name = Names::FindName(*node); |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 489 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 490 | os << name << "\t" |
| 491 | << "NA" |
| 492 | << "\t" << 0 << "\t" << 0 << "\n"; |
| 493 | } |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 494 | }; |
| 495 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 496 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 497 | RocketfuelMapReader::SaveTopology(const std::string& file) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 498 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 499 | ofstream os(file.c_str(), ios::trunc); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 500 | os << "# any empty lines and lines starting with '#' symbol is ignored\n" |
| 501 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 502 | << "# The file should contain exactly two sections: router and link, each starting with the " |
| 503 | "corresponding keyword\n" |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 504 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 505 | << "# router section defines topology nodes and their relative positions (e.g., to use in " |
| 506 | "visualizer)\n" |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 507 | << "router\n" |
| 508 | << "\n" |
| 509 | << "# each line in this section represents one router and should have the following data\n" |
| 510 | << "# node comment yPos xPos\n"; |
| 511 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 512 | nodeWriter(os, m_backboneRouters); |
| 513 | nodeWriter(os, m_gatewayRouters); |
| 514 | nodeWriter(os, m_customerRouters); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 515 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 516 | os << "# link section defines point-to-point links between nodes and characteristics of these " |
| 517 | "links\n" |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 518 | << "\n" |
| 519 | << "link\n" |
| 520 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 521 | << "# Each line should be in the following format (only first two are required, the rest can " |
| 522 | "be omitted)\n" |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 523 | << "# srcNode dstNode bandwidth metric delay queue\n" |
| 524 | << "# bandwidth: link bandwidth\n" |
| 525 | << "# metric: routing metric\n" |
| 526 | << "# delay: link delay\n" |
| 527 | << "# queue: MaxPackets for transmission queue on the link (both directions)\n"; |
| 528 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 529 | for (std::list<Link>::iterator link = m_linksList.begin(); link != m_linksList.end(); link++) { |
| 530 | string src = Names::FindName(link->GetFromNode()); |
| 531 | string dst = Names::FindName(link->GetToNode()); |
| 532 | os << src << "\t"; |
| 533 | os << dst << "\t"; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 534 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 535 | string tmp; |
| 536 | if (link->GetAttributeFailSafe("DataRate", tmp)) |
| 537 | os << link->GetAttribute("DataRate") << "\t"; |
| 538 | else |
| 539 | NS_FATAL_ERROR("DataRate must be specified for the link"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 540 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 541 | if (link->GetAttributeFailSafe("OSPF", tmp)) |
| 542 | os << link->GetAttribute("OSPF") << "\t"; |
| 543 | else { |
| 544 | DataRate rate = boost::lexical_cast<DataRate>(link->GetAttribute("DataRate")); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 545 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 546 | int32_t cost = std::max(1, static_cast<int32_t>(1.0 * m_referenceOspfRate.GetBitRate() |
| 547 | / rate.GetBitRate())); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 548 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 549 | os << cost << "\t"; |
Alexander Afanasyev | 29c10ed | 2013-01-23 18:26:07 -0800 | [diff] [blame] | 550 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 551 | |
| 552 | if (link->GetAttributeFailSafe("Delay", tmp)) { |
| 553 | os << link->GetAttribute("Delay") << "\t"; |
| 554 | |
| 555 | if (link->GetAttributeFailSafe("MaxPackets", tmp)) { |
| 556 | os << link->GetAttribute("MaxPackets") << "\t"; |
| 557 | } |
| 558 | } |
| 559 | os << "\n"; |
| 560 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 563 | template<class Names, class Colors> |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 564 | class name_color_writer { |
| 565 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 566 | name_color_writer(Names _names, Colors _colors) |
| 567 | : names(_names) |
| 568 | , colors(_colors) |
| 569 | { |
| 570 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 571 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 572 | template<class VertexOrEdge> |
| 573 | void |
| 574 | operator()(std::ostream& out, const VertexOrEdge& v) const |
| 575 | { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 576 | // out << "[label=\"" << names[v] << "\",style=filled,fillcolor=\"" << colors[v] << "\"]"; |
| 577 | out << "[shape=\"circle\",width=0.1,label=\"\",style=filled,fillcolor=\"" << colors[v] << "\"]"; |
| 578 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 579 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 580 | private: |
| 581 | Names names; |
| 582 | Colors colors; |
| 583 | }; |
| 584 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 585 | template<class Names, class Colors> |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 586 | inline name_color_writer<Names, Colors> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 587 | make_name_color_writer(Names n, Colors c) |
| 588 | { |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 589 | return name_color_writer<Names, Colors>(n, c); |
| 590 | } |
| 591 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 592 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 593 | RocketfuelMapReader::SaveGraphviz(const std::string& file) |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 594 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 595 | ofstream of(file.c_str()); |
| 596 | property_map<Graph, vertex_name_t>::type names = get(vertex_name, m_graph); |
| 597 | property_map<Graph, vertex_color_t>::type colors = get(vertex_color, m_graph); |
| 598 | write_graphviz(of, m_graph, make_name_color_writer(names, colors)); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 601 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 602 | RocketfuelMapReader::KeepOnlyBiggestConnectedComponent() |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 603 | { |
| 604 | std::map<graph_traits<Graph>::vertex_descriptor, int> temp; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 605 | associative_property_map<std::map<graph_traits<Graph>::vertex_descriptor, int>> components(temp); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 606 | |
| 607 | // //check if topology has breaks in its structure and trim it if yes |
| 608 | // property_map<Graph, vertex_index1_t>::type components = get (vertex_index1, m_graph); |
| 609 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 610 | int num = connected_components(m_graph, components); |
| 611 | NS_LOG_DEBUG("Topology has " << num << " components"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 612 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 613 | vector<int> sizes(num, 0); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 614 | |
| 615 | graph_traits<Graph>::vertex_iterator v, endv; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 616 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 617 | sizes[get(components, *v)]++; |
| 618 | } |
| 619 | int largestComponent = max_element(sizes.begin(), sizes.end()) - sizes.begin(); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 620 | // cout << "Largest: " << largestComponent << endl; |
| 621 | |
| 622 | // for (int i =0 ; i < num; i++) cout << sizes[i] << " "; |
| 623 | // cout << endl; |
| 624 | |
| 625 | //////////////////////////////////////////////////// |
| 626 | // remove nodes and edges from smaller components // |
| 627 | //////////////////////////////////////////////////// |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 628 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 629 | if (get(components, *v) == largestComponent) |
| 630 | continue; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 631 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 632 | clear_vertex(*v, m_graph); |
| 633 | } |
| 634 | |
| 635 | // this works only if vertices are organized in listS or setS (iterator is not invalidated on |
| 636 | // remove) |
| 637 | for (tie(v, endv) = vertices(m_graph); v != endv;) { |
| 638 | if (get(components, *v) == largestComponent) { |
| 639 | v++; |
| 640 | continue; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 643 | graph_traits<Graph>::vertex_iterator tmp = v; |
| 644 | tmp++; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 645 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 646 | remove_vertex(*v, m_graph); |
| 647 | v = tmp; |
| 648 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 649 | |
| 650 | int index = 0; |
| 651 | // renumber nodes |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 652 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 653 | put(vertex_index, m_graph, *v, index++); |
| 654 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 658 | RocketfuelMapReader::ConnectBackboneRouters() |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 659 | { |
| 660 | // not the tricky part. we want backbone to be a fully connected component, |
| 661 | // so traffic doesn't bounce from backbone to gateway and back |
| 662 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 663 | typedef adjacency_list<setS, setS, boost::undirectedS, |
| 664 | property<vertex_name_t, Traits::vertex_descriptor, |
| 665 | property<vertex_index_t, int, property<vertex_index1_t, int>>>> |
| 666 | BbGraph; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 667 | BbGraph bbGraph; |
| 668 | map<Traits::vertex_descriptor, graph_traits<BbGraph>::vertex_descriptor> nodeMap; |
| 669 | |
| 670 | int index = 0; |
| 671 | |
| 672 | graph_traits<Graph>::vertex_iterator v, endv; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 673 | for (tie(v, endv) = vertices(m_graph); v != endv; v++) { |
| 674 | node_type_t type = get(vertex_rank, m_graph, *v); |
| 675 | if (type == BACKBONE) { |
| 676 | graph_traits<BbGraph>::vertex_descriptor newv = add_vertex(*v, bbGraph); |
| 677 | put(vertex_index, bbGraph, newv, index++); |
| 678 | nodeMap[*v] = newv; |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 679 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 680 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 681 | |
| 682 | graph_traits<BbGraph>::vertex_iterator bb, endBb; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 683 | for (tie(bb, endBb) = vertices(bbGraph); bb != endBb; bb++) { |
| 684 | Traits::vertex_descriptor actualVertex = get(vertex_name, bbGraph, *bb); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 685 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 686 | graph_traits<Graph>::adjacency_iterator u, endu; |
| 687 | for (tie(u, endu) = adjacent_vertices(actualVertex, m_graph); u != endu; u++) { |
| 688 | if (nodeMap.find(*u) != nodeMap.end()) { |
| 689 | add_edge(nodeMap[actualVertex], nodeMap[*u], bbGraph); |
| 690 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 691 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 692 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 693 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 694 | property_map<BbGraph, vertex_index1_t>::type components = get(vertex_index1, bbGraph); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 695 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 696 | int num = connected_components(bbGraph, components); |
| 697 | NS_LOG_DEBUG("Backbone has " << num << " components"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 698 | if (num == 1) |
| 699 | return; // nothing to do |
| 700 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 701 | vector<vector<graph_traits<BbGraph>::vertex_descriptor>> subgraphs(num); |
| 702 | for (tie(bb, endBb) = vertices(bbGraph); bb != endBb; bb++) { |
| 703 | int component = get(vertex_index1, bbGraph, *bb); |
| 704 | subgraphs[component].push_back(*bb); |
| 705 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 706 | |
| 707 | UniformVariable randVar; |
| 708 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 709 | for (int i = 1; i < num; i++) { |
| 710 | int node1 = randVar.GetInteger(0, subgraphs[i - 1].size() - 1); |
| 711 | int node2 = randVar.GetInteger(0, subgraphs[i].size() - 1); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 712 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 713 | Traits::vertex_descriptor v1 = get(vertex_name, bbGraph, subgraphs[i - 1][node1]), |
| 714 | v2 = get(vertex_name, bbGraph, subgraphs[i][node2]); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 715 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 716 | NS_LOG_DEBUG("Connecting " << get(vertex_name, m_graph, v1) << "[" << node1 << "] with " |
| 717 | << get(vertex_name, m_graph, v2) << "[" << node2 << "]"); |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 718 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 719 | add_edge(v1, v2, m_graph); |
| 720 | } |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Alexander Afanasyev | 957a84a | 2013-01-23 10:21:06 -0800 | [diff] [blame] | 723 | } /* namespace ns3 */ |