author | Junling Bu <linlinjavaer@gmail.com> |
Tue, 09 Jul 2013 21:13:38 +0200 | |
changeset 9916 | 725d7c8811b8 |
parent 8997 | 9222fc5291ca |
child 10157 | 02e3d2d7d7e1 |
permissions | -rw-r--r-- |
6116 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright (c) 2010 IITP RAS |
|
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 |
* Authors: Pavel Boyko <boyko@iitp.ru> |
|
19 |
*/ |
|
20 |
||
21 |
/* |
|
22 |
* Classical hidden terminal problem and its RTS/CTS solution. |
|
23 |
* |
|
24 |
* Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2] |
|
25 |
* |
|
26 |
* This example illustrates the use of |
|
27 |
* - Wifi in ad-hoc mode |
|
28 |
* - Matrix propagation loss model |
|
29 |
* - Use of OnOffApplication to generate CBR stream |
|
30 |
* - IP flow monitor |
|
31 |
*/ |
|
32 |
#include "ns3/core-module.h" |
|
6795
7dfe87d4b790
fix example and sample dependencies
Tom Henderson <tomh@tomh.org>
parents:
6664
diff
changeset
|
33 |
#include "ns3/propagation-module.h" |
6823
a27f86fb4e55
Merge node and common modules into new network module
Tom Henderson <tomh@tomh.org>
parents:
6803
diff
changeset
|
34 |
#include "ns3/network-module.h" |
6847
138f00c56381
Move applications to a single module
Mitch Watrous <watrous@u.washington.edu>
parents:
6823
diff
changeset
|
35 |
#include "ns3/applications-module.h" |
6116 | 36 |
#include "ns3/mobility-module.h" |
6848
1f453ad50ef3
Converts csma, emu, tap-bridge, point-to-point, wifi and wimax modules into modular format
Lalith Suresh <suresh.lalith@gmail.com>
parents:
6847
diff
changeset
|
37 |
#include "ns3/internet-module.h" |
6116 | 38 |
#include "ns3/flow-monitor-module.h" |
6848
1f453ad50ef3
Converts csma, emu, tap-bridge, point-to-point, wifi and wimax modules into modular format
Lalith Suresh <suresh.lalith@gmail.com>
parents:
6847
diff
changeset
|
39 |
#include "ns3/wifi-module.h" |
6116 | 40 |
|
41 |
using namespace ns3; |
|
42 |
||
43 |
/// Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism |
|
44 |
void experiment (bool enableCtsRts) |
|
45 |
{ |
|
46 |
// 0. Enable or disable CTS/RTS |
|
47 |
UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200)); |
|
48 |
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
49 |
|
6116 | 50 |
// 1. Create 3 nodes |
51 |
NodeContainer nodes; |
|
52 |
nodes.Create (3); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
53 |
|
6116 | 54 |
// 2. Place nodes somehow, this is required by every wireless simulation |
55 |
for (size_t i = 0; i < 3; ++i) |
|
56 |
{ |
|
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7196
diff
changeset
|
57 |
nodes.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ()); |
6116 | 58 |
} |
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
59 |
|
6116 | 60 |
// 3. Create propagation loss matrix |
61 |
Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel> (); |
|
62 |
lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link) |
|
6803
f07c4f00b07e
Bug 1048: MatrixPropagationLossModel API changed from Ptr<Object> to Ptr<MobilityModel>
Pavel Boyko <boyko@iitp.ru>
parents:
6795
diff
changeset
|
63 |
lossModel->SetLoss (nodes.Get (0)->GetObject<MobilityModel>(), nodes.Get (1)->GetObject<MobilityModel>(), 50); // set symmetric loss 0 <-> 1 to 50 dB |
f07c4f00b07e
Bug 1048: MatrixPropagationLossModel API changed from Ptr<Object> to Ptr<MobilityModel>
Pavel Boyko <boyko@iitp.ru>
parents:
6795
diff
changeset
|
64 |
lossModel->SetLoss (nodes.Get (2)->GetObject<MobilityModel>(), nodes.Get (1)->GetObject<MobilityModel>(), 50); // set symmetric loss 2 <-> 1 to 50 dB |
6116 | 65 |
|
66 |
// 4. Create & setup wifi channel |
|
67 |
Ptr<YansWifiChannel> wifiChannel = CreateObject <YansWifiChannel> (); |
|
68 |
wifiChannel->SetPropagationLossModel (lossModel); |
|
69 |
wifiChannel->SetPropagationDelayModel (CreateObject <ConstantSpeedPropagationDelayModel> ()); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
70 |
|
6116 | 71 |
// 5. Install wireless devices |
72 |
WifiHelper wifi; |
|
6664 | 73 |
wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
6116 | 74 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
6360
d8975477ff6a
Bug 871: Rework construction of Wi-Fi rates
Dean Armstrong <deanarm@gmail.com>
parents:
6116
diff
changeset
|
75 |
"DataMode",StringValue ("DsssRate2Mbps"), |
d8975477ff6a
Bug 871: Rework construction of Wi-Fi rates
Dean Armstrong <deanarm@gmail.com>
parents:
6116
diff
changeset
|
76 |
"ControlMode",StringValue ("DsssRate1Mbps")); |
6116 | 77 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
78 |
wifiPhy.SetChannel (wifiChannel); |
|
79 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
|
80 |
wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC |
|
81 |
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes); |
|
6664 | 82 |
|
83 |
// uncomment the following to have athstats output |
|
84 |
// AthstatsHelper athstats; |
|
85 |
// athstats.EnableAthstats(enableCtsRts ? "basic-athstats-node" : "rtscts-athstats-node", nodes); |
|
86 |
||
87 |
// uncomment the following to have pcap output |
|
88 |
//wifiPhy.EnablePcap (enableCtsRts ? "basic-pcap-node" : "rtscts-pcap-node", nodes); |
|
89 |
||
90 |
||
6116 | 91 |
// 6. Install TCP/IP stack & assign IP addresses |
92 |
InternetStackHelper internet; |
|
93 |
internet.Install (nodes); |
|
94 |
Ipv4AddressHelper ipv4; |
|
95 |
ipv4.SetBase ("10.0.0.0", "255.0.0.0"); |
|
96 |
ipv4.Assign (devices); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
97 |
|
6664 | 98 |
// 7. Install applications: two CBR streams each saturating the channel |
99 |
ApplicationContainer cbrApps; |
|
100 |
uint16_t cbrPort = 12345; |
|
101 |
OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address ("10.0.0.2"), cbrPort)); |
|
6116 | 102 |
onOffHelper.SetAttribute ("PacketSize", UintegerValue (200)); |
8997
9222fc5291ca
Replace more instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents:
7256
diff
changeset
|
103 |
onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); |
9222fc5291ca
Replace more instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents:
7256
diff
changeset
|
104 |
onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); |
6664 | 105 |
|
106 |
// flow 1: node 0 -> node 1 |
|
107 |
onOffHelper.SetAttribute ("DataRate", StringValue ("3000000bps")); |
|
108 |
onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.000000))); |
|
109 |
cbrApps.Add (onOffHelper.Install (nodes.Get (0))); |
|
110 |
||
111 |
// flow 2: node 2 -> node 1 |
|
112 |
// The slightly different start times and data rates are a workround |
|
113 |
// for Bug 388 and Bug 912 |
|
114 |
// http://www.nsnam.org/bugzilla/show_bug.cgi?id=912 |
|
115 |
// http://www.nsnam.org/bugzilla/show_bug.cgi?id=388 |
|
116 |
onOffHelper.SetAttribute ("DataRate", StringValue ("3001100bps")); |
|
117 |
onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.001))); |
|
118 |
cbrApps.Add (onOffHelper.Install (nodes.Get (2))); |
|
119 |
||
120 |
// we also use separate UDP applications that will send a single |
|
121 |
// packet before the CBR flows start. |
|
122 |
// This is a workround for the lack of perfect ARP, see Bug 187 |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
123 |
// http://www.nsnam.org/bugzilla/show_bug.cgi?id=187 |
6664 | 124 |
|
125 |
uint16_t echoPort = 9; |
|
126 |
UdpEchoClientHelper echoClientHelper (Ipv4Address ("10.0.0.2"), echoPort); |
|
127 |
echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (1)); |
|
128 |
echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (0.1))); |
|
129 |
echoClientHelper.SetAttribute ("PacketSize", UintegerValue (10)); |
|
130 |
ApplicationContainer pingApps; |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
131 |
|
6664 | 132 |
// again using different start times to workaround Bug 388 and Bug 912 |
133 |
echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.001))); |
|
134 |
pingApps.Add (echoClientHelper.Install (nodes.Get (0))); |
|
135 |
echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.006))); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
136 |
pingApps.Add (echoClientHelper.Install (nodes.Get (2))); |
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
137 |
|
6664 | 138 |
|
139 |
||
140 |
||
6116 | 141 |
// 8. Install FlowMonitor on all nodes |
142 |
FlowMonitorHelper flowmon; |
|
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7196
diff
changeset
|
143 |
Ptr<FlowMonitor> monitor = flowmon.InstallAll (); |
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
144 |
|
6116 | 145 |
// 9. Run simulation for 10 seconds |
146 |
Simulator::Stop (Seconds (10)); |
|
147 |
Simulator::Run (); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
148 |
|
6116 | 149 |
// 10. Print per flow statistics |
150 |
monitor->CheckForLostPackets (); |
|
151 |
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ()); |
|
152 |
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats (); |
|
153 |
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i) |
|
154 |
{ |
|
6664 | 155 |
// first 2 FlowIds are for ECHO apps, we don't want to display them |
156 |
if (i->first > 2) |
|
157 |
{ |
|
158 |
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); |
|
9916
725d7c8811b8
Bug 1675 - Throughput computation error in Wireless examples
Junling Bu <linlinjavaer@gmail.com>
parents:
8997
diff
changeset
|
159 |
std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; |
725d7c8811b8
Bug 1675 - Throughput computation error in Wireless examples
Junling Bu <linlinjavaer@gmail.com>
parents:
8997
diff
changeset
|
160 |
std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; |
6664 | 161 |
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; |
9916
725d7c8811b8
Bug 1675 - Throughput computation error in Wireless examples
Junling Bu <linlinjavaer@gmail.com>
parents:
8997
diff
changeset
|
162 |
std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 10.0 / 1000 / 1000 << " Mbps\n"; |
6664 | 163 |
} |
6116 | 164 |
} |
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
165 |
|
6116 | 166 |
// 11. Cleanup |
167 |
Simulator::Destroy (); |
|
168 |
} |
|
169 |
||
170 |
int main (int argc, char **argv) |
|
171 |
{ |
|
172 |
std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush; |
|
173 |
experiment (false); |
|
174 |
std::cout << "------------------------------------------------\n"; |
|
175 |
std::cout << "Hidden station experiment with RTS/CTS enabled:\n"; |
|
176 |
experiment (true); |
|
7196
0f12b1572bca
general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6865
diff
changeset
|
177 |
|
6116 | 178 |
return 0; |
179 |
} |