A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-example-apps.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 * Authors: Joe Kopena <tjkopena@cs.drexel.edu>
16 *
17 * These applications are used in the WiFi Distance Test experiment,
18 * described and implemented in test02.cc. That file should be in the
19 * same place as this file. The applications have two very simple
20 * jobs, they just generate and receive packets. We could use the
21 * standard Application classes included in the NS-3 distribution.
22 * These have been written just to change the behavior a little, and
23 * provide more examples.
24 *
25 */
26
27#include "wifi-example-apps.h"
28
29#include "ns3/core-module.h"
30#include "ns3/internet-module.h"
31#include "ns3/network-module.h"
32#include "ns3/stats-module.h"
33
34#include <ostream>
35
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE("WiFiDistanceApps");
39
40// ==============================================
41// SENDER
42// ==============================================
43
46{
47 static TypeId tid = TypeId("Sender")
49 .AddConstructor<Sender>()
50 .AddAttribute("PacketSize",
51 "The size of packets transmitted.",
52 UintegerValue(64),
54 MakeUintegerChecker<uint32_t>(1))
55 .AddAttribute("Destination",
56 "Target host address.",
57 Ipv4AddressValue("255.255.255.255"),
60 .AddAttribute("Port",
61 "Destination app port.",
62 UintegerValue(1603),
64 MakeUintegerChecker<uint32_t>())
65 .AddAttribute("NumPackets",
66 "Total number of packets to send.",
67 UintegerValue(30),
69 MakeUintegerChecker<uint32_t>(1))
70 .AddAttribute("Interval",
71 "Delay between transmissions.",
72 StringValue("ns3::ConstantRandomVariable[Constant=0.5]"),
74 MakePointerChecker<RandomVariableStream>())
75 .AddTraceSource("Tx",
76 "A new packet is created and is sent",
78 "ns3::Packet::TracedCallback");
79 return tid;
80}
81
83{
85 m_interval = CreateObject<ConstantRandomVariable>();
86}
87
89{
91}
92
93void
95{
97
98 m_socket = nullptr;
99 // chain up
101}
102
103void
105{
107
108 if (!m_socket)
109 {
110 Ptr<SocketFactory> socketFactory =
112 m_socket = socketFactory->CreateSocket();
113 m_socket->Bind();
114 }
115
116 m_count = 0;
117
120}
121
122void
124{
127}
128
129void
131{
132 // NS_LOG_FUNCTION_NOARGS();
133 NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " << m_destAddr);
134
135 Ptr<Packet> packet = Create<Packet>(m_packetSize);
136
137 TimestampTag timestamp;
138 timestamp.SetTimestamp(Simulator::Now());
139 packet->AddByteTag(timestamp);
140
141 // Could connect the socket since the address never changes; using SendTo
142 // here simply because all of the standard apps do not.
144
145 // Report the event to the trace.
146 m_txTrace(packet);
147
148 if (++m_count < m_nPackets)
149 {
152 }
153}
154
155// ==============================================
156// RECEIVER
157// ==============================================
158
159TypeId
161{
162 static TypeId tid = TypeId("Receiver")
164 .AddConstructor<Receiver>()
165 .AddAttribute("Port",
166 "Listening port.",
167 UintegerValue(1603),
169 MakeUintegerChecker<uint32_t>());
170 return tid;
171}
172
174{
176}
177
179{
181}
182
183void
185{
187
188 m_socket = nullptr;
189 // chain up
191}
192
193void
195{
197
198 if (!m_socket)
199 {
200 Ptr<SocketFactory> socketFactory =
202 m_socket = socketFactory->CreateSocket();
204 m_socket->Bind(local);
205 }
206
208}
209
210void
212{
214
215 if (m_socket)
216 {
218 }
219}
220
221void
223{
224 m_calc = calc;
225}
226
227void
229{
230 m_delay = delay;
231}
232
233void
235{
236 // NS_LOG_FUNCTION (this << socket << packet << from);
237
238 Ptr<Packet> packet;
239 Address from;
240 while ((packet = socket->RecvFrom(from)))
241 {
243 {
244 NS_LOG_INFO("Received " << packet->GetSize() << " bytes from "
246 }
247
248 TimestampTag timestamp;
249 // Should never not be found since the sender is adding it, but
250 // you never know.
251 if (packet->FindFirstMatchingByteTag(timestamp))
252 {
253 Time tx = timestamp.GetTimestamp();
254
255 if (m_delay)
256 {
258 }
259 }
260
261 if (m_calc)
262 {
263 m_calc->Update();
264 }
265 }
266}
~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:101
The base class for all ns3 applications.
Definition: application.h:62
void DoDispose() override
Destructor implementation.
Definition: application.cc:86
Ptr< Node > GetNode() const
Definition: application.cc:108
double GetValue(double constant)
Get the next random value drawn from the distribution.
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()
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:341
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
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 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:285
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
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:128
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:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
void Update(const Time i)
Updates all variables of TimeMinMaxAvgTotalCalculator.
Timestamp tag for associating a timestamp with a packet.
Definition: timestamp-tag.h:39
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition: uinteger.h:45
Ptr< const AttributeChecker > MakeIpv4AddressChecker()
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Definition: ipv4-address.h:341
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:747
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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.
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:704