Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2010 Hajime Tazaki |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Hajime Tazaki (tazaki@sfc.wide.ad.jp) |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 22 | #include "rocketfuel-weights-reader.h" |
| 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 | |
| 42 | #include "ns3/constant-position-mobility-model.h" |
| 43 | #include "ns3/random-variable.h" |
| 44 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 45 | #include <regex.h> |
| 46 | |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 47 | #include <boost/foreach.hpp> |
| 48 | #include <boost/lexical_cast.hpp> |
| 49 | |
| 50 | #include <iomanip> |
| 51 | #include <set> |
| 52 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 53 | using namespace std; |
| 54 | |
| 55 | NS_LOG_COMPONENT_DEFINE ("RocketfuelWeightsReader"); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 56 | |
| 57 | namespace ns3 { |
| 58 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 59 | RocketfuelWeightsReader::RocketfuelWeightsReader () |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 61 | NS_LOG_FUNCTION (this); |
| 62 | } |
| 63 | |
| 64 | RocketfuelWeightsReader::~RocketfuelWeightsReader () |
| 65 | { |
| 66 | NS_LOG_FUNCTION (this); |
| 67 | } |
Ilya Moiseenko | 09ac899 | 2011-12-12 17:55:42 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 69 | void |
| 70 | RocketfuelWeightsReader::SetFileType (uint8_t inputType) |
| 71 | { |
| 72 | m_inputType = inputType; |
| 73 | } |
| 74 | |
| 75 | NodeContainer |
| 76 | RocketfuelWeightsReader::Read () |
| 77 | { |
| 78 | ifstream topgen; |
| 79 | topgen.open (GetFileName ().c_str ()); |
| 80 | NodeContainer nodes; |
| 81 | |
| 82 | if ( !topgen.is_open () ) |
| 83 | { |
| 84 | NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading"); |
| 85 | return nodes; |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 88 | map<string, set<string> > processedLinks; // to eliminate duplications |
| 89 | bool repeatedRun = LinksSize () > 0; |
| 90 | std::list<Link>::iterator linkIterator = m_linksList.begin (); |
| 91 | |
| 92 | while (!topgen.eof ()) |
| 93 | { |
| 94 | string line; |
| 95 | getline (topgen,line); |
| 96 | if (line == "") continue; |
| 97 | if (line[0] == '#') continue; // comments |
| 98 | |
| 99 | // NS_LOG_DEBUG ("Input: [" << line << "]"); |
| 100 | |
| 101 | istringstream lineBuffer (line); |
| 102 | string from, to, attribute; |
| 103 | |
| 104 | lineBuffer >> from >> to >> attribute; |
| 105 | |
| 106 | if (processedLinks[to].size () != 0 && |
| 107 | processedLinks[to].find (from) != processedLinks[to].end ()) |
| 108 | { |
| 109 | continue; // duplicated link |
| 110 | } |
| 111 | processedLinks[from].insert (to); |
| 112 | |
| 113 | Ptr<Node> fromNode = Names::Find<Node> (m_path, from); |
| 114 | if (fromNode == 0) |
| 115 | { |
| 116 | fromNode = CreateNode (from); |
| 117 | nodes.Add (fromNode); |
| 118 | } |
| 119 | |
| 120 | Ptr<Node> toNode = Names::Find<Node> (m_path, to); |
| 121 | if (toNode == 0) |
| 122 | { |
| 123 | toNode = CreateNode (to); |
| 124 | nodes.Add (toNode); |
| 125 | } |
| 126 | |
| 127 | Link *link; |
| 128 | if (!repeatedRun) |
| 129 | link = new Link (fromNode, from, toNode, to); |
| 130 | else |
| 131 | { |
| 132 | NS_ASSERT (linkIterator != m_linksList.end ()); |
| 133 | link = &(*linkIterator); |
| 134 | |
| 135 | linkIterator++; |
| 136 | } |
| 137 | |
| 138 | switch (m_inputType) |
| 139 | { |
| 140 | case WEIGHTS: |
| 141 | link->SetAttribute ("OSPF", attribute); |
| 142 | break; |
| 143 | case LATENCIES: |
| 144 | link->SetAttribute ("Delay", attribute); |
| 145 | break; |
| 146 | default: |
| 147 | ; // |
| 148 | } |
| 149 | |
| 150 | NS_LOG_DEBUG ("Link " << from << " <==> " << to << " / " << attribute); |
| 151 | if (!repeatedRun) |
| 152 | { |
| 153 | AddLink (*link); |
| 154 | delete link; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | topgen.close (); |
| 159 | NS_LOG_INFO ("Rocketfuel topology created with " << nodes.GetN () << " nodes and " << LinksSize () << " links"); |
| 160 | return nodes; |
| 161 | } |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 162 | |
| 163 | } /* namespace ns3 */ |