A Discrete-Event Network Simulator
API
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
42{
43 static TypeId tid = TypeId("Sender")
45 .AddConstructor<Sender>()
46 .AddAttribute("PacketSize",
47 "The size of packets transmitted.",
48 UintegerValue(64),
50 MakeUintegerChecker<uint32_t>(1))
51 .AddAttribute("Destination",
52 "Target host address.",
53 Ipv4AddressValue("255.255.255.255"),
54 MakeIpv4AddressAccessor(&Sender::m_destAddr),
55 MakeIpv4AddressChecker())
56 .AddAttribute("Port",
57 "Destination app port.",
58 UintegerValue(1603),
60 MakeUintegerChecker<uint32_t>())
61 .AddAttribute("NumPackets",
62 "Total number of packets to send.",
63 UintegerValue(30),
65 MakeUintegerChecker<uint32_t>(1))
66 .AddAttribute("Interval",
67 "Delay between transmissions.",
68 StringValue("ns3::ConstantRandomVariable[Constant=0.5]"),
70 MakePointerChecker<RandomVariableStream>())
71 .AddTraceSource("Tx",
72 "A new packet is created and is sent",
74 "ns3::Packet::TracedCallback");
75 return tid;
76}
77
79{
81 m_interval = CreateObject<ConstantRandomVariable>();
82 m_socket = nullptr;
83}
84
86{
88}
89
90void
92{
94
95 m_socket = nullptr;
96 // chain up
97 Application::DoDispose();
98}
99
100void
102{
104
105 if (!m_socket)
106 {
107 Ptr<SocketFactory> socketFactory =
108 GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
109 m_socket = socketFactory->CreateSocket();
110 m_socket->Bind();
111 }
112
113 m_count = 0;
114
115 Simulator::Cancel(m_sendEvent);
116 m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this);
117
118 // end Sender::StartApplication
119}
120
121void
123{
125 Simulator::Cancel(m_sendEvent);
126 // end Sender::StopApplication
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_pktSize);
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_numPkts)
149 {
151 Simulator::Schedule(Seconds(m_interval->GetValue()), &Sender::SendPacket, this);
152 }
153
154 // end Sender::SendPacket
155}
156
157//----------------------------------------------------------------------
158//-- Receiver
159//------------------------------------------------------
160TypeId
162{
163 static TypeId tid = TypeId("Receiver")
165 .AddConstructor<Receiver>()
166 .AddAttribute("Port",
167 "Listening port.",
168 UintegerValue(1603),
170 MakeUintegerChecker<uint32_t>());
171 return tid;
172}
173
175 : m_calc(nullptr),
176 m_delay(nullptr)
177{
179 m_socket = nullptr;
180}
181
183{
185}
186
187void
189{
191
192 m_socket = nullptr;
193 // chain up
194 Application::DoDispose();
195}
196
197void
199{
201
202 if (!m_socket)
203 {
204 Ptr<SocketFactory> socketFactory =
205 GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
206 m_socket = socketFactory->CreateSocket();
207 InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), m_port);
208 m_socket->Bind(local);
209 }
210
212
213 // end Receiver::StartApplication
214}
215
216void
218{
220
221 if (m_socket)
222 {
224 }
225
226 // end Receiver::StopApplication
227}
228
229void
231{
232 m_calc = calc;
233 // end Receiver::SetCounter
234}
235
236void
238{
239 m_delay = delay;
240 // end Receiver::SetDelayTracker
241}
242
243void
245{
246 // NS_LOG_FUNCTION (this << socket << packet << from);
247
248 Ptr<Packet> packet;
249 Address from;
250 while ((packet = socket->RecvFrom(from)))
251 {
252 if (InetSocketAddress::IsMatchingType(from))
253 {
254 NS_LOG_INFO("Received " << packet->GetSize() << " bytes from "
255 << InetSocketAddress::ConvertFrom(from).GetIpv4());
256 }
257
258 TimestampTag timestamp;
259 // Should never not be found since the sender is adding it, but
260 // you never know.
261 if (packet->FindFirstMatchingByteTag(timestamp))
262 {
263 Time tx = timestamp.GetTimestamp();
264
265 if (m_delay)
266 {
268 }
269 }
270
271 if (m_calc)
272 {
273 m_calc->Update();
274 }
275
276 // end receiving packets
277 }
278
279 // end Receiver::Receive
280}
281
282//----------------------------------------------------------------------
283//-- TimestampTag
284//------------------------------------------------------
285TypeId
287{
288 static TypeId tid = TypeId("TimestampTag")
289 .SetParent<Tag>()
290 .AddConstructor<TimestampTag>()
291 .AddAttribute("Timestamp",
292 "Some momentous point in time!",
296 return tid;
297}
298
299TypeId
301{
302 return GetTypeId();
303}
304
307{
308 return 8;
309}
310
311void
313{
314 int64_t t = m_timestamp.GetNanoSeconds();
315 i.Write((const uint8_t*)&t, 8);
316}
317
318void
320{
321 int64_t t;
322 i.Read((uint8_t*)&t, 8);
324}
325
326void
328{
329 m_timestamp = time;
330}
331
332Time
334{
335 return m_timestamp;
336}
337
338void
339TimestampTag::Print(std::ostream& os) const
340{
341 os << "t=" << m_timestamp;
342}
~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.
uint32_t m_numPkts
Number of packets to send.
~Sender() override
TracedCallback< Ptr< const Packet > > m_txTrace
Tx TracedCallback.
void StartApplication() override
Application specific startup code.
uint32_t m_pktSize
The packet size.
Timestamp tag - it carries when the packet has been sent.
void Print(std::ostream &os) const override
void Serialize(TagBuffer i) const override
static TypeId GetTypeId()
Get the type ID.
uint32_t GetSerializedSize() const override
void SetTimestamp(Time time)
Set the timestamp.
void Deserialize(TagBuffer i) override
Time GetTimestamp() const
Get the timestamp.
Time m_timestamp
Timestamp.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
a polymophic address class
Definition: address.h:92
The base class for all ns3 applications.
Definition: application.h:61
Ptr< Node > GetNode() const
Definition: application.cc:107
double GetValue(double constant)
Get the next random value, as a double equal to the argument.
Template class CounterCalculator.
A class for an empty attribute value.
Definition: attribute.h:234
an Inet address class
AttributeValue implementation for Ipv4Address.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:962
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:934
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Object to create transport layer instances that provide a socket API to applications.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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:42
read and write tag data
Definition: tag-buffer.h:52
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:183
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:129
tag a set of bytes in a packet
Definition: tag.h:39
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:417
void Update(const Time i)
Updates all variables of TimeMinMaxAvgTotalCalculator.
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:230
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1426
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:734
#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 Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1374
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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:691
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535