Difference between revisions of "Ns-3-Network-Simulator/C3/Creating-an-ad-hoc-wireless-network/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || Show Slide: '''Title Slide''' || Welcome to Spoken tutorial on <span style="background-color:#ffffff;">'''Creatin...")
 
 
Line 5: Line 5:
 
|-
 
|-
 
|| Show Slide: '''Title Slide'''
 
|| Show Slide: '''Title Slide'''
|| Welcome to Spoken tutorial on <span style="background-color:#ffffff;">'''Creating an Ad-hoc wireless network.'''</span>
+
|| Welcome to Spoken tutorial on '''Creating an Ad-hoc wireless network'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn to
 
|| In this tutorial, we will learn to
* <div style="margin-left:1.27cm;margin-right:0cm;">Create an '''Ad-hoc wireless network'''.</div>
+
* Create an '''Ad-hoc wireless network'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">Create three '''UDP flows''' in the channels.</div>
+
* Create three '''UDP flows''' in the channels.
* <div style="margin-left:1.27cm;margin-right:0cm;">Analyze the flow using a '''flow monitor'''.</div>
+
* Analyze the flow using a '''flow monitor'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">Visualize the network using '''NetAnim.'''</div>
+
* Visualize the network using '''NetAnim'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show Slide:  
 
|| Show Slide:  
  
 
'''System Requirements'''
 
'''System Requirements'''
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux OS version 22.04.'''</div>
+
* '''Ubuntu Linux OS version 22.04.'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''ns-3 version''' 3.38.</div>
+
* '''ns-3 version''' 3.38.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''NetAnim''' visualizer tool.</div>
+
* '''NetAnim''' visualizer tool.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show slide:  
 
|| Show slide:  
  
Line 33: Line 33:
 
[https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org]
 
[https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org]
 
|| To follow this tutorial
 
|| To follow this tutorial
* <div style="margin-left:1.27cm;margin-right:0cm;">The learner must have basic knowledge of ns-3 software.</div>
+
* The learner must have basic knowledge of ns-3 software.
* <div style="margin-left:1.27cm;margin-right:0cm;">For pre-requisite ns-3 tutorials, please visit this website</div>
+
* For pre-requisite ns-3 tutorials, please visit this website
  
 
[https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org]
 
[https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org]
|- style="border:0.6pt solid #808080;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide:</div>
+
|| Show Slide:
  
<div style="color:#252525;">'''Code Files'''</div>
+
'''Code Files'''
 
||  
 
||  
* <div style="margin-left:1.87cm;margin-right:0cm;"><span style="color:#252525;">The files used in this tutorial are provided in the </span><span style="color:#252525;">'''Code files'''</span><span style="color:#252525;"> link.</span></div>
+
* The files used in this tutorial are provided in the '''Code files''' link.
* <div style="color:#252525;margin-left:1.87cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="color:#252525;margin-left:1.87cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show diagram: '''Ad-hoc network'''
 
|| Show diagram: '''Ad-hoc network'''
 
|| For this tutorial, we would be creating the following topology.
 
|| For this tutorial, we would be creating the following topology.
  
 
We will create a simple '''ad-hoc 2 cross 2 grid with 4 nodes''' in this tutorial.  
 
We will create a simple '''ad-hoc 2 cross 2 grid with 4 nodes''' in this tutorial.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Open code editor
 
|| Open code editor
 
|| I have created the source file '''wifi-simple-adhoc-grid.cc''' for this program.
 
|| I have created the source file '''wifi-simple-adhoc-grid.cc''' for this program.
Line 58: Line 58:
  
 
Now we will go through the source code in the text editor.
 
Now we will go through the source code in the text editor.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''std::string phyMode("DsssRate1Mbps");'''
 
|| Highlight '''std::string phyMode("DsssRate1Mbps");'''
 
|| We set the '''dsss rate''' of the '''physical layer''' to '''1 Mbps'''.
 
|| We set the '''dsss rate''' of the '''physical layer''' to '''1 Mbps'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''double distance = 100;'''
 
|| Highlight '''double distance = 100;'''
|| Let us set the '''distance''' between the nodes as '''100 meters.'''
+
|| Let us set the distance between the nodes as 100 ''' meters'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''uint32_t packetSize = 1000;'''
 
|| Highlight '''uint32_t packetSize = 1000;'''
  
Line 71: Line 71:
 
|| Now we shall set the '''packet size''' to '''1000 bytes'''.
 
|| Now we shall set the '''packet size''' to '''1000 bytes'''.
  
Then let us set the '''number of packets''' to be transmitted as '''100'''.
+
Then let us set the '''number of packets''' to be transmitted as 100.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''double interval = 0.004;'''
 
|| Highlight '''double interval = 0.004;'''
|| We set the '''time interval''' between the '''packet transfer''' as '''0.004 seconds'''.
+
|| We set the '''time interval''' between the '''packet transfer''' as 0.004 '''seconds'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''uint32_t sinkNode = 0;'''
 
|| Highlight '''uint32_t sinkNode = 0;'''
  
 
'''uint32_t sourceNode = 3;'''
 
