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; |
| 348 | tokenizer tok(link.GetAttribute("MaxPackets")); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 349 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 350 | tokenizer::iterator token = tok.begin(); |
| 351 | p2p.SetQueue(*token); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 352 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 353 | for (token++; token != tok.end(); token++) { |
| 354 | boost::escaped_list_separator<char> separator('\\', '=', '\"'); |
| 355 | tokenizer attributeTok(*token, separator); |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 357 | tokenizer::iterator attributeToken = attributeTok.begin(); |
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 | string attribute = *attributeToken; |
| 360 | attributeToken++; |
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 | if (attributeToken == attributeTok.end()) { |
| 363 | NS_LOG_ERROR("Queue attribute [" << *token |
| 364 | << "] should be in form <Attribute>=<Value>"); |
| 365 | continue; |
| 366 | } |
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 value = *attributeToken; |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 369 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 370 | p2p.SetQueueAttribute(attribute, StringValue(value)); |
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 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 373 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 374 | |
| 375 | if (link.GetAttributeFailSafe("DataRate", tmp)) { |
| 376 | NS_LOG_INFO("DataRate = " + link.GetAttribute("DataRate")); |
| 377 | p2p.SetDeviceAttribute("DataRate", StringValue(link.GetAttribute("DataRate"))); |
| 378 | } |
| 379 | |
| 380 | if (link.GetAttributeFailSafe("Delay", tmp)) { |
| 381 | NS_LOG_INFO("Delay = " + link.GetAttribute("Delay")); |
| 382 | p2p.SetChannelAttribute("Delay", StringValue(link.GetAttribute("Delay"))); |
| 383 | } |
| 384 | |
| 385 | NetDeviceContainer nd = p2p.Install(link.GetFromNode(), link.GetToNode()); |
| 386 | link.SetNetDevices(nd.Get(0), nd.Get(1)); |
| 387 | |
| 388 | //////////////////////////////////////////////// |
| 389 | if (link.GetAttributeFailSafe("LossRate", tmp)) { |
| 390 | NS_LOG_INFO("LinkError = " + link.GetAttribute("LossRate")); |
| 391 | |
| 392 | typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer; |
| 393 | tokenizer tok(link.GetAttribute("LossRate")); |
| 394 | |
| 395 | tokenizer::iterator token = tok.begin(); |
| 396 | ObjectFactory factory(*token); |
| 397 | |
| 398 | for (token++; token != tok.end(); token++) { |
| 399 | boost::escaped_list_separator<char> separator('\\', '=', '\"'); |
| 400 | tokenizer attributeTok(*token, separator); |
| 401 | |
| 402 | tokenizer::iterator attributeToken = attributeTok.begin(); |
| 403 | |
| 404 | string attribute = *attributeToken; |
| 405 | attributeToken++; |
| 406 | |
| 407 | if (attributeToken == attributeTok.end()) { |
| 408 | NS_LOG_ERROR("ErrorModel attribute [" << *token |
| 409 | << "] should be in form <Attribute>=<Value>"); |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | string value = *attributeToken; |
| 414 | |
| 415 | factory.Set(attribute, StringValue(value)); |
| 416 | } |
| 417 | |
| 418 | nd.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>())); |
| 419 | nd.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>())); |
| 420 | } |
| 421 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 422 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 423 | |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 424 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 425 | AnnotatedTopologyReader::SaveTopology(const std::string& file) |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 426 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 427 | ofstream os(file.c_str(), ios::trunc); |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 428 | os << "# any empty lines and lines starting with '#' symbol is ignored\n" |
| 429 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 430 | << "# The file should contain exactly two sections: router and link, each starting with the " |
| 431 | "corresponding keyword\n" |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 432 | << "\n" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 433 | << "# router section defines topology nodes and their relative positions (e.g., to use in " |
| 434 | "visualizer)\n" |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 435 | << "router\n" |
| 436 | << "\n" |
| 437 | << "# each line in this section represents one router and should have the following data\n" |
| 438 | << "# node comment yPos xPos\n"; |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 439 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 440 | for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) { |
| 441 | std::string name = Names::FindName(*node); |
| 442 | Ptr<MobilityModel> mobility = (*node)->GetObject<MobilityModel>(); |
| 443 | Vector position = mobility->GetPosition(); |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 444 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 445 | os << name << "\t" |
| 446 | << "NA" |
| 447 | << "\t" << -position.y << "\t" << position.x << "\n"; |
| 448 | } |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 449 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 450 | os |
| 451 | << "# link section defines point-to-point links between nodes and characteristics of these " |
| 452 | "links\n" |
| 453 | << "\n" |
| 454 | << "link\n" |
| 455 | << "\n" |
| 456 | << "# Each line should be in the following format (only first two are required, the rest can " |
| 457 | "be omitted)\n" |
| 458 | << "# srcNode dstNode bandwidth metric delay queue\n" |
| 459 | << "# bandwidth: link bandwidth\n" |
| 460 | << "# metric: routing metric\n" |
| 461 | << "# delay: link delay\n" |
| 462 | << "# queue: MaxPackets for transmission queue on the link (both directions)\n" |
| 463 | << "# error: comma-separated list, specifying class for ErrorModel and necessary attributes\n"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 464 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 465 | for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end(); |
| 466 | link++) { |
| 467 | os << Names::FindName(link->GetFromNode()) << "\t"; |
| 468 | os << Names::FindName(link->GetToNode()) << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 469 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 470 | string tmp; |
| 471 | if (link->GetAttributeFailSafe("DataRate", tmp)) |
| 472 | os << link->GetAttribute("DataRate") << "\t"; |
| 473 | else |
| 474 | NS_FATAL_ERROR("DataRate must be specified for the link"); |
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 | if (link->GetAttributeFailSafe("OSPF", tmp)) |
| 477 | os << link->GetAttribute("OSPF") << "\t"; |
| 478 | else |
| 479 | os << "1\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 | if (link->GetAttributeFailSafe("Delay", tmp)) { |
| 482 | os << link->GetAttribute("Delay") << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 483 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 484 | if (link->GetAttributeFailSafe("MaxPackets", tmp)) { |
| 485 | os << link->GetAttribute("MaxPackets") << "\t"; |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 486 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 487 | if (link->GetAttributeFailSafe("LossRate", tmp)) { |
| 488 | os << link->GetAttribute("LossRate") << "\t"; |
Alexander Afanasyev | 7102973 | 2012-11-19 23:50:52 -0800 | [diff] [blame] | 489 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 490 | } |
Alexander Afanasyev | da2f23a | 2013-04-14 22:52:50 -0700 | [diff] [blame] | 491 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 492 | os << "\n"; |
| 493 | } |
Alexander Afanasyev | ed78b63 | 2012-01-25 19:26:43 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 496 | template<class Names> |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 497 | class name_writer { |
| 498 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 499 | name_writer(Names _names) |
| 500 | : names(_names) |
| 501 | { |
| 502 | } |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 503 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 504 | template<class VertexOrEdge> |
| 505 | void |
| 506 | operator()(std::ostream& out, const VertexOrEdge& v) const |
| 507 | { |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 508 | // out << "[label=\"" << names[v] << "\",style=filled,fillcolor=\"" << colors[v] << "\"]"; |
| 509 | out << "[shape=\"circle\",width=0.1,label=\"\",style=filled,fillcolor=\"green\"]"; |
| 510 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 511 | |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 512 | private: |
| 513 | Names names; |
| 514 | }; |
| 515 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 516 | template<class Names> |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 517 | inline name_writer<Names> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 518 | make_name_writer(Names n) |
| 519 | { |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 520 | return name_writer<Names>(n); |
| 521 | } |
| 522 | |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 523 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 524 | AnnotatedTopologyReader::SaveGraphviz(const std::string& file) |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 525 | { |
| 526 | typedef boost::adjacency_list_traits<boost::setS, boost::setS, boost::undirectedS> Traits; |
| 527 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 528 | typedef boost::property<boost::vertex_name_t, std::string, |
| 529 | boost::property<boost::vertex_index_t, uint32_t>> nodeProperty; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 530 | |
| 531 | typedef boost::no_property edgeProperty; |
| 532 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 533 | typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, nodeProperty, |
| 534 | edgeProperty> Graph; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 535 | |
| 536 | typedef map<string, Traits::vertex_descriptor> node_map_t; |
| 537 | node_map_t graphNodes; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 538 | Graph graph; |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 539 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 540 | for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) { |
| 541 | std::pair<node_map_t::iterator, bool> retval = graphNodes.insert( |
| 542 | make_pair(Names::FindName(*node), add_vertex(nodeProperty(Names::FindName(*node)), graph))); |
| 543 | // NS_ASSERT (ok == true); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 544 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 545 | put(boost::vertex_index, graph, retval.first->second, (*node)->GetId()); |
| 546 | } |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 547 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 548 | for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end(); |
| 549 | link++) { |
| 550 | node_map_t::iterator from = graphNodes.find(Names::FindName(link->GetFromNode())); |
| 551 | node_map_t::iterator to = graphNodes.find(Names::FindName(link->GetToNode())); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 552 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 553 | // add_edge (node->second, otherNode->second, m_graph); |
| 554 | boost::add_edge(from->second, to->second, graph); |
| 555 | } |
Alexander Afanasyev | 0aa1136 | 2013-07-14 15:35:00 -0700 | [diff] [blame] | 556 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 557 | ofstream of(file.c_str()); |
| 558 | boost::property_map<Graph, boost::vertex_name_t>::type names = get(boost::vertex_name, graph); |
| 559 | write_graphviz(of, graph, make_name_writer(names)); |
Alexander Afanasyev | 455e441 | 2013-05-11 12:51:11 -0700 | [diff] [blame] | 560 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 561 | } |