|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
3 |
* Copyright (c) 2011 University of Kansas |
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: Justin Rohrer <rohrej@ittc.ku.edu> |
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 Experimentation on GENI), |
29 |
* US Department of Defense (DoD), and ITTC at The University of Kansas. |
30 |
*/ |
31 |
|
32 |
/* |
33 |
* This example program allows one to run ns-3 DSDV, AODV, or OLSR under |
34 |
* a typical random waypoint mobility model. |
35 |
* |
36 |
* By default, the simulation runs for 200 simulated seconds, of which |
37 |
* the first 50 are used for start-up time. The number of nodes is 50. |
38 |
* Nodes move according to RandomWaypointMobilityModel with a speed of |
39 |
* 20 m/s and no pause time within a 300x1500 m region. The WiFi is |
40 |
* in ad hoc mode with a 2 Mb/s rate (802.11b) and a Friis loss model. |
41 |
* The transmit power is set to 7.5 dBm. |
42 |
* |
43 |
* It is possible to change the mobility and density of the network by |
44 |
* directly modifying the speed and the number of nodes. It is also |
45 |
* possible to change the characteristics of the network by changing |
46 |
* the transmit power (as power increases, the impact of mobility |
47 |
* decreases and the effective density increases). |
48 |
* |
49 |
* By default, OLSR is used, but specifying a value of 2 for the protocol |
50 |
* will cause AODV to be used, and specifying a value of 3 will cause |
51 |
* DSDV to be used. |
52 |
* |
53 |
* By default, there are 10 source/sink data pairs sending UDP data |
54 |
* at an application rate of 2.048 Kb/s each. This is typically done |
55 |
* at a rate of 4 64-byte packets per second. Application data is |
56 |
* started at a random time between 50 and 51 seconds and continues |
57 |
* to the end of the simulation. |
58 |
* |
59 |
* The program outputs a few items: |
60 |
* - packet receptions are notified to stdout such as: |
61 |
* <timestamp> <node-id> received one packet from <src-address> |
62 |
* - each second, the data reception statistics are tabulated and output |
63 |
* to a comma-separated value (csv) file |
64 |
* - some tracing and flow monitor configuration that used to work is |
65 |
* left commented inline in the program |
66 |
*/ |
67 |
|
68 |
#include <fstream> |
69 |
#include <iostream> |
70 |
#include "ns3/core-module.h" |
71 |
#include "ns3/network-module.h" |
72 |
#include "ns3/internet-module.h" |
73 |
#include "ns3/mobility-module.h" |
74 |
#include "ns3/wifi-module.h" |
75 |
#include "ns3/aodv-module.h" |
76 |
#include "ns3/olsr-module.h" |
77 |
#include "ns3/dsdv-module.h" |
78 |
#include "ns3/applications-module.h" |
79 |
|
80 |
using namespace ns3; |
81 |
|
82 |
NS_LOG_COMPONENT_DEFINE ("manet-routing-compare"); |
83 |
|
84 |
class RoutingExperiment |
85 |
{ |
86 |
public: |
87 |
RoutingExperiment (); |
88 |
void Run (int nSinks, int protocol, double txp, std::string CSVfileName); |
89 |
//static void SetMACParam (ns3::NetDeviceContainer & devices, |
90 |
// int slotDistance); |
91 |
std::string CommandSetup (int argc, char **argv); |
92 |
|
93 |
private: |
94 |
Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node); |
95 |
void ReceivePacket (Ptr<Socket> socket); |
96 |
void CheckThroughput (); |
97 |
|
98 |
uint32_t port; |
99 |
uint32_t bytesTotal; |
100 |
uint32_t packetsReceived; |
101 |
|
102 |
std::string m_CSVfileName; |
103 |
int m_nSinks; |
104 |
std::string m_protocolName; |
105 |
double m_txp; |
106 |
}; |
107 |
|
108 |
RoutingExperiment::RoutingExperiment () |
109 |
: port (9), |
110 |
bytesTotal (0), |
111 |
packetsReceived (0), |
112 |
m_CSVfileName ("manet-routing.output.csv") |
113 |
{ |
114 |
} |
115 |
|
116 |
void |
117 |
RoutingExperiment::ReceivePacket (Ptr<Socket> socket) |
118 |
{ |
119 |
Ptr<Packet> packet; |
120 |
while (packet = socket->Recv ()) |
121 |
{ |
122 |
bytesTotal += packet->GetSize (); |
123 |
packetsReceived += 1; |
124 |
SocketAddressTag tag; |
125 |
bool found; |
126 |
found = packet->PeekPacketTag (tag); |
127 |
if (found) |
128 |
{ |
129 |
InetSocketAddress addr = InetSocketAddress::ConvertFrom (tag.GetAddress ()); |
130 |
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId () |
131 |
<< " received one packet from " << addr.GetIpv4 ()); |
132 |
} |
133 |
else |
134 |
{ |
135 |
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId () |
136 |
<< " received one packet!"); |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
void |
142 |
RoutingExperiment::CheckThroughput () |
143 |
{ |
144 |
double kbs = (bytesTotal * 8.0) / 1000; |
145 |
bytesTotal = 0; |
146 |
|
147 |
std::ofstream out (m_CSVfileName.c_str (), std::ios::app); |
148 |
|
149 |
out << (Simulator::Now ()).GetSeconds () << "," |
150 |
<< kbs << "," |
151 |
<< packetsReceived << "," |
152 |
<< m_nSinks << "," |
153 |
<< m_protocolName << "," |
154 |
<< m_txp << "" |
155 |
<< std::endl; |
156 |
|
157 |
out.close (); |
158 |
packetsReceived = 0; |
159 |
Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this); |
160 |
} |
161 |
|
162 |
Ptr<Socket> |
163 |
RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr<Node> node) |
164 |
{ |
165 |
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
166 |
Ptr<Socket> sink = Socket::CreateSocket (node, tid); |
167 |
InetSocketAddress local = InetSocketAddress (addr, port); |
168 |
sink->Bind (local); |
169 |
sink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket, this)); |
170 |
|
171 |
return sink; |
172 |
} |
173 |
|
174 |
std::string |
175 |
RoutingExperiment::CommandSetup (int argc, char **argv) |
176 |
{ |
177 |
CommandLine cmd; |
178 |
cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName); |
179 |
cmd.Parse (argc, argv); |
180 |
return m_CSVfileName; |
181 |
} |
182 |
|
183 |
int |
184 |
main (int argc, char *argv[]) |
185 |
{ |
186 |
RoutingExperiment experiment; |
187 |
std::string CSVfileName = experiment.CommandSetup (argc,argv); |
188 |
|
189 |
//blank out the last output file and write the column headers |
190 |
std::ofstream out (CSVfileName.c_str ()); |
191 |
out << "SimulationSecond," << |
192 |
"ReceiveRate," << |
193 |
"PacketsReceived," << |
194 |
"NumberOfSinks," << |
195 |
"RoutingProtocol," << |
196 |
"TransmissionPower" << |
197 |
std::endl; |
198 |
out.close (); |
199 |
|
200 |
int nSinks = 10; |
201 |
int protocol = 2; |
202 |
double txp = 7.5; |
203 |
|
204 |
experiment = RoutingExperiment (); |
205 |
experiment.Run (nSinks, protocol, txp, CSVfileName); |
206 |
} |
207 |
|
208 |
void |
209 |
RoutingExperiment::Run (int nSinks, int protocol, double txp, std::string CSVfileName) |
210 |
{ |
211 |
Packet::EnablePrinting (); |
212 |
m_nSinks = nSinks; |
213 |
m_txp = txp; |
214 |
m_CSVfileName = CSVfileName; |
215 |
|
216 |
int nWifis = 50; |
217 |
|
218 |
double TotalTime = 200.0; |
219 |
std::string rate ("2048bps"); |
220 |
std::string phyMode ("DsssRate11Mbps"); |
221 |
std::string tr_name ("Brock"); |
222 |
int nodeSpeed = 20; //in m/s |
223 |
int nodePause = 0; //in s |
224 |
m_protocolName = "protocol"; |
225 |
|
226 |
Config::SetDefault ("ns3::OnOffApplication::PacketSize",StringValue ("64")); |
227 |
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate)); |
228 |
|
229 |
//Set Non-unicastMode rate to unicast mode |
230 |
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue (phyMode)); |
231 |
|
232 |
NodeContainer adhocNodes; |
233 |
adhocNodes.Create (nWifis); |
234 |
|
235 |
// setting up wifi phy and channel using helpers |
236 |
WifiHelper wifi; |
237 |
wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
238 |
|
239 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
240 |
YansWifiChannelHelper wifiChannel; |
241 |
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
242 |
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); |
243 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
244 |
|
245 |
// Add a non-QoS upper mac, and disable rate control |
246 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
247 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
248 |
"DataMode",StringValue (phyMode), |
249 |
"ControlMode",StringValue (phyMode)); |
250 |
|
251 |
wifiPhy.Set ("TxPowerStart",DoubleValue (txp)); |
252 |
wifiPhy.Set ("TxPowerEnd", DoubleValue (txp)); |
253 |
|
254 |
wifiMac.SetType ("ns3::AdhocWifiMac"); |
255 |
NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes); |
256 |
|
257 |
AodvHelper aodv; |
258 |
OlsrHelper olsr; |
259 |
DsdvHelper dsdv; |
260 |
Ipv4ListRoutingHelper list; |
261 |
|
262 |
switch (protocol) |
263 |
{ |
264 |
case 1: |
265 |
list.Add (olsr, 100); |
266 |
m_protocolName = "OLSR"; |
267 |
break; |
268 |
case 2: |
269 |
list.Add (aodv, 100); |
270 |
m_protocolName = "AODV"; |
271 |
break; |
272 |
case 3: |
273 |
list.Add (dsdv, 100); |
274 |
m_protocolName = "DSDV"; |
275 |
break; |
276 |
default: |
277 |
NS_FATAL_ERROR ("No such protocol:" << protocol); |
278 |
} |
279 |
|
280 |
InternetStackHelper internet; |
281 |
internet.SetRoutingHelper (list); |
282 |
internet.Install (adhocNodes); |
283 |
|
284 |
NS_LOG_INFO ("assigning ip address"); |
285 |
|
286 |
Ipv4AddressHelper addressAdhoc; |
287 |
addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0"); |
288 |
Ipv4InterfaceContainer adhocInterfaces; |
289 |
adhocInterfaces = addressAdhoc.Assign (adhocDevices); |
290 |
|
291 |
MobilityHelper mobilityAdhoc; |
292 |
|
293 |
ObjectFactory pos; |
294 |
pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); |
295 |
pos.Set ("X", RandomVariableValue (UniformVariable (0.0, 300.0))); |
296 |
pos.Set ("Y", RandomVariableValue (UniformVariable (0.0, 1500.0))); |
297 |
|
298 |
Ptr<PositionAllocator> taPositionAlloc = pos.Create ()->GetObject<PositionAllocator> (); |
299 |
mobilityAdhoc.SetMobilityModel ("ns3::RandomWaypointMobilityModel", |
300 |
"Speed", RandomVariableValue (UniformVariable (0.0, nodeSpeed)), |
301 |
"Pause", RandomVariableValue (ConstantVariable (nodePause)), |
302 |
"PositionAllocator", PointerValue (taPositionAlloc)); |
303 |
mobilityAdhoc.SetPositionAllocator (taPositionAlloc); |
304 |
mobilityAdhoc.Install (adhocNodes); |
305 |
|
306 |
OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ()); |
307 |
onoff1.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); |
308 |
onoff1.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); |
309 |
|
310 |
for (int i = 0; i <= nSinks - 1; i++) |
311 |
{ |
312 |
Ptr<Socket> sink = SetupPacketReceive (adhocInterfaces.GetAddress (i), adhocNodes.Get (i)); |
313 |
|
314 |
AddressValue remoteAddress (InetSocketAddress (adhocInterfaces.GetAddress (i), port)); |
315 |
onoff1.SetAttribute ("Remote", remoteAddress); |
316 |
|
317 |
UniformVariable var; |
318 |
ApplicationContainer temp = onoff1.Install (adhocNodes.Get (i + nSinks)); |
319 |
temp.Start (Seconds (var.GetValue (50.0,51.0))); |
320 |
temp.Stop (Seconds (TotalTime)); |
321 |
} |
322 |
|
323 |
std::stringstream ss; |
324 |
ss << nWifis; |
325 |
std::string nodes = ss.str (); |
326 |
|
327 |
std::stringstream ss2; |
328 |
ss2 << nodeSpeed; |
329 |
std::string sNodeSpeed = ss2.str (); |
330 |
|
331 |
std::stringstream ss3; |
332 |
ss3 << nodePause; |
333 |
std::string sNodePause = ss3.str (); |
334 |
|
335 |
std::stringstream ss4; |
336 |
ss4 << rate; |
337 |
std::string sRate = ss4.str (); |
338 |
|
339 |
//NS_LOG_INFO ("Configure Tracing."); |
340 |
//tr_name = tr_name + "_" + m_protocolName +"_" + nodes + "nodes_" + sNodeSpeed + "speed_" + sNodePause + "pause_" + sRate + "rate"; |
341 |
|
342 |
//std::ofstream ascii; |
343 |
//ascii.open ((tr_name+".tr").c_str()); |
344 |
//YansWifiPhyHelper::EnableAsciiAll (ascii); |
345 |
//MobilityHelper::EnableAsciiAll (ascii); |
346 |
|
347 |
//Ptr<FlowMonitor> flowmon; |
348 |
//FlowMonitorHelper flowmonHelper; |
349 |
//flowmon = flowmonHelper.InstallAll (); |
350 |
|
351 |
|
352 |
NS_LOG_INFO ("Run Simulation."); |
353 |
|
354 |
CheckThroughput (); |
355 |
|
356 |
Simulator::Stop (Seconds (TotalTime)); |
357 |
Simulator::Run (); |
358 |
|
359 |
//flowmon->SerializeToXmlFile ((tr_name + ".flowmon").c_str(), false, false); |
360 |
|
361 |
Simulator::Destroy (); |
362 |
} |
363 |
|