A Discrete-Event Network Simulator
API
nix-double-wifi.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2021 NITK Surathkal
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  * This example is inspired from examples/tutorial/third.cc by
19  * substituting the CSMA network with another WiFi network.
20  *
21  * Author: Ameya Deshpande <ameyanrd@outlook.com>
22  */
23 
24 #include "ns3/core-module.h"
25 #include "ns3/point-to-point-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/applications-module.h"
28 #include "ns3/mobility-module.h"
29 #include "ns3/csma-module.h"
30 #include "ns3/internet-module.h"
31 #include "ns3/wifi-module.h"
32 #include "ns3/ssid.h"
33 #include "ns3/nix-vector-routing-module.h"
34 
84 using namespace ns3;
85 
86 NS_LOG_COMPONENT_DEFINE ("NixDoubleWifiExample");
87 
88 int
89 main (int argc, char *argv[])
90 {
91  bool useIpv6 = false;
92  bool enableNixLog = false;
93 
94  CommandLine cmd (__FILE__);
95  cmd.AddValue ("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
96  cmd.AddValue ("enableNixLog", "Enable NixVectorRouting logging", enableNixLog);
97  cmd.Parse (argc,argv);
98 
99  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
100  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
101  if (enableNixLog)
102  {
103  LogComponentEnable ("NixVectorRouting", LOG_LEVEL_LOGIC);
104  }
105 
107  p2pNodes.Create (2);
108 
110  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
111  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
112 
114  p2pDevices = pointToPoint.Install (p2pNodes);
115 
116  NodeContainer wifiStaNodes1;
117  wifiStaNodes1.Create (3);
118  NodeContainer wifiApNode1 = p2pNodes.Get (0);
119 
120  NodeContainer wifiStaNodes2;
121  wifiStaNodes2.Create (3);
122  NodeContainer wifiApNode2 = p2pNodes.Get (1);
123 
126  phy.SetChannel (channel.Create ());
127 
129  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
130 
132  Ssid ssid = Ssid ("ns-3-ssid-first");
133  mac.SetType ("ns3::StaWifiMac",
134  "Ssid", SsidValue (ssid),
135  "ActiveProbing", BooleanValue (false));
136 
137  NetDeviceContainer staDevices1;
138  staDevices1 = wifi.Install (phy, mac, wifiStaNodes1);
139 
140  mac.SetType ("ns3::ApWifiMac",
141  "Ssid", SsidValue (ssid));
142 
143  NetDeviceContainer apDevices1;
144  apDevices1 = wifi.Install (phy, mac, wifiApNode1);
145 
146  ssid = Ssid ("ns-3-ssid-second");
147  mac.SetType ("ns3::StaWifiMac",
148  "Ssid", SsidValue (ssid),
149  "ActiveProbing", BooleanValue (false));
150 
151  NetDeviceContainer staDevices2;
152  staDevices2 = wifi.Install (phy, mac, wifiStaNodes2);
153 
154  mac.SetType ("ns3::ApWifiMac",
155  "Ssid", SsidValue (ssid));
156 
157  NetDeviceContainer apDevices2;
158  apDevices2 = wifi.Install (phy, mac, wifiApNode2);
159 
161 
162  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
163  "MinX", DoubleValue (0.0),
164  "MinY", DoubleValue (0.0),
165  "DeltaX", DoubleValue (5.0),
166  "DeltaY", DoubleValue (10.0),
167  "GridWidth", UintegerValue (3),
168  "LayoutType", StringValue ("RowFirst"));
169 
170  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
171  "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
172  mobility.Install (wifiStaNodes1);
173  mobility.Install (wifiStaNodes2);
174 
175  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
176  mobility.Install (wifiApNode1);
177  mobility.Install (wifiApNode2);
178 
179  Address udpServerAddress;
180 
181  if (!useIpv6)
182  {
184  Ipv4NixVectorHelper nixRouting;
185  stack.SetRoutingHelper (nixRouting);
186  stack.Install (wifiApNode1);
187  stack.Install (wifiStaNodes1);
188  stack.Install (wifiApNode2);
189  stack.Install (wifiStaNodes2);
190 
192 
193  address.SetBase ("10.1.1.0", "255.255.255.0");
194  address.Assign (staDevices1);
195  address.Assign (apDevices1);
196 
197  address.SetBase ("10.1.2.0", "255.255.255.0");
199  p2pInterfaces = address.Assign (p2pDevices);
200 
201  address.SetBase ("10.1.3.0", "255.255.255.0");
202  Ipv4InterfaceContainer staDevicesInterfaces2;
203  staDevicesInterfaces2 = address.Assign (staDevices2);
204  address.Assign (apDevices2);
205 
206  udpServerAddress = staDevicesInterfaces2.GetAddress (2);
207 
208  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-double-wifi-ipv4.routes", std::ios::out);
209  nixRouting.PrintRoutingPathAt (Seconds (7), wifiStaNodes1.Get (2), staDevicesInterfaces2.GetAddress (2), routingStream);
210  }
211  else
212  {
214  Ipv6NixVectorHelper nixRouting;
215  stack.SetRoutingHelper (nixRouting);
216  stack.Install (wifiApNode1);
217  stack.Install (wifiStaNodes1);
218  stack.Install (wifiApNode2);
219  stack.Install (wifiStaNodes2);
220 
222 
223  address.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
224  address.Assign (staDevices1);
225  address.Assign (apDevices1);
226 
227  address.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
229  p2pInterfaces = address.Assign (p2pDevices);
230 
231  address.SetBase (Ipv6Address ("2001:3::"), Ipv6Prefix (64));
232  Ipv6InterfaceContainer staDevicesInterfaces2;
233  staDevicesInterfaces2 = address.Assign (staDevices2);
234  address.Assign (apDevices2);
235 
236  udpServerAddress = staDevicesInterfaces2.GetAddress (2, 1);
237 
238  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-double-wifi-ipv6.routes", std::ios::out);
239  nixRouting.PrintRoutingPathAt (Seconds (7), wifiStaNodes1.Get (2), staDevicesInterfaces2.GetAddress (2, 1), routingStream);
240  }
241 
243 
244  ApplicationContainer serverApps = echoServer.Install (wifiStaNodes2.Get (2));
245  serverApps.Start (Seconds (1.0));
246  serverApps.Stop (Seconds (10.0));
247 
248  UdpEchoClientHelper echoClient (udpServerAddress, 9);
249  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
250  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
251  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
252 
253  ApplicationContainer clientApps = echoClient.Install (wifiStaNodes1.Get (2));
254  clientApps.Start (Seconds (2.0));
255  clientApps.Stop (Seconds (10.0));
256 
257  Simulator::Stop (Seconds (10.0));
258 
259  Simulator::Run ();
261  return 0;
262 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::YansWifiChannelHelper::Default
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Definition: yans-wifi-helper.cc:41
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
second.p2pInterfaces
p2pInterfaces
Definition: second.py:75
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:327
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
ns3::RectangleValue
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
third.channel
channel
Definition: third.py:92
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
third.mac
mac
Definition: third.py:99
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::SsidValue
AttributeValue implementation for Ssid.
Definition: ssid.h:105
ns3::Ssid
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
third.wifi
wifi
Definition: third.py:96
ns3::Ptr< OutputStreamWrapper >
ns3::NixVectorHelper
Helper class that adds Nix-vector routing to nodes.
Definition: nix-vector-helper.h:51
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Address
a polymophic address class
Definition: address.h:91
second.p2pNodes
p2pNodes
Definition: second.py:50
ns3::Ipv6InterfaceContainer::GetAddress
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Definition: ipv6-interface-container.cc:57
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
first.address
address
Definition: first.py:44
second.cmd
cmd
Definition: second.py:35
second.p2pDevices
p2pDevices
Definition: second.py:61
ns3::Rectangle
a 2d rectangle
Definition: rectangle.h:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
first.echoClient
echoClient
Definition: first.py:56
first.clientApps
clientApps
Definition: first.py:61
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
first.serverApps
serverApps
Definition: first.py:52
third.ssid
ssid
Definition: third.py:100
first.pointToPoint
pointToPoint
Definition: first.py:35
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NixVectorHelper::PrintRoutingPathAt
void PrintRoutingPathAt(Time printTime, Ptr< Node > source, IpAddress dest, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing path for a source and destination at a particular time.
Definition: nix-vector-helper.cc:67
ns3::LOG_LEVEL_LOGIC
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:113
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
third.mobility
mobility
Definition: third.py:108
first.echoServer
echoServer
Definition: first.py:50
third.phy
phy
Definition: third.py:93