Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | // Based on the code by Hajime Tazaki <tazaki@sfc.wide.ad.jp> |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "rocketfuel-weights-reader.hpp" |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 23 | |
| 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" |
| 41 | |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 42 | #include "ns3/mobility-model.h" |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 43 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 44 | #include <regex.h> |
| 45 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 46 | #include <boost/foreach.hpp> |
| 47 | #include <boost/lexical_cast.hpp> |
| 48 | |
| 49 | #include <iomanip> |
| 50 | #include <set> |
| 51 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 52 | using namespace std; |
| 53 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | NS_LOG_COMPONENT_DEFINE("RocketfuelWeightsReader"); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 55 | |
| 56 | namespace ns3 { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | |
| 58 | RocketfuelWeightsReader::RocketfuelWeightsReader(const std::string& path /*=""*/, |
| 59 | double scale /*=1.0*/) |
| 60 | : AnnotatedTopologyReader(path, scale) |
| 61 | , m_defaultBandwidth("100Mbps") |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | ac46d45 | 2012-05-31 15:33:21 -0700 | [diff] [blame] | 64 | |
| 65 | // TypeId tid; |
| 66 | // bool ok = TypeId::LookupByNameFailSafe ("ns3::SpringMobilityModel", &tid); |
| 67 | // if (ok) |
| 68 | // SetMobilityModel ("ns3::SpringMobilityModel"); |
| 69 | // else |
| 70 | // Use default mobility model (supplied by AnnotatedTopologyReader) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 71 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | |
| 73 | RocketfuelWeightsReader::~RocketfuelWeightsReader() |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 76 | } |
Ilya Moiseenko | 09ac899 | 2011-12-12 17:55:42 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 78 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | RocketfuelWeightsReader::SetFileType(uint8_t inputType) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 80 | { |
| 81 | m_inputType = inputType; |
| 82 | } |
| 83 | |
| 84 | NodeContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | RocketfuelWeightsReader::Read() |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 87 | if (m_inputType == POSITIONS) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | return AnnotatedTopologyReader::Read(); |
| 89 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 90 | ifstream topgen; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | topgen.open(GetFileName().c_str()); |
| 92 | |
| 93 | if (!topgen.is_open()) { |
| 94 | NS_LOG_ERROR("Cannot open file " << GetFileName() << " for reading"); |
| 95 | return m_nodes; |
| 96 | } |
| 97 | |
| 98 | map<string, set<string>> processedLinks; // to eliminate duplications |
| 99 | bool repeatedRun = LinksSize() > 0; |
| 100 | std::list<Link>::iterator linkIterator = m_linksList.begin(); |
| 101 | |
| 102 | while (!topgen.eof()) { |
| 103 | string line; |
| 104 | getline(topgen, line); |
| 105 | if (line == "") |
| 106 | continue; |
| 107 | if (line[0] == '#') |
| 108 | continue; // comments |
| 109 | |
| 110 | // NS_LOG_DEBUG ("Input: [" << line << "]"); |
| 111 | |
| 112 | istringstream lineBuffer(line); |
| 113 | string from, to, attribute; |
| 114 | |
| 115 | lineBuffer >> from >> to >> attribute; |
| 116 | |
| 117 | if (processedLinks[to].size() != 0 |
| 118 | && processedLinks[to].find(from) != processedLinks[to].end()) { |
| 119 | continue; // duplicated link |
| 120 | } |
| 121 | processedLinks[from].insert(to); |
| 122 | |
| 123 | Ptr<Node> fromNode = Names::Find<Node>(m_path, from); |
| 124 | if (fromNode == 0) { |
| 125 | fromNode = CreateNode(from, 0); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | Ptr<Node> toNode = Names::Find<Node>(m_path, to); |
| 129 | if (toNode == 0) { |
| 130 | toNode = CreateNode(to, 0); |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 131 | } |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 133 | Link* link; |
| 134 | if (!repeatedRun) |
| 135 | link = new Link(fromNode, from, toNode, to); |
| 136 | else { |
| 137 | NS_ASSERT(linkIterator != m_linksList.end()); |
| 138 | link = &(*linkIterator); |
| 139 | |
| 140 | linkIterator++; |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 141 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 142 | |
| 143 | switch (m_inputType) { |
| 144 | case LINKS: { |
| 145 | // links only |
| 146 | // do nothing |
| 147 | break; |
| 148 | } |
| 149 | case WEIGHTS: { |
| 150 | if (attribute == "") |
| 151 | attribute = "1"; |
| 152 | uint16_t metric = boost::lexical_cast<uint16_t>(attribute); |
| 153 | link->SetAttribute("OSPF", boost::lexical_cast<string>(metric)); |
| 154 | break; |
| 155 | } |
| 156 | case LATENCIES: |
| 157 | if (attribute == "") |
| 158 | attribute = "1"; |
| 159 | |
| 160 | link->SetAttribute("DataRate", m_defaultBandwidth); |
| 161 | link->SetAttribute("Delay", attribute + "ms"); |
| 162 | if (!m_queue.empty()) { |
| 163 | link->SetAttribute("MaxPackets", m_queue); |
| 164 | } |
| 165 | break; |
| 166 | default: |
| 167 | ; // |
| 168 | } |
| 169 | |
| 170 | NS_LOG_DEBUG("Link " << from << " <==> " << to << " / " << attribute); |
| 171 | if (!repeatedRun) { |
| 172 | AddLink(*link); |
| 173 | delete link; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | topgen.close(); |
| 178 | |
| 179 | if (!repeatedRun) { |
| 180 | NS_LOG_INFO("Rocketfuel topology created with " << m_nodes.GetN() << " nodes and " |
| 181 | << LinksSize() << " links"); |
| 182 | } |
Alexander Afanasyev | 5beb35a | 2011-12-21 16:45:13 -0800 | [diff] [blame] | 183 | return m_nodes; |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame] | 184 | } |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 185 | |
| 186 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | RocketfuelWeightsReader::Commit() |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 188 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 189 | ApplySettings(); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | ac46d45 | 2012-05-31 15:33:21 -0700 | [diff] [blame] | 191 | // SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ()); |
Alexander Afanasyev | 7dbdcaf | 2011-12-13 21:40:37 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 194 | } /* namespace ns3 */ |