1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/mixed-wireless.cc Tue Apr 08 23:12:19 2008 -0700
1.3 @@ -0,0 +1,358 @@
1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 +/*
1.6 + * This program is free software; you can redistribute it and/or modify
1.7 + * it under the terms of the GNU General Public License version 2 as
1.8 + * published by the Free Software Foundation;
1.9 + *
1.10 + * This program is distributed in the hope that it will be useful,
1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.13 + * GNU General Public License for more details.
1.14 + *
1.15 + * You should have received a copy of the GNU General Public License
1.16 + * along with this program; if not, write to the Free Software
1.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.18 + *
1.19 + */
1.20 +
1.21 +//
1.22 +// This ns-3 example demonstrates the use of helper functions to ease
1.23 +// the construction of simulation scenarios.
1.24 +//
1.25 +// The simulation topology consists of a mixed wired and wireless
1.26 +// scenario in which a hierarchical mobility model is used.
1.27 +//
1.28 +// The simulation layout consists of N backbone routers interconnected
1.29 +// by an ad hoc wifi network.
1.30 +// Each backbone router also has a local 802.11 network and is connected
1.31 +// to a local LAN. An additional set of (K-1) nodes are connected to
1.32 +// this backbone. Finally, a local LAN is connected to each router
1.33 +// on the backbone, with L-1 additional hosts.
1.34 +//
1.35 +// The nodes are populated with TCP/IP stacks, and OLSR unicast routing
1.36 +// on the backbone. An example UDP transfer is shown. The simulator
1.37 +// be configured to output tcpdumps or traces from different nodes.
1.38 +//
1.39 +//
1.40 +// +--------------------------------------------------------+
1.41 +// | |
1.42 +// | 802.11 ad hoc, ns-2 mobility |
1.43 +// | |
1.44 +// +--------------------------------------------------------+
1.45 +// | o o o (N backbone routers) |
1.46 +// +--------+ +--------+
1.47 +// wired LAN | mobile | wired LAN | mobile |
1.48 +// -----------| router | -----------| router |
1.49 +// --------- ---------
1.50 +// | |
1.51 +// +----------------+ +----------------+
1.52 +// | 802.11 | | 802.11 |
1.53 +// | net | | net |
1.54 +// | K-1 hosts | | K-1 hosts |
1.55 +// +----------------+ +----------------+
1.56 +//
1.57 +
1.58 +#include <fstream>
1.59 +#include "ns3/core-module.h"
1.60 +#include "ns3/common-module.h"
1.61 +#include "ns3/node-module.h"
1.62 +#include "ns3/helper-module.h"
1.63 +#include "ns3/mobility-module.h"
1.64 +#include "ns3/contrib-module.h"
1.65 +#include "ns3/wifi-module.h"
1.66 +
1.67 +using namespace ns3;
1.68 +
1.69 +//
1.70 +// Define logging keyword for this file
1.71 +//
1.72 +NS_LOG_COMPONENT_DEFINE ("MixedWireless");
1.73 +
1.74 +//
1.75 +// This function will be used below as a trace sink
1.76 +//
1.77 +#ifdef ENABLE_FOR_TRACING_EXAMPLE
1.78 +static void
1.79 +CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
1.80 +{
1.81 + Vector position = model->GetPosition ();
1.82 + std::cout << "CourseChange " << path << " x=" << position.x << ", y=" << position.y << ", z=" << position.z << std::endl;
1.83 +}
1.84 +#endif
1.85 +
1.86 +int
1.87 +main (int argc, char *argv[])
1.88 +{
1.89 + //
1.90 + // First, we declare and initialize a few local variables that control some
1.91 + // simulation parameters.
1.92 + //
1.93 + uint32_t backboneNodes = 10;
1.94 + uint32_t infraNodes = 5;
1.95 + uint32_t lanNodes = 5;
1.96 + uint32_t stopTime = 10;
1.97 +
1.98 + //
1.99 + // Simulation defaults are typically set next, before command line
1.100 + // arguments are parsed.
1.101 + //
1.102 + Config::SetDefault ("ns3::OnOffApplication::PacketSize", String ("210"));
1.103 + Config::SetDefault ("ns3::OnOffApplication::DataRate", String ("448kb/s"));
1.104 +
1.105 + //
1.106 + // For convenience, we add the local variables to the command line argument
1.107 + // system so that they can be overridden with flags such as
1.108 + // "--backboneNodes=20"
1.109 + //
1.110 + CommandLine cmd;
1.111 + cmd.AddValue("backboneNodes", "number of backbone nodes", backboneNodes);
1.112 + cmd.AddValue ("infraNodes", "number of leaf nodes", infraNodes);
1.113 + cmd.AddValue("lanNodes", "number of LAN nodes", lanNodes);
1.114 + cmd.AddValue("stopTime", "simulation stop time (seconds)", stopTime);
1.115 +
1.116 + //
1.117 + // The system global variables and the local values added to the argument
1.118 + // system can be overridden by command line arguments by using this call.
1.119 + //
1.120 + cmd.Parse (argc, argv);
1.121 +
1.122 + // The metadata system (off by default) is used by ascii tracing below
1.123 + Packet::EnableMetadata ();
1.124 +
1.125 + ///////////////////////////////////////////////////////////////////////////
1.126 + // //
1.127 + // Construct the backbone //
1.128 + // //
1.129 + ///////////////////////////////////////////////////////////////////////////
1.130 +
1.131 + //
1.132 + // Create a container to manage the nodes of the adhoc (backbone) network.
1.133 + // Later we'll create the rest of the nodes we'll need.
1.134 + //
1.135 + NodeContainer backbone;
1.136 + backbone.Create (backboneNodes);
1.137 + //
1.138 + // Create the backbone wifi net devices and install them into the nodes in
1.139 + // our container
1.140 + //
1.141 + WifiHelper wifi;
1.142 + wifi.SetMac ("ns3::AdhocWifiMac");
1.143 + wifi.SetPhy ("ns3::WifiPhy");
1.144 + NetDeviceContainer backboneDevices = wifi.Install (backbone);
1.145 + //
1.146 + // Add the IPv4 protocol stack to the nodes in our container
1.147 + //
1.148 + InternetStackHelper internet;
1.149 + internet.Install (backbone);
1.150 + //
1.151 + // Assign IPv4 addresses to the device drivers (actually to the associated
1.152 + // IPv4 interfaces) we just created.
1.153 + //
1.154 + Ipv4AddressHelper ipAddrs;
1.155 + ipAddrs.SetBase ("192.168.0.0", "255.255.255.0");
1.156 + ipAddrs.Assign (backboneDevices);
1.157 +
1.158 + //
1.159 + // The ad-hoc network nodes need a mobility model so we aggregate one to
1.160 + // each of the nodes we just finished building.
1.161 + //
1.162 + MobilityHelper mobility;
1.163 + Ptr<ListPositionAllocator> positionAlloc =
1.164 + CreateObject<ListPositionAllocator> ();
1.165 + positionAlloc->Add (Vector (0.0, 0.0, 0.0));
1.166 + positionAlloc->Add (Vector (5.0, 0.0, 0.0));
1.167 + mobility.SetPositionAllocator (positionAlloc);
1.168 + mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel",
1.169 + "Bounds", Rectangle (0, 1000, 0, 1000),
1.170 + "Speed", ConstantVariable (2000),
1.171 + "Pause", ConstantVariable (0.2));
1.172 + mobility.Layout (backbone);
1.173 +
1.174 + ///////////////////////////////////////////////////////////////////////////
1.175 + // //
1.176 + // Construct the LANs //
1.177 + // //
1.178 + ///////////////////////////////////////////////////////////////////////////
1.179 +
1.180 + // Reset the address base-- all of the CSMA networks will be in
1.181 + // the "172.16 address space
1.182 + ipAddrs.SetBase ("172.16.0.0", "255.255.255.0");
1.183 +
1.184 + for (uint32_t i = 0; i < backboneNodes; ++i)
1.185 + {
1.186 + NS_LOG_INFO ("Configuring local area network for backbone node " << i);
1.187 + //
1.188 + // Create a container to manage the nodes of the LAN. Pick one of
1.189 + // the backbone nodes to be part of the LAN and first add it to
1.190 + // the container. Then create the rest of the nodes we'll need.
1.191 + //
1.192 + NodeContainer lan;
1.193 + lan.Add (backbone.Get (i));
1.194 + lan.Create (lanNodes - 1);
1.195 + //
1.196 + // Create the CSMA net devices and install them into the nodes in our
1.197 + // collection.
1.198 + //
1.199 + CsmaHelper csma;
1.200 + csma.SetChannelParameter ("BitRate", DataRate (5000000));
1.201 + csma.SetChannelParameter ("Delay", MilliSeconds (2));
1.202 + NetDeviceContainer lanDevices = csma.Install (lan);
1.203 + //
1.204 + // Add the IPv4 protocol stack to the nodes in our container
1.205 + //
1.206 + internet.Install (lan);
1.207 + //
1.208 + // Assign IPv4 addresses to the device drivers (actually to the
1.209 + // associated IPv4 interfaces) we just created.
1.210 + //
1.211 + ipAddrs.Assign (lanDevices);
1.212 + //
1.213 + // Assign a new network prefix for the next LAN, according to the
1.214 + // network mask initialized above
1.215 + //
1.216 + ipAddrs.NewNetwork ();
1.217 + }
1.218 +
1.219 + ///////////////////////////////////////////////////////////////////////////
1.220 + // //
1.221 + // Construct the mobile networks //
1.222 + // //
1.223 + ///////////////////////////////////////////////////////////////////////////
1.224 +
1.225 + // Reset the address base-- all of the 802.11 networks will be in
1.226 + // the "10.0" address space
1.227 + ipAddrs.SetBase ("10.0.0.0", "255.255.255.0");
1.228 +
1.229 + for (uint32_t i = 0; i < backboneNodes; ++i)
1.230 + {
1.231 + NS_LOG_INFO ("Configuring wireless network for backbone node " << i);
1.232 + //
1.233 + // Create a container to manage the nodes of the network. Pick one of
1.234 + // the backbone nodes to be part of the network and first add it to
1.235 + // the container. Then create the rest of the nodes we'll need.
1.236 + //
1.237 + NodeContainer infra;
1.238 + infra.Add (backbone.Get (i));
1.239 + infra.Create (infraNodes - 1);
1.240 + //
1.241 + // Create another ad hoc network and devices
1.242 + //
1.243 + WifiHelper wifiInfra;
1.244 + wifiInfra.SetMac ("ns3::AdhocWifiMac");
1.245 + wifiInfra.SetPhy ("ns3::WifiPhy");
1.246 + NetDeviceContainer infraDevices = wifiInfra.Install (infra);
1.247 +
1.248 + // Add the IPv4 protocol stack to the nodes in our container
1.249 + //
1.250 + internet.Install (infra);
1.251 + //
1.252 + // Assign IPv4 addresses to the device drivers (actually to the associated
1.253 + // IPv4 interfaces) we just created.
1.254 + //
1.255 + ipAddrs.Assign (infraDevices);
1.256 + //
1.257 + // Assign a new network prefix for each mobile network, according to
1.258 + // the network mask initialized above
1.259 + //
1.260 + ipAddrs.NewNetwork ();
1.261 + //
1.262 + // The new wireless nodes need a mobility model so we aggregate one
1.263 + // to each of the nodes we just finished building.
1.264 + //
1.265 + Ptr<ListPositionAllocator> subnetAlloc =
1.266 + CreateObject<ListPositionAllocator> ();
1.267 + for (uint32_t j = 0; j < infra.GetN (); ++j)
1.268 + {
1.269 + subnetAlloc->Add (Vector (0.0, j, 0.0));
1.270 + }
1.271 + mobility.EnableNotifier ();
1.272 + mobility.PushReferenceMobilityModel (backbone.Get (i));
1.273 + mobility.SetPositionAllocator (subnetAlloc);
1.274 + mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel",
1.275 + "Bounds", Rectangle (-25, 25, -25, 25),
1.276 + "Speed", ConstantVariable (30),
1.277 + "Pause", ConstantVariable (0.4));
1.278 + mobility.Layout (infra);
1.279 + }
1.280 + ///////////////////////////////////////////////////////////////////////////
1.281 + // //
1.282 + // Routing configuration //
1.283 + // //
1.284 + ///////////////////////////////////////////////////////////////////////////
1.285 +
1.286 + NS_LOG_INFO ("Enabling OLSR routing on all backbone nodes");
1.287 + OlsrHelper olsr;
1.288 + olsr.Enable (backbone);
1.289 +
1.290 + ///////////////////////////////////////////////////////////////////////////
1.291 + // //
1.292 + // Application configuration //
1.293 + // //
1.294 + ///////////////////////////////////////////////////////////////////////////
1.295 +
1.296 + // Create the OnOff application to send UDP datagrams of size
1.297 + // 210 bytes at a rate of 448 Kb/s, between two nodes
1.298 + NS_LOG_INFO ("Create Applications.");
1.299 + uint16_t port = 9; // Discard port (RFC 863)
1.300 +
1.301 + // Let's make sure that the user does not define too few LAN nodes
1.302 + // to make this example work. We need lanNodes >= 5
1.303 + NS_ASSERT (lanNodes >= 5);
1.304 + Ptr<Node> appSource = NodeList::GetNode (11);
1.305 + Ptr<Node> appSink = NodeList::GetNode (13);
1.306 + Ipv4Address remoteAddr = Ipv4Address ("172.16.0.5");
1.307 +
1.308 + OnOffHelper onoff ("ns3::Udp",
1.309 + Address (InetSocketAddress (remoteAddr, port)));
1.310 + onoff.SetAttribute ("OnTime", ConstantVariable (1));
1.311 + onoff.SetAttribute ("OffTime", ConstantVariable (0));
1.312 + ApplicationContainer apps = onoff.Install (appSource);
1.313 + apps.Start (Seconds (3.0));
1.314 + apps.Stop (Seconds (20.0));
1.315 +
1.316 + // Create a packet sink to receive these packets
1.317 + PacketSinkHelper sink ("ns3::Udp",
1.318 + InetSocketAddress (Ipv4Address::GetAny (), port));
1.319 + apps = sink.Install (appSink);
1.320 + apps.Start (Seconds (3.0));
1.321 +
1.322 + ///////////////////////////////////////////////////////////////////////////
1.323 + // //
1.324 + // Tracing configuration //
1.325 + // //
1.326 + ///////////////////////////////////////////////////////////////////////////
1.327 +
1.328 + NS_LOG_INFO ("Configure Tracing.");
1.329 + //
1.330 + // Let's set up some ns-2-like ascii traces, using another helper class
1.331 + //
1.332 + // Look at nodes 11, 13 only
1.333 + // XXX todo
1.334 + // asciiTrace.TraceQueues ("/NodeList/11|13/DeviceList/0");
1.335 + // asciiTrace.TraceNetDeviceRx ("/NodeList/11|13/DeviceList/0");
1.336 + std::ofstream ascii;
1.337 + ascii.open ("mixed-wireless.tr");
1.338 + WifiHelper::EnableAscii (ascii);
1.339 + CsmaHelper::EnableAscii (ascii);
1.340 +
1.341 + // Let's do a pcap trace on the backbone devices
1.342 + WifiHelper::EnablePcap ("mixed-wireless.pcap", backboneDevices);
1.343 + // Let's additionally trace the application Sink, ifIndex 0
1.344 + CsmaHelper::EnablePcap ("mixed-wireless.pcap", appSink->GetId (), 0);
1.345 +
1.346 +#ifdef ENABLE_FOR_TRACING_EXAMPLE
1.347 + Config::Connect ("/NodeList/*/$MobilityModelNotifier/CourseChange",
1.348 + MakeCallback (&CourseChangeCallback));
1.349 +#endif
1.350 +
1.351 + ///////////////////////////////////////////////////////////////////////////
1.352 + // //
1.353 + // Run simulation //
1.354 + // //
1.355 + ///////////////////////////////////////////////////////////////////////////
1.356 +
1.357 + NS_LOG_INFO ("Run Simulation.");
1.358 + Simulator::StopAt (Seconds (stopTime));
1.359 + Simulator::Run ();
1.360 + Simulator::Destroy ();
1.361 +}
2.1 --- a/examples/wscript Tue Apr 08 23:10:39 2008 -0700
2.2 +++ b/examples/wscript Tue Apr 08 23:12:19 2008 -0700
2.3 @@ -2,6 +2,10 @@
2.4
2.5 def build(bld):
2.6
2.7 + obj = bld.create_ns3_program('mixed-wireless',
2.8 + ['core', 'simulator', 'mobility', 'wifi', 'point-to-point', 'internet-node'])
2.9 + obj.source = 'mixed-wireless.cc'
2.10 +
2.11 obj = bld.create_ns3_program('simple-global-routing',
2.12 ['point-to-point', 'internet-node', 'global-routing'])
2.13 obj.source = 'simple-global-routing.cc'