-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadhoccsma.cc~
281 lines (239 loc) · 11 KB
/
adhoccsma.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/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/config-store.h"
#include "ns3/propagation-delay-model.h"
#include "ns3/aodv-routing-protocol.h"
#include "ns3/aodv-helper.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include <string.h>
// Default Network Topology
// There are only 3 wifi nodes in this simple network.
// The program is to realize the function that sending
// data from n1 to n2, transmiting by ap, three nodes are
// in the same network, their ip address are assigned
// as following.
// n1 n2 ========== n3 n4
// * * * *
// 1.1 1.2 2.1 2.2
//
// 192.168.1.0 192.168.2.0
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE("adhoccsma");
void Sta0DevTxTrace(std::string context, Ptr<const Packet> p)
{
std::cout<<Simulator::Now().As(Time::S)<<std::endl;
std::cout<<context<<", TX p:"<<*p<<std::endl;
}
void Sta5DevRxTrace(std::string context, Ptr<const Packet> p)
{
std::cout<<Simulator::Now().As(Time::S)<<std::endl;
std::cout<<context<<", RX p:"<<*p<<std::endl;
}
int main(int argc, char* argv[])
{
Time::SetResolution(Time::NS);
bool verbose = true;
uint32_t nWifi = 4;
bool tracing = true;
uint16_t distance = 10;
uint16_t sid = 2;
double startTime = 15;//s
double stopTime = startTime + 15;//s
CommandLine cmd;
cmd.AddValue("verbose","Boolean value, enable log component when it is true", verbose);
cmd.AddValue("nWifi", "Number of wifi nodes", nWifi);
cmd.AddValue("tracing", "Boolean value, enable tracing function when it is true",tracing);
if(verbose){
LogComponentEnable("adhoccsma", LOG_LEVEL_INFO);
// LogComponentEnable("OnOffApplication", LOG_LEVEL_INFO);
LogComponentEnable("ArfWifiManager", LOG_LEVEL_INFO);
//LogComponentEnable("Address", LOG_LEVEL_INFO);
//LogComponentEnable("AnimationInterface", LOG_LEVEL_INFO);
//LogComponentEnable("ApWifiMac", LOG_LEVEL_INFO);
//LogComponentEnable("GlobalRouter", LOG_LEVEL_INFO);
//LogComponentEnable("InetSocketAddress", LOG_LEVEL_INFO);
//LogComponentEnable("IpLProtocol", LOG_LEVEL_INFO);
//LogComponentEnable("Ipv4Address", LOG_LEVEL_INFO);
//LogComponentEnable("Ipv4GlobalRouting", LOG_LEVEL_INFO);
}
GlobalValue::Bind ("ChecksumEnabled", BooleanValue(true));
NS_LOG_INFO("Create nodes ...");
NodeContainer nc, net1nc, net2nc, csmanc;
nc.Create(nWifi);
net1nc.Add(nc.Get(0));
net1nc.Add(nc.Get(1));
net2nc.Add(nc.Get(2));
net2nc.Add(nc.Get(3));
csmanc.Add(nc.Get(1));
csmanc.Add(nc.Get(2));
NS_LOG_INFO("Set mobility model ...");
MobilityHelper mobility;
//mobility.SetPositionAllocator("ns3::GridPositionAllocator",
// "MinX", DoubleValue(10.0),
// "MinY", DoubleValue(10.0),
// "DeltaX", DoubleValue(40.0),
// "DeltaY", DoubleValue(10.0),
// "GridWidth", UintegerValue(40),
// "LayoutType", StringValue("RowFirst"));
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nc);
Ptr<ConstantPositionMobilityModel> ptr_cpmm = CreateObject<
ConstantPositionMobilityModel>();
// string ptr_mm = mobility.GetMobilityModelType();
// cout << "mobility model is:" << ptr_mm << endl;
NS_LOG_INFO("Set phy and channel ...");
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
Ptr<YansWifiChannel> ptr_channel = channel.Create();
Ptr<ConstantSpeedPropagationDelayModel> ptr_propagation_delay_model =
CreateObject<ConstantSpeedPropagationDelayModel>();
cout << "propagation model's delay is: "
<< ptr_propagation_delay_model->GetDelay(ptr_cpmm, ptr_cpmm).As(
Time::NS) << endl;
ptr_channel->SetPropagationDelayModel(ptr_propagation_delay_model);
phy.SetChannel(ptr_channel);
phy.Set("ChannelNumber", UintegerValue(1));
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211g);
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode",
StringValue("ErpOfdmRate54Mbps"));
NqosWifiMacHelper mac = NqosWifiMacHelper::Default();
mac.SetType("ns3::AdhocWifiMac", "Slot", StringValue("16us"));
NS_LOG_INFO("Install netdevices ...");
NetDeviceContainer net1dev, net2dev, csmadev;
//Ptr<WifiNetDevice> ptrWifiNetDev = DynamicCast<WifiNetDevice>(netdev.Get(0));
//ptrWifiNetDev->GetMac()->SetAckTimeout(Time(NanoSeconds(1000000)));
net1dev = wifi.Install(phy, mac, net1nc);
phy.SetChannel(channel.Create());
phy.Set("ChannelNumber", UintegerValue(6));
net2dev = wifi.Install(phy, mac, net2nc);
NS_LOG_INFO("Set csma network's channel");
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
csmadev = csma.Install(csmanc);
NS_LOG_INFO("Install internet stack ...");
InternetStackHelper stack;
AodvHelper aodv;
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (aodv, 10);
stack.SetRoutingHelper(list);
stack.Install(nc);
NS_LOG_INFO("Set nodes' address ...");
Ipv4AddressHelper address;
address.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer net1intfc,net2intfc, csmaintfc;
net1intfc = address.Assign(net1dev);
address.SetBase("192.168.2.0", "255.255.255.0");
net2intfc = address.Assign(net2dev);
address.SetBase("192.168.3.0", "255.255.255.0");
csmaintfc = address.Assign(csmadev);
Ptr<Ipv4StaticRouting> ptr_staticRouting = CreateObject<Ipv4StaticRouting>();
ptr_staticRouting = staticRouting.GetStaticRouting(csmanc.Get(0)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.3.2"), 2, 0);
ptr_staticRouting = staticRouting.GetStaticRouting(csmanc.Get(1)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.1.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.3.1"), 2, 0);
for(uint16_t i = 0; i < nc.GetN(); i++){
cout<<"Node "<<i<<" ip addr: "<< nc.Get(i)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal()<<endl;
}
cout<<"Node 1 ip addr: "<< nc.Get(1)->GetObject<Ipv4>()->GetAddress(2, 0).GetLocal()<<endl;
cout<<"Node 2 ip addr: "<< nc.Get(2)->GetObject<Ipv4>()->GetAddress(2, 0).GetLocal()<<endl;
NS_LOG_INFO("Set nodes' position ...");
AnimationInterface anim("xml/adhoccsma.xml");
for(uint16_t i = 0; i < nc.GetN(); i++){
anim.SetConstantPosition(nc.Get(i), distance*i, distance);
}
anim.UpdateNodeColor(nc.Get(0), 0, 255, 0);
anim.UpdateNodeColor(nc.Get(sid), 0, 0, 255);
NS_LOG_INFO("Creating onoff application ...");
uint16_t port = 1024;
OnOffHelper onoff("ns3::TcpSocketFactory", Address(InetSocketAddress(net2intfc.GetAddress(sid-1), port)));
onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
onoff.SetAttribute("OffTime",
StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
onoff.SetConstantRate(DataRate("5Kbps"), 512);
ApplicationContainer app_onoff = onoff.Install(nc.Get(0));
app_onoff.Start(Seconds(startTime));
app_onoff.Stop(Seconds(stopTime));
NS_LOG_INFO("Creating sink application ...");
PacketSinkHelper sink("ns3::TcpSocketFactory", Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
ApplicationContainer app_sink = sink.Install(nc.Get(sid));
app_sink.Start(Seconds(startTime-1));
app_sink.Stop(Seconds(stopTime));
NS_LOG_INFO("Enable tracing ...");
if(tracing){
phy.EnablePcapAll("pcap/adhoccsma");
}
for (int i = 0; i < nWifi; i++) {
Ptr<Ipv4> ptr_ipv4 = nc.Get(i)->GetObject<Ipv4>();
Ptr<NetDevice> ptr_netdev = nc.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> ptr_wifi_netdev = DynamicCast<WifiNetDevice>(
ptr_netdev);
Ptr<WifiMac> ptr_wifimac = ptr_wifi_netdev->GetMac();
// ptr_wifimac->SetAckTimeout(Time(NanoSeconds(100000)));
Ptr<WifiPhy> ptr_wifiphy = ptr_wifi_netdev->GetPhy();
// cout << "Antenna's receive number is: "
// << ptr_wifiphy->GetNumberOfReceiveAntennas() << endl;
// cout << "Antenna's transmit number is: "
// << ptr_wifiphy->GetNumberOfTransmitAntennas() << endl;
// cout << "channel number is:" << ptr_wifiphy->GetChannelNumber() << endl;
//cout << "wifi mac's ack timeout is:"
// << ptr_wifimac->GetAckTimeout().As(Time::NS) << endl;
// cout << "node " << i << "'s mac address is "
// << ptr_wifimac->GetAddress() << endl;
// cout << "node " << i << "'s max propagation delay is: "
// << ptr_wifimac->GetMaxPropagationDelay().As(Time::NS)
// << endl;
// cout << "node " << i << "'s slot is: "
// << ptr_wifimac->GetSlot().As(Time::NS) << endl;
// cout << "node " << i << "'s ssid is: "
// << ptr_wifimac->GetSsid().PeekString()
// << endl << endl;
// std::cout<<"node "<<i<<"'s address is "<<ptr_ipv4->GetAddress(1,0).GetLocal()<<std::endl;
// std::cout<<"node "<<i<<"'s netdevice's MTU is "<<ptr_netdev->GetMtu()<<std::endl;
//
//
}
//
//
// std::cout<<"Whether the program enables checksum(1:yes,0:no): "<<ns3::Node::ChecksumEnabled()<<std::endl;
// Config::SetDefault("ns3::ConfigStore::Filename",
// StringValue("output-attributes.xml"));
// Config::SetDefault("ns3::ConfigStore::FileFormat", StringValue("Xml"));
// Config::SetDefault("ns3::ConfigStore::Mode", StringValue("Save"));
// ConfigStore outputConfig;
// outputConfig.ConfigureDefaults();
// outputConfig.ConfigureAttributes();
// Output config store to txt format
// Config::SetDefault("ns3::ConfigStore::Filename",
// StringValue("output-attributes.txt"));
// Config::SetDefault("ns3::ConfigStore::FileFormat", StringValue("RawText"));
// Config::SetDefault("ns3::ConfigStore::Mode", StringValue("Save"));
// ConfigStore outputConfig2;
// outputConfig2.ConfigureDefaults();
// outputConfig2.ConfigureAttributes();
Config::Connect("/NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Mac/$ns3::StaWifiMac/MacTx", MakeCallback(&Sta0DevTxTrace));
Config::Connect("/NodeList/2/DeviceList/0/$ns3::WifiNetDevice/Mac/$ns3::StaWifiMac/MacRx", MakeCallback(&Sta5DevRxTrace));
//Ipv4GlobalRoutingHelper::PopulateRoutingTables();
Simulator::Stop(Seconds(stopTime));
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("route/adhoccsma.routes", std::ios::out);
AodvHelper::PrintRoutingTableAllAt (Seconds (startTime-1), routingStream);
//OlsrHelper::PrintRoutingTableAllAt (Seconds (startTime-1), routingStream);
NS_LOG_INFO("Simulator is running ...");
Simulator::Run();
Simulator::Destroy();
return 0;
}