'''uint32_t sourceNode = 3;'''
  
|| Let us set up the '''source''' and''' sink nodes''' for the '''flow''' that we are yet to create.
+
|| Let us set up the '''source''' and ''' sink nodes''' for the flow that we are yet to create.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));'''
 
|| Highlight '''Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));'''
  
Line 87: Line 87:
  
 
'''Non-unicast mode''' means that the data may be sent from one node to many nodes.
 
'''Non-unicast mode''' means that the data may be sent from one node to many nodes.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''NodeContainer c;'''
 
|| Highlight '''NodeContainer c;'''
  
 
'''c.Create(numnodes);'''
 
'''c.Create(numnodes);'''
  
|| Now, let us create a '''node container''' with '''four nodes'''.
+
|| Now, let us create a '''node container''' with four '''nodes'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''WifiHelper wifi;'''
 
|| Highlight '''WifiHelper wifi;'''
  
Line 101: Line 101:
 
|| We shall now put the '''wifi''' '''network interface cards '''together.
 
|| We shall now put the '''wifi''' '''network interface cards '''together.
  
For that, let us use the''' WifiHelper class.'''
+
For that, let us use the''' WifiHelper class'''.
  
 
Using '''Wifi-helper''' class, we '''enable logging components '''and also set the wifi standard.
 
Using '''Wifi-helper''' class, we '''enable logging components '''and also set the wifi standard.
  
 
We set the wifi standard as '''IEEE 802.11b.'''
 
We set the wifi standard as '''IEEE 802.11b.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''YansWifiPhyHelper wifiPhy;'''
 
|| Highlight '''YansWifiPhyHelper wifiPhy;'''
  
Line 113: Line 113:
 
|| Then let us use the '''YansWifiPhyHelper class '''to set the '''RxGain attribute''' to''' zero'''.
 
|| Then let us use the '''YansWifiPhyHelper class '''to set the '''RxGain attribute''' to''' zero'''.
  
'''RxGain''' is used to adjust the '''sensitivity''' of the '''receiving signal.'''
+
'''RxGain''' is used to adjust the sensitivity of the '''receiving signal.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''YansWifiChannelHelper wifiChannel;'''
 
|| Highlight '''YansWifiChannelHelper wifiChannel;'''
  
Line 124: Line 124:
 
Let us set the '''Propagation delay'''.
 
Let us set the '''Propagation delay'''.
  
We shall also '''add '''the '''propagation loss''' to our model.
+
We shall also add the '''propagation loss''' to our model.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''wifiPhy.SetChannel(wifiChannel.Create());'''
 
|| Highlight '''wifiPhy.SetChannel(wifiChannel.Create());'''
 
|| Next, we connect the''' wifi physical layer '''to the '''wifi-channel'''.
 
|| Next, we connect the''' wifi physical layer '''to the '''wifi-channel'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''WifiMacHelper wifiMac;'''
 
|| Highlight '''WifiMacHelper wifiMac;'''
  
 
'''wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager","DataMode", StringValue(phyMode), "ControlMode", StringValue(phyMode));'''
 
'''wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager","DataMode", StringValue(phyMode), "ControlMode", StringValue(phyMode));'''
  
||  
+
||Now let us set a '''constant rate''' for '''data''' and '''control modes '''of '''wifi stations.'''
 
+
|-
Now let us set a '''constant rate''' for '''data''' and '''control modes '''of '''wifi stations.'''
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
|| Highlight '''wifiMac.SetType("ns3::AdhocWifiMac");'''
 
|| Highlight '''wifiMac.SetType("ns3::AdhocWifiMac");'''
 
|| Then, we set the '''MAC protocol '''of the '''Wi-Fi network''' to be '''Ad-hoc'''.
 
|| Then, we set the '''MAC protocol '''of the '''Wi-Fi network''' to be '''Ad-hoc'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);'''
 
|| Highlight '''NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);'''
|| Let us now connect '''wifiphy, wifimac''' to the''' nodes '''we created'''.'''
+
|| Let us now connect '''wifiphy, wifimac''' to the''' nodes '''we created.
  
 
This above setup includes''' wifiphy '''and '''wifimac. '''
 
This above setup includes''' wifiphy '''and '''wifimac. '''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''MobilityHelper mobility;'''
 
|| Highlight '''MobilityHelper mobility;'''
  
Line 173: Line 171:
  
 
'''StringValue("RowFirst"));'''
 
'''StringValue("RowFirst"));'''
|| Now allocate the '''positions '''for the '''nodes''' created using '''MobilityHelper'''.
+
|| Now allocate the positions for the '''nodes''' created using '''MobilityHelper'''.
  
Here, we are assigning''' positions '''of the '''nodes''' in a '''grid''' '''arrangement'''.
+
Here, we are assigning positions of the '''nodes''' in a '''grid''' arrangement.
  
'''MinX''' and '''MinY''' refers to '''minimum values '''of '''x '''and''' y''' '''axes''' which are set to '''zero'''.  
+
'''MinX''' and '''MinY''' refers to minimum values of '''x '''and''' y''' '''axes''' which are set to zero.  
  
'''DeltaX '''and '''DeltaY''' '''parameters''' define''' spacing''' between '''nodes''' in the '''x''' and''' y axes'''.
+
'''DeltaX '''and '''DeltaY''' parameters define spacing between '''nodes''' in the '''x''' and ''' y axes'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");'''
 
