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 }
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:846
static TypeId GetTypeId(void)
Get the type ID.
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SendPacket()
an Inet address class
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Ipv4Address m_destAddr
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Hold variables of type string.
Definition: string.h:41
virtual void StopApplication(void)
Application specific shutdown code.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
virtual Ptr< Socket > CreateSocket(void)=0
Time GetTimestamp(void) const
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:290
virtual uint32_t GetSerializedSize(void) const
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Callback< R > MakeNullCallback(void)
Definition: callback.h:1786
static TypeId GetTypeId(void)
virtual void DoDispose(void)
Destructor implementation.
virtual void DoDispose(void)
Destructor implementation.
Object to create transport layer instances that provide a socket API to applications.
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void Serialize(TagBuffer i) const
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:449
static TypeId GetTypeId(void)
uint32_t m_numPkts
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
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
The base class for all ns3 applications.
Definition: application.h:60
A class for an empty attribute value.
Definition: attribute.h:232
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1094
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< ConstantRandomVariable > m_interval
uint32_t m_destPort
uint32_t m_pktSize
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1610
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
Ptr< Node > GetNode() const
Definition: application.cc:104
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:464
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
tag a set of bytes in a packet
Definition: tag.h:36
virtual void StopApplication(void)
Application specific shutdown code.
void Print(std::ostream &os) const
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:373
virtual void StartApplication(void)
Application specific startup code.
static TypeId GetTypeId(void)
virtual ~Receiver()
virtual void Deserialize(TagBuffer i)
void SetDelayTracker(Ptr< TimeMinMaxAvgTotalCalculator > delay)
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1579
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:357
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:1133
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
Ptr< Socket > m_socket
uint32_t m_port
read and write tag data
Definition: tag-buffer.h:51
void Receive(Ptr< Socket > socket)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
void Update()
Increments count by 1.
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TracedCallback< Ptr< const Packet > > m_txTrace
void SetTimestamp(Time time)
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
void Update(const Time i)
Updates all variables of TimeMinMaxAvgTotalCalculator.
Ptr< TimeMinMaxAvgTotalCalculator > m_delay
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.
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:357
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
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:819
virtual void StartApplication(void)
Application specific startup code.
virtual ~Sender()
Ptr< CounterCalculator<> > m_calc
void SetCounter(Ptr< CounterCalculator<> > calc)