Ns-3-Network-Simulator/C3/Creating-an-ad-hoc-wireless-network/English
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
|
Show Slide:
System Requirements |
To record this tutorial, I am using
|
Show slide:
Prerequisite |
To follow this tutorial
|
Show Slide:
Code Files |
|
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.
|
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
|
Show Slide : Assignment | As an assignment, please do the following
|
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. |