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