Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
2 /* | |
3 * Copyright (c) 2011 Hemanth Narra | |
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 * Author: Hemanth Narra <hemanthnarra222@gmail.com> | |
19 * | |
20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director | |
21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets | |
22 * Information and Telecommunication Technology Center (ITTC) | |
23 * and Department of Electrical Engineering and Computer Science | |
24 * The University of Kansas Lawrence, KS USA. | |
25 * | |
26 * Work supported in part by NSF FIND (Future Internet Design) Program | |
27 * under grant CNS-0626918 (Postmodern Internet Architecture), | |
28 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimenta tion on GENI), | |
29 * US Department of Defense (DoD), and ITTC at The University of Kansas. | |
30 */ | |
31 #include "ns3/core-module.h" | |
32 #include "ns3/network-module.h" | |
33 #include "ns3/applications-module.h" | |
34 #include "ns3/mobility-module.h" | |
35 #include "ns3/config-store-module.h" | |
36 #include "ns3/wifi-module.h" | |
37 #include "ns3/internet-module.h" | |
38 #include "ns3/simple-wireless-tdma-module.h" | |
39 #include "ns3/dsdv-module.h" | |
40 #include <iostream> | |
41 #include <cmath> | |
42 | |
43 using namespace ns3; | |
44 | |
45 uint16_t port = 9; | |
46 | |
47 NS_LOG_COMPONENT_DEFINE ("TdmaExample"); | |
48 | |
Tom Henderson
2011/08/16 05:39:49
please comment here on what this example is trying
| |
49 class TdmaExample | |
50 { | |
51 public: | |
52 TdmaExample (); | |
53 void CaseRun (uint32_t nWifis, | |
54 uint32_t nSinks, | |
55 double totalTime, | |
56 std::string rate, | |
57 std::string phyMode, | |
58 uint32_t nodeSpeed, | |
59 uint32_t periodicUpdateInterval, | |
60 uint32_t settlingTime, | |
61 double dataStart, | |
62 std::string CSVfileName, | |
63 bool usingWifi, | |
64 double txpDistance, | |
65 uint32_t nSlots, | |
66 uint32_t slotTime, | |
67 uint32_t gaurdTime, | |
68 uint32_t interFrameGap); | |
69 | |
70 private: | |
71 uint32_t m_nWifis; | |
72 uint32_t m_nSinks; | |
73 double m_totalTime; | |
74 std::string m_rate; | |
75 std::string m_phyMode; | |
76 uint32_t m_nodeSpeed; | |
77 uint32_t m_periodicUpdateInterval; | |
78 uint32_t m_settlingTime; | |
79 double m_dataStart; | |
80 uint32_t bytesTotal; | |
81 uint32_t packetsReceived; | |
82 std::string m_CSVfileName; | |
83 uint32_t m_slots; | |
84 uint32_t m_slotTime; | |
85 uint32_t m_gaurdTime; | |
86 uint32_t m_interFrameGap; | |
87 | |
88 std::map<double, double> m_transmitRangeMap; | |
89 | |
90 NodeContainer nodes; | |
91 NetDeviceContainer devices; | |
92 Ipv4InterfaceContainer interfaces; | |
93 | |
94 private: | |
95 void CreateNodes (); | |
96 void CreateDevices (std::string tr_name, bool usingWifi, double txpDistance); | |
97 void InstallInternetStack (); | |
98 void InstallApplications (); | |
99 void SetupMobility (); | |
100 void ReceivePacket (Ptr <Socket> ); | |
101 Ptr <Socket> SetupPacketReceive (Ipv4Address, Ptr <Node> ); | |
102 void CheckThroughput (); | |
103 void InsertIntoTxp (void); | |
104 | |
105 }; | |
106 | |
107 int main (int argc, char **argv) | |
108 { | |
109 TdmaExample test; | |
110 uint32_t nWifis = 4; | |
111 uint32_t nSinks = 1; | |
112 double totalTime = 100.0; | |
113 std::string rate ("8kbps"); | |
114 std::string phyMode ("DsssRate11Mbps"); | |
115 uint32_t nodeSpeed = 10; //in m/s | |
116 std::string appl = "all"; | |
117 uint32_t periodicUpdateInterval = 15; | |
118 uint32_t settlingTime = 6; | |
119 double dataStart = 50.0; | |
120 std::string CSVfileName = "TdmaExample.csv"; | |
121 bool usingWifi = false; | |
122 double txpDistance = 400.0; | |
123 | |
124 // tdma parameters | |
125 uint32_t nSlots = nWifis; | |
126 uint32_t slotTime = 1000; | |
127 uint32_t interFrameGap = 0; | |
128 uint32_t gaurdTime = 0; | |
129 | |
130 CommandLine cmd; | |
131 cmd.AddValue ("nWifis", "Number of wifi nodes[Default:30]", nWifis); | |
Tom Henderson
2011/08/16 05:39:49
are these always wifi nodes, or only if "usingWifi
| |
132 cmd.AddValue ("nSinks", "Number of wifi sink nodes[Default:10]", nSinks); | |
133 cmd.AddValue ("usingWifi", "Do you want to use WifiMac(1/0)[Default:false(0)]" , usingWifi); | |
134 cmd.AddValue ("totalTime", "Total Simulation time[Default:100]", totalTime); | |
135 cmd.AddValue ("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode); | |
136 cmd.AddValue ("rate", "CBR traffic rate[Default:8kbps]", rate); | |
137 cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", n odeSpeed); | |
138 cmd.AddValue ("periodicUpdateInterval", "Periodic Interval Time[Default=15]", periodicUpdateInterval); | |
139 cmd.AddValue ("settlingTime", "Settling Time before sending out an update for changed metric[Default=6]", settlingTime); | |
140 cmd.AddValue ("dataStart", "Time at which nodes start to transmit data[Default =50.0]", dataStart); | |
141 cmd.AddValue ("CSVfileName", "The name of the CSV output file name[Default:Tdm aExample.csv]", CSVfileName); | |
142 cmd.AddValue ("txpDistance", "MaxRange for the node transmissions [Default:400 .0]", txpDistance); | |
143 cmd.AddValue ("nSlots", "Number of slots per gframe [Default:nWifis]", nSlots) ; | |
144 cmd.AddValue ("slotTime", "Slot transmission Time [Default(us):1000]", slotTim e); | |
145 cmd.AddValue ("gaurdTime", "Duration to wait between slots [Default(us):0]", g aurdTime); | |
Tom Henderson
2011/08/16 05:39:49
guardTime
| |
146 cmd.AddValue ("interFrameGap", "Duration between frames [Default(us):0]", inte rFrameGap); | |
147 cmd.Parse (argc, argv); | |
148 | |
149 std::ofstream out (CSVfileName.c_str ()); | |
150 out << "SimulationSecond," << | |
151 "ReceiveRate," << | |
152 "PacketsReceived," << | |
153 "NumberOfSinks," << | |
154 std::endl; | |
155 out.close (); | |
156 | |
157 SeedManager::SetSeed (12345); | |
158 | |
159 Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("1000")) ; | |
160 Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate)); | |
161 | |
162 test = TdmaExample (); | |
163 test.CaseRun (nWifis, nSinks, totalTime, rate, phyMode, nodeSpeed, periodicUpd ateInterval, | |
164 settlingTime, dataStart,CSVfileName,usingWifi,txpDistance, nSlot s, slotTime, gaurdTime, interFrameGap); | |
165 | |
166 return 0; | |
167 } | |
168 | |
169 TdmaExample::TdmaExample () | |
170 : bytesTotal (0), | |
171 packetsReceived (0) | |
172 { | |
173 } | |
174 | |
175 void | |
176 TdmaExample::ReceivePacket (Ptr <Socket> socket) | |
177 { | |
178 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " Received one packet!"); | |
179 Ptr <Packet> packet; | |
180 while (packet = socket->Recv ()) | |
181 { | |
182 bytesTotal += packet->GetSize (); | |
183 packetsReceived += 1; | |
184 } | |
185 } | |
186 | |
187 void | |
188 TdmaExample::CheckThroughput () | |
189 { | |
190 double kbs = (bytesTotal * 8.0) / 1000; | |
191 bytesTotal = 0; | |
192 | |
193 std::ofstream out (m_CSVfileName.c_str (), std::ios::app); | |
194 | |
195 out << (Simulator::Now ()).GetSeconds () << "," << kbs << "," << packetsReceiv ed << "," << m_nSinks << std::endl; | |
196 | |
197 out.close (); | |
198 packetsReceived = 0; | |
199 Simulator::Schedule (Seconds (1.0), &TdmaExample::CheckThroughput, this); | |
200 } | |
201 | |
202 Ptr <Socket> | |
203 TdmaExample::SetupPacketReceive (Ipv4Address addr, Ptr <Node> node) | |
204 { | |
205 | |
206 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); | |
207 Ptr <Socket> sink = Socket::CreateSocket (node, tid); | |
208 InetSocketAddress local = InetSocketAddress (addr, port); | |
209 sink->Bind (local); | |
210 sink->SetRecvCallback (MakeCallback ( &TdmaExample::ReceivePacket, this)); | |
211 | |
212 return sink; | |
213 } | |
214 | |
215 void | |
216 TdmaExample::CaseRun (uint32_t nWifis, uint32_t nSinks, double totalTime, std::s tring rate, | |
217 std::string phyMode, uint32_t nodeSpeed, uint32_t periodic UpdateInterval, uint32_t settlingTime, | |
218 double dataStart, std::string CSVfileName, bool usingWifi, double txpDistance, uint32_t nSlots, | |
219 uint32_t slotTime, uint32_t gaurdTime, uint32_t interFrame Gap) | |
220 { | |
221 m_nWifis = nWifis; | |
222 m_nSinks = nSinks; | |
223 m_totalTime = totalTime; | |
224 m_rate = rate; | |
225 m_phyMode = phyMode; | |
226 m_nodeSpeed = nodeSpeed; | |
227 m_periodicUpdateInterval = periodicUpdateInterval; | |
228 m_settlingTime = settlingTime; | |
229 m_dataStart = dataStart; | |
230 m_CSVfileName = CSVfileName; | |
231 m_slots = nSlots; | |
232 m_slotTime = slotTime; | |
233 m_gaurdTime = gaurdTime; | |
234 m_interFrameGap = interFrameGap; | |
235 | |
236 std::stringstream ss; | |
237 ss << m_nWifis; | |
238 std::string t_nodes = ss.str (); | |
239 | |
240 std::stringstream ss2; | |
241 ss2 << m_totalTime; | |
242 std::string sTotalTime = ss2.str (); | |
243 | |
244 std::stringstream ss3; | |
245 ss3 << txpDistance; | |
246 std::string t_txpDistance = ss3.str (); | |
247 | |
248 std::string macProtocol = ""; | |
249 if (usingWifi) | |
250 { | |
251 macProtocol = "wifi"; | |
252 } | |
253 else | |
254 { | |
255 macProtocol = "tdma"; | |
256 } | |
257 | |
258 std::string tr_name = "Dsdv_Manet_" | |
259 + t_nodes + "Nodes_" | |
260 + macProtocol + "_" | |
261 + t_txpDistance + "txDistance_" | |
262 + sTotalTime + "SimTime"; | |
263 std::cout << "Trace file generated is " << tr_name << ".tr\n"; | |
264 | |
265 InsertIntoTxp (); | |
266 CreateNodes (); | |
267 CreateDevices (tr_name,usingWifi,txpDistance); | |
268 SetupMobility (); | |
269 InstallInternetStack (); | |
270 InstallApplications (); | |
271 | |
272 std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n"; | |
273 | |
274 CheckThroughput (); | |
275 | |
276 Simulator::Stop (Seconds (m_totalTime)); | |
277 Simulator::Run (); | |
278 Simulator::Destroy (); | |
279 } | |
280 | |
281 void | |
282 TdmaExample::CreateNodes () | |
283 { | |
284 std::cout << "Creating " << (unsigned) m_nWifis << " nodes.\n"; | |
285 nodes.Create (m_nWifis); | |
286 NS_ASSERT_MSG (m_nWifis > m_nSinks, "Sinks must be less or equal to the number of nodes in network"); | |
287 } | |
288 | |
289 void | |
290 TdmaExample::SetupMobility () | |
291 { | |
292 MobilityHelper mobility; | |
293 /* ObjectFactory pos; | |
294 pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); | |
295 pos.Set ("X", RandomVariableValue (UniformVariable (0, 1000))); | |
296 pos.Set ("Y", RandomVariableValue (UniformVariable (0, 1000))); | |
297 | |
298 Ptr <PositionAllocator> positionAlloc = pos.Create ()->GetObject <PositionAllo cator> (); | |
299 mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed", Random VariableValue (ConstantVariable (m_nodeSpeed)), | |
300 "Pause", RandomVariableValue (ConstantVariable (2.0)), "PositionAllocator" , PointerValue (positionAlloc)); | |
301 mobility.SetPositionAllocator (positionAlloc);*/ | |
302 | |
303 ObjectFactory pos; | |
304 pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); | |
305 pos.Set ("X", RandomVariableValue (UniformVariable (0.0, 300.0))); | |
306 pos.Set ("Y", RandomVariableValue (UniformVariable (0.0, 0.0))); | |
307 Ptr<PositionAllocator> positionAlloc = pos.Create ()->GetObject<PositionAlloca tor> (); | |
308 mobility.SetPositionAllocator (positionAlloc); | |
309 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); | |
310 mobility.Install (nodes); | |
311 } | |
312 | |
313 void | |
314 TdmaExample::CreateDevices (std::string tr_name, bool usingWifi, double txpDista nce) | |
315 { | |
316 if (usingWifi) | |
317 { | |
318 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", Strin gValue (m_phyMode)); | |
319 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", Stri ngValue ("2000")); | |
320 | |
321 double txp; | |
322 if (m_transmitRangeMap.find (txpDistance) == m_transmitRangeMap.end ()) | |
323 { | |
324 txp = 0.0; | |
325 } | |
326 else | |
327 { | |
328 txp = m_transmitRangeMap[txpDistance]; | |
329 } | |
330 | |
331 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); | |
332 wifiMac.SetType ("ns3::AdhocWifiMac"); | |
333 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); | |
334 YansWifiChannelHelper wifiChannel; | |
335 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel" ); | |
336 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); | |
337 wifiPhy.SetChannel (wifiChannel.Create ()); | |
338 WifiHelper wifi; | |
339 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); | |
340 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", | |
341 "DataMode", StringValue (m_phyMode), | |
342 "ControlMode", StringValue (m_phyMode)); | |
343 //set the tx range based on txpDistance | |
344 wifiPhy.Set ("TxPowerStart", DoubleValue (txp)); | |
345 wifiPhy.Set ("TxPowerEnd", DoubleValue (txp)); | |
346 | |
347 devices = wifi.Install (wifiPhy, wifiMac, nodes); | |
348 | |
349 AsciiTraceHelper ascii; | |
350 wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tr_name + ".tr")); | |
351 wifiPhy.EnablePcapAll (tr_name); | |
352 } | |
353 else | |
354 { | |
355 Config::SetDefault ("ns3::SimpleWirelessChannel::MaxRange", DoubleValue (t xpDistance)); | |
356 // default allocation, each node gets a slot to transmit | |
357 TdmaHelper tdma = TdmaHelper (nodes.GetN (),nodes.GetN ()); // in this cas e selected, numSlots = nodes | |
358 /* can make custom allocation through simulation script | |
359 * will override default allocation | |
360 */ | |
361 /*tdma.SetSlots(4, | |
362 0,1,1,0,0, | |
363 1,0,0,0,0, | |
364 2,0,0,1,0, | |
365 3,0,0,0,1);*/ | |
366 // if TDMA slot assignment is through a file | |
367 //TdmaHelper tdma = TdmaHelper ("tdmaSlots.txt"); | |
368 TdmaControllerHelper controller; | |
369 controller.Set ("SlotTime", TimeValue (MicroSeconds (1100))); | |
370 controller.Set ("GaurdTime", TimeValue (MicroSeconds (100))); | |
371 controller.Set ("InterFrameTime", TimeValue (MicroSeconds (0))); | |
372 tdma.SetTdmaControllerHelper (controller); | |
373 devices = tdma.Install (nodes); | |
374 | |
375 AsciiTraceHelper ascii; | |
376 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream (tr_name + ".tr") ; | |
377 tdma.EnableAsciiAll (stream); | |
378 } | |
379 } | |
380 | |
381 void | |
382 TdmaExample::InstallInternetStack () | |
383 { | |
384 DsdvHelper dsdv; | |
385 dsdv.Set ("PeriodicUpdateInterval", TimeValue (Seconds (m_periodicUpdateInterv al))); | |
386 dsdv.Set ("SettlingTime", TimeValue (Seconds (m_settlingTime))); | |
387 InternetStackHelper stack; | |
388 stack.SetRoutingHelper (dsdv); | |
389 stack.Install (nodes); | |
390 Ipv4AddressHelper address; | |
391 address.SetBase ("10.1.1.0", "255.255.255.0"); | |
392 interfaces = address.Assign (devices); | |
393 } | |
394 | |
395 void | |
396 TdmaExample::InstallApplications () | |
397 { | |
398 for (uint32_t i = 0; i <= m_nSinks - 1; i++ ) | |
399 { | |
400 Ptr<Node> node = NodeList::GetNode (i); | |
401 Ipv4Address nodeAddress = node->GetObject<Ipv4> ()->GetAddress (1, 0).GetL ocal (); | |
402 Ptr<Socket> sink = SetupPacketReceive (nodeAddress, node); | |
403 } | |
404 | |
405 for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++ ) | |
406 { | |
407 for (uint32_t j = 0; j <= m_nSinks - 1; j++ ) | |
408 { | |
409 OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddres s (interfaces.GetAddress (j), port))); | |
410 onoff1.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable ( 1))); | |
411 onoff1.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); | |
412 | |
413 if (j != clientNode) | |
414 { | |
415 ApplicationContainer apps1 = onoff1.Install (nodes.Get (clientNode )); | |
416 UniformVariable var; | |
417 apps1.Start (Seconds (var.GetValue (m_dataStart, m_dataStart + 1)) ); | |
418 apps1.Stop (Seconds (m_totalTime)); | |
419 } | |
420 } | |
421 } | |
422 } | |
423 | |
424 void | |
Tom Henderson
2011/08/16 05:39:49
maybe a comment here would help on what this metho
| |
425 TdmaExample::InsertIntoTxp () | |
426 { | |
427 m_transmitRangeMap.insert (std::pair<double, double> (100.0, 1.2251)); | |
428 m_transmitRangeMap.insert (std::pair<double, double> (130.0, 3.5049)); | |
429 m_transmitRangeMap.insert (std::pair<double, double> (150.0, 4.7478)); | |
430 m_transmitRangeMap.insert (std::pair<double, double> (180.0, 6.3314)); | |
431 m_transmitRangeMap.insert (std::pair<double, double> (200.0, 7.2457)); | |
432 m_transmitRangeMap.insert (std::pair<double, double> (300.0, 10.7675)); | |
433 m_transmitRangeMap.insert (std::pair<double, double> (350.0, 12.1065)); | |
434 m_transmitRangeMap.insert (std::pair<double, double> (400.0, 13.2663)); | |
435 m_transmitRangeMap.insert (std::pair<double, double> (450.0, 14.2893)); | |
436 m_transmitRangeMap.insert (std::pair<double, double> (500.0, 15.2045)); | |
437 m_transmitRangeMap.insert (std::pair<double, double> (550.0, 16.0323)); | |
438 m_transmitRangeMap.insert (std::pair<double, double> (600.0, 16.7881)); | |
439 m_transmitRangeMap.insert (std::pair<double, double> (650.0, 17.4834)); | |
440 m_transmitRangeMap.insert (std::pair<double, double> (700.0, 18.1271)); | |
441 m_transmitRangeMap.insert (std::pair<double, double> (800.0, 19.2869)); | |
442 m_transmitRangeMap.insert (std::pair<double, double> (900.0, 20.3099)); | |
443 m_transmitRangeMap.insert (std::pair<double, double> (950.0, 20.7796)); | |
444 m_transmitRangeMap.insert (std::pair<double, double> (1000.0, 21.2251)); | |
445 m_transmitRangeMap.insert (std::pair<double, double> (27800.0, 50.0)); | |
446 } | |
447 | |
OLD | NEW |