3589
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License version 2 as
|
|
5 |
* published by the Free Software Foundation;
|
|
6 |
*
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
* GNU General Public License for more details.
|
|
11 |
*
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
13 |
* along with this program; if not, write to the Free Software
|
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
15 |
*
|
|
16 |
* Authors: Joe Kopena <tjkopena@cs.drexel.edu>
|
|
17 |
*
|
|
18 |
* This program conducts a simple experiment: It places two nodes at a
|
|
19 |
* parameterized distance apart. One node generates packets and the
|
|
20 |
* other node receives. The stat framework collects data on packet
|
|
21 |
* loss. Outside of this program, a control script uses that data to
|
|
22 |
* produce graphs presenting performance at the varying distances.
|
|
23 |
* This isn't a typical simulation but is a common "experiment"
|
|
24 |
* performed in real life and serves as an accessible exemplar for the
|
|
25 |
* stat framework. It also gives some intuition on the behavior and
|
|
26 |
* basic reasonability of the NS-3 WiFi models.
|
|
27 |
*
|
|
28 |
* Applications used by this program are in test02-apps.h and
|
|
29 |
* test02-apps.cc, which should be in the same place as this file.
|
|
30 |
*
|
|
31 |
*/
|
|
32 |
|
|
33 |
// #define NS3_LOG_ENABLE // Now defined by Makefile
|
|
34 |
|
|
35 |
#include <sstream>
|
|
36 |
|
|
37 |
#include "ns3/core-module.h"
|
|
38 |
#include "ns3/common-module.h"
|
|
39 |
#include "ns3/node-module.h"
|
|
40 |
#include "ns3/helper-module.h"
|
|
41 |
#include "ns3/mobility-module.h"
|
|
42 |
#include "ns3/wifi-module.h"
|
|
43 |
|
|
44 |
#include "ns3/stats-module.h"
|
|
45 |
|
|
46 |
#include "wifi-example-apps.h"
|
|
47 |
|
|
48 |
using namespace ns3;
|
|
49 |
using namespace std;
|
|
50 |
|
|
51 |
NS_LOG_COMPONENT_DEFINE ("WiFiDistanceExperiment");
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
void TxCallback(Ptr<CounterCalculator<uint32_t> > datac,
|
|
57 |
std::string path, Ptr<const Packet> packet,
|
|
58 |
Mac48Address realto) {
|
|
59 |
NS_LOG_INFO("Sent frame to " << realto << "; counted in " <<
|
|
60 |
datac->GetKey());
|
|
61 |
datac->Update();
|
|
62 |
// end TxCallback
|
|
63 |
}
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
//----------------------------------------------------------------------
|
|
69 |
//-- main
|
|
70 |
//----------------------------------------------
|
|
71 |
int main(int argc, char *argv[]) {
|
|
72 |
|
|
73 |
double distance = 50.0;
|
|
74 |
string format("omnet");
|
|
75 |
|
|
76 |
string experiment("wifi-distance-test");
|
|
77 |
string strategy("wifi-default");
|
|
78 |
string input;
|
|
79 |
string runID;
|
|
80 |
|
|
81 |
{
|
|
82 |
stringstream sstr;
|
|
83 |
sstr << "run-" << time(NULL);
|
|
84 |
runID = sstr.str();
|
|
85 |
}
|
|
86 |
|
|
87 |
// Set up command line parameters used to control the experiment.
|
|
88 |
CommandLine cmd;
|
|
89 |
cmd.AddValue("distance", "Distance apart to place nodes (in meters).",
|
|
90 |
distance);
|
|
91 |
cmd.AddValue("format", "Format to use for data output.",
|
|
92 |
format);
|
|
93 |
cmd.AddValue("experiment", "Identifier for experiment.",
|
|
94 |
experiment);
|
|
95 |
cmd.AddValue("strategy", "Identifier for strategy.",
|
|
96 |
strategy);
|
|
97 |
cmd.AddValue("run", "Identifier for run.",
|
|
98 |
runID);
|
|
99 |
cmd.Parse (argc, argv);
|
|
100 |
|
|
101 |
if (format != "omnet" && format != "db") {
|
|
102 |
NS_LOG_ERROR("Unknown output format '" << format << "'");
|
|
103 |
return -1;
|
|
104 |
}
|
|
105 |
|
|
106 |
#ifndef STATS_HAS_SQLITE3
|
|
107 |
if (format == "db") {
|
|
108 |
NS_LOG_ERROR("sqlite support not compiled in.");
|
|
109 |
return -1;
|
|
110 |
}
|
|
111 |
#endif
|
|
112 |
|
|
113 |
{
|
|
114 |
stringstream sstr("");
|
|
115 |
sstr << distance;
|
|
116 |
input = sstr.str();
|
|
117 |
}
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
//------------------------------------------------------------
|
|
123 |
//-- Create nodes and network stacks
|
|
124 |
//--------------------------------------------
|
|
125 |
NS_LOG_INFO("Creating nodes.");
|
|
126 |
NodeContainer nodes;
|
|
127 |
nodes.Create(2);
|
|
128 |
|
|
129 |
NS_LOG_INFO("Installing WiFi and Internet stack.");
|
|
130 |
WifiHelper wifi;
|
|
131 |
wifi.SetMac("ns3::AdhocWifiMac");
|
|
132 |
wifi.SetPhy("ns3::WifiPhy");
|
|
133 |
NetDeviceContainer nodeDevices = wifi.Install(nodes);
|
|
134 |
|
|
135 |
InternetStackHelper internet;
|
|
136 |
internet.Install(nodes);
|
|
137 |
Ipv4AddressHelper ipAddrs;
|
|
138 |
ipAddrs.SetBase("192.168.0.0", "255.255.255.0");
|
|
139 |
ipAddrs.Assign(nodeDevices);
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
//------------------------------------------------------------
|
|
145 |
//-- Setup physical layout
|
|
146 |
//--------------------------------------------
|
|
147 |
NS_LOG_INFO("Installing static mobility; distance " << distance << " .");
|
|
148 |
MobilityHelper mobility;
|
|
149 |
Ptr<ListPositionAllocator> positionAlloc =
|
|
150 |
CreateObject<ListPositionAllocator>();
|
|
151 |
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
|
|
152 |
positionAlloc->Add(Vector(0.0, distance, 0.0));
|
|
153 |
mobility.SetPositionAllocator(positionAlloc);
|
|
154 |
mobility.Install(nodes);
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
//------------------------------------------------------------
|
|
160 |
//-- Create a custom traffic source and sink
|
|
161 |
//--------------------------------------------
|
|
162 |
NS_LOG_INFO ("Create traffic source & sink.");
|
|
163 |
Ptr<Node> appSource = NodeList::GetNode(0);
|
|
164 |
Ptr<Sender> sender = CreateObject<Sender>();
|
|
165 |
appSource->AddApplication(sender);
|
|
166 |
sender->Start(Seconds(1));
|
|
167 |
|
|
168 |
Ptr<Node> appSink = NodeList::GetNode(1);
|
|
169 |
Ptr<Receiver> receiver = CreateObject<Receiver>();
|
|
170 |
appSink->AddApplication(receiver);
|
|
171 |
receiver->Start(Seconds(0));
|
|
172 |
|
|
173 |
// Config::Set("/NodeList/*/ApplicationList/*/$Sender/Destination",
|
|
174 |
// Ipv4AddressValue("192.168.0.2"));
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
//------------------------------------------------------------
|
|
180 |
//-- Setup stats and data collection
|
|
181 |
//--------------------------------------------
|
|
182 |
|
|
183 |
// Create a DataCollector object to hold information about this run.
|
|
184 |
DataCollector data;
|
|
185 |
data.DescribeRun(experiment,
|
|
186 |
strategy,
|
|
187 |
input,
|
|
188 |
runID);
|
|
189 |
|
|
190 |
// Add any information we wish to record about this run.
|
|
191 |
data.AddMetadata("author", "tjkopena");
|
|
192 |
|
|
193 |
|
|
194 |
// Create a counter to track how many frames are generated. Updates
|
|
195 |
// are triggered by the trace signal generated by the WiFi MAC model
|
|
196 |
// object. Here we connect the counter to the signal via the simple
|
|
197 |
// TxCallback() glue function defined above.
|
|
198 |
Ptr<CounterCalculator<uint32_t> > totalTx =
|
|
199 |
CreateObject<CounterCalculator<uint32_t> >();
|
|
200 |
totalTx->SetKey("wifi-tx-frames");
|
|
201 |
Config::Connect("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Tx",
|
|
202 |
MakeBoundCallback(&TxCallback, totalTx));
|
|
203 |
data.AddDataCalculator(totalTx);
|
|
204 |
|
|
205 |
// This is similar, but creates a counter to track how many frames
|
|
206 |
// are received. Instead of our own glue function, this uses a
|
|
207 |
// method of an adapter class to connect a counter directly to the
|
|
208 |
// trace signal generated by the WiFi MAC.
|
|
209 |
Ptr<PacketCounterCalculator> totalRx =
|
|
210 |
CreateObject<PacketCounterCalculator>();
|
|
211 |
totalRx->SetKey("wifi-rx-frames");
|
|
212 |
Config::Connect("/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/Rx",
|
|
213 |
MakeCallback(&PacketCounterCalculator::FrameUpdate,
|
|
214 |
totalRx));
|
|
215 |
data.AddDataCalculator(totalRx);
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
// This counter tracks how many packets---as opposed to frames---are
|
|
221 |
// generated. This is connected directly to a trace signal provided
|
|
222 |
// by our Sender class.
|
|
223 |
Ptr<PacketCounterCalculator> appTx =
|
|
224 |
CreateObject<PacketCounterCalculator>();
|
|
225 |
appTx->SetKey("sender-tx-packets");
|
|
226 |
Config::Connect("/NodeList/0/ApplicationList/*/$Sender/Tx",
|
|
227 |
MakeCallback(&PacketCounterCalculator::PacketUpdate,
|
|
228 |
appTx));
|
|
229 |
data.AddDataCalculator(appTx);
|
|
230 |
|
|
231 |
// Here a counter for received packets is directly manipulated by
|
|
232 |
// one of the custom objects in our simulation, the Receiver
|
|
233 |
// Application. The Receiver object is given a pointer to the
|
|
234 |
// counter and calls its Update() method whenever a packet arrives.
|
|
235 |
Ptr<CounterCalculator<> > appRx =
|
|
236 |
CreateObject<CounterCalculator<> >();
|
|
237 |
appRx->SetKey("receiver-rx-packets");
|
|
238 |
receiver->SetCounter(appRx);
|
|
239 |
data.AddDataCalculator(appRx);
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Just to show this is here...
|
|
246 |
Ptr<MinMaxAvgTotalCalculator<uint32_t> > test =
|
|
247 |
CreateObject<MinMaxAvgTotalCalculator<uint32_t> >();
|
|
248 |
test->SetKey("test-dc");
|
|
249 |
data.AddDataCalculator(test);
|
|
250 |
|
|
251 |
test->Update(4);
|
|
252 |
test->Update(8);
|
|
253 |
test->Update(24);
|
|
254 |
test->Update(12);
|
|
255 |
**/
|
|
256 |
|
|
257 |
// This DataCalculator connects directly to the transmit trace
|
|
258 |
// provided by our Sender Application. It records some basic
|
|
259 |
// statistics about the sizes of the packets received (min, max,
|
|
260 |
// avg, total # bytes), although in this scenaro they're fixed.
|
|
261 |
Ptr<PacketSizeMinMaxAvgTotalCalculator> appTxPkts =
|
|
262 |
CreateObject<PacketSizeMinMaxAvgTotalCalculator>();
|
|
263 |
appTxPkts->SetKey("tx-pkt-size");
|
|
264 |
Config::Connect("/NodeList/0/ApplicationList/*/$Sender/Tx",
|
|
265 |
MakeCallback
|
|
266 |
(&PacketSizeMinMaxAvgTotalCalculator::PacketUpdate,
|
|
267 |
appTxPkts));
|
|
268 |
data.AddDataCalculator(appTxPkts);
|
|
269 |
|
|
270 |
|
|
271 |
// Here we directly manipulate another DataCollector tracking min,
|
|
272 |
// max, total, and average propagation delays. Check out the Sender
|
|
273 |
// and Receiver classes to see how packets are tagged with
|
|
274 |
// timestamps to do this.
|
|
275 |
Ptr<TimeMinMaxAvgTotalCalculator> delayStat =
|
|
276 |
CreateObject<TimeMinMaxAvgTotalCalculator>();
|
|
277 |
delayStat->SetKey("delay");
|
|
278 |
receiver->SetDelayTracker(delayStat);
|
|
279 |
data.AddDataCalculator(delayStat);
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
//------------------------------------------------------------
|
|
285 |
//-- Run the simulation
|
|
286 |
//--------------------------------------------
|
|
287 |
NS_LOG_INFO("Run Simulation.");
|
|
288 |
Simulator::Run();
|
|
289 |
Simulator::Destroy();
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
//------------------------------------------------------------
|
|
295 |
//-- Generate statistics output.
|
|
296 |
//--------------------------------------------
|
|
297 |
|
|
298 |
// Pick an output writer based in the requested format.
|
|
299 |
Ptr<DataOutputInterface> output = 0;
|
|
300 |
if (format == "omnet") {
|
|
301 |
NS_LOG_INFO("Creating omnet formatted data output.");
|
|
302 |
output = CreateObject<OmnetDataOutput>();
|
|
303 |
} else if (format == "db") {
|
|
304 |
#ifdef STATS_HAS_SQLITE3
|
|
305 |
NS_LOG_INFO("Creating sqlite formatted data output.");
|
|
306 |
output = CreateObject<SqliteDataOutput>();
|
|
307 |
#endif
|
|
308 |
} else {
|
|
309 |
NS_LOG_ERROR("Unknown output format " << format);
|
|
310 |
}
|
|
311 |
|
|
312 |
// Finally, have that writer interrogate the DataCollector and save
|
|
313 |
// the results.
|
|
314 |
if (output != 0)
|
|
315 |
output->Output(data);
|
|
316 |
|
|
317 |
// end main
|
|
318 |
}
|