1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/wns3-example.cc Thu Feb 26 09:07:31 2009 +0100
1.3 @@ -0,0 +1,186 @@
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 +// Default network topology includes some number of AP nodes specified by
1.22 +// the variable nWifis (defaults to two). Off of each AP node, there are some
1.23 +// number of STA nodes specified by the variable nStas (defaults to two).
1.24 +// Each AP talks to its associated STA nodes. There are bridge net devices
1.25 +// on each AP node that bridge the whole thing into one network.
1.26 +//
1.27 +// +-----+ +-----+ +-----+ +-----+
1.28 +// | STA | | STA | | STA | | STA |
1.29 +// +-----+ +-----+ +-----+ +-----+
1.30 +// 192.168.0.2 192.168.0.3 192.168.1.2 192.168.1.3
1.31 +// -------- -------- -------- --------
1.32 +// WIFI STA WIFI STA WIFI STA WIFI STA
1.33 +// -------- -------- -------- --------
1.34 +// ((*)) ((*)) | ((*)) ((*))
1.35 +// |
1.36 +// ((*)) | ((*))
1.37 +// 192.168.0.1 192.168.1.1
1.38 +// ------- -------
1.39 +// WIFI AP CSMA ========= CSMA WIFI AP
1.40 +// ------- ---- ---- -------
1.41 +// 10.0.0.1 10.0.0.2
1.42 +// ############## ##############
1.43 +// ROUTER ROUTER
1.44 +// ############## ##############
1.45 +// +---------+ +---------+
1.46 +// | AP Node | | AP Node |
1.47 +// +---------+ +---------+
1.48 +//
1.49 +
1.50 +#include "ns3/core-module.h"
1.51 +#include "ns3/simulator-module.h"
1.52 +#include "ns3/mobility-module.h"
1.53 +#include "ns3/helper-module.h"
1.54 +#include "ns3/wifi-module.h"
1.55 +#include "ns3/node-module.h"
1.56 +#include "ns3/global-route-manager.h"
1.57 +#include <vector>
1.58 +#include <stdint.h>
1.59 +#include <sstream>
1.60 +#include <fstream>
1.61 +
1.62 +using namespace ns3;
1.63 +
1.64 +int main (int argc, char *argv[])
1.65 +{
1.66 + uint32_t nWifis = 2;
1.67 + uint32_t nStas = 2;
1.68 +
1.69 + RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
1.70 +
1.71 + CommandLine cmd;
1.72 + cmd.AddValue ("nWifis", "Number of wifi networks", nWifis);
1.73 + cmd.AddValue ("nStas", "Number of stations per wifi network", nStas);
1.74 + cmd.Parse (argc, argv);
1.75 +
1.76 + NodeContainer backboneNodes;
1.77 + NetDeviceContainer backboneDevices;
1.78 + Ipv4InterfaceContainer backboneInterfaces;
1.79 + std::vector<NodeContainer> staNodes;
1.80 + std::vector<NetDeviceContainer> staDevices;
1.81 + std::vector<NetDeviceContainer> apDevices;
1.82 + std::vector<Ipv4InterfaceContainer> staInterfaces;
1.83 + std::vector<Ipv4InterfaceContainer> apInterfaces;
1.84 +
1.85 + InternetStackHelper stack;
1.86 + CsmaHelper csma;
1.87 + Ipv4AddressHelper ip;
1.88 + ip.SetBase ("192.168.0.0", "255.255.255.0");
1.89 + Ipv4AddressHelper ipBackbone;
1.90 + ipBackbone.SetBase ("10.0.0.0", "255.255.255.0");
1.91 +
1.92 + backboneNodes.Create (nWifis);
1.93 + stack.Install (backboneNodes);
1.94 +
1.95 + backboneDevices = csma.Install (backboneNodes);
1.96 + ipBackbone.Assign (backboneDevices);
1.97 +
1.98 + double wifiX = 0.0;
1.99 + for (uint32_t i = 0; i < nWifis; ++i)
1.100 + {
1.101 + // calculate ssid for wifi subnetwork
1.102 + std::ostringstream oss;
1.103 + oss << "wifi-default-" << i;
1.104 + Ssid ssid = Ssid (oss.str ());
1.105 +
1.106 + NodeContainer sta;
1.107 + NetDeviceContainer staDev;
1.108 + NetDeviceContainer apDev;
1.109 + Ipv4InterfaceContainer staInterface;
1.110 + Ipv4InterfaceContainer apInterface;
1.111 + MobilityHelper mobility;
1.112 + WifiHelper wifi = WifiHelper::Default ();
1.113 + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
1.114 + YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
1.115 + wifiPhy.SetChannel (wifiChannel.Create ());
1.116 +
1.117 + sta.Create (nStas);
1.118 + mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
1.119 + "MinX", DoubleValue (wifiX),
1.120 + "MinY", DoubleValue (0.0),
1.121 + "DeltaX", DoubleValue (5.0),
1.122 + "DeltaY", DoubleValue (5.0),
1.123 + "GridWidth", UintegerValue (1),
1.124 + "LayoutType", StringValue ("RowFirst"));
1.125 +
1.126 +
1.127 + // setup the AP.
1.128 + mobility.SetMobilityModel ("ns3::StaticMobilityModel");
1.129 + mobility.Install (backboneNodes.Get (i));
1.130 + wifi.SetMac ("ns3::NqapWifiMac",
1.131 + "Ssid", SsidValue (ssid),
1.132 + "BeaconGeneration", BooleanValue (true),
1.133 + "BeaconInterval", TimeValue (Seconds (2.5)));
1.134 + apDev = wifi.Install (wifiPhy, backboneNodes.Get (i));
1.135 +
1.136 + // assign AP IP address
1.137 + apInterface = ip.Assign (apDev);
1.138 +
1.139 + // setup the STAs
1.140 + stack.Install (sta);
1.141 + mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
1.142 + "Mode", StringValue ("Time"),
1.143 + "Time", StringValue ("2s"),
1.144 + "Speed", StringValue ("Constant:1.0"),
1.145 + "Bounds", RectangleValue (Rectangle (wifiX, wifiX+5.0,0.0, (nStas+1)*5.0)));
1.146 + mobility.Install (sta);
1.147 + wifi.SetMac ("ns3::NqstaWifiMac",
1.148 + "Ssid", SsidValue (ssid),
1.149 + "ActiveProbing", BooleanValue (false));
1.150 + staDev = wifi.Install (wifiPhy, sta);
1.151 + staInterface = ip.Assign (staDev);
1.152 +
1.153 + // save everything in containers.
1.154 + staNodes.push_back (sta);
1.155 + apDevices.push_back (apDev);
1.156 + apInterfaces.push_back (apInterface);
1.157 + staDevices.push_back (staDev);
1.158 + staInterfaces.push_back (staInterface);
1.159 +
1.160 + wifiX += 20.0;
1.161 + ip.NewNetwork ();
1.162 + }
1.163 +
1.164 + GlobalRouteManager::PopulateRoutingTables ();
1.165 +
1.166 + Address dest;
1.167 + std::string protocol;
1.168 + dest = InetSocketAddress (staInterfaces[1].GetAddress (1), 1025);
1.169 + protocol = "ns3::UdpSocketFactory";
1.170 +
1.171 + OnOffHelper onoff = OnOffHelper (protocol, dest);
1.172 + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
1.173 + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
1.174 + ApplicationContainer apps = onoff.Install (staNodes[0].Get (0));
1.175 + apps.Start (Seconds (0.5));
1.176 + apps.Stop (Seconds (3.0));
1.177 +
1.178 + YansWifiPhyHelper::EnablePcap ("wns3-example", staNodes[1].Get (1));
1.179 + YansWifiPhyHelper::EnablePcap ("wns3-example", staNodes[0].Get (0));
1.180 + std::ofstream os;
1.181 + os.open ("wns3-example.mob");
1.182 + MobilityHelper::EnableAsciiAll (os);
1.183 +
1.184 + Simulator::Stop (Seconds (100.0));
1.185 + Simulator::Run ();
1.186 + Simulator::Destroy ();
1.187 +}
1.188 +
1.189 +