author | Tom Henderson <tomh@tomh.org> |
Fri, 20 Aug 2010 12:17:19 -0700 | |
changeset 6589 | 9c325569fb01 |
parent 6360 | d8975477ff6a |
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" |
|
33 |
#include "ns3/common-module.h" |
|
34 |
#include "ns3/node-module.h" |
|
35 |
#include "ns3/helper-module.h" |
|
36 |
#include "ns3/mobility-module.h" |
|
37 |
#include "ns3/flow-monitor-module.h" |
|
38 |
||
39 |
using namespace ns3; |
|
40 |
||
41 |
/// Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism |
|
42 |
void experiment (bool enableCtsRts) |
|
43 |
{ |
|
44 |
// 0. Enable or disable CTS/RTS |
|
45 |
UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200)); |
|
46 |
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr); |
|
47 |
||
48 |
// 1. Create 3 nodes |
|
49 |
NodeContainer nodes; |
|
50 |
nodes.Create (3); |
|
51 |
||
52 |
// 2. Place nodes somehow, this is required by every wireless simulation |
|
53 |
for (size_t i = 0; i < 3; ++i) |
|
54 |
{ |
|
55 |
nodes.Get(i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ()); |
|
56 |
} |
|
57 |
||
58 |
// 3. Create propagation loss matrix |
|
59 |
Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel> (); |
|
60 |
lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link) |
|
61 |
lossModel->SetLoss (nodes.Get (0), nodes.Get (1), 50); // set symmetric loss 0 <-> 1 to 50 dB |
|
62 |
lossModel->SetLoss (nodes.Get (2), nodes.Get (1), 50); // set symmetric loss 2 <-> 1 to 50 dB |
|
63 |
||
64 |
// 4. Create & setup wifi channel |
|
65 |
Ptr<YansWifiChannel> wifiChannel = CreateObject <YansWifiChannel> (); |
|
66 |
wifiChannel->SetPropagationLossModel (lossModel); |
|
67 |
wifiChannel->SetPropagationDelayModel (CreateObject <ConstantSpeedPropagationDelayModel> ()); |
|
68 |
||
69 |
// 5. Install wireless devices |
|
70 |
WifiHelper wifi; |
|
71 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
|
6360
d8975477ff6a
Bug 871: Rework construction of Wi-Fi rates
Dean Armstrong <deanarm@gmail.com>
parents:
6116
diff
changeset
|
72 |
"DataMode",StringValue ("DsssRate2Mbps"), |
d8975477ff6a
Bug 871: Rework construction of Wi-Fi rates
Dean Armstrong <deanarm@gmail.com>
parents:
6116
diff
changeset
|
73 |
"ControlMode",StringValue ("DsssRate1Mbps")); |
6116 | 74 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
75 |
wifiPhy.SetChannel (wifiChannel); |
|
76 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
|
77 |
wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC |
|
78 |
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes); |
|
79 |
||
80 |
// 6. Install TCP/IP stack & assign IP addresses |
|
81 |
InternetStackHelper internet; |
|
82 |
internet.Install (nodes); |
|
83 |
Ipv4AddressHelper ipv4; |
|
84 |
ipv4.SetBase ("10.0.0.0", "255.0.0.0"); |
|
85 |
ipv4.Assign (devices); |
|
86 |
||
87 |
// 7. Install applications: two dense CBR streams node 0 -> node 1 and node 2 -> node 1 |
|
88 |
ApplicationContainer apps; |
|
89 |
OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address ("10.0.0.2"), 9)); |
|
90 |
onOffHelper.SetAttribute ("DataRate", StringValue ("10Mbps")); |
|
91 |
onOffHelper.SetAttribute ("PacketSize", UintegerValue (200)); |
|
92 |
onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); |
|
93 |
onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); |
|
94 |
for (size_t i = 0; i < 3; i += 2) |
|
95 |
{ |
|
96 |
apps.Add (onOffHelper.Install (nodes.Get (i))); |
|
97 |
} |
|
98 |
||
99 |
// 8. Install FlowMonitor on all nodes |
|
100 |
FlowMonitorHelper flowmon; |
|
101 |
Ptr<FlowMonitor> monitor = flowmon.InstallAll(); |
|
102 |
||
103 |
// 9. Run simulation for 10 seconds |
|
104 |
Simulator::Stop (Seconds (10)); |
|
105 |
Simulator::Run (); |
|
106 |
||
107 |
// 10. Print per flow statistics |
|
108 |
monitor->CheckForLostPackets (); |
|
109 |
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ()); |
|
110 |
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats (); |
|
111 |
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i) |
|
112 |
{ |
|
113 |
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); |
|
114 |
std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; |
|
115 |
std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; |
|
116 |
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; |
|
117 |
std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 10.0 / 1024 / 1024 << " Mbps\n"; |
|
118 |
} |
|
119 |
||
120 |
// 11. Cleanup |
|
121 |
Simulator::Destroy (); |
|
122 |
} |
|
123 |
||
124 |
int main (int argc, char **argv) |
|
125 |
{ |
|
126 |
std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush; |
|
127 |
experiment (false); |
|
128 |
std::cout << "------------------------------------------------\n"; |
|
129 |
std::cout << "Hidden station experiment with RTS/CTS enabled:\n"; |
|
130 |
experiment (true); |
|
131 |
||
132 |
return 0; |
|
133 |
} |