Question
using namespace ns3; 79 80 NS_LOG_COMPONENT_DEFINE (WifiSimpleAdhocGrid); 81 82 void ReceivePacket (Ptr socket) 83 { 84 while (socket->Recv ()) 85 { 86 NS_LOG_UNCOND (Received one
using namespace ns3;
79
80 NS_LOG_COMPONENT_DEFINE ("WifiSimpleAdhocGrid");
81
82 void ReceivePacket (Ptr
83 {
84 while (socket->Recv ())
85 {
86 NS_LOG_UNCOND ("Received one packet!");
87 }
88 }
89
90 static void GenerateTraffic (Ptr
91 uint32_t pktCount, Time pktInterval )
92 {
93 if (pktCount > 0)
94 {
95 socket->Send (Create
96 Simulator::Schedule (pktInterval, &GenerateTraffic,
97 socket, pktSize,pktCount - 1, pktInterval);
98 }
99 else
100 {
101 socket->Close ();
102 }
103 }
104
105
106 int main (int argc, char *argv[])
107 {
108 std::string phyMode ("DsssRate1Mbps");
109 double distance = 500; // m
110 uint32_t packetSize = 1000; // bytes
111 uint32_t numPackets = 1;
112 uint32_t numNodes = 25; // by default, 5x5
113 uint32_t sinkNode = 0;
114 uint32_t sourceNode = 24;
115 double interval = 1.0; // seconds
116 bool verbose = false;
117 bool tracing = false;
118
119 CommandLine cmd;
120
121 cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
122 cmd.AddValue ("distance", "distance (m)", distance);
123 cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
124 cmd.AddValue ("numPackets", "number of packets generated", numPackets);
125 cmd.AddValue ("interval", "interval (seconds) between packets", interval);
126 cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
127 cmd.AddValue ("tracing", "turn on ascii and pcap tracing", tracing);
128 cmd.AddValue ("numNodes", "number of nodes", numNodes);
129 cmd.AddValue ("sinkNode", "Receiver node number", sinkNode);
130 cmd.AddValue ("sourceNode", "Sender node number", sourceNode);
131
132 cmd.Parse (argc, argv);
133 // Convert to time object
134 Time interPacketInterval = Seconds (interval);
135
136 // disable fragmentation for frames below 2200 bytes
137 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
138 // turn off RTS/CTS for frames below 2200 bytes
139 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
140 // Fix non-unicast data rate to be the same as that of unicast
141 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
142 StringValue (phyMode));
143
144 NodeContainer c;
145 c.Create (numNodes);
146
147 // The below set of helpers will help us to put together the wifi NICs we want
148 WifiHelper wifi;
149 if (verbose)
150 {
151 wifi.EnableLogComponents (); // Turn on all Wifi logging
152 }
153
154 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
155 // set it to zero; otherwise, gain will be added
156 wifiPhy.Set ("RxGain", DoubleValue (-10) );
157 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
158 wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
159
160 YansWifiChannelHelper wifiChannel;
161 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
162 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
163 wifiPhy.SetChannel (wifiChannel.Create ());
164
165 // Add an upper mac and disable rate control
166 WifiMacHelper wifiMac;
167 wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
168 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
169 "DataMode",StringValue (phyMode),
170 "ControlMode",StringValue (phyMode));
171 // Set it to adhoc mode
172 wifiMac.SetType ("ns3::AdhocWifiMac");
173 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
174
175 MobilityHelper mobility;
176 mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
177 "MinX", DoubleValue (0.0),
178 "MinY", DoubleValue (0.0),
179 "DeltaX", DoubleValue (distance),
180 "DeltaY", DoubleValue (distance),
181 "GridWidth", UintegerValue (5),
182 "LayoutType", StringValue ("RowFirst"));
183 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
184 mobility.Install (c);
185
186 // Enable OLSR
187 OlsrHelper olsr;
188 Ipv4StaticRoutingHelper staticRouting;
189
190 Ipv4ListRoutingHelper list;
191 list.Add (staticRouting, 0);
192 list.Add (olsr, 10);
193
194 InternetStackHelper internet;
195 internet.SetRoutingHelper (list); // has effect on the next Install ()
196 internet.Install (c);
197
198 Ipv4AddressHelper ipv4;
199 NS_LOG_INFO ("Assign IP Addresses.");
200 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
201 Ipv4InterfaceContainer i = ipv4.Assign (devices);
202
203 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
204 Ptr
205 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
206 recvSink->Bind (local);
207 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
208
209 Ptr
210 InetSocketAddress remote = InetSocketAddress (i.GetAddress (sinkNode, 0), 80);
211 source->Connect (remote);
212
213 if (tracing == true)
214 {
215 AsciiTraceHelper ascii;
216 wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("wifi-simple-adhoc-grid.tr"));
217 wifiPhy.EnablePcap ("wifi-simple-adhoc-grid", devices);
218 // Trace routing tables
219 Ptr
220 olsr.PrintRoutingTableAllEvery (Seconds (2), routingStream);
221 Ptr
222 olsr.PrintNeighborCacheAllEvery (Seconds (2), neighborStream);
223
224 // To do-- enable an IP-level trace that shows forwarding events only
225 }
226
227 // Give OLSR time to converge-- 30 seconds perhaps
228 Simulator::Schedule (Seconds (30.0), &GenerateTraffic,
229 source, packetSize, numPackets, interPacketInterval);
230
231 // Output what we are doing
232 NS_LOG_UNCOND ("Testing from node " << sourceNode << " to " << sinkNode << " with grid distance " << distance);
233
234 Simulator::Stop (Seconds (33.0));
235 Simulator::Run ();
236 Simulator::Destroy ();
237
238 return 0;
239 }
Please do the following modifications to the example script wifi-simple-adhoc-grid.cc
Copy it from the example folder to the scratch folder and change its name to anything you like such as my-first.cc(otherwise, your change may not be reflected in the simulation).
Let the simulation stop at 60 seconds.
In function void HYPERLINK "https://www.nsnam.org/doxygen/wifi-simple-adhoc-grid_8cc.html" \l "a80dc0d980be124d8d5c4ee0b7943bfae" ReceivePacket ( HYPERLINK "https://www.nsnam.org/doxygen/classns3_1_1_ptr.html" Ptr socket), record current time in the log when a packet is received. The final log is like Received one packet at xxx, where xxx represents the time in the simulation that thepacket is received (Hint: you need to google how to get simulation time in ns3).
Using your modified wifi-simple-adhoc-grid.cc to run the following simulations.
Set the source node as the node 6 and the sink node as node 7. Run the simulation repetitively with the distance between two neighbor nodes (i.e., distance in the script) increasing from 500 to 800, with a 60 increase in each step. In each running, you need to let the source send out 20 packets. Measure the number of received packets in the sink in each run. Draw the result as a figure in which x-axis is the value of distance and the y-axis is the number of received packets.
Do the same experiment as in the above but from node 0 to node 24.
Write a report for the experiment
Please include the two figures resulted from step 3. Please introduce whether there are any differences between the two figures. If yes, explain the reason for the differences. If no, please also explain why we get identical result in the experiment with two sets of nodes.
Find the route from node 22 to node 2 from the route trace
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started