Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 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: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "annotated-topology-reader.h" |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 23 | #include "ns3/nstime.h" |
| 24 | #include "ns3/log.h" |
| 25 | #include "ns3/assert.h" |
| 26 | #include "ns3/names.h" |
| 27 | #include "ns3/net-device-container.h" |
| 28 | #include "ns3/point-to-point-helper.h" |
| 29 | #include "ns3/point-to-point-net-device.h" |
| 30 | #include "ns3/internet-stack-helper.h" |
| 31 | #include "ns3/ipv4-address-helper.h" |
| 32 | #include "ns3/ipv4-global-routing-helper.h" |
| 33 | #include "ns3/drop-tail-queue.h" |
| 34 | #include "ns3/ipv4-interface.h" |
| 35 | #include "ns3/ipv4.h" |
| 36 | #include "ns3/string.h" |
| 37 | #include "ns3/pointer.h" |
| 38 | #include "ns3/uinteger.h" |
| 39 | #include "ns3/ipv4-address.h" |
| 40 | |
| 41 | #include "ns3/constant-position-mobility-model.h" |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 42 | |
| 43 | #include <boost/foreach.hpp> |
| 44 | #include <boost/lexical_cast.hpp> |
| 45 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 46 | #include <set> |
| 47 | |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 48 | using namespace std; |
| 49 | |
| 50 | namespace ns3 |
| 51 | { |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 52 | |
| 53 | NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReader"); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 55 | AnnotatedTopologyReader::AnnotatedTopologyReader (const std::string &path) |
| 56 | : m_path (path) |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 57 | , m_randX (0, 100.0) |
| 58 | , m_randY (0, 100.0) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 59 | { |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 60 | NS_LOG_FUNCTION (this); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 63 | void |
| 64 | AnnotatedTopologyReader::SetBoundingBox (double ulx, double uly, double lrx, double lry) |
| 65 | { |
| 66 | NS_LOG_FUNCTION (this << ulx << uly << lrx << lry); |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 68 | m_randX = UniformVariable (ulx, lrx); |
| 69 | m_randY = UniformVariable (uly, lry); |
| 70 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 71 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 72 | AnnotatedTopologyReader::~AnnotatedTopologyReader () |
| 73 | { |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 74 | NS_LOG_FUNCTION (this); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 77 | Ptr<Node> |
| 78 | AnnotatedTopologyReader::CreateNode (const std::string name) |
| 79 | { |
| 80 | return CreateNode (name, m_randX.GetValue (), m_randX.GetValue ()); |
| 81 | } |
| 82 | |
| 83 | Ptr<Node> |
| 84 | AnnotatedTopologyReader::CreateNode (const std::string name, double posX, double posY) |
| 85 | { |
| 86 | Ptr<Node> node = CreateObject<Node> (); |
| 87 | Ptr<ConstantPositionMobilityModel> loc = CreateObject<ConstantPositionMobilityModel> (); |
| 88 | node->AggregateObject (loc); |
| 89 | |
| 90 | loc->SetPosition (Vector (posX, posY, 0)); |
| 91 | |
| 92 | Names::Add (m_path, name, node); |
| 93 | |
| 94 | return node; |
| 95 | } |
| 96 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 97 | NodeContainer |
| 98 | AnnotatedTopologyReader::Read (void) |
| 99 | { |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 100 | ifstream topgen; |
| 101 | topgen.open (GetFileName ().c_str ()); |
| 102 | NodeContainer nodes; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 104 | if ( !topgen.is_open () ) |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 105 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 106 | NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading"); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 107 | return nodes; |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 108 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 110 | while (!topgen.eof ()) |
| 111 | { |
| 112 | string line; |
| 113 | getline (topgen, line); |
| 114 | |
| 115 | if (line == "router") break; |
| 116 | } |
| 117 | |
| 118 | while (!topgen.eof ()) |
| 119 | { |
| 120 | string line; |
| 121 | getline (topgen,line); |
| 122 | if (line[0] == '#') continue; // comments |
| 123 | if (line=="link") break; // stop reading nodes |
| 124 | |
| 125 | istringstream lineBuffer (line); |
| 126 | string name, city; |
| 127 | double latitude, longitude; |
| 128 | |
| 129 | lineBuffer >> name >> city >> latitude >> longitude; |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 130 | Ptr<Node> node = CreateNode (name, 2*longitude, -2*latitude); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 131 | nodes.Add (node); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | map<string, set<string> > processedLinks; // to eliminate duplications |
| 135 | |
| 136 | // SeekToSection ("link"); |
| 137 | while (!topgen.eof ()) |
| 138 | { |
| 139 | string line; |
| 140 | getline (topgen,line); |
| 141 | if (line == "") continue; |
| 142 | if (line[0] == '#') continue; // comments |
| 143 | |
| 144 | // NS_LOG_DEBUG ("Input: [" << line << "]"); |
| 145 | |
| 146 | istringstream lineBuffer (line); |
| 147 | string from, to, capacity, metric; |
| 148 | |
| 149 | lineBuffer >> from >> to >> capacity >> metric; |
| 150 | |
| 151 | if (processedLinks[to].size () != 0 && |
| 152 | processedLinks[to].find (from) != processedLinks[to].end ()) |
| 153 | { |
| 154 | continue; // duplicated link |
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 | processedLinks[from].insert (to); |
| 157 | |
| 158 | Ptr<Node> fromNode = Names::Find<Node> (m_path, from); |
| 159 | NS_ASSERT (fromNode != 0); |
| 160 | Ptr<Node> toNode = Names::Find<Node> (m_path, to); |
| 161 | NS_ASSERT (fromNode != 0); |
| 162 | |
| 163 | Link link (fromNode, from, toNode, to); |
| 164 | |
| 165 | link.SetAttribute ("DataRate", capacity); |
| 166 | link.SetAttribute ("OSPF", metric); |
| 167 | // link.SetAttribute ("Delay", delay); |
| 168 | // link.SetAttribute ("QueueSizeNode1", queueSizeNode1); |
| 169 | // link.SetAttribute ("QueueSizeNode2", queueSizeNode2); |
| 170 | |
| 171 | AddLink (link); |
| 172 | NS_LOG_DEBUG ("New link " << from << " <==> " << to << " / " << capacity << "Kbps with " << metric << " metric"); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Alexander Afanasyev | ae3b7c3 | 2011-12-13 13:20:06 -0800 | [diff] [blame^] | 175 | NS_LOG_INFO ("Annotated topology created with " << nodes.GetN () << " nodes and " << LinksSize () << " links"); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 176 | topgen.close (); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 178 | ApplySettings (); |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 179 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 180 | return nodes; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 181 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 182 | |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 183 | void |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 184 | AnnotatedTopologyReader::AssignIpv4Addresses (Ipv4Address base) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 185 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 186 | Ipv4AddressHelper address (base, Ipv4Mask ("/24")); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 188 | BOOST_FOREACH (const Link &link, m_linksList) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 190 | address.Assign (NetDeviceContainer (link.GetFromNetDevice (), |
| 191 | link.GetToNetDevice ())); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 193 | base = Ipv4Address (base.Get () + 256); |
| 194 | address.SetBase (base, Ipv4Mask ("/24")); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 195 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 196 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 197 | ApplyOspfMetric (); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 198 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 200 | void |
| 201 | AnnotatedTopologyReader::ApplyOspfMetric () |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 202 | { |
Alexander Afanasyev | 0777f1c | 2011-12-12 20:22:45 -0800 | [diff] [blame] | 203 | BOOST_FOREACH (const Link &link, m_linksList) |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 204 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 205 | uint16_t metric = boost::lexical_cast<uint16_t> (link.GetAttribute ("OSPF")); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 206 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 208 | Ptr<Ipv4> ipv4 = link.GetFromNode ()->GetObject<Ipv4> (); |
| 209 | NS_ASSERT (ipv4 != 0); |
Alexander Afanasyev | 0777f1c | 2011-12-12 20:22:45 -0800 | [diff] [blame] | 210 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 211 | int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetFromNetDevice ()); |
| 212 | NS_ASSERT (interfaceId >= 0); |
Alexander Afanasyev | 0777f1c | 2011-12-12 20:22:45 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 214 | ipv4->SetMetric (interfaceId,metric); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 215 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 217 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 218 | Ptr<Ipv4> ipv4 = link.GetToNode ()->GetObject<Ipv4> (); |
| 219 | NS_ASSERT (ipv4 != 0); |
| 220 | |
| 221 | int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetToNetDevice ()); |
| 222 | NS_ASSERT (interfaceId >= 0); |
| 223 | |
| 224 | ipv4->SetMetric (interfaceId,metric); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 225 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | AnnotatedTopologyReader::ApplySettings () |
| 231 | { |
| 232 | PointToPointHelper p2p; |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 233 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 234 | // temporary queue, will be changed later |
| 235 | p2p.SetQueue ("ns3::DropTailQueue", |
| 236 | "MaxPackets", StringValue("100")); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 238 | BOOST_FOREACH (Link &link, m_linksList) |
| 239 | { |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 240 | string tmp; |
| 241 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 242 | NS_LOG_INFO ("DataRate = " + link.GetAttribute("DataRate")+"Kbps"); |
| 243 | p2p.SetDeviceAttribute ("DataRate", StringValue(link.GetAttribute("DataRate")+"Kbps")); |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 244 | |
| 245 | if (link.GetAttributeFailSafe("Delay", tmp)) |
| 246 | { |
| 247 | NS_LOG_INFO ("Delay = " + link.GetAttribute("Delay")+"ms"); |
| 248 | p2p.SetChannelAttribute ("Delay", StringValue(link.GetAttribute("Delay")+"ms")); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | NS_LOG_INFO ("Default delay 1ms"); |
| 253 | p2p.SetChannelAttribute ("Delay", StringValue("1ms")); |
| 254 | } |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 255 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 256 | NetDeviceContainer nd = p2p.Install(link.GetFromNode (), link.GetToNode ()); |
| 257 | link.SetNetDevices (nd.Get (0), nd.Get (1)); |
| 258 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 259 | // NS_LOG_INFO ("Queue: " << link.GetAttribute("QueueSizeNode1") << " <==> " << link.GetAttribute("QueueSizeNode2")); |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 260 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 261 | if (link.GetAttributeFailSafe("QueueSizeNode1", tmp)) |
| 262 | { |
| 263 | PointerValue txQueueFrom; |
| 264 | link.GetFromNetDevice ()->GetAttribute ("TxQueue", txQueueFrom); |
| 265 | NS_ASSERT (txQueueFrom.Get<DropTailQueue> () != 0); |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 267 | txQueueFrom.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute("QueueSizeNode1"))); |
| 268 | } |
| 269 | |
| 270 | if (link.GetAttributeFailSafe("QueueSizeNode2", tmp)) |
| 271 | { |
| 272 | PointerValue txQueueTo; |
| 273 | link.GetToNetDevice ()->GetAttribute ("TxQueue", txQueueTo); |
| 274 | NS_ASSERT (txQueueTo.Get<DropTailQueue> () != 0); |
Ilya Moiseenko | 7e14efa | 2011-12-12 17:56:22 -0800 | [diff] [blame] | 275 | |
Alexander Afanasyev | a174aa5 | 2011-12-13 01:30:32 -0800 | [diff] [blame] | 276 | txQueueTo.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute("QueueSizeNode2"))); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 277 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 278 | } |
Alexander Afanasyev | 66e6fd7 | 2011-12-12 21:34:51 -0800 | [diff] [blame] | 279 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame] | 280 | |
| 281 | } |