-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifitest.cc
261 lines (215 loc) · 7.14 KB
/
wifitest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* wifitest.cc
*
* Created on: Nov 18, 2015
* Author: ufam
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/stats-module.h"
#include "ns3/wifi-module.h"
#include "ns3/netanim-module.h"
#include "Vehicle.h"
#include <iostream>
double speedA = 40;
double speedB = 60;
double speedC = 80;
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("Main");
class Experiment
{
public:
Experiment ();
Experiment (std::string name);
Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
private:
void ReceivePacket (Ptr<Socket> socket);
void SetPosition (Ptr<Node> node, Vector position);
Vector GetPosition (Ptr<Node> node);
void AdvancePositionX (Ptr<Node> node, double* vel, double finalPosition);
void AdvancePositionY (Ptr<Node> node, double* vel, double finalPosition);
Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
uint32_t m_bytesTotal;
Gnuplot2dDataset m_output;
};
Experiment::Experiment ()
{
}
Experiment::Experiment (std::string name)
: m_output (name)
{
m_output.SetStyle (Gnuplot2dDataset::LINES);
}
//void Leave(const uint32_t& my_id) {
// Ptr<MobilityModel> mobility = Config::Instance()->GetMobileNodes().Get(my_id)->GetObject<MobilityModel>();
// NS_ASSERT(mobility);
// mobility->SetPosition(Vector(mobility->GetPosition().x, mobility->GetPosition().y, 384e6));
// nodes[my_id].on_leave = true;
//}
void
Experiment::SetPosition (Ptr<Node> node, Vector position)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
mobility->SetPosition (position);
}
Vector
Experiment::GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition ();
}
void
Experiment::AdvancePositionX (Ptr<Node> node, double* vel, double finalPosition)
{
//speed in kph
double speed=*vel;
double time;
//advance position in meters
double advance = 0.1;
//time = advance/(speedA/3.6);
Vector pos = GetPosition (node);
// Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
// ns3::Ptr<const ns3::MobilityModel> p = pos;
// double d = mobility->GetDistanceFrom(p);
// std::cout << "distance="<<d << std::endl;
double mbs = ((m_bytesTotal * 8.0) / 1000000);
m_bytesTotal = 0;
m_output.Add (pos.x, mbs);
pos.x += advance;
time = advance/(speed/3.6);
if (pos.x >= finalPosition)
{
SetPosition (node, Vector(384e6, 384e6, 384e6));
return;
}
SetPosition (node, pos);
//std::cout << "x="<<pos.x << std::endl;
Simulator::Schedule (Seconds (time), &Experiment::AdvancePositionX, this, node,vel,finalPosition);
}
void
Experiment::AdvancePositionY (Ptr<Node> node, double* vel, double finalPosition)
{
//speed in kph
double speed=*vel;
double time;
//advance position in meters
double advance = 0.1;
time = advance/(speed/3.6);
Vector pos = GetPosition (node);
double mbs = ((m_bytesTotal * 8.0) / 1000000);
m_bytesTotal = 0;
m_output.Add (pos.y, mbs);
pos.y -= advance;
if (pos.y <= 0.0)
{
pos.y = 0.0;
speed = 35;
pos.x += advance;
time = advance/(speed/3.6);
if (pos.x >= finalPosition)
{
SetPosition (node, Vector(384e6, 384e6, 384e6));
return;
}
//SetPosition (node, pos);
//return;
}
SetPosition (node, pos);
//std::cout << "x="<<pos.x << std::endl;
Simulator::Schedule (Seconds (time), &Experiment::AdvancePositionY, this, node,vel,finalPosition);
}
void
Experiment::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;
while ((packet = socket->Recv ()))
{
m_bytesTotal += packet->GetSize ();
}
}
Ptr<Socket>
Experiment::SetupPacketReceive (Ptr<Node> node)
{
TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
Ptr<Socket> sink = Socket::CreateSocket (node, tid);
sink->Bind ();
sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
return sink;
}
Gnuplot2dDataset
Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
{
double velA = 40.0;
double velB = 45.0;
double velC = 80.0;
double finalPositionA = 100;
double finalPositionB = 100;
double finalPositionC = 100;
Ptr<Vehicle> vehicle;
m_bytesTotal = 0;
NodeContainer c;
c.Create (4);
vehicle.m_node=c.Get(0);
PacketSocketHelper packetSocket;
packetSocket.Install (c);
YansWifiPhyHelper phy = wifiPhy;
phy.SetChannel (wifiChannel.Create ());
NqosWifiMacHelper mac = wifiMac;
NetDeviceContainer devices = wifi.Install (phy, mac, c);
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
positionAlloc->Add (Vector (0.0, 0.0, 0.0));
positionAlloc->Add (Vector (10.0, 0.0, 0.0));
positionAlloc->Add (Vector (50.0, -10.0, 0.0));
positionAlloc->Add (Vector (50.0, 100.0, 0.0));
mobility.SetPositionAllocator (positionAlloc);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (c);
PacketSocketAddress socket;
socket.SetSingleDevice (devices.Get (1)->GetIfIndex ());
socket.SetPhysicalAddress (devices.Get (2)->GetAddress ());
socket.SetProtocol (1);
OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
onoff.SetConstantRate (DataRate (60000000));
onoff.SetAttribute ("PacketSize", UintegerValue (2000));
ApplicationContainer apps = onoff.Install (c);
apps.Start (Seconds (0.0));
apps.Stop (Seconds (500.0));
Simulator::Schedule (Seconds (0.0), &Experiment::AdvancePositionX, this, c.Get (1), &velA, finalPositionA);
Simulator::Schedule (Seconds (3.0), &Experiment::AdvancePositionX, this, c.Get (0), &velB, finalPositionB);
Simulator::Schedule (Seconds (0.0), &Experiment::AdvancePositionY, this, c.Get (3), &velC, finalPositionC);
Ptr<Socket> recvSink = SetupPacketReceive (c.Get (2));
Simulator::Run ();
Simulator::Destroy ();
return m_output;
}
int main (int argc, char *argv[])
{
// disable fragmentation
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
CommandLine cmd;
cmd.Parse (argc, argv);
Gnuplot gnuplot = Gnuplot ("reference-rates.png");
Experiment experiment;
//set the technology to communicate
WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
Gnuplot2dDataset dataset;
wifiMac.SetType ("ns3::AdhocWifiMac");
NS_LOG_DEBUG ("54");
experiment = Experiment ("54mb");
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("OfdmRate54Mbps"));
dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
gnuplot.AddDataset (dataset);
gnuplot.GenerateOutput (std::cout);
return 0;
}