|| Highlight '''mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");'''
  
 
'''mobility.Install(c);'''
 
'''mobility.Install(c);'''
  
|| Let us '''set''' the''' model '''to be '''ConstantPositionMobilityModel.'''
+
|| Let us set the model to be '''ConstantPositionMobilityModel.'''
  
It represents the '''stationary''' or '''static nodes''' in a '''network simulation.'''
+
It represents the stationary or '''static nodes''' in a '''network simulation.'''
  
Also, '''install '''the''' model '''that we '''set,''' to all the '''nodes''' that we created.  
+
Also, '''install '''the model that we '''set''', to all the '''nodes''' that we created.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''OlsrHelper olsr;'''
 
|| Highlight '''OlsrHelper olsr;'''
  
Line 199: Line 197:
  
 
It maintains the '''routing table''' of all the '''station nodes'''.
 
It maintains the '''routing table''' of all the '''station nodes'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Ipv4ListRoutingHelper list;'''
 
|| Highlight '''Ipv4ListRoutingHelper list;'''
  
Line 206: Line 204:
 
|| Then let us add the list of nodes to assign '''Ipv4 addresses'''.
 
|| Then let us add the list of nodes to assign '''Ipv4 addresses'''.
  
'''0 '''refers to high '''priority '''to '''OLSR protocol '''to be added to the nodes.
+
'''0 '''refers to high priority to '''OLSR protocol '''to be added to the nodes.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''InternetStackHelper internet;'''
 
|| Highlight '''InternetStackHelper internet;'''
  
Line 214: Line 212:
 
'''internet.Install(c);'''
 
'''internet.Install(c);'''
 
|| Then let us install the''' internet stack''' on all the nodes.
 
|| Then let us install the''' internet stack''' on all the nodes.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Ipv4AddressHelper ipv4;'''
 
|| Highlight '''Ipv4AddressHelper ipv4;'''
  
Line 224: Line 222:
  
 
'''Base address''' and''' subnet mask''' are assigned to the station nodes of the network.  
 
'''Base address''' and''' subnet mask''' are assigned to the station nodes of the network.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");'''
 
|| Highlight '''TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");'''
  
 
|| The highlighted command is used to get the '''TypeId''' of '''UdpSocketFactory.'''
 
|| The highlighted command is used to get the '''TypeId''' of '''UdpSocketFactory.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid);'''
 
|| Highlight '''Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid);'''
 
|| We shall create a receiving '''socket''' for the '''UDP Flow'''.  
 
|| We shall create a receiving '''socket''' for the '''UDP Flow'''.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80);'''
 
|| Highlight '''InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80);'''
 
|| Let us create an '''internet socket''' '''address''' using any '''IPv4 address''' and '''port 80.'''
 
|| Let us create an '''internet socket''' '''address''' using any '''IPv4 address''' and '''port 80.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''recvSink->Bind(local);'''
 
|| Highlight '''recvSink->Bind(local);'''
|| Then '''bind '''the '''receiving socket''' to the '''local addres'''s and to the '''port 80'''.
+
|| Then bind the '''receiving socket''' to the '''local address''' and to the '''port 80'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| Highlight <span style="background-color:#ffffff;">'''recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));'''</span>
+
|| Highlight '''recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));'''
 
|| Now set the '''receive''' '''callback function''' for the '''receiving socket'''.
 
|| Now set the '''receive''' '''callback function''' for the '''receiving socket'''.
  
 
When the '''packet '''is received in the''' sinknode,''' we call '''RecievePacket function'''.
 
When the '''packet '''is received in the''' sinknode,''' we call '''RecievePacket function'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid);'''
 
|| Highlight '''Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid);'''
|| Let us then '''create''' a'''n UDP socket '''on '''node 3, '''which is our '''sourcenode'''.
+
|| Let us then create an '''UDP socket '''on '''node 3, '''which is our '''sourcenode'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80);'''
 
|| Highlight '''InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80);'''
 
|| Let us create an''' internet socket '''using '''Ipv4 address''' of '''sinknode''' and '''port 80'''.
 
|| Let us create an''' internet socket '''using '''Ipv4 address''' of '''sinknode''' and '''port 80'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''source->Connect(remote);'''
 
|| Highlight '''source->Connect(remote);'''
 
|| Let us connect the '''source socket '''to the '''remote address''' and '''port 80.'''  
 
|| Let us connect the '''source socket '''to the '''remote address''' and '''port 80.'''  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''void'''
 
|| Highlight '''void'''
  
Line 262: Line 260:
  
 
'''}'''
 
'''}'''
|| Now let’s define the '''function''' to '''call''' when the '''packets''' are received.  
+
|| Now let’s define the function to '''call''' when the '''packets''' are received.  
  
  
 
Here, we are invoking the '''receive functionality''' of the '''socket'''.
 
Here, we are invoking the '''receive functionality''' of the '''socket'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''static void'''
 
