author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Tue, 04 Nov 2008 14:06:34 +0100 | |
changeset 3912 | 4d1a61f80745 |
parent 3906 | 01acc159ffb1 |
child 4271 | 125101b3e24d |
permissions | -rw-r--r-- |
3570 | 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"); |
|
3912
4d1a61f80745
new wifi API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3906
diff
changeset
|
132 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
4d1a61f80745
new wifi API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3906
diff
changeset
|
133 |
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); |
4d1a61f80745
new wifi API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3906
diff
changeset
|
134 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
4d1a61f80745
new wifi API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3906
diff
changeset
|
135 |
NetDeviceContainer nodeDevices = wifi.Install(wifiPhy, nodes); |
3570 | 136 |
|
137 |
InternetStackHelper internet; |
|
138 |
internet.Install(nodes); |
|
139 |
Ipv4AddressHelper ipAddrs; |
|
140 |
ipAddrs.SetBase("192.168.0.0", "255.255.255.0"); |
|
141 |
ipAddrs.Assign(nodeDevices); |
|
142 |
||
143 |
||
144 |
||
145 |
||
146 |
//------------------------------------------------------------ |
|
147 |
//-- Setup physical layout |
|
148 |
//-------------------------------------------- |
|
149 |
NS_LOG_INFO("Installing static mobility; distance " << distance << " ."); |
|
150 |
MobilityHelper mobility; |
|
151 |
Ptr<ListPositionAllocator> positionAlloc = |
|
152 |
CreateObject<ListPositionAllocator>(); |
|
153 |
positionAlloc->Add(Vector(0.0, 0.0, 0.0)); |
|
154 |
positionAlloc->Add(Vector(0.0, distance, 0.0)); |
|
155 |
mobility.SetPositionAllocator(positionAlloc); |
|
156 |
mobility.Install(nodes); |
|
157 |
||
158 |
||
159 |
||
160 |
||
161 |
//------------------------------------------------------------ |
|
162 |
//-- Create a custom traffic source and sink |
|
163 |
//-------------------------------------------- |
|
164 |
NS_LOG_INFO ("Create traffic source & sink."); |
|
165 |
Ptr<Node> appSource = NodeList::GetNode(0); |
|
166 |
Ptr<Sender> sender = CreateObject<Sender>(); |
|
167 |
appSource->AddApplication(sender); |
|
168 |
sender->Start(Seconds(1)); |
|
169 |
||
170 |
Ptr<Node> appSink = NodeList::GetNode(1); |
|
171 |
Ptr<Receiver> receiver = CreateObject<Receiver>(); |
|
172 |
appSink->AddApplication(receiver); |
|
173 |
receiver->Start(Seconds(0)); |
|
174 |
||
175 |
// Config::Set("/NodeList/*/ApplicationList/*/$Sender/Destination", |
|
176 |
// Ipv4AddressValue("192.168.0.2")); |
|
177 |
||
178 |
||
179 |
||
180 |
||
181 |
//------------------------------------------------------------ |
|
182 |
//-- Setup stats and data collection |
|
183 |
//-------------------------------------------- |
|
184 |
||
185 |
// Create a DataCollector object to hold information about this run. |
|
186 |
DataCollector data; |
|
187 |
data.DescribeRun(experiment, |
|
188 |
strategy, |
|
189 |
input, |
|
190 |
runID); |
|
191 |
||
192 |
// Add any information we wish to record about this run. |
|
193 |
data.AddMetadata("author", "tjkopena"); |
|
194 |
||
195 |
||
196 |
// Create a counter to track how many frames are generated. Updates |
|
197 |
// are triggered by the trace signal generated by the WiFi MAC model |
|
198 |
// object. Here we connect the counter to the signal via the simple |
|
199 |
// TxCallback() glue function defined above. |
|
200 |
Ptr<CounterCalculator<uint32_t> > totalTx = |
|
201 |
CreateObject<CounterCalculator<uint32_t> >(); |
|
202 |
totalTx->SetKey("wifi-tx-frames"); |
|
203 |
Config::Connect("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Tx", |
|
204 |
MakeBoundCallback(&TxCallback, totalTx)); |
|
205 |
data.AddDataCalculator(totalTx); |
|
206 |
||
207 |
// This is similar, but creates a counter to track how many frames |
|
208 |
// are received. Instead of our own glue function, this uses a |
|
209 |
// method of an adapter class to connect a counter directly to the |
|
210 |
// trace signal generated by the WiFi MAC. |
|
211 |
Ptr<PacketCounterCalculator> totalRx = |
|
212 |
CreateObject<PacketCounterCalculator>(); |
|
213 |
totalRx->SetKey("wifi-rx-frames"); |
|
214 |
Config::Connect("/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/Rx", |
|
215 |
MakeCallback(&PacketCounterCalculator::FrameUpdate, |
|
216 |
totalRx)); |
|
217 |
data.AddDataCalculator(totalRx); |
|
218 |
||
219 |
||
220 |
||
221 |
||
222 |
// This counter tracks how many packets---as opposed to frames---are |
|
223 |
// generated. This is connected directly to a trace signal provided |
|
224 |
// by our Sender class. |
|
225 |
Ptr<PacketCounterCalculator> appTx = |
|
226 |
CreateObject<PacketCounterCalculator>(); |
|
227 |
appTx->SetKey("sender-tx-packets"); |
|
228 |
Config::Connect("/NodeList/0/ApplicationList/*/$Sender/Tx", |
|
229 |
MakeCallback(&PacketCounterCalculator::PacketUpdate, |
|
230 |
appTx)); |
|
231 |
data.AddDataCalculator(appTx); |
|
232 |
||
233 |
// Here a counter for received packets is directly manipulated by |
|
234 |
// one of the custom objects in our simulation, the Receiver |
|
235 |
// Application. The Receiver object is given a pointer to the |
|
236 |
// counter and calls its Update() method whenever a packet arrives. |
|
237 |
Ptr<CounterCalculator<> > appRx = |
|
238 |
CreateObject<CounterCalculator<> >(); |
|
239 |
appRx->SetKey("receiver-rx-packets"); |
|
240 |
receiver->SetCounter(appRx); |
|
241 |
data.AddDataCalculator(appRx); |
|
242 |
||
243 |
||
244 |
||
245 |
||
246 |
/** |
|
247 |
* Just to show this is here... |
|
248 |
Ptr<MinMaxAvgTotalCalculator<uint32_t> > test = |
|
249 |
CreateObject<MinMaxAvgTotalCalculator<uint32_t> >(); |
|
250 |
test->SetKey("test-dc"); |
|
251 |
data.AddDataCalculator(test); |
|
252 |
||
253 |
test->Update(4); |
|
254 |
test->Update(8); |
|
255 |
test->Update(24); |
|
256 |
test->Update(12); |
|
257 |
**/ |
|
258 |
||
259 |
// This DataCalculator connects directly to the transmit trace |
|
260 |
// provided by our Sender Application. It records some basic |
|
261 |
// statistics about the sizes of the packets received (min, max, |
|
262 |
// avg, total # bytes), although in this scenaro they're fixed. |
|
263 |
Ptr<PacketSizeMinMaxAvgTotalCalculator> appTxPkts = |
|
264 |
CreateObject<PacketSizeMinMaxAvgTotalCalculator>(); |
|
265 |
appTxPkts->SetKey("tx-pkt-size"); |
|
266 |
Config::Connect("/NodeList/0/ApplicationList/*/$Sender/Tx", |
|
267 |
MakeCallback |
|
268 |
(&PacketSizeMinMaxAvgTotalCalculator::PacketUpdate, |
|
269 |
appTxPkts)); |
|
270 |
data.AddDataCalculator(appTxPkts); |
|
271 |
||
272 |
||
273 |
// Here we directly manipulate another DataCollector tracking min, |
|
274 |
// max, total, and average propagation delays. Check out the Sender |
|
275 |
// and Receiver classes to see how packets are tagged with |
|
276 |
// timestamps to do this. |
|
277 |
Ptr<TimeMinMaxAvgTotalCalculator> delayStat = |
|
278 |
CreateObject<TimeMinMaxAvgTotalCalculator>(); |
|
279 |
delayStat->SetKey("delay"); |
|
280 |
receiver->SetDelayTracker(delayStat); |
|
281 |
data.AddDataCalculator(delayStat); |
|
282 |
||
283 |
||
284 |
||
285 |
||
286 |
//------------------------------------------------------------ |
|
287 |
//-- Run the simulation |
|
288 |
//-------------------------------------------- |
|
289 |
NS_LOG_INFO("Run Simulation."); |
|
290 |
Simulator::Run(); |
|
291 |
Simulator::Destroy(); |
|
292 |
||
293 |
||
294 |
||
295 |
||
296 |
//------------------------------------------------------------ |
|
297 |
//-- Generate statistics output. |
|
298 |
//-------------------------------------------- |
|
299 |
||
300 |
// Pick an output writer based in the requested format. |
|
301 |
Ptr<DataOutputInterface> output = 0; |
|
302 |
if (format == "omnet") { |
|
303 |
NS_LOG_INFO("Creating omnet formatted data output."); |
|
304 |
output = CreateObject<OmnetDataOutput>(); |
|
305 |
} else if (format == "db") { |
|
306 |
#ifdef STATS_HAS_SQLITE3 |
|
307 |
NS_LOG_INFO("Creating sqlite formatted data output."); |
|
308 |
output = CreateObject<SqliteDataOutput>(); |
|
309 |
#endif |
|
310 |
} else { |
|
311 |
NS_LOG_ERROR("Unknown output format " << format); |
|
312 |
} |
|
313 |
||
314 |
// Finally, have that writer interrogate the DataCollector and save |
|
315 |
// the results. |
|
316 |
if (output != 0) |
|
317 |
output->Output(data); |
|
318 |
||
319 |
// end main |
|
320 |
} |