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 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE("WiFiDistanceApps");
39 
40 // ==============================================
41 // SENDER
42 // ==============================================
43 
44 TypeId
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"),
58  MakeIpv4AddressAccessor(&Sender::m_destAddr),
59  MakeIpv4AddressChecker())
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 
93 void
95 {
97 
98  m_socket = nullptr;
99  // chain up
101 }
102 
103 void
105 {
107 
108  if (!m_socket)
109  {
110  Ptr<SocketFactory> socketFactory =
111  GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
112  m_socket = socketFactory->CreateSocket();
113  m_socket->Bind();
114  }
115 
116  m_count = 0;
117 
118  Simulator::Cancel(m_sendEvent);
119  m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this);
120 }
121 
122 void
124 {
126  Simulator::Cancel(m_sendEvent);
127 }
128 
129 void
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.
143  m_socket->SendTo(packet, 0, InetSocketAddress(m_destAddr, m_destPort));
144 
145  // Report the event to the trace.
146  m_txTrace(packet);
147 
148  if (++m_count < m_nPackets)
149  {
150  m_sendEvent =
151  Simulator::Schedule(Seconds(m_interval->GetValue()), &Sender::SendPacket, this);
152  }
153 }
154 
155 // ==============================================
156 // RECEIVER
157 // ==============================================
158 
159 TypeId
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 
183 void
185 {
187 
188  m_socket = nullptr;
189  // chain up
191 }
192 
193 void
195 {
197 
198  if (!m_socket)
199  {
200  Ptr<SocketFactory> socketFactory =
201  GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
202  m_socket = socketFactory->CreateSocket();
204  m_socket->Bind(local);
205  }
206 
207  m_socket->SetRecvCallback(MakeCallback(&Receiver::Receive, this));
208 }
209 
210 void
212 {
214 
215  if (m_socket)
216  {
217  m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
218  }
219 }
220 
221 void
223 {
224  m_calc = calc;
225 }
226 
227 void
229 {
230  m_delay = delay;
231 }
232 
233 void
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  {
257  m_delay->Update(Simulator::Now() - tx);
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.
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.
void StartApplication() override
Application specific startup code.
void SetCounter(Ptr< CounterCalculator<>> calc)
Set the counter calculator for received packets.
void SendPacket()
Send a packet.
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.
~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
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()
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:943
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:915
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.
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.
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
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:931
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition: uinteger.h:45
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
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46