|| Highlight '''static void'''
  
 
'''GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)'''
 
'''GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)'''
  
|| Then let us define a '''function '''called '''generate packet traffic'''.  
+
|| Then let us define a function called '''generate packet traffic'''.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
||  
 
||  
  
|| When the '''number of''' '''packets''' is not''' zero''', we '''invoke''' the '''send '''function.
+
|| When the '''number of''' '''packets''' is not''' zero''', we invoke the '''send ''' function.
  
We also schedule the '''simulator '''to '''invoke '''the''' GenerateTraffic function.'''
+
We also schedule the '''simulator '''to invoke the ''' GenerateTraffic function.'''
  
When the '''number of packets''' is''' zero''', we '''close '''the '''socket'''.
+
When the '''number of packets''' is zero, we close the '''socket'''.
  
 
Then, we '''stop invoking send function'''.
 
Then, we '''stop invoking send function'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Only Narration
 
|| Only Narration
|| We have created''' three''' other '''UDP flows as well'''.
+
|| We have created three other '''UDP flows''' as well.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Simulator::Schedule(Seconds('''
 
|| Highlight '''Simulator::Schedule(Seconds('''
  
 
'''10),&GenerateTraffic,source,packetSize,numPackets,interval);'''
 
'''10),&GenerateTraffic,source,packetSize,numPackets,interval);'''
|| Let us '''schedule''' the '''simulator''' to '''callback''' the '''generate_traffic function.'''
+
|| Let us schedule the '''simulator''' to '''callback''' the '''generate_traffic function.'''
  
This procedure happens after '''ten seconds '''from the start''' of execution'''.
+
This procedure happens after ten seconds from the start of '''execution'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Ptr<FlowMonitor> flowMonitor;'''
 
|| Highlight '''Ptr<FlowMonitor> flowMonitor;'''
 
|| Then, '''Initialize''' flow monitor for tracing.
 
|| Then, '''Initialize''' flow monitor for tracing.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| HIghlight '''FlowMonitorHelper flowHelper;'''
 
|| HIghlight '''FlowMonitorHelper flowHelper;'''
  
Line 301: Line 299:
  
 
|| We shall then install a '''flow monitor''' on all the '''nodes'''.
 
|| We shall then install a '''flow monitor''' on all the '''nodes'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''AnimationInterface anim ("animationwifi-adhoc-wireless.xml"); '''
 
|| Highlight '''AnimationInterface anim ("animationwifi-adhoc-wireless.xml"); '''
 
|| Let us create the''' XML output''' of the program.
 
|| Let us create the''' XML output''' of the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| HIghlight '''Ptr<Ipv4FlowClassifier> classifier=DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier());'''
 
|| HIghlight '''Ptr<Ipv4FlowClassifier> classifier=DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier());'''
 
|| Let us now create an object of the '''Ipv4FlowClassifier class.'''
 
|| Let us now create an object of the '''Ipv4FlowClassifier class.'''
  
The created '''object '''returns a''' pointer''' to the '''flow classifier.'''
+
The created '''object '''returns a ''' pointer''' to the '''flow classifier.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''std::map<FlowId,FlowMonitor::FlowStats> stats=flowMonitor->GetFlowStats();'''
 
|| Highlight '''std::map<FlowId,FlowMonitor::FlowStats> stats=flowMonitor->GetFlowStats();'''
|| To store the information about the '''network flow, '''let us use this command.
+
|| To store the information about the '''network flow''', let us use this command.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight the '''entire for loop.'''
 
|| Highlight the '''entire for loop.'''
 
|| Let’s retrieve the details of the flow using a '''for loop.'''
 
|| Let’s retrieve the details of the flow using a '''for loop.'''
Line 319: Line 317:
 
'''iter arrow first '''is used to collect the '''ID''' of the '''flow.'''
 
'''iter arrow first '''is used to collect the '''ID''' of the '''flow.'''
  
'''iter arrow second '''is used to collect the '''data '''associated with the''' flow.'''
+
'''iter arrow second '''is used to collect the '''data '''associated with the''' flow'''.
  
 
We will analyze the '''type of flow''' and '''print''' the''' delay''' and '''throughput'''.
 
