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