blob: 886a58f3cb4fa7fe87a64ab82c061b4a5201da99 [file] [log] [blame]
Ilya Moiseenko58d26672011-12-08 13:48:06 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08002/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Ilya Moiseenko58d26672011-12-08 13:48:06 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Ilya Moiseenko58d26672011-12-08 13:48:06 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Moiseenko58d26672011-12-08 13:48:06 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Moiseenko58d26672011-12-08 13:48:06 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Moiseenko58d26672011-12-08 13:48:06 -080021
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "rocketfuel-weights-reader.hpp"
Alexander Afanasyev07827182011-12-13 01:07:32 -080023
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 Afanasyev5beb35a2011-12-21 16:45:13 -080042#include "ns3/mobility-model.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080043
Ilya Moiseenko58d26672011-12-08 13:48:06 -080044#include <regex.h>
45
Alexander Afanasyev07827182011-12-13 01:07:32 -080046#include <boost/foreach.hpp>
47#include <boost/lexical_cast.hpp>
48
49#include <iomanip>
50#include <set>
51
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080052using namespace std;
53
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054NS_LOG_COMPONENT_DEFINE("RocketfuelWeightsReader");
Ilya Moiseenko58d26672011-12-08 13:48:06 -080055
56namespace ns3 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057
58RocketfuelWeightsReader::RocketfuelWeightsReader(const std::string& path /*=""*/,
59 double scale /*=1.0*/)
60 : AnnotatedTopologyReader(path, scale)
61 , m_defaultBandwidth("100Mbps")
Ilya Moiseenko58d26672011-12-08 13:48:06 -080062{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 NS_LOG_FUNCTION(this);
Alexander Afanasyevac46d452012-05-31 15:33:21 -070064
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 Afanasyevae3b7c32011-12-13 13:20:06 -080071}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072
73RocketfuelWeightsReader::~RocketfuelWeightsReader()
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080074{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 NS_LOG_FUNCTION(this);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080076}
Ilya Moiseenko09ac8992011-12-12 17:55:42 -080077
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080078void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079RocketfuelWeightsReader::SetFileType(uint8_t inputType)
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080080{
81 m_inputType = inputType;
82}
83
84NodeContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085RocketfuelWeightsReader::Read()
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080086{
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -080087 if (m_inputType == POSITIONS)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 return AnnotatedTopologyReader::Read();
89
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080090 ifstream topgen;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 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 Moiseenko58d26672011-12-08 13:48:06 -0800126 }
127
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 Ptr<Node> toNode = Names::Find<Node>(m_path, to);
129 if (toNode == 0) {
130 toNode = CreateNode(to, 0);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800131 }
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800132
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 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 Afanasyev7dbdcaf2011-12-13 21:40:37 -0800141 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142
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 Afanasyev5beb35a2011-12-21 16:45:13 -0800183 return m_nodes;
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800184}
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800185
186void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800187RocketfuelWeightsReader::Commit()
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800188{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 ApplySettings();
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800190
Alexander Afanasyevac46d452012-05-31 15:33:21 -0700191 // SpringMobilityHelper::InstallSprings (LinksBegin (), LinksEnd ());
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800192}
193
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800194} /* namespace ns3 */