We will analyze the '''type of flow''' and '''print''' the''' delay''' and '''throughput'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Highlight '''Simulator::Destroy()'''
 
|| Highlight '''Simulator::Destroy()'''
 
|| The '''Destroy() '''function ends the simulation.
 
|| The '''Destroy() '''function ends the simulation.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Go to terminal and navigate to the '''ns-allinone-3.38/ns3.38 '''directory
 
|| Go to terminal and navigate to the '''ns-allinone-3.38/ns3.38 '''directory
 
|| Now we will observe the simulation.
 
|| Now we will observe the simulation.
  
Open the terminal by pressing '''Ctrl, Alt''' and '''T keys''' simultaneously.
+
Open the terminal by pressing '''Ctrl''', '''Alt''' and '''T keys''' simultaneously.
  
 
Using the '''cd '''command, navigate to the installation directory of ns-3.
 
Using the '''cd '''command, navigate to the installation directory of ns-3.
Line 337: Line 335:
  
 
Move your '''source file''' to the '''scratch '''directory within the '''ns-3.38 directory'''.
 
Move your '''source file''' to the '''scratch '''directory within the '''ns-3.38 directory'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Type ./'''ns3 run scratch/wifi-simple-adhoc-grid.cc'''
 
|| Type ./'''ns3 run scratch/wifi-simple-adhoc-grid.cc'''
 
|| Run the command ./'''ns3 run scratch/wifi-simple-adhoc-grid.cc '''
 
|| Run the command ./'''ns3 run scratch/wifi-simple-adhoc-grid.cc '''
  
 
'''wifi-simple-adhoc-grid.cc '''is the name of the source file.  
 
'''wifi-simple-adhoc-grid.cc '''is the name of the source file.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Keep the terminal open and show the output of the command  
 
|| Keep the terminal open and show the output of the command  
 
|| After compilation, we get the details of the '''packet transfer'''.
 
|| After compilation, we get the details of the '''packet transfer'''.
Line 348: Line 346:
 
We also see the '''average end to end delay''' and''' throughput '''of the wireless network.
 
We also see the '''average end to end delay''' and''' throughput '''of the wireless network.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Navigate to '''netanim '''directory under '''ns-allinone-3.38'''
 
|| Navigate to '''netanim '''directory under '''ns-allinone-3.38'''
  
Line 361: Line 359:
  
 
Then type '''cd netanim'''
 
Then type '''cd netanim'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Type ./'''NetAnim'''
 
|| Type ./'''NetAnim'''
 
|| Now type '''./NetAnim. '''
 
|| Now type '''./NetAnim. '''
  
 
The '''NetAnim''' window should open.
 
The '''NetAnim''' window should open.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Click on the Open''' XML trace file '''icon on the top left corner of the window.
 
|| Click on the Open''' XML trace file '''icon on the top left corner of the window.
 
|| Click on the Open''' XML trace file '''icon on the top left corner of the window.
 
|| Click on the Open''' XML trace file '''icon on the top left corner of the window.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| In the file picker, navigate to the '''ns-allinone-3.38/ns3.38 '''directory and select the '''animationwifi-adhoc-wireless.xml '''file.
 
|| In the file picker, navigate to the '''ns-allinone-3.38/ns3.38 '''directory and select the '''animationwifi-adhoc-wireless.xml '''file.
 
|| In the file picker, '''navigate '''to the '''ns-allinone-3.38, '''then to''' ns-3.38 '''directory.
 
|| In the file picker, '''navigate '''to the '''ns-allinone-3.38, '''then to''' ns-3.38 '''directory.
  
 
Then select the '''animationwifi-adhoc-wireless.xml '''file.
 
Then select the '''animationwifi-adhoc-wireless.xml '''file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| On the toolbar, click on the play button
 
|| On the toolbar, click on the play button
 
|| On the toolbar, '''click''' on the '''play button''' to view the''' simulation.'''
 
|| On the toolbar, '''click''' on the '''play button''' to view the''' simulation.'''
Line 381: Line 379:
  
 
Now we can close the '''netanim '''window.
 
Now we can close the '''netanim '''window.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show Slide: '''Summary'''
 
|| Show Slide: '''Summary'''
|| <div style="color:#252525;">This brings us to the end of this tutorial. Let us summarize. </div>
+
|| This brings us to the end of this tutorial. Let us summarize.  
  
<div style="color:#252525;">In this tutorial, we have</div>
+
In this tutorial, we have
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Created an '''Ad-hoc''' '''wireless network'''.</div>
+
* Created an '''Ad-hoc''' '''wireless network'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">Created three '''UDP flows''' in the channels.</div>
+
* Created three '''UDP flows''' in the channels.
* <div style="margin-left:1.27cm;margin-right:0cm;">Analyzed the flow using a '''flow monitor'''.</div>
+
* Analyzed the flow using a '''flow monitor'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">Visualized the network using NetAnim.</div>
+
* Visualized the network using NetAnim.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show Slide : '''Assignment'''
 
|| Show Slide : '''Assignment'''
 
|| As an assignment, please do the following
 
|| As an assignment, please do the following
  
