-
Notifications
You must be signed in to change notification settings - Fork 17
/
mesh-handover.cc
269 lines (237 loc) · 10.7 KB
/
mesh-handover.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
262
263
264
265
266
267
268
269
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* Net topology
*
* With a 3x3 mesh grid:
*
*
* ((( n1 )))
*
* n0 ))) n3
*
* ((( n2 )))
*
*
* n0: internet node
* n1: access node
* n2: access node
* n3: mesh node
*
* n1 has 2 interfaces (P2P and wireless)
* n2 has 2 interfaces (P2P and wireless)
* n0 has ip 1.1.1.1 to emulate internet access
* n1 has ip 1.1.1.2 and 10.0.0.1
* n2 has ip 1.1.1.3 and 10.0.0.2
* n3 has ip 10.0.0.3
*
*/
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/v4ping-helper.h"
#include "ns3/olsr-helper.h"
#include "ns3/aodv-helper.h"
#include "src/core/model/string.h"
// Needed for mesh protocol stack
#include "ns3/mesh-helper.h"
// Needed for setting the mobility
#include "myapp.h"
using namespace ns3;
static void
SetPosition (Ptr<Node> node, double x, double y)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
Vector pos = mobility->GetPosition();
pos.x = x;
pos.y = y;
mobility->SetPosition(pos);
}
//
//void
//ReceivePacket(Ptr<const Packet> p, const Address & addr)
//{
// std::cout << Simulator::Now ().GetSeconds () << "\t" << p->GetSize() <<"\n";
//}
int
main (int argc, char *argv[])
{
ns3::PacketMetadata::Enable ();
NS_LOG_COMPONENT_DEFINE ("mesh-handover");
//LogComponentEnable ("V4Ping", LOG_LEVEL_DEBUG);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// LogComponentEnable ("AodvRoutingProtocol", LOG_LEVEL_DEBUG);
// LogComponentEnable ("YansWifiPhy", LOG_LEVEL_DEBUG);
// LogComponentEnableAll (LOG_LEVEL_DEBUG);
NodeContainer nc_all; // Contains every node in order
NodeContainer nc_p2p; // Contains n0, n1 and n2 nodes
NodeContainer nc_mesh; // Contains n1, n2 and n3 nodes
nc_all.Create (4);
// Contains n0, n1 and n2
nc_p2p.Add (nc_all.Get (0));
nc_p2p.Add (nc_all.Get (1));
nc_p2p.Add (nc_all.Get (2));
// Contains n1, n2 and n3
nc_mesh.Add (nc_all.Get (1));
nc_mesh.Add (nc_all.Get (2));
nc_mesh.Add (nc_all.Get (3));
#if 0 // Initialization for mesh protocol stack
std::string m_stack = "ns3::Dot11sStack";
std::string m_root = "ff:ff:ff:ff:ff:ff";
bool m_chan = true;
double m_randomStart = 0.1;
uint32_t m_nIfaces = 1;
MeshHelper meshHelper;
#endif
/* PointToPointHelper p2ph; // Set p2p link between node 0 and 1
p2ph.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));
//p2ph.SetChannelAttribute ("DataRate", StringValue ("1000000"));
p2ph.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
NetDeviceContainer de_p2p = p2ph.Install (nc_p2p);*/
WifiHelper wifiInternet; // Set up extra wifi internet interface
YansWifiPhyHelper wifiInternetPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiInternetChannel = YansWifiChannelHelper::Default ();
NqosWifiMacHelper wifiInternetMac = NqosWifiMacHelper::Default ();
wifiInternet.SetStandard (WIFI_PHY_STANDARD_80211a);
wifiInternet.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("OfdmRate6Mbps"),
"ControlMode", StringValue ("OfdmRate6Mbps"));
wifiInternetPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
wifiInternetChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (100));
wifiInternetMac.SetType ("ns3::AdhocWifiMac");
wifiInternetPhy.SetChannel (wifiInternetChannel.Create ());
/* wifiPhy.Set ("TxPowerStart", DoubleValue(16.0206));
wifiPhy.Set ("TxPowerEnd", DoubleValue(16.0206));
wifiPhy.Set ("TxPowerLevels", UintegerValue(1));
wifiPhy.Set ("TxGain", DoubleValue(0) );
wifiPhy.Set ("RxGain", DoubleValue (0) );*/
NetDeviceContainer p2pDevice = wifiInternet.Install (wifiInternetPhy, wifiInternetMac, nc_p2p);
WifiHelper wifiMesh; // Set up wifi adhoc interface for mesh nodes
YansWifiPhyHelper wifiMeshPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiMeshChannel = YansWifiChannelHelper::Default ();
NqosWifiMacHelper wifiMesMac = NqosWifiMacHelper::Default ();
wifiMesh.SetStandard (WIFI_PHY_STANDARD_80211a);
wifiMesh.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("OfdmRate6Mbps"),
"ControlMode", StringValue ("OfdmRate6Mbps"));
wifiMeshPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
wifiMeshChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (100));
wifiMesMac.SetType ("ns3::AdhocWifiMac");
wifiMeshPhy.SetChannel (wifiMeshChannel.Create ());
/* wifiPhy.Set ("TxPowerStart", DoubleValue(16.0206));
wifiPhy.Set ("TxPowerEnd", DoubleValue(16.0206));
wifiPhy.Set ("TxPowerLevels", UintegerValue(1));
wifiPhy.Set ("TxGain", DoubleValue(0) );
wifiPhy.Set ("RxGain", DoubleValue (0) );*/
NetDeviceContainer meshDevice = wifiMesh.Install (wifiMeshPhy, wifiMesMac, nc_mesh);
#if 0 // MeshHelper
meshHelper = MeshHelper::Default ();
if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
{
meshHelper.SetStackInstaller (m_stack, "Root", Mac48AddressValue (Mac48Address (m_root.c_str ())));
}
else
{
//If root is not set, we do not use "Root" attribute, because it
//is specified only for 11s
meshHelper.SetStackInstaller (m_stack);
}
if (m_chan)
{
meshHelper.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
}
else
{
meshHelper.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
}
meshHelper.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
// Set number of interfaces - default is single-interface meshHelper point
meshHelper.SetNumberOfInterfaces (m_nIfaces);
// Install protocols and return container if MeshPointDevices
meshDevice = meshHelper.Install (wifiMeshPhy, nc_mesh);
#endif
// // Set mobility for the internet node and access points
// MobilityHelper constantMobility; //Position nodes
// constantMobility.SetPositionAllocator ("ns3::GridPositionAllocator",
// "MinX", DoubleValue (0),
// "MinY", DoubleValue (0),
// "DeltaX", DoubleValue (75),
// "DeltaY", DoubleValue (75),
// "GridWidth", UintegerValue (5),
// "LayoutType", StringValue ("RowFirst"));
// constantMobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
// constantMobility.Install (nc_p2p);
//
// // Set mobility for mesh node
// MobilityHelper randomMobility; //Position nodes
// randomMobility.SetPositionAllocator ("ns3::GridPositionAllocator",
// "MinX", DoubleValue (0),
// "MinY", DoubleValue (0),
// "DeltaX", DoubleValue (75),
// "DeltaY", DoubleValue (75),
// "GridWidth", UintegerValue (5),
// "LayoutType", StringValue ("RowFirst"));
// randomMobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-500, 500, -500, 500)),
// "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
// "Mode", StringValue("Time"));
// randomMobility.Install (nc_mesh.Get (2));
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
positionAlloc ->Add(Vector(150, 37, 0)); // node0
positionAlloc ->Add(Vector(75, 0, 0)); // node1
positionAlloc ->Add(Vector(75, 75, 0)); // node2
positionAlloc ->Add(Vector(0, 0, 0)); // node3
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nc_all);
// Set new position for node 3 (client)
Simulator::Schedule (Seconds (9.0), &SetPosition, nc_all.Get (3), 0.0, 75.0);
InternetStackHelper internetStack; //Config IP, addresses and routing protocol
OlsrHelper routingProtocol;
internetStack.SetRoutingHelper (routingProtocol);
internetStack.Install (nc_all);
Ipv4AddressHelper addrMesh;
addrMesh.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer meshInterface;
meshInterface = addrMesh.Assign (meshDevice);
Ipv4AddressHelper addrInternet;
addrInternet.SetBase ("1.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterface;
p2pInterface = addrInternet.Assign (p2pDevice);
/*V4PingHelper ping1 (if_p2p.GetAddress (0)); // Install App ping - from mesh node to internet
ping1.SetAttribute ("Verbose", BooleanValue (true));
ping1.SetAttribute ("Interval", TimeValue (Seconds (5.0)));
ApplicationContainer appPingInternet1 = ping1.Install (nc_all.Get(2));
appPingInternet1.Start (Seconds (4));
appPingInternet1.Stop (Seconds (25));*/
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nc_all.Get (0)); // Server is on n0 node
serverApps.Start (Seconds (7.0));
serverApps.Stop (Seconds (50.0));
UdpEchoClientHelper echoClient (p2pInterface.GetAddress (0), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (25));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nc_all.Get (3)); // Client is on n3 node
clientApps.Start (Seconds (7.0));
clientApps.Stop (Seconds (50.0));
Simulator::Stop (Seconds (50));
AnimationInterface animation ("mesh-handover.xml");
// // Set position for the nodes
// animation.SetConstantPosition (nc_all.Get (0), 150.0, 37.0); // n0 -> internet node
// animation.SetConstantPosition (nc_all.Get (1), 75.0, 0.0); // n1 -> AP
// animation.SetConstantPosition (nc_all.Get (2), 75.0, 75.0); // n2 -> AP
// animation.SetConstantPosition (nc_all.Get (3), 0.0, 0.0); // n3 -> Mesh Node
animation.EnablePacketMetadata (true);
animation.EnableIpv4RouteTracking ("mesh-handover-route.xml", Seconds (0), Seconds (10), Seconds (0.25));
//Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}