A Discrete-Event Network Simulator
API
wifi-example-apps.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Authors: Joe Kopena <tjkopena@cs.drexel.edu>
17  *
18  * These applications are used in the WiFi Distance Test experiment,
19  * described and implemented in test02.cc. That file should be in the
20  * same place as this file. The applications have two very simple
21  * jobs, they just generate and receive packets. We could use the
22  * standard Application classes included in the NS-3 distribution.
23  * These have been written just to change the behavior a little, and
24  * provide more examples.
25  *
26  */
27 
28 #include <ostream>
29 
30 #include "ns3/core-module.h"
31 #include "ns3/network-module.h"
32 #include "ns3/internet-module.h"
33 
34 #include "ns3/stats-module.h"
35 
36 #include "wifi-example-apps.h"
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("WiFiDistanceApps");
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("Sender")
47  .AddConstructor<Sender> ()
48  .AddAttribute ("PacketSize", "The size of packets transmitted.",
49  UintegerValue (64),
51  MakeUintegerChecker<uint32_t>(1))
52  .AddAttribute ("Destination", "Target host address.",
53  Ipv4AddressValue ("255.255.255.255"),
56  .AddAttribute ("Port", "Destination app port.",
57  UintegerValue (1603),
59  MakeUintegerChecker<uint32_t>())
60  .AddAttribute ("NumPackets", "Total number of packets to send.",
61  UintegerValue (30),
63  MakeUintegerChecker<uint32_t>(1))
64  .AddAttribute ("Interval", "Delay between transmissions.",
65  StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"),
67  MakePointerChecker <RandomVariableStream>())
68  .AddTraceSource ("Tx", "A new packet is created and is sent",
70  "ns3::Packet::TracedCallback")
71  ;
72  return tid;
73 }
74 
75 
77 {
79  m_interval = CreateObject<ConstantRandomVariable> ();
80  m_socket = 0;
81 }
82 
84 {
86 }
87 
88 void
90 {
92 
93  m_socket = 0;
94  // chain up
96 }
97 
99 {
101 
102  if (m_socket == 0) {
103  Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory>
105  m_socket = socketFactory->CreateSocket ();
106  m_socket->Bind ();
107  }
108 
109  m_count = 0;
110 
111  Simulator::Cancel (m_sendEvent);
112  m_sendEvent = Simulator::ScheduleNow (&Sender::SendPacket, this);
113 
114  // end Sender::StartApplication
115 }
116 
118 {
120  Simulator::Cancel (m_sendEvent);
121  // end Sender::StopApplication
122 }
123 
125 {
126  // NS_LOG_FUNCTION_NOARGS ();
127  NS_LOG_INFO ("Sending packet at " << Simulator::Now () << " to " <<
128  m_destAddr);
129 
130  Ptr<Packet> packet = Create<Packet>(m_pktSize);
131 
132  TimestampTag timestamp;
133  timestamp.SetTimestamp (Simulator::Now ());
134  packet->AddByteTag (timestamp);
135 
136  // Could connect the socket since the address never changes; using SendTo
137  // here simply because all of the standard apps do not.
138  m_socket->SendTo (packet, 0, InetSocketAddress (m_destAddr, m_destPort));
139 
140  // Report the event to the trace.
141  m_txTrace (packet);
142 
143  if (++m_count < m_numPkts) {
144  m_sendEvent = Simulator::Schedule (Seconds (m_interval->GetValue ()),
145  &Sender::SendPacket, this);
146  }
147 
148  // end Sender::SendPacket
149 }
150 
151 
152 
153 
154 //----------------------------------------------------------------------
155 //-- Receiver
156 //------------------------------------------------------
157 TypeId
159 {
160  static TypeId tid = TypeId ("Receiver")
162  .AddConstructor<Receiver> ()
163  .AddAttribute ("Port", "Listening port.",
164  UintegerValue (1603),
166  MakeUintegerChecker<uint32_t>())
167  ;
168  return tid;
169 }
170 
172  m_calc (0),
173  m_delay (0)
174 {
176  m_socket = 0;
177 }
178 
180 {
182 }
183 
184 void
186 {
188 
189  m_socket = 0;
190  // chain up
191  Application::DoDispose ();
192 }
193 
194 void
196 {
198 
199  if (m_socket == 0) {
201  (UdpSocketFactory::GetTypeId ());
202  m_socket = socketFactory->CreateSocket ();
203  InetSocketAddress local =
204  InetSocketAddress (Ipv4Address::GetAny (), m_port);
205  m_socket->Bind (local);
206  }
207 
209 
210  // end Receiver::StartApplication
211 }
212 
213 void
215 {
217 
218  if (m_socket != 0) {
220  }
221 
222  // end Receiver::StopApplication
223 }
224 
225 void
227 {
228  m_calc = calc;
229  // end Receiver::SetCounter
230 }
231 void
233 {
234  m_delay = delay;
235  // end Receiver::SetDelayTracker
236 }
237 
238 void
240 {
241  // NS_LOG_FUNCTION (this << socket << packet << from);
242 
243  Ptr<Packet> packet;
244  Address from;
245  while ((packet = socket->RecvFrom (from))) {
246  if (InetSocketAddress::IsMatchingType (from)) {
247  NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " <<
248  InetSocketAddress::ConvertFrom (from).GetIpv4 ());
249  }
250 
251  TimestampTag timestamp;
252  // Should never not be found since the sender is adding it, but
253  // you never know.
254  if (packet->FindFirstMatchingByteTag (timestamp)) {
255  Time tx = timestamp.GetTimestamp ();
256 
257  if (m_delay != 0) {
258  m_delay->Update (Simulator::Now () - tx);
259  }
260  }
261 
262  if (m_calc != 0) {
263  m_calc->Update ();
264  }
265 
266  // end receiving packets
267  }
268 
269  // end Receiver::Receive
270 }
271 
272 
273 
274 
275 //----------------------------------------------------------------------
276 //-- TimestampTag
277 //------------------------------------------------------
278 TypeId
280 {
281  static TypeId tid = TypeId ("TimestampTag")
282  .SetParent<Tag> ()
283  .AddConstructor<TimestampTag> ()
284  .AddAttribute ("Timestamp",
285  "Some momentous point in time!",
288  MakeTimeChecker ())
289  ;
290  return tid;
291 }
292 TypeId
294 {
295  return GetTypeId ();
296 }
297 
298 uint32_t
300 {
301  return 8;
302 }
303 void
305 {
306  int64_t t = m_timestamp.GetNanoSeconds ();
307  i.Write ((const uint8_t *)&t, 8);
308 }
309 void
311 {
312  int64_t t;
313  i.Read ((uint8_t *)&t, 8);
314  m_timestamp = NanoSeconds (t);
315 }
316 
317 void
319 {
320  m_timestamp = time;
321 }
322 Time
324 {
325  return m_timestamp;
326 }
327 
328 void
329 TimestampTag::Print (std::ostream &os) const
330 {
331  os << "t=" << m_timestamp;
332 }
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
Receiver::m_port
uint32_t m_port
Definition: wifi-example-apps.h:93
TimestampTag
Definition: wifi-example-apps.h:105
TimestampTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition: wifi-example-apps.cc:304
ns3::TimeMinMaxAvgTotalCalculator::Update
void Update(const Time i)
Updates all variables of TimeMinMaxAvgTotalCalculator.
Definition: time-data-calculators.cc:63
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::Socket::Bind
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
ns3::MakeIpv4AddressAccessor
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: ipv4-address.h:341
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Receiver::GetTypeId
static TypeId GetTypeId(void)
Definition: wifi-example-apps.cc:158
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Receiver::m_delay
Ptr< TimeMinMaxAvgTotalCalculator > m_delay
Definition: wifi-example-apps.h:96
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
TimestampTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition: wifi-example-apps.cc:299
Sender::m_pktSize
uint32_t m_pktSize
Definition: wifi-example-apps.h:53
Sender::StopApplication
virtual void StopApplication(void)
Application specific shutdown code.
Definition: wifi-example-apps.cc:117
Receiver::StopApplication
virtual void StopApplication(void)
Application specific shutdown code.
Definition: wifi-example-apps.cc:214
Sender::SendPacket
void SendPacket()
Definition: wifi-example-apps.cc:124
ns3::MakeIpv4AddressChecker
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
Definition: ipv4-address.cc:446
ns3::Application::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
Sender::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: wifi-example-apps.cc:89
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Receiver::SetDelayTracker
void SetDelayTracker(Ptr< TimeMinMaxAvgTotalCalculator > delay)
Definition: wifi-example-apps.cc:232
Receiver::m_calc
Ptr< CounterCalculator<> > m_calc
Definition: wifi-example-apps.h:95
Sender::m_destPort
uint32_t m_destPort
Definition: wifi-example-apps.h:55
ns3::MakeNullCallback
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1682
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
Receiver::m_socket
Ptr< Socket > m_socket
Definition: wifi-example-apps.h:91
TimestampTag::m_timestamp
Time m_timestamp
Definition: wifi-example-apps.h:121
Sender::m_interval
Ptr< ConstantRandomVariable > m_interval
Definition: wifi-example-apps.h:56
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Receiver::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: wifi-example-apps.cc:185
ns3::Socket::SetRecvCallback
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
TimestampTag::GetTimestamp
Time GetTimestamp(void) const
Definition: wifi-example-apps.cc:323
ns3::TagBuffer::Write
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
ns3::Now
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Sender::m_txTrace
TracedCallback< Ptr< const Packet > > m_txTrace
Definition: wifi-example-apps.h:62
TimestampTag::Print
void Print(std::ostream &os) const
Definition: wifi-example-apps.cc:329
ns3::Tag
tag a set of bytes in a packet
Definition: tag.h:37
ns3::NanoSeconds
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1313
ns3::Address
a polymophic address class
Definition: address.h:91
Receiver::Receive
void Receive(Ptr< Socket > socket)
Definition: wifi-example-apps.cc:239
wifi-example-apps.h
ns3::Simulator::Cancel
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:268
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Receiver::~Receiver
virtual ~Receiver()
Definition: wifi-example-apps.cc:179
ns3::Socket::RecvFrom
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.
Sender::~Sender
virtual ~Sender()
Definition: wifi-example-apps.cc:83
ns3::SocketFactory
Object to create transport layer instances that provide a socket API to applications.
Definition: socket-factory.h:49
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
Sender::StartApplication
virtual void StartApplication(void)
Application specific startup code.
Definition: wifi-example-apps.cc:98
ns3::Time::GetNanoSeconds
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
ns3::CounterCalculator<>
ns3::SocketFactory::CreateSocket
virtual Ptr< Socket > CreateSocket(void)=0
TimestampTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: wifi-example-apps.cc:293
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::MakePointerAccessor
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:227
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::UdpSocketFactory::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: udp-socket-factory.cc:27
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
Sender::Sender
Sender()
Definition: wifi-example-apps.cc:76
TimestampTag::GetTypeId
static TypeId GetTypeId(void)
Definition: wifi-example-apps.cc:279
ns3::CounterCalculator::Update
void Update()
Increments count by 1.
Definition: basic-data-calculators.h:346
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition: attribute.h:233
Sender::m_destAddr
Ipv4Address m_destAddr
Definition: wifi-example-apps.h:54
Sender::GetTypeId
static TypeId GetTypeId(void)
Definition: wifi-example-apps.cc:43
ns3::TagBuffer::Read
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TimestampTag::SetTimestamp
void SetTimestamp(Time time)
Definition: wifi-example-apps.cc:318
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
Receiver::StartApplication
virtual void StartApplication(void)
Application specific startup code.
Definition: wifi-example-apps.cc:195
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Application::GetNode
Ptr< Node > GetNode() const
Definition: application.cc:104
ns3::Packet::FindFirstMatchingByteTag
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:939
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:45
Receiver::SetCounter
void SetCounter(Ptr< CounterCalculator<> > calc)
Definition: wifi-example-apps.cc:226
ns3::Application
The base class for all ns3 applications.
Definition: application.h:61
ns3::TagBuffer
read and write tag data
Definition: tag-buffer.h:52
Sender::m_numPkts
uint32_t m_numPkts
Definition: wifi-example-apps.h:57
Receiver::Receiver
Receiver()
Definition: wifi-example-apps.cc:171
ns3::Packet::AddByteTag
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:912
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
TimestampTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition: wifi-example-apps.cc:310
ns3::MakeTimeAccessor
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1354
ns3::Ipv4AddressValue
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:341