Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [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. |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [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. |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [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. |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [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> |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "annotated-topology-reader.hpp" |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 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" |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 41 | #include "ns3/random-variable.h" |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 42 | #include "ns3/error-model.h" |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 43 | #include "ns3/constant-position-mobility-model.h" |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | dca091a | 2015-01-01 20:51:27 -0800 | [diff] [blame] | 45 | #include "model/ndn-l3-protocol.hpp" |
| 46 | #include "model/ndn-net-device-face.hpp" |
| 47 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 48 | #include <boost/foreach.hpp> |
| 49 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 50 | #include <boost/tokenizer.hpp> |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 52 | #include <boost/graph/adjacency_list.hpp> |
| 53 | #include <boost/graph/graphviz.hpp> |
| 54 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 55 | #include <set> |
| 56 | |
Alexander Afanasyev | ff6e369 | 2012-07-30 00:11:02 -0700 | [diff] [blame] | 57 | #ifdef NS3_MPI |
| 58 | #include <ns3/mpi-interface.h> |
| 59 | #endif |
| 60 | |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 61 | using namespace std; |
| 62 | |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 63 | namespace ns3 { |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | NS_LOG_COMPONENT_DEFINE("AnnotatedTopologyReader"); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | AnnotatedTopologyReader::AnnotatedTopologyReader(const std::string& path, double scale /*=1.0*/) |
| 68 | : m_path(path) |
| 69 | , m_randX(0, 100.0) |
| 70 | , m_randY(0, 100.0) |
| 71 | , m_scale(scale) |
| 72 | , m_requiredPartitions(1) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 73 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | SetMobilityModel("ns3::ConstantPositionMobilityModel"); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 77 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 79 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | AnnotatedTopologyReader::SetBoundingBox(double ulx, double uly, double lrx, double lry) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 81 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 82 | NS_LOG_FUNCTION(this << ulx << uly << lrx << lry); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | m_randX = UniformVariable(ulx, lrx); |
| 85 | m_randY = UniformVariable(uly, lry); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 86 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 88 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 89 | AnnotatedTopologyReader::SetMobilityModel(const std::string& model) |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 90 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | NS_LOG_FUNCTION(this << model); |
| 92 | m_mobilityFactory.SetTypeId(model); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | AnnotatedTopologyReader::~AnnotatedTopologyReader() |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 96 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 97 | NS_LOG_FUNCTION(this); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 98 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 100 | Ptr<Node> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | AnnotatedTopologyReader::CreateNode(const std::string name, uint32_t systemId) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | NS_LOG_FUNCTION(this << name); |
| 104 | m_requiredPartitions = std::max(m_requiredPartitions, systemId + 1); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | Ptr<Node> node = CreateObject<Node>(systemId); |
Alexander Afanasyev | 5bcdc99 | 2012-11-19 22:25:55 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | Names::Add(m_path, name, node); |
| 109 | m_nodes.Add(node); |
Alexander Afanasyev | 5bcdc99 | 2012-11-19 22:25:55 -0800 | [diff] [blame] | 110 | |
| 111 | return node; |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Ptr<Node> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | AnnotatedTopologyReader::CreateNode(const std::string name, double posX, double posY, |
| 116 | uint32_t systemId) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 118 | NS_LOG_FUNCTION(this << name << posX << posY); |
| 119 | m_requiredPartitions = std::max(m_requiredPartitions, systemId + 1); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 121 | Ptr<Node> node = CreateObject<Node>(systemId); |
| 122 | Ptr<MobilityModel> loc = DynamicCast<MobilityModel>(m_mobilityFactory.Create()); |
| 123 | node->AggregateObject(loc); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | loc->SetPosition(Vector(posX, posY, 0)); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 127 | Names::Add(m_path, name, node); |
| 128 | m_nodes.Add(node); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 129 | |
| 130 | return node; |
| 131 | } |
| 132 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 133 | NodeContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 134 | AnnotatedTopologyReader::GetNodes() const |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 135 | { |
| 136 | return m_nodes; |
| 137 | } |
| 138 | |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 139 | const std::list<TopologyReader::Link>& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | AnnotatedTopologyReader::GetLinks() const |
Ilya Moiseenko | ad9e8ab | 2012-01-11 19:58:34 -0800 | [diff] [blame] | 141 | { |
| 142 | return m_linksList; |
| 143 | } |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 144 | |
| 145 | NodeContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | AnnotatedTopologyReader::Read(void) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 147 | { |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 148 | ifstream topgen; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | topgen.open(GetFileName().c_str()); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | if (!topgen.is_open() || !topgen.good()) { |
| 152 | NS_FATAL_ERROR("Cannot open file " << GetFileName() << " for reading"); |
| 153 | return m_nodes; |
| 154 | } |
| 155 | |
| 156 | while (!topgen.eof()) { |
| 157 | string line; |
| 158 | getline(topgen, line); |
| 159 | |
| 160 | if (line == "router") |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (topgen.eof()) { |
| 165 | NS_FATAL_ERROR("Topology file " << GetFileName() << " does not have \"router\" section"); |
| 166 | return m_nodes; |
| 167 | } |
| 168 | |
| 169 | while (!topgen.eof()) { |
| 170 | string line; |
| 171 | getline(topgen, line); |
| 172 | if (line[0] == '#') |
| 173 | continue; // comments |
| 174 | if (line == "link") |
| 175 | break; // stop reading nodes |
| 176 | |
| 177 | istringstream lineBuffer(line); |
| 178 | string name, city; |
| 179 | double latitude = 0, longitude = 0; |
| 180 | uint32_t systemId = 0; |
| 181 | |
| 182 | lineBuffer >> name >> city >> latitude >> longitude >> systemId; |
| 183 | if (name.empty()) |
| 184 | continue; |
| 185 | |
| 186 | Ptr<Node> node; |
| 187 | |
| 188 | if (abs(latitude) > 0.001 && abs(latitude) > 0.001) |
| 189 | node = CreateNode(name, m_scale * longitude, -m_scale * latitude, systemId); |
| 190 | else { |
| 191 | UniformVariable var(0, 200); |
| 192 | node = CreateNode(name, var.GetValue(), var.GetValue(), systemId); |
| 193 | // node = CreateNode (name, systemId); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 194 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 195 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 197 | map<string, set<string>> processedLinks; // to eliminate duplications |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 198 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 199 | if (topgen.eof()) { |
| 200 | NS_LOG_ERROR("Topology file " << GetFileName() << " does not have \"link\" section"); |
| 201 | return m_nodes; |
| 202 | } |
Alexander Afanasyev | b5e54f9 | 2012-10-29 10:15:59 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 204 | // SeekToSection ("link"); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 205 | while (!topgen.eof()) { |
| 206 | string line; |
| 207 | getline(topgen, line); |
| 208 | if (line == "") |
| 209 | continue; |
| 210 | if (line[0] == '#') |
| 211 | continue; // comments |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 212 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 213 | // NS_LOG_DEBUG ("Input: [" << line << "]"); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 214 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 215 | istringstream lineBuffer(line); |
| 216 | string from, to, capacity, metric, delay, maxPackets, lossRate; |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 217 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 218 | lineBuffer >> from >> to >> capacity >> metric >> delay >> maxPackets >> lossRate; |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 219 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 220 | if (processedLinks[to].size() != 0 |
| 221 | && processedLinks[to].find(from) != processedLinks[to].end()) { |
| 222 | continue; // duplicated link |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 223 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 224 | processedLinks[from].insert(to); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 225 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 226 | Ptr<Node> fromNode = Names::Find<Node>(m_path, from); |
| 227 | NS_ASSERT_MSG(fromNode != 0, from << " node not found"); |
| 228 | Ptr<Node> toNode = Names::Find<Node>(m_path, to); |
| 229 | NS_ASSERT_MSG(toNode != 0, to << " node not found"); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 231 | Link link(fromNode, from, toNode, to); |
| 232 | |
| 233 | link.SetAttribute("DataRate", capacity); |
| 234 | link.SetAttribute("OSPF", metric); |
| 235 | |
| 236 | if (!delay.empty()) |
| 237 | link.SetAttribute("Delay", delay); |
| 238 | if (!maxPackets.empty()) |
| 239 | link.SetAttribute("MaxPackets", maxPackets); |
| 240 | |
| 241 | // Saran Added lossRate |
| 242 | if (!lossRate.empty()) |
| 243 | link.SetAttribute("LossRate", lossRate); |
| 244 | |
| 245 | AddLink(link); |
| 246 | NS_LOG_DEBUG("New link " << from << " <==> " << to << " / " << capacity << " with " << metric |
| 247 | << " metric (" << delay << ", " << maxPackets << ", " << lossRate |
| 248 | << ")"); |
| 249 | } |
| 250 | |
| 251 | NS_LOG_INFO("Annotated topology created with " << m_nodes.GetN() << " nodes and " << LinksSize() |
| 252 | << " links"); |
| 253 | topgen.close(); |
| 254 | |
| 255 | ApplySettings(); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 256 | |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 257 | return m_nodes; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 258 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 259 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 260 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 261 | AnnotatedTopologyReader::AssignIpv4Addresses(Ipv4Address base) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 262 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 263 | Ipv4AddressHelper address(base, Ipv4Mask("/24")); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 264 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 265 | BOOST_FOREACH (const Link& link, m_linksList) { |
| 266 | address.Assign(NetDeviceContainer(link.GetFromNetDevice(), link.GetToNetDevice())); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 267 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 268 | base = Ipv4Address(base.Get() + 256); |
| 269 | address.SetBase(base, Ipv4Mask("/24")); |
| 270 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 271 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 272 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 273 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 274 | AnnotatedTopologyReader::ApplyOspfMetric() |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 275 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 276 | BOOST_FOREACH (const Link& link, m_linksList) { |
| 277 | NS_LOG_DEBUG("OSPF: " << link.GetAttribute("OSPF")); |
| 278 | uint16_t metric = boost::lexical_cast<uint16_t>(link.GetAttribute("OSPF")); |
| 279 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 280 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 281 | Ptr<Ipv4> ipv4 = link.GetFromNode()->GetObject<Ipv4>(); |
| 282 | if (ipv4 != 0) { |
| 283 | int32_t interfaceId = ipv4->GetInterfaceForDevice(link.GetFromNetDevice()); |
| 284 | NS_ASSERT(interfaceId >= 0); |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 285 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 286 | ipv4->SetMetric(interfaceId, metric); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 287 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 288 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 289 | Ptr<ndn::L3Protocol> ndn = link.GetFromNode()->GetObject<ndn::L3Protocol>(); |
| 290 | if (ndn != 0) { |
Alexander Afanasyev | dca091a | 2015-01-01 20:51:27 -0800 | [diff] [blame] | 291 | shared_ptr<ndn::Face> face = ndn->getFaceByNetDevice(link.GetFromNetDevice()); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 292 | NS_ASSERT(face != 0); |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 293 | |
Alexander Afanasyev | dca091a | 2015-01-01 20:51:27 -0800 | [diff] [blame] | 294 | face->setMetric(metric); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 295 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 296 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 297 | |
| 298 | { |
| 299 | Ptr<Ipv4> ipv4 = link.GetToNode()->GetObject<Ipv4>(); |
| 300 | if (ipv4 != 0) { |
| 301 | int32_t interfaceId = ipv4->GetInterfaceForDevice(link.GetToNetDevice()); |
| 302 | NS_ASSERT(interfaceId >= 0); |
| 303 | |
| 304 | ipv4->SetMetric(interfaceId, metric); |
| 305 | } |
| 306 | |
| 307 | Ptr<ndn::L3Protocol> ndn = link.GetToNode()->GetObject<ndn::L3Protocol>(); |
| 308 | if (ndn != 0) { |
Alexander Afanasyev | dca091a | 2015-01-01 20:51:27 -0800 | [diff] [blame] | 309 | shared_ptr<ndn::Face> face = ndn->getFaceByNetDevice(link.GetToNetDevice()); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 310 | NS_ASSERT(face != 0); |
| 311 | |
Alexander Afanasyev | dca091a | 2015-01-01 20:51:27 -0800 | [diff] [blame] | 312 | face->setMetric(metric); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 319 | AnnotatedTopologyReader::ApplySettings() |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 320 | { |
Alexander Afanasyev | ff6e369 | 2012-07-30 00:11:02 -0700 | [diff] [blame] | 321 | #ifdef NS3_MPI |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 322 | if (MpiInterface::IsEnabled() && MpiInterface::GetSize() != m_requiredPartitions) { |
| 323 | std::cerr << "MPI interface is enabled, but number of partitions (" << MpiInterface::GetSize() |
| 324 | << ") is not equal to number of partitions in the topology (" << m_requiredPartitions |
| 325 | << ")"; |
| 326 | exit(-1); |
| 327 | } |
Alexander Afanasyev | ff6e369 | 2012-07-30 00:11:02 -0700 | [diff] [blame] | 328 | #endif |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 329 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 330 | PointToPointHelper p2p; |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 331 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 332 | BOOST_FOREACH (Link& link, m_linksList) { |
| 333 | // cout << "Link: " << Findlink.GetFromNode () << ", " << link.GetToNode () << endl; |
| 334 | string tmp; |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 336 | //////////////////////////////////////////////// |
| 337 | if (link.GetAttributeFailSafe("MaxPackets", tmp)) { |
| 338 | NS_LOG_INFO("MaxPackets = " + link.GetAttribute("MaxPackets")); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 339 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 340 | try { |
| 341 | uint32_t maxPackets = boost::lexical_cast<uint32_t>(link.GetAttribute("MaxPackets")); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 342 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 343 | // compatibility mode. Only DropTailQueue is supported |
| 344 | p2p.SetQueue("ns3::DropTailQueue", "MaxPackets", UintegerValue(maxPackets)); |
| 345 | } |
| 346 | catch (...) { |
| 347 | typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer; |
Alexander Afanasyev | 66ae18b | 2015-01-06 18:30:39 -0800 | [diff] [blame] | 348 | std::string value = link.GetAttribute("MaxPackets"); |
| 349 | tokenizer tok(value); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 350 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 351 | tokenizer::iterator token = tok.begin(); |
| 352 | p2p.SetQueue(*token); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 353 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 354 | for (token++; token != tok.end(); token++) { |
| 355 | boost::escaped_list_separator<char> separator('\\', '=', '\"'); |
| 356 | tokenizer attributeTok(*token, separator); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 357 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 358 | tokenizer::iterator attributeToken = attributeTok.begin(); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 359 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 360 | string attribute = *attributeToken; |
| 361 | attributeToken++; |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 362 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 363 | if (attributeToken == attributeTok.end()) { |
| 364 | NS_LOG_ERROR("Queue attribute [" << *token |
| 365 | << "] should be in form <Attribute>=<Value>"); |
| 366 | continue; |
| 367 | } |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 368 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 369 | string value = *attributeToken; |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 370 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 371 | p2p.SetQueueAttribute(attribute, StringValue(value)); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 372 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 373 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 374 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 375 | |
| 376 | if (link.GetAttributeFailSafe("DataRate", tmp)) { |
| 377 | NS_LOG_INFO("DataRate = " + link.GetAttribute("DataRate")); |
| 378 | p2p.SetDeviceAttribute("DataRate", StringValue(link.GetAttribute("DataRate"))); |
| 379 | } |
| 380 | |
| 381 | if (link.GetAttributeFailSafe("Delay", tmp)) { |
| 382 | NS_LOG_INFO("Delay = " + link.GetAttribute("Delay")); |
| 383 | p2p.SetChannelAttribute("Delay", StringValue(link.GetAttribute("Delay"))); |
| 384 | } |
| 385 | |
| 386 | NetDeviceContainer nd = p2p.Install(link.GetFromNode(), link.GetToNode()); |
| 387 | link.SetNetDevices(nd.Get(0), nd.Get(1)); |
| 388 | |
| 389 | //////////////////////////////////////////////// |
| 390 | if (link.GetAttributeFailSafe("LossRate", tmp)) { |
| 391 | NS_LOG_INFO("LinkError = " + link.GetAttribute("LossRate")); |
| 392 | |
| 393 | typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer; |
Alexander Afanasyev | 66ae18b | 2015-01-06 18:30:39 -0800 | [diff] [blame] | 394 | std::string value = link.GetAttribute("LossRate"); |
| 395 | tokenizer tok(value); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 396 | |
| 397 | tokenizer::iterator token = tok.begin(); |
| 398 | ObjectFactory factory(*token); |
| 399 | |
| 400 | for (token++; token != tok.end(); token++) { |
| 401 | boost::escaped_list_separator<char> separator('\\', '=', '\"'); |
| 402 | tokenizer attributeTok(*token, separator); |
| 403 | |
| 404 | tokenizer::iterator attributeToken = attributeTok.begin(); |
| 405 | |
| 406 | string attribute = *attributeToken; |
| 407 | attributeToken++; |
| 408 | |
| 409 | if (attributeToken == attributeTok.end()) { |
| 410 | NS_LOG_ERROR("ErrorModel attribute [" << *token |
| 411 | << "] should be in form <Attribute>=<Value>"); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | string value = *attributeToken; |
| 416 | |
| 417 | factory.Set(attribute, StringValue(value)); |
| 418 | } |
| 419 | |
| 420 | nd.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>())); |
| 421 | nd.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>())); |
| 422 | } |
| 423 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 424 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 425 | |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 426 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 427 | AnnotatedTopologyReader::SaveTopology(const std::string& file) |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 428 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 429 | ofstream os(file.c_str(), ios::trunc); |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 430 | os << "# any empty lines and lines starting with '#' symbol is ignored\n" |
| 431 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 432 | << "# The file should contain exactly two sections: router and link, each starting with the " |
| 433 | "corresponding keyword\n" |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 434 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 435 | << "# router section defines topology nodes and their relative positions (e.g., to use in " |
| 436 | "visualizer)\n" |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 437 | << "router\n" |
| 438 | << "\n" |
| 439 | << "# each line in this section represents one router and should have the following data\n" |
| 440 | << "# node comment yPos xPos\n"; |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 441 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 442 | for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) { |
| 443 | std::string name = Names::FindName(*node); |
| 444 | Ptr<MobilityModel> mobility = (*node)->GetObject<MobilityModel>(); |
| 445 | Vector position = mobility->GetPosition(); |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 446 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 447 | os << name << "\t" |
| 448 | << "NA" |
| 449 | << "\t" << -position.y << "\t" << position.x << "\n"; |
| 450 | } |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 451 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 452 | os |
| 453 | << "# link section defines point-to-point links between nodes and characteristics of these " |
| 454 | "links\n" |
| 455 | << "\n" |
| 456 | << "link\n" |
| 457 | << "\n" |
| 458 | << "# Each line should be in the following format (only first two are required, the rest can " |
| 459 | "be omitted)\n" |
| 460 | << "# srcNode dstNode bandwidth metric delay queue\n" |
| 461 | << "# bandwidth: link bandwidth\n" |
| 462 | << "# metric: routing metric\n" |
| 463 | << "# delay: link delay\n" |
| 464 | << "# queue: MaxPackets for transmission queue on the link (both directions)\n" |
| 465 | << "# error: comma-separated list, specifying class for ErrorModel and necessary attributes\n"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 466 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 467 | for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end(); |
| 468 | link++) { |
| 469 | os << Names::FindName(link->GetFromNode()) << "\t"; |
| 470 | os << Names::FindName(link->GetToNode()) << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 471 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 472 | string tmp; |
| 473 | if (link->GetAttributeFailSafe("DataRate", tmp)) |
| 474 | os << link->GetAttribute("DataRate") << "\t"; |
| 475 | else |
| 476 | NS_FATAL_ERROR("DataRate must be specified for the link"); |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 477 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 478 | if (link->GetAttributeFailSafe("OSPF", tmp)) |
| 479 | os << link->GetAttribute("OSPF") << "\t"; |
| 480 | else |
| 481 | os << "1\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 482 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 483 | if (link->GetAttributeFailSafe("Delay", tmp)) { |
| 484 | os << link->GetAttribute("Delay") << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 485 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 486 | if (link->GetAttributeFailSafe("MaxPackets", tmp)) { |
| 487 | os << link->GetAttribute("MaxPackets") << "\t"; |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 488 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 489 | if (link->GetAttributeFailSafe("LossRate", tmp)) { |
| 490 | os << link->GetAttribute("LossRate") << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 491 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 492 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 493 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 494 | os << "\n"; |
| 495 | } |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 498 | /// @cond include_hidden |
| 499 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 500 | template<class Names> |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 501 | class name_writer { |
| 502 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 503 | name_writer(Names _names) |
| 504 | : names(_names) |
| 505 | { |
| 506 | } |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 507 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 508 | template<class VertexOrEdge> |
| 509 | void |
| 510 | operator()(std::ostream& out, const VertexOrEdge& v) const |
| 511 | { |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 512 | // out << "[label=\"" << names[v] << "\",style=filled,fillcolor=\"" << colors[v] << "\"]"; |
| 513 | out << "[shape=\"circle\",width=0.1,label=\"\",style=filled,fillcolor=\"green\"]"; |
| 514 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 515 | |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 516 | private: |
| 517 | Names names; |
| 518 | }; |
| 519 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 520 | template<class Names> |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 521 | inline name_writer<Names> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 522 | make_name_writer(Names n) |
| 523 | { |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 524 | return name_writer<Names>(n); |
| 525 | } |
| 526 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 527 | /// @endcond |
| 528 | |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 529 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 530 | AnnotatedTopologyReader::SaveGraphviz(const std::string& file) |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 531 | { |
| 532 | typedef boost::adjacency_list_traits<boost::setS, boost::setS, boost::undirectedS> Traits; |
| 533 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 534 | typedef boost::property<boost::vertex_name_t, std::string, |
| 535 | boost::property<boost::vertex_index_t, uint32_t>> nodeProperty; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 536 | |
| 537 | typedef boost::no_property edgeProperty; |
| 538 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 539 | typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, nodeProperty, |
| 540 | edgeProperty> Graph; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 541 | |
| 542 | typedef map<string, Traits::vertex_descriptor> node_map_t; |
| 543 | node_map_t graphNodes; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 544 | Graph graph; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 545 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 546 | for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) { |
| 547 | std::pair<node_map_t::iterator, bool> retval = graphNodes.insert( |
| 548 | make_pair(Names::FindName(*node), add_vertex(nodeProperty(Names::FindName(*node)), graph))); |
| 549 | // NS_ASSERT (ok == true); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 550 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 551 | put(boost::vertex_index, graph, retval.first->second, (*node)->GetId()); |
| 552 | } |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 553 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 554 | for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end(); |
| 555 | link++) { |
| 556 | node_map_t::iterator from = graphNodes.find(Names::FindName(link->GetFromNode())); |
| 557 | node_map_t::iterator to = graphNodes.find(Names::FindName(link->GetToNode())); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 558 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 559 | // add_edge (node->second, otherNode->second, m_graph); |
| 560 | boost::add_edge(from->second, to->second, graph); |
| 561 | } |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 562 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 563 | ofstream of(file.c_str()); |
| 564 | boost::property_map<Graph, boost::vertex_name_t>::type names = get(boost::vertex_name, graph); |
| 565 | write_graphviz(of, graph, make_name_writer(names)); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 566 | } |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 567 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 568 | } |