-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp1.cc
283 lines (230 loc) · 9.31 KB
/
exp1.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#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"
// Default Network Topology
// Description:
// * and # indicate two routers using 802.11n 2.4GHz
// wifi standard and connected with csma protocol but
// different channel number, the routers of asterisk use
// channel 1 while another using channel number 6.
// Every pair of routes are at a distance of 50 meters
// in horizon and vertical direction.
//
// We use coordinate (1,1),(1,2) ... (5,5) to delegate
// them, choosing the asterisk routers as access point
// and choosing the # routers as four gateways by defaut
// indicated with sign G in (2,2), (2,4), (4,2), (4,4).
// Asterisk and sharp are a pair of routers, if a
// asterisk router has been choosed as the access point,
// then the sharp router connected with it will be the
// gateway. we choose the sharp routers of (3,3) as a
// sink node(SN) station associated with gateway, which
// is used to receieve traffic from gateways.
//
//
//
//
// *-# *-# *-# *-# *-#
//
//
//
// *-# A-G *-# A-G *-#
//
//
// SN
// *-# *-# *-# *-# *-#
//
//
//
// *-# A-G *-# A-G *-#
//
//
//
// *-# *-# *-# *-# *-#
//
//
//
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("EXP1");
int main(int argc, char *argv[])
{
uint16_t acNodeCount = 25;
uint16_t gwNodeCount = 25;
uint16_t acChannelNum = 1;
uint16_t gwChannelNum = 6;
uint16_t rowNum = 5;
uint16_t colNum = 5;
double distance = 10;
bool tracing = true;
bool verbose = true;
std::string phymode = "HtMcs0";
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("EXP1", LOG_LEVEL_INFO);
}
//ac_nc: all potential nodes that can be the ap(include ap)
//gw_nc: all potential nodes that can be the gateway(include gw)
//apNC : all ap nodes
//gwNC : all gw nodes
//staNC: all station nodes(except ap and middle node)
//snNC : the node which is used to receieve the traffic
NodeContainer ac_nc, gw_nc, apNC, gwNC, staNC, snNC;
ac_nc.Create(acNodeCount);
gw_nc.Create(gwNodeCount);
uint16_t i = 0;
//assign all nodes to different node container objects.
NS_LOG_INFO("assign all nodes to different node container objects.");
for(i=0; i<acNodeCount; i++){
if(i!=6 && i!=8 && i!=16 && i!=18 && i!=12){
staNC.Add(ac_nc.Get(i));
}else if(i!=12){
apNC.Add(ac_nc.Get(i));
gwNC.Add(gw_nc.Get(i));
}else{
snNC.Add(gw_nc.Get(i));
}
}
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211n_2_4GHZ);
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
"DataMode", StringValue(phymode),
"ControlMode", StringValue(phymode));
//************* Solve all the station and ap nodes *****************//
//
NS_LOG_INFO("Set wifi channel, mac and ssid of station and ap nodes");
WifiMacHelper acMac;
YansWifiPhyHelper acPhy = YansWifiPhyHelper::Default();
YansWifiChannelHelper acChannel = YansWifiChannelHelper::Default();
acPhy.SetChannel( acChannel.Create() );
acPhy.Set("ChannelNumber", UintegerValue(acChannelNum));
Ssid acSsid = Ssid("ac-ssid");
acMac.SetType("ns3::StaWifiMac",
"Ssid", SsidValue(acSsid));
NetDeviceContainer staNdc;
staNdc = wifi.Install(acPhy, acMac, staNC);
acMac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(acSsid));
NetDeviceContainer apNdc;
apNdc = wifi.Install(acPhy, acMac, apNC);
//************* Solve all the gateway nodes *****************//
//
NS_LOG_INFO("Set wifi channel, mac and ssid of gateways");
WifiMacHelper gwMac;
YansWifiPhyHelper gwPhy = YansWifiPhyHelper::Default();
YansWifiChannelHelper gwChannel = YansWifiChannelHelper::Default();
gwPhy.SetChannel( gwChannel.Create() );
gwPhy.Set("ChannelNumber", UintegerValue(gwChannelNum));
NetDeviceContainer gwNdc;
Ssid gwSsid = Ssid("gw-ssid");
gwMac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(gwSsid));
gwNdc = wifi.Install(gwPhy, gwMac, gwNC);
//************* Solve the sink node *****************//
//
NS_LOG_INFO("Set the sink node ");
gwMac.SetType("ns3::StaWifiMac",
"Ssid", SsidValue(gwSsid));
NetDeviceContainer snNdc;
snNdc = wifi.Install(gwPhy, gwMac, snNC);
//*** Establish connection between access node and gateway node ***//
//
NS_LOG_INFO("Install csma device to all nodes");
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer apCsmaNdc, gwCsmaNdc;
apCsmaNdc = csma.Install(apNC);
gwCsmaNdc = csma.Install(gwNC);
InternetStackHelper stack;
stack.Install (ac_nc);
stack.Install (gw_nc);
//******* Set ip address for all nodes which will be used *******//
//
NS_LOG_INFO("Set ip address");
Ipv4AddressHelper addr;
addr.SetBase("192.168.2.0", "255.255.255.0");
Ipv4InterfaceContainer apIntf, staIntf;
apIntf = addr.Assign(apNdc);
staIntf = addr.Assign(staNdc);
addr.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer gwIntf, snIntf;
gwIntf = addr.Assign(gwNdc);
snIntf = addr.Assign(snNdc);
addr.SetBase("192.168.3.0", "255.255.255.0");
Ipv4InterfaceContainer apCsmaIntf, gwCsmaIntf;
apCsmaIntf = addr.Assign(apCsmaNdc);
gwCsmaIntf = addr.Assign(gwCsmaNdc);
//******* Output all nodes' ip address *******//
//
NS_LOG_INFO("Output all nodes' ip address");
NS_LOG_INFO("Gw nodes ip address:");
for(i=0; i<gwNC.GetN(); i++){
Ptr<Ipv4> ptr_ipv4 = gwNC.Get(i)->GetObject<Ipv4>();
std::cout<<"Gw node's id:"<<gwNC.Get(i)->GetId()<<", NIC1's ip: "<<ptr_ipv4->GetAddress(1,0).GetLocal()<<std::endl;
std::cout<<"Gw node's id:"<<gwNC.Get(i)->GetId()<<", NIC2's ip: "<<ptr_ipv4->GetAddress(2,0).GetLocal()<<std::endl;
}
NS_LOG_INFO("Ap nodes ip address:");
for(i=0; i<apNC.GetN(); i++){
Ptr<Ipv4> ptr_ipv4 = apNC.Get(i)->GetObject<Ipv4>();
std::cout<<"Ap node's id:"<<apNC.Get(i)->GetId()<<", NIC1's ip: "<<ptr_ipv4->GetAddress(1,0).GetLocal()<<std::endl;
std::cout<<"Ap node's id:"<<apNC.Get(i)->GetId()<<", NIC2's ip: "<<ptr_ipv4->GetAddress(2,0).GetLocal()<<std::endl;
}
NS_LOG_INFO("Sta nodes ip address:");
for(i=0; i<staNC.GetN(); i++){
Ptr<Ipv4> ptr_ipv4 = staNC.Get(i)->GetObject<Ipv4>();
std::cout<<"Sta node's id:"<<staNC.Get(i)->GetId()<<", NIC's ip: "<<ptr_ipv4->GetAddress(1,0).GetLocal()<<std::endl;
}
std::cout<<"Sink node's id:"<<snNC.Get(0)->GetId()<<", NIC's ip: "<<snNC.Get(0)->GetObject<Ipv4>()->GetAddress(1,0).GetLocal()<<std::endl;
NS_LOG_INFO("Set mobility model and position allocator");
MobilityHelper acMobility, gwMobility;
NS_LOG_INFO("Set mobility model for all access nodes");
acMobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
Ptr<ListPositionAllocator> acPositionAlloc = CreateObject<ListPositionAllocator> ();
for(i=0; i<rowNum; i++)
for(uint16_t j=0; j<colNum; j++)
acPositionAlloc->Add (Vector (j*distance+1, i*distance+1, 0.0));
acMobility.SetPositionAllocator(acPositionAlloc);
acMobility.Install(ac_nc);
NS_LOG_INFO("Set mobility model for all gateways nodes");
gwMobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
Ptr<ListPositionAllocator> gwPositionAlloc = CreateObject<ListPositionAllocator> ();
for(i=0; i<rowNum; i++)
for(uint16_t j=0; j<colNum; j++)
gwPositionAlloc->Add (Vector (j*distance+5, i*distance+1, 0.0));
gwMobility.SetPositionAllocator(gwPositionAlloc);
gwMobility.Install(gw_nc);
NS_LOG_INFO("Create traffic producing application ...");
UdpEchoServerHelper echoServer (9);
uint16_t serverid = 0;
uint16_t clientid = 0;
ApplicationContainer serverApps = echoServer.Install (snNC.Get (serverid));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (snIntf.GetAddress (serverid), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (3));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (staNC.Get (clientid));
clientApps.Start (Seconds (5.0));
clientApps.Stop (Seconds (10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds(10.0));
if(tracing == true){
csma.EnablePcapAll("pcap/exp1");
acPhy.EnablePcapAll("pcap/exp1");
gwPhy.EnablePcapAll("pcap/exp1");
}
AnimationInterface anim("xml/exp1");
Simulator::Run();
Simulator::Destroy();
return 0;
}