A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-example-apps.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Authors: Joe Kopena <tjkopena@cs.drexel.edu>
5 *
6 * These applications are used in the WiFi Distance Test experiment,
7 * described and implemented in test02.cc. That file should be in the
8 * same place as this file. The applications have two very simple
9 * jobs, they just generate and receive packets. We could use the
10 * standard Application classes included in the NS-3 distribution.
11 * These have been written just to change the behavior a little, and
12 * provide more examples.
13 *
14 */
15
16#include "wifi-example-apps.h"
17
18#include "ns3/core-module.h"
19#include "ns3/internet-module.h"
20#include "ns3/network-module.h"
21#include "ns3/stats-module.h"
22
23#include <ostream>
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("WiFiDistanceApps");
28
29// ==============================================
30// SENDER
31// ==============================================
32
35{
36 static TypeId tid = TypeId("Sender")
38 .AddConstructor<Sender>()
39 .AddAttribute("PacketSize",
40 "The size of packets transmitted.",
41 UintegerValue(64),
44 .AddAttribute("Destination",
45 "Target host address.",
46 Ipv4AddressValue("255.255.255.255"),
49 .AddAttribute("Port",
50 "Destination app port.",
51 UintegerValue(1603),
54 .AddAttribute("NumPackets",
55 "Total number of packets to send.",
56 UintegerValue(30),
59 .AddAttribute("Interval",
60 "Delay between transmissions.",
61 StringValue("ns3::ConstantRandomVariable[Constant=0.5]"),
64 .AddTraceSource("Tx",
65 "A new packet is created and is sent",
67 "ns3::Packet::TracedCallback");
68 return tid;
69}
70
76
81
82void
84{
86
87 m_socket = nullptr;
88 // chain up
90}
91
92void
94{
96
97 if (!m_socket)
98 {
99 Ptr<SocketFactory> socketFactory =
101 m_socket = socketFactory->CreateSocket();
102 m_socket->Bind();
103 }
104
105 m_count = 0;
106
109}
110
111void
117
118void
120{
121 // NS_LOG_FUNCTION_NOARGS();
122 NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " << m_destAddr);
123
125
126 TimestampTag timestamp;
127 timestamp.SetTimestamp(Simulator::Now());
128 packet->AddByteTag(timestamp);
129
130 // Could connect the socket since the address never changes; using SendTo
131 // here simply because all of the standard apps do not.
133
134 // Report the event to the trace.
135 m_txTrace(packet);
136
137 if (++m_count < m_nPackets)
138 {
141 }
142}
143
144// ==============================================
145// RECEIVER
146// ==============================================
147
148TypeId
150{
151 static TypeId tid = TypeId("Receiver")
153 .AddConstructor<Receiver>()
154 .AddAttribute("Port",
155 "Listening port.",
156 UintegerValue(1603),
159 return tid;
160}
161
166
171
172void
174{
176
177 m_socket = nullptr;
178 // chain up
180}
181
182void
184{
186
187 if (!m_socket)
188 {
189 Ptr<SocketFactory> socketFactory =
191 m_socket = socketFactory->CreateSocket();
193 m_socket->Bind(local);
194 }
195
197}
198
199void
209
210void
215
216void
221
222void
224{
225 // NS_LOG_FUNCTION (this << socket << packet << from);
226
227 Ptr<Packet> packet;
228 Address from;
229 while ((packet = socket->RecvFrom(from)))
230 {
232 {
233 NS_LOG_INFO("Received " << packet->GetSize() << " bytes from "
235 }
236
237 TimestampTag timestamp;
238 // Should never not be found since the sender is adding it, but
239 // you never know.
240 if (packet->FindFirstMatchingByteTag(timestamp))
241 {
242 Time tx = timestamp.GetTimestamp();
243
244 if (m_delay)
245 {
246 m_delay->Update(Simulator::Now() - tx);
247 }
248 }
249
250 if (m_calc)
251 {
252 m_calc->Update();
253 }
254 }
255}
~Receiver() override
void StopApplication() override
Application specific shutdown code.
uint32_t m_port
Listening port.
Ptr< Socket > m_socket
Receiving socket.
void DoDispose() override
Destructor implementation.
void SetDelayTracker(Ptr< TimeMinMaxAvgTotalCalculator > delay)
Set the delay tracker for received packets.
static TypeId GetTypeId()
Get the type ID.
void Receive(Ptr< Socket > socket)
Receive a packet.
Ptr< CounterCalculator<> > m_calc
Counter of the number of received packets.
void StartApplication() override
Application specific startup code.
void SetCounter(Ptr< CounterCalculator<> > calc)
Set the counter calculator for received packets.
Ptr< TimeMinMaxAvgTotalCalculator > m_delay
Delay calculator.
Ptr< Socket > m_socket
Sending socket.
void SendPacket()
Send a packet.
uint32_t m_count
Number of packets sent.
uint32_t m_destPort
Destination port.
Ptr< ConstantRandomVariable > m_interval
Rng for sending packets.
void StopApplication() override
Application specific shutdown code.
Ipv4Address m_destAddr
Destination address.
void DoDispose() override
Destructor implementation.
static TypeId GetTypeId()
Get the type ID.
EventId m_sendEvent
Send packet event.
~Sender() override
uint32_t m_packetSize
The packet size.
TracedCallback< Ptr< const Packet > > m_txTrace
Tx TracedCallback.
uint32_t m_nPackets
Number of packets to send.
void StartApplication() override
Application specific startup code.
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
Template class CounterCalculator.
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static Ipv4Address GetAny()
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
Object to create transport layer instances that provide a socket API to applications.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Timestamp tag for associating a timestamp with a packet.
void SetTimestamp(Time timestamp)
Set the Timestamp object.
Time GetTimestamp() const
Get the Timestamp object.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Ptr< const AttributeChecker > MakeIpv4AddressChecker()