* <div style="margin-left:2.54cm;margin-right:0cm;">Create a '''three cross three''' grid</div>
+
* Create a '''three cross three''' grid
* <div style="margin-left:2.54cm;margin-right:0cm;">Create''' six UDP flows'''.</div>
+
* Create''' six UDP flows'''.
* <div style="margin-left:2.54cm;margin-right:0cm;">The simulation should run for '''200 seconds.'''</div>
+
* The simulation should run for '''200 seconds.'''
* <div style="margin-left:2.54cm;margin-right:0cm;">Analyze the '''flow''' using a '''flow monitor.'''</div>
+
* Analyze the '''flow''' using a '''flow monitor.'''
* <div style="margin-left:2.54cm;margin-right:0cm;">Visualize the network using '''NetAnim.'''</div>
+
* Visualize the network using '''NetAnim.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show terminal window
 
|| Show terminal window
  
Line 414: Line 412:
  
 
We could see the details of all the six flows.
 
We could see the details of all the six flows.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
Line 421: Line 419:
  
 
Observe the packet transfer.
 
Observe the packet transfer.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide:</div>
+
|| Show Slide:
  
<div style="color:#252525;">'''About Spoken Tutorial Project'''</div>
+
'''About Spoken Tutorial Project'''
|| <span style="color:#252525;">The video at the following link summarizes the </span><span style="color:#252525;">'''Spoken Tutorial project'''</span><span style="color:#252525;">.</span>
+
|| The video at the following link summarizes the '''Spoken Tutorial project'''.
  
<div style="color:#252525;">Please download and watch it.</div>
+
Please download and watch it.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide:</div>
+
|| Show Slide:
  
<div style="color:#252525;">'''Spoken Tutorial Workshops'''</div>
+
'''Spoken Tutorial Workshops'''
|| <span style="color:#252525;">The </span><span style="color:#252525;">'''Spoken Tutorial Project'''</span><span style="color:#252525;"> team conducts workshops and gives certificates.</span>
+
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
  
<div style="color:#252525;">For more details, please write to us.</div>
+
For more details, please write to us.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide:</div>
+
|| Show Slide:
  
<div style="color:#252525;">'''Answers for THIS Spoken Tutorial'''</div>
+
'''Answers for THIS Spoken Tutorial'''
| style="color:#252525;" | Please post your timed queries in this forum.
+
|| Please post your timed queries in this forum.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide:</div>
+
|| Show Slide:
  
<div style="color:#252525;">'''FOSSEE Forum'''</div>
+
'''FOSSEE Forum'''
|| <span style="color:#252525;">For any general or technical questions on ns-3, visit the</span><span style="color:#252525;">''' FOSSEE forum'''</span><span style="color:#252525;"> and post your question.</span>
+
|| For any general or technical questions on ns-3, visit the''' FOSSEE forum''' and post your question.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
|| <span style="color:#252525;">Show Slide: </span><span style="color:#252525;">'''Acknowledgement'''</span>
+
|| Show Slide: '''Acknowledgement'''
|| <span style="color:#252525;">'''Spoken Tutorial'''</span><span style="color:#252525;"> Project was established by the </span><span style="color:#252525;">'''Ministry of Education, Government of India'''</span><span style="color:#252525;">.</span>
+
|| '''Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
|- style="border:1pt solid #252525;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide: </div>
+
|| Show Slide:  
  
<div style="color:#252525;">'''Acknowledgement'''</div>
+
'''Acknowledgement'''
|| <span style="color:#252525;">We thank </span><span style="color:#252525;">'''Dr. Moyukh Laha'''</span><span style="color:#252525;"> from </span><span style="color:#252525;">'''IIT Kharagpur'''</span><span style="color:#252525;"> for his domain support.</span>
+
|| We thank '''Dr. Moyukh Laha''' from '''IIT Kharagpur''' for his domain support.
  
<span style="color:#252525;">We would also like to thank </span><span style="color:#252525;">'''Dr. R. Radha, Dr. X. Anita, '''</span><span style="color:#252525;">and</span><span style="color:#252525;">''' Dr. T. Subbulakshmi '''</span><span style="color:#252525;">from</span><span style="color:#252525;">''' VIT Chennai '''</span><span style="color:#252525;">for their support.</span>
+
We would also like to thank '''Dr. R. Radha, Dr. X. Anita, '''and''' Dr. T. Subbulakshmi '''from''' VIT Chennai '''for their support.
|- style="border:1pt solid #252525;padding:0.176cm;"
+
|-  
|| <div style="color:#252525;">Show Slide: </div>
+
|| Show Slide:  
  
<div style="color:#252525;">'''Thank you'''</div>
+
'''Thank you'''
|| <span style="color:#252525;">This is </span><span style="color:#252525;">'''Arun Santhosh,'''</span><span style="color:#252525;"> a FOSSEE Summer Fellow 2023, IIT Bombay signing off.</span>
+
|| This is '''Arun Santhosh,''' a FOSSEE Summer Fellow 2023, IIT Bombay signing off.
  
<div style="color:#252525;">Thank you for joining.</div>
+
Thank you for joining.
 
|-
 
|-
 
|}
 
|}

Latest revision as of 20:39, 22 April 2024

Visual Cue Narration
Show Slide: Title Slide Welcome to Spoken tutorial on Creating an Ad-hoc wireless network.
Show Slide:

Learning Objectives

In this tutorial, we will learn to
  • Create an Ad-hoc wireless network.
  • Create three UDP flows in the channels.
  • Analyze the flow using a flow monitor.
  • Visualize the network using NetAnim
Show Slide:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04.
  • ns-3 version 3.38.
  • NetAnim visualizer tool.
