-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplewirelesstcp.cc
281 lines (241 loc) · 10.5 KB
/
simplewirelesstcp.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
#include "ns3/core-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/gnuplot.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/udp-client.h"
#include "ns3/seq-ts-header.h"
#include "ns3/propagation-delay-model.h"
#include "ns3/propagation-loss-model.h"
#include "ns3/netanim-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
//Network topology
//
// S * * * *
//
//
// * * * * *
//
//
// * * * * *
//
//
// * * * * *
//
//
// * * * * D
using namespace ns3;
using namespace std;
int counter = 0;
int m_sent = 0;
NS_LOG_COMPONENT_DEFINE("SimpleWirelessTcp");
void ReceivePacket(Ptr<Socket> socket)
{
while(socket->Recv()){
//NS_LOG_UNCOND ("Received one packet!");
std::cout << Simulator::Now().GetSeconds() << " Received packet: " << ++counter << std::endl;
}
}
static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
SeqTsHeader seqTs;
seqTs.SetSeq (m_sent);
Ptr<Packet> p = Create<Packet> (pktSize);
p->AddHeader (seqTs);
if (pktCount > 0)
{
//socket->Send (Create<Packet> (pktSize));
socket->Send (p);
++m_sent;
Simulator::Schedule (pktInterval, &GenerateTraffic,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
int main(int argc, char* argv[])
{
//Set all the default time unit is nanosecond
Time::SetResolution(Time::NS);
string phyMode("HtMcs0");
uint32_t packetSize = 1024;
uint32_t numPackets = 20;
uint32_t numNodes = 25;
uint32_t distance = 200; //meter
//waiting time before sending next packet
double interval = 0.10;
bool TRACING_TR = false;
bool TRACING_PCAP = true;
bool GENERATE_XML = true;
bool ENABLE_LOG_COMPONENT = true;
bool ENABLE_LOG_INFO = true;
bool ENABLE_ALL = false;
bool TRACING_ROUTE = true;
string chnDelayModel("ConstantSpeedPropagationDelayModel");
string chnLossModel("FriisPropagationLossModel");
CommandLine cmd;
cmd.AddValue ("phyMode", "Wifi phy mode, format: string", phyMode);
cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
cmd.AddValue ("numPackets", "number of packets generated", numPackets);
cmd.AddValue ("interval", "interval (seconds) between packets", interval);
cmd.AddValue ("enable_log_component", "turn on all log components", ENABLE_LOG_COMPONENT);
cmd.AddValue ("enable_log_info", "turn on all log components", ENABLE_LOG_INFO);
cmd.AddValue ("tracing_tr", "turn on ascii tracing", TRACING_TR);
cmd.AddValue ("tracing_pcap", "turn on pcap tracing", TRACING_PCAP);
cmd.AddValue ("generate_xml", "turn on xml generator", GENERATE_XML);
cmd.AddValue ("enable_all", "turn on all asistant output", ENABLE_ALL);
cmd.AddValue ("numNodes", "number of nodes", numNodes);
cmd.AddValue ("tracing_route", "turn on route tracing", TRACING_ROUTE);
cmd.Parse (argc, argv);
if(ENABLE_LOG_COMPONENT){
LogComponentEnable("SimpleWirelessTcp", LOG_LEVEL_INFO);
}
// disable fragmentation for frames below 2200 bytes
//Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
//// turn off RTS/CTS for frames below 2200 bytes
//Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set physical channel ...");
Ptr<YansWifiChannel> ptr_yansWifiChannel = CreateObject<YansWifiChannel>();
//Config::SetDefault("ns3::YansWifiChannel::PropagationLossModel",StringValue(chnLossModel));
//Config::SetDefault("ns3::YansWifiChannel::PropagationDelayModel",StringValue(chnDelayModel));
Ptr<ConstantSpeedPropagationDelayModel> ptr_ppgdelayModel = CreateObject<ConstantSpeedPropagationDelayModel>();
Ptr<FriisPropagationLossModel> ptr_ppglossModel = CreateObject<FriisPropagationLossModel>();
ptr_yansWifiChannel->SetPropagationDelayModel(ptr_ppgdelayModel);
ptr_yansWifiChannel->SetPropagationLossModel(ptr_ppglossModel);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set phy layer ...");
Ptr<YansWifiPhy> ptr_yansWifiPhy = CreateObject<YansWifiPhy>();
ptr_yansWifiPhy->SetChannel(ptr_yansWifiChannel);
ptr_yansWifiPhy->SetTxPowerStart(double(5));
ptr_yansWifiPhy->SetTxPowerEnd(double(5));
ptr_yansWifiPhy->SetEdThreshold(double(-83.0));
ptr_yansWifiPhy->ConfigureStandard(WIFI_PHY_STANDARD_80211n_2_4GHZ);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set wifi remote manager ...");
Ptr<ConstantRateWifiManager> ptr_crwm = CreateObject<ConstantRateWifiManager>();
ptr_crwm->SetFragmentationThreshold(2200);
ptr_crwm->SetRtsCtsThreshold(2200);
ptr_crwm->SetHtSupported(true);
Config::SetDefault("ns3::ConstantRateWifiManager::DataMode",StringValue(phyMode));
Config::SetDefault("ns3::ConstantRateWifiManager::ControlMode",StringValue(phyMode));
// Fix non-unicast data rate to be the same as that of unicast
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set wifi mac ...");
Ptr<AdhocWifiMac> ptr_wifiMac = CreateObject<AdhocWifiMac>();
ptr_wifiMac->SetWifiPhy(ptr_yansWifiPhy);
ptr_wifiMac->SetWifiRemoteStationManager(ptr_crwm);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set wifi netdevice ...");
Ptr<WifiNetDevice> ptr_wifiNetDevice = CreateObject<WifiNetDevice>();
ptr_wifiNetDevice->SetMac(ptr_wifiMac);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Creating nodes and install netdevice.");
NodeContainer nodesContainer;
nodesContainer.Create(numNodes);
NetDeviceContainer netdeviceCtn;
for(NodeContainer::Iterator i=nodesContainer.Begin(); i != nodesContainer.End(); ++i){
(*i)->AddDevice(ptr_wifiNetDevice);
netdeviceCtn.Add( (*i)->GetDevice(0) );
}
//cout<<"Number of netdevices: "<<netdeviceCtn.GetN()<<endl;
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set position allocator and mobility model ...");
MobilityHelper mobility;
mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX", DoubleValue(0),
"MinY", DoubleValue(0),
"DeltaX", DoubleValue(distance),
"DeltaY", DoubleValue(distance),
"GridWidth", UintegerValue(5),
"LayoutType", StringValue("RowFirst")
);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodesContainer);
// Enable OLSR
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper listRouting;
//The second parameter indicates the priority of routing protocol.
//the first protocol(index 0) the highest priority, the next one
//(index 1) the second highest priority, and so on.
listRouting.Add(staticRouting, 0);
listRouting.Add(olsr, 10);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Install routing protocol ...");
InternetStackHelper internetStack;
internetStack.SetRoutingHelper(listRouting);
internetStack.Install(nodesContainer);
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Assign ip address ...");
Ipv4AddressHelper ipv4Address;
ipv4Address.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer ipv4IntfCtn;
ipv4IntfCtn = ipv4Address.Assign(netdeviceCtn);
//cout<<"Number of ipv4 interface: "<<ipv4IntfCtn.GetN()<<endl;
// Convert to time object
Time interPacketInterval = Seconds (interval);
uint32_t sourceNodeId = 0;
uint32_t sinkNodeId = numNodes - 1;
GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Creating udp server and binding with local netdevices ...");
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> recvSink = Socket::CreateSocket (nodesContainer.Get(sinkNodeId), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny(), 80);
recvSink->Bind(local);
recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Creating udp client and connecting to server ...");
Ptr<Socket> source = Socket::CreateSocket(nodesContainer.Get(sourceNodeId), tid);
InetSocketAddress remote = InetSocketAddress(ipv4IntfCtn.GetAddress(sinkNodeId, 0), 80);
source->Connect(remote);
string fileNameRoot("simpleWirelessTcp");
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set pcap trace nodes ... ");
if(TRACING_PCAP){
//PcapHelperForDevice pcapHelperForDevice;
//Ptr<WifiNetDevice> ptr_wifiNetDev = DynamicCast<WifiNetDevice>( nodesContainer.Get(sourceNodeId)->GetDevice(0) );
//pcapHelperForDevice.EnablePcap(fileNameRoot, ptr_wifiNetDev );
//ptr_wifiNetDev = DynamicCast<WifiNetDevice>( nodesContainer.Get(sinkNodeId)->GetDevice(0) );
//pcapHelperForDevice.EnablePcap(fileNameRoot, ptr_wifiNetDev );
//PcapHelper pcapHelper;
//vector<Ptr<WifiPhy>> v_wifiPhy;
//v_wifiPhy.push_back(ptr_wifiNetDev->GetPhy());
//v_wifiPhy.push_back(ptr_wifiNetDev->GetPhy());
//vector<Ptr<WifiPhy>>::iterator i;
//Ptr<PcapFileWrapper> file = pcapHelper.CreateFile(fileNameRoot, ios::out, PcapHelper::DLT_IEEE802_11_RADIO);
//for(i = v_wifiPhy.begin(); i != v_wifiPhy.end(); ++i){
// (*i)->TraceConnectWithoutContext("MonitorSinfferTx", MakeBoundCallBack(&YansWifiPhyHelper::PcapSniffTxEvent, fileNameRoot));
//}
}
if(ENABLE_LOG_INFO)
NS_LOG_INFO("Set route trace ...");
if(TRACING_ROUTE){
// Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>("./route/"+fileNameRoot+".routes", ios::out);
// olsr.PrintRoutingTableAllEvery (Seconds(2), routingStream);
// Ptr<OutputStreamWrapper> neighborStream = Create<OutputStreamWrapper>("./route/"+fileNameRoot+".neighbors", ios::out);
// olsr.PrintRoutingTableAllEvery (Seconds(2), neighborStream);
}
Simulator::Schedule(Seconds(30.0), &GenerateTraffic,
source, packetSize, numPackets, interPacketInterval);
AnimationInterface anim("xml/simpleWirelessTcp.xml");
Simulator::Stop (Seconds(50.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}