1
+ // Extended this from third.cc which is default file given under examples/tutorial
2
+ #include " ns3/core-module.h"
3
+ #include " ns3/point-to-point-module.h"
4
+ #include " ns3/network-module.h"
5
+ #include " ns3/applications-module.h"
6
+ #include " ns3/mobility-module.h"
7
+ #include " ns3/csma-module.h"
8
+ #include " ns3/internet-module.h"
9
+ #include " ns3/yans-wifi-helper.h"
10
+ #include " ns3/ssid.h"
11
+ #include < cmath>
12
+ // For NetAnim Animation
13
+ #include " ns3/netanim-module.h"
14
+
15
+ using namespace ns3 ;
16
+
17
+ NS_LOG_COMPONENT_DEFINE (" Wifi two way communication" );
18
+
19
+ int main (int argc, char *argv[]){
20
+ // Since we are unaware where its range run out
21
+ for (int i=0 ;i<60 ;i++){
22
+
23
+ bool verbose = true , tracing = true ;
24
+ uint32_t nWifi = 2 ;
25
+ if (verbose)
26
+ {
27
+ // In order to take logs
28
+ // Logs can be taken in different types as per user requirements
29
+ // log_level_all, debug, error, function, info, logic, warn
30
+ // U can select to what extend you need
31
+ LogComponentEnable (" UdpEchoClientApplication" , LOG_LEVEL_INFO);
32
+ LogComponentEnable (" UdpEchoServerApplication" , LOG_LEVEL_INFO);
33
+ }
34
+
35
+ // Create 2 base stationary nodes and 1 Access point node
36
+ NodeContainer wifiStaNodes, wifiApNode;
37
+ wifiStaNodes.Create (nWifi);
38
+ wifiApNode.Create (1 );
39
+
40
+ // Choosing technology channel to communicate
41
+ YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
42
+ YansWifiPhyHelper phy;
43
+ phy.SetChannel (channel.Create ());
44
+
45
+ WifiHelper wifi;
46
+ wifi.SetRemoteStationManager (" ns3::AarfWifiManager" );
47
+
48
+ WifiMacHelper mac;
49
+ Ssid ssid = Ssid (" ns-3-ssid" );
50
+ mac.SetType (" ns3::StaWifiMac" ," Ssid" , SsidValue (ssid)," ActiveProbing" , BooleanValue (false ));
51
+
52
+ // Install Channels on node
53
+ NetDeviceContainer staDevices;
54
+ staDevices = wifi.Install (phy, mac, wifiStaNodes);
55
+ mac.SetType (" ns3::ApWifiMac" ," Ssid" , SsidValue (ssid));
56
+
57
+ NetDeviceContainer apDevice;
58
+ apDevice = wifi.Install (phy, mac, wifiApNode);
59
+
60
+
61
+ MobilityHelper mobility;
62
+ mobility.SetPositionAllocator (" ns3::GridPositionAllocator" ,
63
+ " MinX" , DoubleValue (0.0 ),
64
+ " MinY" , DoubleValue (0.0 ),
65
+ " DeltaX" , DoubleValue (5.0 ),
66
+ " DeltaY" , DoubleValue (5.0 ),
67
+ " GridWidth" , UintegerValue (3 ),
68
+ " LayoutType" , StringValue (" RowFirst" ));
69
+ mobility.SetMobilityModel (" ns3::ConstantPositionMobilityModel" );
70
+ mobility.Install (wifiApNode);
71
+ mobility.Install (wifiStaNodes);
72
+
73
+ // Set up Internet stack on nodes
74
+ InternetStackHelper stack;
75
+ stack.Install (wifiApNode);
76
+ stack.Install (wifiStaNodes);
77
+
78
+ // Assign IP address to communicate
79
+ Ipv4AddressHelper address;
80
+ Ipv4InterfaceContainer wifiInterfaces, wifiApInterface;
81
+
82
+ address.SetBase (" 192.168.2.0" , " 255.255.255.0" );
83
+ wifiApInterface = address.Assign (apDevice);
84
+ wifiInterfaces = address.Assign (staDevices);
85
+
86
+ UdpEchoServerHelper echoServer (9 );
87
+ ApplicationContainer serverApps = echoServer.Install (wifiApNode.Get (0 ));
88
+ serverApps.Start (Seconds (1.0 ));
89
+ serverApps.Stop (Seconds (10.0 ));
90
+
91
+ UdpEchoClientHelper echoClient (wifiApInterface.GetAddress (0 ), 9 );
92
+
93
+ echoClient.SetAttribute (" MaxPackets" , UintegerValue (1 ));
94
+ echoClient.SetAttribute (" Interval" , TimeValue (Seconds (1.0 )));
95
+ echoClient.SetAttribute (" PacketSize" , UintegerValue (1024 ));
96
+
97
+ ApplicationContainer clientApp1 = echoClient.Install (wifiStaNodes.Get (0 ));
98
+ clientApp1.Start (Seconds (2.0 ));
99
+ clientApp1.Stop (Seconds (10.0 ));
100
+
101
+ ApplicationContainer clientApp2 = echoClient.Install (wifiStaNodes.Get (1 ));
102
+ clientApp2.Start (Seconds (2.0 ));
103
+ clientApp2.Stop (Seconds (10.0 ));
104
+
105
+ Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
106
+ Simulator::Stop (Seconds (10.0 ));
107
+
108
+ AnimationInterface anim (" wifi.xml" );
109
+ anim.SetConstantPosition (wifiApNode.Get (0 ), 100.0 , 100.0 );
110
+ anim.SetConstantPosition (wifiStaNodes.Get (0 ), 100.0 +i, 100.0 );
111
+ anim.SetConstantPosition (wifiStaNodes.Get (1 ), 100.0 -i, 100.0 );
112
+
113
+ if (tracing == true ){
114
+ phy.EnablePcap (" wifi" ,apDevice);
115
+ }
116
+ printf (" \n -------------------\n " );
117
+
118
+ Simulator::Run ();
119
+
120
+ printf (" Node 1 distance: %lf\n " , sqrt (mobility.GetDistanceSquaredBetween (wifiStaNodes.Get (0 ), wifiApNode.Get (0 ))));
121
+ printf (" Node 2 distance: %lf\n " , sqrt (mobility.GetDistanceSquaredBetween (wifiStaNodes.Get (1 ), wifiApNode.Get (0 ))));
122
+ fflush (stdout);
123
+
124
+ Simulator::Destroy ();
125
+ }
126
+ return 0 ;
127
+ }
0 commit comments