Show slide:

Prerequisite

https://www.spoken-tutorial.org

To follow this tutorial
  • The learner must have basic knowledge of ns-3 software.
  • For pre-requisite ns-3 tutorials, please visit this website

https://www.spoken-tutorial.org

Show Slide:

Code Files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show diagram: Ad-hoc network For this tutorial, we would be creating the following topology.

We will create a simple ad-hoc 2 cross 2 grid with 4 nodes in this tutorial.

Open code editor I have created the source file wifi-simple-adhoc-grid.cc for this program.

The source code contains the required functions to create an ad-hoc wireless network.

Now we will go through the source code in the text editor.

Highlight std::string phyMode("DsssRate1Mbps"); We set the dsss rate of the physical layer to 1 Mbps.
Highlight double distance = 100; Let us set the distance between the nodes as 100 meters.
Highlight uint32_t packetSize = 1000;

uint32_t numPackets = 100;

Now we shall set the packet size to 1000 bytes.

Then let us set the number of packets to be transmitted as 100.

Highlight double interval = 0.004; We set the time interval between the packet transfer as 0.004 seconds.
Highlight uint32_t sinkNode = 0;

uint32_t sourceNode = 3;

Let us set up the source and sink nodes for the flow that we are yet to create.
Highlight Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode)); We shall configure non-unicast mode for all the station nodes in the simulation.

Non-unicast mode means that the data may be sent from one node to many nodes.

Highlight NodeContainer c;

c.Create(numnodes);

Now, let us create a node container with four nodes.
Highlight WifiHelper wifi;

if (verbose) { WifiHelper::EnableLogComponents(); }

wifi.SetStandard(WIFI_STANDARD_80211b);

We shall now put the wifi network interface cards together.

For that, let us use the WifiHelper class.

Using Wifi-helper class, we enable logging components and also set the wifi standard.

We set the wifi standard as IEEE 802.11b.

Highlight YansWifiPhyHelper wifiPhy;

wifiPhy.Set("RxGain", DoubleValue(0));

Then let us use the YansWifiPhyHelper class to set the RxGain attribute to zero.

RxGain is used to adjust the sensitivity of the receiving signal.

Highlight YansWifiChannelHelper wifiChannel;

wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");

wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");

We use YansWifiChannelHelper class to set attributes of the transmitting channel.

Let us set the Propagation delay.

We shall also add the propagation loss to our model.

Highlight wifiPhy.SetChannel(wifiChannel.Create()); Next, we connect the wifi physical layer to the wifi-channel.
Highlight WifiMacHelper wifiMac;

wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager","DataMode", StringValue(phyMode), "ControlMode", StringValue(phyMode));

Now let us set a constant rate for data and control modes of wifi stations.
Highlight wifiMac.SetType("ns3::AdhocWifiMac"); Then, we set the MAC protocol of the Wi-Fi network to be Ad-hoc.
Highlight NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c); Let us now connect wifiphy, wifimac to the nodes we created.

This above setup includes wifiphy and wifimac.

Highlight MobilityHelper mobility;

mobility.SetPositionAllocator("ns3::GridPositionAllocator",

"MinX",

DoubleValue(0.0),

"MinY",

DoubleValue(0.0),

"DeltaX",

DoubleValue(distance),

"DeltaY",

DoubleValue(distance),

"GridWidth",

UintegerValue(2),

"LayoutType",

StringValue("RowFirst"));

Now allocate the positions for the nodes created using MobilityHelper.

Here, we are assigning positions of the nodes in a grid arrangement.

MinX and MinY refers to minimum values of x and y axes which are set to zero.

DeltaX and DeltaY parameters define spacing between nodes in the x and y axes.

Highlight mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");

mobility.Install(c);

Let us set the model to be ConstantPositionMobilityModel.

It represents the stationary or static nodes in a network simulation.

Also, install the model that we set, to all the nodes that we created.

Highlight OlsrHelper olsr; Now we shall enable the OLSR protocol in the network channels.

OLSR refers to Optimized link state routing protocol .

It maintains the routing table of all the station nodes.

Highlight Ipv4ListRoutingHelper list;

list.Add(olsr, 0);

Then let us add the list of nodes to assign Ipv4 addresses.

0 refers to high priority to OLSR protocol to be added to the nodes.

Highlight InternetStackHelper internet;

internet.SetRoutingHelper(list);

internet.Install(c);

Then let us install the internet stack on all the nodes.
Highlight Ipv4AddressHelper ipv4;

ipv4.SetBase("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer i = ipv4.Assign(devices);

Now, we shall assign IP addresses to the nodes that we created.

Base address and subnet mask are assigned to the station nodes of the network.

Highlight TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory"); The highlighted command is used to get the TypeId of UdpSocketFactory.
Highlight Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid); We shall create a receiving socket for the UDP Flow.
Highlight InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80); Let us create an internet socket address using any IPv4 address and port 80.
Highlight recvSink->Bind(local); Then bind the receiving socket to the local address and to the port 80.
Highlight recvSink->SetRecvCallback(MakeCallback(&ReceivePacket)); Now set the receive callback function for the receiving socket.

