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" |
| 42 | #include "ns3/random-variable.h" |
| 43 | |
| 44 | #include <boost/foreach.hpp> |
| 45 | #include <boost/lexical_cast.hpp> |
| 46 | |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 47 | using namespace std; |
| 48 | |
| 49 | namespace ns3 |
| 50 | { |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 51 | |
| 52 | NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReader"); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 53 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 54 | NS_OBJECT_ENSURE_REGISTERED (AnnotatedTopologyReader); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 55 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 56 | TypeId AnnotatedTopologyReader::GetTypeId (void) |
| 57 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 58 | static TypeId tid = TypeId ("ns3::AnnotatedTopologyReader") |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 59 | .SetParent<Object> () |
| 60 | ; |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 61 | return tid; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 64 | AnnotatedTopologyReader::AnnotatedTopologyReader (const std::string &path) |
| 65 | : m_path (path) |
| 66 | , m_ulx (0) |
| 67 | , m_uly (0) |
| 68 | , m_lrx (100.0) |
| 69 | , m_lry (100.0) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 70 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 71 | NS_LOG_FUNCTION (this); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 72 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 73 | |
| 74 | void |
| 75 | AnnotatedTopologyReader::SetBoundingBox (double ulx, double uly, double lrx, double lry) |
| 76 | { |
| 77 | NS_LOG_FUNCTION (this << ulx << uly << lrx << lry); |
| 78 | |
| 79 | m_ulx = ulx; |
| 80 | m_uly = uly; |
| 81 | m_lrx = lrx; |
| 82 | m_lry = lry; |
| 83 | } |
| 84 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 85 | AnnotatedTopologyReader::~AnnotatedTopologyReader () |
| 86 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 87 | NS_LOG_FUNCTION (this); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | NodeContainer |
| 91 | AnnotatedTopologyReader::Read (void) |
| 92 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 93 | ifstream topgen; |
| 94 | topgen.open (GetFileName ().c_str ()); |
| 95 | NodeContainer nodes; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 97 | if (!topgen.is_open ()) |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 98 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 99 | NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading"); |
| 100 | return nodes; |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 101 | } |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 103 | int linksNumber = 0; |
| 104 | int nodesNumber = 0; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 106 | string line; |
| 107 | getline (topgen,line); |
| 108 | istringstream headerLineBuffer (line); |
| 109 | |
| 110 | int totnode; |
| 111 | int totlink; |
| 112 | headerLineBuffer >> totnode; |
| 113 | headerLineBuffer >> totlink; |
| 114 | |
| 115 | NS_LOG_INFO ("Annotated topology should have " << totnode << " nodes and " << totlink << " links"); |
| 116 | |
| 117 | for (int i = 0; i < totlink && !topgen.eof (); i++) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 118 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 119 | getline (topgen, line); |
| 120 | istringstream lineBuffer (line); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 122 | string from; |
| 123 | string to; |
| 124 | lineBuffer >> from; |
| 125 | lineBuffer >> to; |
| 126 | |
| 127 | if ( (!from.empty ()) && (!to.empty ()) ) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 129 | NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to ); |
| 130 | |
| 131 | Ptr<Node> fromNode = Names::Find<Node> (m_path, from); |
| 132 | Ptr<Node> toNode = Names::Find<Node> (m_path, to); |
| 133 | |
| 134 | if (fromNode == 0) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 135 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 136 | fromNode = CreateObject<Node> (); |
| 137 | Names::Add (m_path, from, fromNode); |
| 138 | nodes.Add (fromNode); |
| 139 | nodesNumber++; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 142 | if (toNode == 0) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 144 | toNode = CreateObject<Node> (); |
| 145 | Names::Add (m_path, to, toNode); |
| 146 | nodes.Add (toNode); |
| 147 | nodesNumber++; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 150 | Link link (fromNode, from, toNode, to); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 152 | string dataRate; |
| 153 | lineBuffer >> dataRate; |
| 154 | |
| 155 | string ospf; |
| 156 | lineBuffer >> ospf; |
| 157 | |
| 158 | string delay; |
| 159 | lineBuffer >> delay; |
| 160 | |
| 161 | string queueSizeNode1; |
| 162 | lineBuffer >> queueSizeNode1; |
| 163 | |
| 164 | string queueSizeNode2; |
| 165 | lineBuffer >> queueSizeNode2; |
| 166 | |
| 167 | if (dataRate.empty () || |
| 168 | ospf.empty () || |
| 169 | delay.empty () || |
| 170 | queueSizeNode1.empty () || |
| 171 | queueSizeNode2.empty ()) |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 172 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 173 | NS_LOG_ERROR ("File [" << GetFileName () << ":" << i+2 << " wrong format, skipping"); |
| 174 | continue; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 175 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 176 | |
| 177 | link.SetAttribute ("DataRate", dataRate); |
| 178 | link.SetAttribute ("OSPF", ospf); |
| 179 | link.SetAttribute ("Delay", delay); |
| 180 | link.SetAttribute ("QueueSizeNode1", queueSizeNode1); |
| 181 | link.SetAttribute ("QueueSizeNode2", queueSizeNode2); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 183 | AddLink (link); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 184 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 185 | linksNumber++; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 188 | |
| 189 | NS_ASSERT (nodesNumber == totnode && linksNumber == totlink); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 191 | NS_LOG_INFO ("Annotated topology created with " << nodesNumber << " nodes and " << linksNumber << " links"); |
| 192 | topgen.close (); |
| 193 | |
| 194 | ApplySettings (); |
| 195 | AssignCoordinates (); |
| 196 | |
| 197 | return nodes; |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 198 | } |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 199 | |
| 200 | void |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 201 | AnnotatedTopologyReader::AssignIpv4Addresses (Ipv4Address base) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 202 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 203 | Ipv4AddressHelper address (base, Ipv4Mask ("/24")); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 205 | BOOST_FOREACH (const Link &link, m_linksList) |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 206 | { |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 207 | address.Assign (NetDeviceContainer (link.GetFromNetDevice (), |
| 208 | link.GetToNetDevice ())); |
| 209 | |
| 210 | base = Ipv4Address (base.Get () + 256); |
| 211 | address.SetBase (base, Ipv4Mask ("/24")); |
Ilya Moiseenko | 58d2667 | 2011-12-08 13:48:06 -0800 | [diff] [blame] | 212 | } |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 213 | |
| 214 | ApplyOspfMetric (); |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 215 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 8633d5d | 2011-12-12 18:02:31 -0800 | [diff] [blame^] | 217 | void |
| 218 | AnnotatedTopologyReader::ApplyOspfMetric () |
| 219 | { |
| 220 | BOOST_FOREACH (const Link &link, m_linksList) |
| 221 | { |
| 222 | uint16_t metric = boost::lexical_cast<uint16_t> (link.GetAttribute ("OSPF")); |
| 223 | |
| 224 | { |
| 225 | Ptr<Ipv4> ipv4 = link.GetFromNode ()->GetObject<Ipv4> (); |
| 226 | NS_ASSERT (ipv4 != 0); |
| 227 | |
| 228 | int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetFromNetDevice ()); |
| 229 | NS_ASSERT (interfaceId >= 0); |
| 230 | |
| 231 | ipv4->SetMetric (interfaceId,metric); |
| 232 | } |
| 233 | |
| 234 | { |
| 235 | Ptr<Ipv4> ipv4 = link.GetToNode ()->GetObject<Ipv4> (); |
| 236 | NS_ASSERT (ipv4 != 0); |
| 237 | |
| 238 | int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetToNetDevice ()); |
| 239 | NS_ASSERT (interfaceId >= 0); |
| 240 | |
| 241 | ipv4->SetMetric (interfaceId,metric); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | AnnotatedTopologyReader::ApplySettings () |
| 248 | { |
| 249 | PointToPointHelper p2p; |
| 250 | |
| 251 | // temporary queue, will be changed later |
| 252 | p2p.SetQueue ("ns3::DropTailQueue", |
| 253 | "MaxPackets", StringValue("100")); |
| 254 | |
| 255 | BOOST_FOREACH (Link &link, m_linksList) |
| 256 | { |
| 257 | NS_LOG_INFO ("DataRate = " + link.GetAttribute("DataRate")+"Kbps"); |
| 258 | p2p.SetDeviceAttribute ("DataRate", StringValue(link.GetAttribute("DataRate")+"Kbps")); |
| 259 | |
| 260 | NS_LOG_INFO ("Delay = " + link.GetAttribute("Delay")+"ms"); |
| 261 | p2p.SetChannelAttribute ("Delay", StringValue(link.GetAttribute("Delay")+"ms")); |
| 262 | |
| 263 | NetDeviceContainer nd = p2p.Install(link.GetFromNode (), link.GetToNode ()); |
| 264 | link.SetNetDevices (nd.Get (0), nd.Get (1)); |
| 265 | |
| 266 | NS_LOG_INFO ("Queue: " << link.GetAttribute("QueueSizeNode1") << " <==> " << link.GetAttribute("QueueSizeNode2")); |
| 267 | |
| 268 | PointerValue txQueueFrom; |
| 269 | link.GetFromNetDevice ()->GetAttribute ("TxQueue", txQueueFrom); |
| 270 | NS_ASSERT (txQueueFrom.Get<DropTailQueue> () != 0); |
| 271 | |
| 272 | PointerValue txQueueTo; |
| 273 | link.GetToNetDevice ()->GetAttribute ("TxQueue", txQueueTo); |
| 274 | NS_ASSERT (txQueueTo.Get<DropTailQueue> () != 0); |
| 275 | |
| 276 | txQueueFrom.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute("QueueSizeNode1"))); |
| 277 | txQueueTo. Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute("QueueSizeNode2"))); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | AnnotatedTopologyReader::AssignCoordinates () |
| 283 | { |
| 284 | UniformVariable randX (m_ulx, m_lrx); |
| 285 | double x = 0.0; |
| 286 | UniformVariable randY (m_uly, m_lry); |
| 287 | double y = 0.0; |
| 288 | |
| 289 | BOOST_FOREACH (Link &link, m_linksList) |
| 290 | { |
| 291 | Ptr<ConstantPositionMobilityModel> loc = link.GetFromNode ()->GetObject<ConstantPositionMobilityModel> (); |
| 292 | if (loc != 0) |
| 293 | continue; // no need to assign twice |
| 294 | |
| 295 | loc = CreateObject<ConstantPositionMobilityModel> (); |
| 296 | link.GetFromNode ()->AggregateObject (loc); |
| 297 | |
| 298 | x = randX.GetValue(); |
| 299 | y = randY.GetValue(); |
| 300 | NS_LOG_INFO("X = "<<x <<"Y = "<<y); |
| 301 | |
| 302 | loc->SetPosition (Vector (x, y, 0)); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | } |