4470
|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* Copyright (c) 2009 The Boeing Company
|
|
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: Guangyu Pei <guangyu.pei@boeing.com>
|
|
19 |
*/
|
|
20 |
|
|
21 |
#include "ns3/core-module.h"
|
|
22 |
#include "ns3/common-module.h"
|
|
23 |
#include "ns3/node-module.h"
|
|
24 |
#include "ns3/helper-module.h"
|
|
25 |
#include "ns3/mobility-module.h"
|
|
26 |
#include "ns3/contrib-module.h"
|
|
27 |
|
|
28 |
#include <iostream>
|
|
29 |
#include <fstream>
|
|
30 |
#include <vector>
|
|
31 |
#include <string>
|
|
32 |
|
|
33 |
NS_LOG_COMPONENT_DEFINE ("Main");
|
|
34 |
|
|
35 |
using namespace ns3;
|
|
36 |
|
|
37 |
class Experiment
|
|
38 |
{
|
|
39 |
public:
|
|
40 |
Experiment ();
|
|
41 |
Experiment (std::string name);
|
|
42 |
uint32_t Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
|
|
43 |
const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
|
|
44 |
private:
|
|
45 |
void ReceivePacket (Ptr<Socket> socket);
|
|
46 |
void SetPosition (Ptr<Node> node, Vector position);
|
|
47 |
Vector GetPosition (Ptr<Node> node);
|
|
48 |
Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
|
|
49 |
void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
|
|
50 |
uint32_t pktCount, Time pktInterval );
|
|
51 |
|
|
52 |
uint32_t m_pktsTotal;
|
|
53 |
Gnuplot2dDataset m_output;
|
|
54 |
};
|
|
55 |
|
|
56 |
Experiment::Experiment ()
|
|
57 |
{}
|
|
58 |
|
|
59 |
Experiment::Experiment (std::string name)
|
|
60 |
: m_output (name)
|
|
61 |
{
|
|
62 |
m_output.SetStyle (Gnuplot2dDataset::LINES);
|
|
63 |
}
|
|
64 |
|
|
65 |
void
|
|
66 |
Experiment::SetPosition (Ptr<Node> node, Vector position)
|
|
67 |
{
|
|
68 |
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
|
|
69 |
mobility->SetPosition (position);
|
|
70 |
}
|
|
71 |
|
|
72 |
Vector
|
|
73 |
Experiment::GetPosition (Ptr<Node> node)
|
|
74 |
{
|
|
75 |
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
|
|
76 |
return mobility->GetPosition ();
|
|
77 |
}
|
|
78 |
|
|
79 |
void
|
|
80 |
Experiment::ReceivePacket (Ptr<Socket> socket)
|
|
81 |
{
|
|
82 |
Ptr<Packet> packet;
|
|
83 |
while (packet = socket->Recv ())
|
|
84 |
{
|
|
85 |
m_pktsTotal ++;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
Ptr<Socket>
|
|
90 |
Experiment::SetupPacketReceive (Ptr<Node> node)
|
|
91 |
{
|
|
92 |
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
|
|
93 |
Ptr<Socket> sink = Socket::CreateSocket (node, tid);
|
|
94 |
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
|
|
95 |
sink->Bind (local);
|
|
96 |
sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
|
|
97 |
return sink;
|
|
98 |
}
|
|
99 |
|
|
100 |
void
|
|
101 |
Experiment::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
|
|
102 |
uint32_t pktCount, Time pktInterval )
|
|
103 |
{
|
|
104 |
if (pktCount > 0)
|
|
105 |
{
|
|
106 |
socket->Send (Create<Packet> (pktSize));
|
|
107 |
Simulator::Schedule (pktInterval, &Experiment::GenerateTraffic, this,
|
|
108 |
socket, pktSize,pktCount-1, pktInterval);
|
|
109 |
}
|
|
110 |
else
|
|
111 |
{
|
|
112 |
socket->Close ();
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
uint32_t
|
|
117 |
Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
|
|
118 |
const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
|
|
119 |
{
|
|
120 |
m_pktsTotal = 0;
|
|
121 |
|
|
122 |
NodeContainer c;
|
|
123 |
c.Create (2);
|
|
124 |
|
|
125 |
InternetStackHelper internet;
|
|
126 |
internet.Install (c);
|
|
127 |
|
|
128 |
YansWifiPhyHelper phy = wifiPhy;
|
|
129 |
phy.SetChannel (wifiChannel.Create ());
|
|
130 |
|
|
131 |
NqosWifiMacHelper mac = wifiMac;
|
|
132 |
NetDeviceContainer devices = wifi.Install (phy, mac, c);
|
|
133 |
|
|
134 |
MobilityHelper mobility;
|
|
135 |
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
|
|
136 |
positionAlloc->Add (Vector (0.0, 0.0, 0.0));
|
|
137 |
positionAlloc->Add (Vector (5.0, 0.0, 0.0));
|
|
138 |
mobility.SetPositionAllocator (positionAlloc);
|
|
139 |
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
|
|
140 |
mobility.Install (c);
|
|
141 |
|
|
142 |
Ipv4AddressHelper ipv4;
|
|
143 |
NS_LOG_INFO ("Assign IP Addresses.");
|
|
144 |
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
|
|
145 |
Ipv4InterfaceContainer i = ipv4.Assign (devices);
|
|
146 |
|
|
147 |
Ptr<Socket> recvSink = SetupPacketReceive (c.Get (0));
|
|
148 |
|
|
149 |
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
|
|
150 |
Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
|
|
151 |
InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
|
|
152 |
source->Connect (remote);
|
|
153 |
uint32_t packetSize = 1014;
|
|
154 |
uint32_t maxPacketCount = 200;
|
|
155 |
Time interPacketInterval = Seconds (1.);
|
|
156 |
Simulator::Schedule (Seconds (1.0), &Experiment::GenerateTraffic,
|
|
157 |
this, source, packetSize, maxPacketCount,interPacketInterval);
|
|
158 |
Simulator::Run ();
|
|
159 |
|
|
160 |
Simulator::Destroy ();
|
|
161 |
|
|
162 |
return m_pktsTotal;
|
|
163 |
}
|
|
164 |
|
|
165 |
int main (int argc, char *argv[])
|
|
166 |
{
|
|
167 |
std::ofstream outfile ("clear-channel.plt");
|
|
168 |
std::vector <std::string> modes;
|
|
169 |
|
|
170 |
modes.push_back ("wifib-1mbs");
|
|
171 |
modes.push_back ("wifib-2mbs");
|
|
172 |
modes.push_back ("wifib-5.5mbs");
|
|
173 |
modes.push_back ("wifib-11mbs");
|
|
174 |
// disable fragmentation
|
|
175 |
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
|
|
176 |
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
|
|
177 |
|
|
178 |
CommandLine cmd;
|
|
179 |
cmd.Parse (argc, argv);
|
|
180 |
|
|
181 |
Gnuplot gnuplot = Gnuplot ("clear-channel.eps");
|
|
182 |
|
|
183 |
for (uint32_t i = 0; i < modes.size(); i++)
|
|
184 |
{
|
|
185 |
std::cout << modes[i] << std::endl;
|
|
186 |
Gnuplot2dDataset dataset (modes[i]);
|
|
187 |
|
|
188 |
for (double rss = -102.0; rss <= -80.0; rss += 0.5)
|
|
189 |
{
|
|
190 |
Experiment experiment;
|
|
191 |
dataset.SetStyle (Gnuplot2dDataset::LINES);
|
|
192 |
|
|
193 |
WifiHelper wifi;
|
|
194 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
|
|
195 |
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
|
|
196 |
StringValue (modes[i]));
|
|
197 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
|
|
198 |
"DataMode",StringValue(modes[i]),
|
|
199 |
"ControlMode",StringValue(modes[i]));
|
|
200 |
wifiMac.SetType ("ns3::AdhocWifiMac");
|
|
201 |
|
|
202 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
|
|
203 |
YansWifiChannelHelper wifiChannel ;
|
|
204 |
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
|
|
205 |
wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue(rss));
|
|
206 |
|
|
207 |
|
|
208 |
NS_LOG_DEBUG (modes[i]);
|
|
209 |
experiment = Experiment (modes[i]);
|
|
210 |
wifiPhy.Set ("Standard", StringValue ("802.11b") );
|
|
211 |
wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-110.0) );
|
|
212 |
wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-110.0) );
|
|
213 |
wifiPhy.Set ("TxPowerStart", DoubleValue (15.0) );
|
|
214 |
wifiPhy.Set ("RxGain", DoubleValue (0) );
|
|
215 |
wifiPhy.Set ("RxNoiseFigure", DoubleValue (7) );
|
|
216 |
uint32_t pktsRecvd = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
|
|
217 |
dataset.Add (rss, pktsRecvd);
|
|
218 |
}
|
|
219 |
|
|
220 |
gnuplot.AddDataset (dataset);
|
|
221 |
}
|
|
222 |
gnuplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
|
|
223 |
gnuplot.SetLegend ("RSS(dBm)", "Number of packets received");
|
|
224 |
gnuplot.SetExtra ("set xrange [-102:-83]");
|
|
225 |
gnuplot.GenerateOutput (outfile);
|
|
226 |
outfile.close ();
|
|
227 |
|
|
228 |
return 0;
|
|
229 |
}
|