When the packet is received in the sinknode, we call RecievePacket function.

Highlight Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid); Let us then create an UDP socket on node 3, which is our sourcenode.
Highlight InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80); Let us create an internet socket using Ipv4 address of sinknode and port 80.
Highlight source->Connect(remote); Let us connect the source socket to the remote address and port 80.
Highlight void

ReceivePacket(Ptr<Socket> socket)

{

while (socket->Recv()){ }

}

Now let’s define the function to call when the packets are received.


Here, we are invoking the receive functionality of the socket.

Highlight static void

GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)

Then let us define a function called generate packet traffic.
When the number of packets is not zero, we invoke the send function.

We also schedule the simulator to invoke the GenerateTraffic function.

When the number of packets is zero, we close the socket.

Then, we stop invoking send function.

Only Narration We have created three other UDP flows as well.
Highlight Simulator::Schedule(Seconds(

10),&GenerateTraffic,source,packetSize,numPackets,interval);

Let us schedule the simulator to callback the generate_traffic function.

This procedure happens after ten seconds from the start of execution.

Highlight Ptr<FlowMonitor> flowMonitor; Then, Initialize flow monitor for tracing.
HIghlight FlowMonitorHelper flowHelper;

flowMonitor= flowHelper.InstallAll();

We shall then install a flow monitor on all the nodes.
Highlight AnimationInterface anim ("animationwifi-adhoc-wireless.xml"); Let us create the XML output of the program.
HIghlight Ptr<Ipv4FlowClassifier> classifier=DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier()); Let us now create an object of the Ipv4FlowClassifier class.

The created object returns a pointer to the flow classifier.

Highlight std::map<FlowId,FlowMonitor::FlowStats> stats=flowMonitor->GetFlowStats(); To store the information about the network flow, let us use this command.
Highlight the entire for loop. Let’s retrieve the details of the flow using a for loop.

iter arrow first is used to collect the ID of the flow.

iter arrow second is used to collect the data associated with the flow.

We will analyze the type of flow and print the delay and throughput.

Highlight Simulator::Destroy() The Destroy() function ends the simulation.
Go to terminal and navigate to the ns-allinone-3.38/ns3.38 directory Now we will observe the simulation.

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.

Using the cd command, navigate to the installation directory of ns-3.

Go to the ns3.38 directory.

Move your source file to the scratch directory within the ns-3.38 directory.

Type ./ns3 run scratch/wifi-simple-adhoc-grid.cc Run the command ./ns3 run scratch/wifi-simple-adhoc-grid.cc

wifi-simple-adhoc-grid.cc is the name of the source file.

Keep the terminal open and show the output of the command After compilation, we get the details of the packet transfer.

We also see the average end to end delay and throughput of the wireless network.

Navigate to netanim directory under ns-allinone-3.38

Type cd .. Type cd netanim

Now, to visualize the network, we will use NetAnim.

Navigate to the netanim directory under ns-allinone-3.38.

For that, type cd .. in the terminal window.

Then type cd netanim

Type ./NetAnim Now type ./NetAnim.

The NetAnim window should open.

Click on the Open XML trace file icon on the top left corner of the window. Click on the Open XML trace file icon on the top left corner of the window.
In the file picker, navigate to the ns-allinone-3.38/ns3.38 directory and select the animationwifi-adhoc-wireless.xml file. In the file picker, navigate to the ns-allinone-3.38, then to ns-3.38 directory.

Then select the animationwifi-adhoc-wireless.xml file.

On the toolbar, click on the play button On the toolbar, click on the play button to view the simulation.

We see the flow of the wireless network.

Now we can close the netanim window.

Show Slide: Summary This brings us to the end of this tutorial. Let us summarize.

In this tutorial, we have

  • Created an Ad-hoc wireless network.
  • Created three UDP flows in the channels.
  • Analyzed the flow using a flow monitor.
  • Visualized the network using NetAnim.
Show Slide : Assignment As an assignment, please do the following
  • Create a three cross three grid
  • Create six UDP flows.
  • The simulation should run for 200 seconds.
  • Analyze the flow using a flow monitor.
  • Visualize the network using NetAnim.
Show terminal window

Type cd ..

Type cd ns-3.38

Type ./ns3 run scratch/exp-10assignment.cc

In the terminal window, you will get the following output.

We could see the details of all the six flows.

Show Slide:

Assignment - Observation

In the NetAnim window, you would observe the following output.

Observe the packet transfer.

Show Slide:

About Spoken Tutorial Project

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Show Slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions on ns-3, visit the FOSSEE forum and post your question.
Show Slide: Acknowledgement Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show Slide:

Acknowledgement

We thank Dr. Moyukh Laha from IIT Kharagpur for his domain support.

We would also like to thank Dr. R. Radha, Dr. X. Anita, and Dr. T. Subbulakshmi from VIT Chennai for their support.

Show Slide:

Thank you

This is Arun Santhosh, a FOSSEE Summer Fellow 2023, IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat