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