A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nsclick-udp-client-server-wifi.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16// Adaptation of examples/udp/udp-client-server.cc for
17// Click based nodes running wifi.
18//
19// Network topology:
20//
21// (1.4)
22// (( n4 ))
23//
24// 172.16.1.0/24
25//
26// (1.1) (1.2) (1.3)
27// n0 )) (( n1 )) (( n2
28// WLAN
29//
30// - UDP flows from n0 to n1 and n2 to n1.
31// - All nodes are Click based.
32// - The single ethernet interface that each node
33// uses is named 'eth0' in the Click file.
34// - Node 4 is running in promiscuous mode and can listen in on
35// the packets being exchanged between n0-n1 and n2-n1.
36//
37
38#include "ns3/applications-module.h"
39#include "ns3/click-internet-stack-helper.h"
40#include "ns3/core-module.h"
41#include "ns3/internet-module.h"
42#include "ns3/ipv4-click-routing.h"
43#include "ns3/mobility-module.h"
44#include "ns3/network-module.h"
45#include "ns3/wifi-module.h"
46
47#include <fstream>
48
49using namespace ns3;
50
51NS_LOG_COMPONENT_DEFINE("NsclickUdpClientServerWifi");
52
53void
55{
56 // Access the handlers
57 NS_LOG_INFO(clickRouter->ReadHandler("wifi/arpquerier", "table"));
58 NS_LOG_INFO(clickRouter->ReadHandler("wifi/arpquerier", "stats"));
59}
60
61void
63{
64 // Access the handler
66 clickRouter->WriteHandler("wifi/arpquerier", "insert", "172.16.1.2 00:00:00:00:00:02"));
67}
68
69int
70main(int argc, char* argv[])
71{
72 std::string clickConfigFolder = "src/click/examples";
73
74 CommandLine cmd(__FILE__);
75 cmd.AddValue("clickConfigFolder",
76 "Base folder for click configuration files",
77 clickConfigFolder);
78 cmd.Parse(argc, argv);
79
80 //
81 // Enable logging
82 //
83 LogComponentEnable("NsclickUdpClientServerWifi", LOG_LEVEL_INFO);
84
85 //
86 // Explicitly create the nodes required by the topology (shown above).
87 //
88 NS_LOG_INFO("Create nodes.");
90 n.Create(4);
91
92 NS_LOG_INFO("Create channels.");
93 //
94 // Explicitly create the channels required by the topology (shown above).
95 //
96 std::string phyMode("DsssRate1Mbps");
97
98 // disable fragmentation for frames below 2200 bytes
99 Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold",
100 StringValue("2200"));
101 // turn off RTS/CTS for frames below 2200 bytes
102 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2200"));
103 // Fix non-unicast data rate to be the same as that of unicast
104 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
105
107 wifi.SetStandard(WIFI_STANDARD_80211b);
108
109 YansWifiPhyHelper wifiPhy;
110 // This is one parameter that matters when using FixedRssLossModel
111 // set it to zero; otherwise, gain will be added
112 wifiPhy.Set("RxGain", DoubleValue(0));
113 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
115
116 YansWifiChannelHelper wifiChannel;
117 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
118 // The below FixedRssLossModel will cause the rss to be fixed regardless
119 // of the distance between the two stations, and the transmit power
120 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(-80));
121 wifiPhy.SetChannel(wifiChannel.Create());
122
123 // Add an upper mac and disable rate control
124 WifiMacHelper wifiMac;
125 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
126 "DataMode",
127 StringValue(phyMode),
128 "ControlMode",
129 StringValue(phyMode));
130 // Set it to adhoc mode
131 wifiMac.SetType("ns3::AdhocWifiMac");
132 NetDeviceContainer d = wifi.Install(wifiPhy, wifiMac, n);
133
135 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
136 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
137 positionAlloc->Add(Vector(10.0, 0.0, 0.0));
138 positionAlloc->Add(Vector(20.0, 0.0, 0.0));
139 positionAlloc->Add(Vector(0.0, 10.0, 0.0));
140 mobility.SetPositionAllocator(positionAlloc);
141 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
142 mobility.Install(n);
143
144 //
145 // Install Click on the nodes
146 //
148 clickinternet.SetClickFile(n.Get(0),
149 clickConfigFolder + "/nsclick-wifi-single-interface.click");
150 clickinternet.SetClickFile(n.Get(1),
151 clickConfigFolder + "/nsclick-wifi-single-interface.click");
152 clickinternet.SetClickFile(n.Get(2),
153 clickConfigFolder + "/nsclick-wifi-single-interface.click");
154
155 // Node 4 is to run in promiscuous mode. This can be verified
156 // from the pcap trace Node4_in_eth0.pcap generated after running
157 // this script.
158 clickinternet.SetClickFile(n.Get(3),
159 clickConfigFolder + "/nsclick-wifi-single-interface-promisc.click");
160 clickinternet.SetRoutingTableElement(n, "rt");
161 clickinternet.Install(n);
163 //
164 // We've got the "hardware" in place. Now we need to add IP addresses.
165 //
166 NS_LOG_INFO("Assign IP Addresses.");
167 ipv4.SetBase("172.16.1.0", "255.255.255.0");
168 Ipv4InterfaceContainer i = ipv4.Assign(d);
169
170 NS_LOG_INFO("Create Applications.");
171 //
172 // Create one udpServer applications on node one.
173 //
174 uint16_t port = 4000;
176 ApplicationContainer apps = server.Install(n.Get(1));
177 apps.Start(Seconds(1.0));
178 apps.Stop(Seconds(10.0));
179
180 //
181 // Create one UdpClient application to send UDP datagrams from node zero to
182 // node one.
183 //
184 uint32_t MaxPacketSize = 1024;
185 Time interPacketInterval = Seconds(0.5);
186 uint32_t maxPacketCount = 320;
188 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
189 client.SetAttribute("Interval", TimeValue(interPacketInterval));
190 client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
191 apps = client.Install(NodeContainer(n.Get(0), n.Get(2)));
192 apps.Start(Seconds(2.0));
193 apps.Stop(Seconds(10.0));
194
195 wifiPhy.EnablePcap("nsclick-udp-client-server-wifi", d);
196
197 // Force the MAC address of the second node: The current ARP
198 // implementation of Click sends only one ARP request per incoming
199 // packet for an unknown destination and does not retransmit if no
200 // response is received. With the scenario of this example, all ARP
201 // requests of node 3 are lost due to interference from node
202 // 1. Hence, we fill in the ARP table of node 2 before at the
203 // beginning of the simulation
207
208 //
209 // Now, do the actual simulation.
210 //
211 NS_LOG_INFO("Run Simulation.");
215 NS_LOG_INFO("Done.");
216
217 return 0;
218}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
aggregate Click/IP/TCP/UDP functionality to existing Nodes.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Class to allow a node to use Click for external routing.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1406
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:163
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
@ WIFI_STANDARD_80211b
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
ns wifi
Definition: third.py:95
ns mobility
Definition: third.py:105
void WriteArp(Ptr< Ipv4ClickRouting > clickRouter)
void ReadArp(Ptr< Ipv4ClickRouting > clickRouter)