A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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),
50  MakeUintegerAccessor (&Sender::m_pktSize),
51  MakeUintegerChecker<uint32_t>(1))
52  .AddAttribute ("Destination", "Target host address.",
53  Ipv4AddressValue ("255.255.255.255"),
54  MakeIpv4AddressAccessor (&Sender::m_destAddr),
55  MakeIpv4AddressChecker ())
56  .AddAttribute ("Port", "Destination app port.",
57  UintegerValue (1603),
58  MakeUintegerAccessor (&Sender::m_destPort),
59  MakeUintegerChecker<uint32_t>())
60  .AddAttribute ("NumPackets", "Total number of packets to send.",
61  UintegerValue (30),
62  MakeUintegerAccessor (&Sender::m_numPkts),
63  MakeUintegerChecker<uint32_t>(1))
64  .AddAttribute ("Interval", "Delay between transmissions.",
65  StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"),
66  MakePointerAccessor (&Sender::m_interval),
67  MakePointerChecker <RandomVariableStream>())
68  .AddTraceSource ("Tx", "A new packet is created and is sent",
70  ;
71  return tid;
72 }
73 
74 
76 {
78  m_interval = CreateObject<ConstantRandomVariable> ();
79  m_socket = 0;
80 }
81 
83 {
85 }
86 
87 void
89 {
91 
92  m_socket = 0;
93  // chain up
95 }
96 
98 {
100 
101  if (m_socket == 0) {
102  Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory>
104  m_socket = socketFactory->CreateSocket ();
105  m_socket->Bind ();
106  }
107 
108  m_count = 0;
109 
110  Simulator::Cancel (m_sendEvent);
111  m_sendEvent = Simulator::ScheduleNow (&Sender::SendPacket, this);
112 
113  // end Sender::StartApplication
114 }
115 
117 {
119  Simulator::Cancel (m_sendEvent);
120  // end Sender::StopApplication
121 }
122 
124 {
125  // NS_LOG_FUNCTION_NOARGS ();
126  NS_LOG_INFO ("Sending packet at " << Simulator::Now () << " to " <<
127  m_destAddr);
128 
129  Ptr<Packet> packet = Create<Packet>(m_pktSize);
130 
131  TimestampTag timestamp;
132  timestamp.SetTimestamp (Simulator::Now ());
133  packet->AddByteTag (timestamp);
134 
135  // Could connect the socket since the address never changes; using SendTo
136  // here simply because all of the standard apps do not.
137  m_socket->SendTo (packet, 0, InetSocketAddress (m_destAddr, m_destPort));
138 
139  // Report the event to the trace.
140  m_txTrace (packet);
141 
142  if (++m_count < m_numPkts) {
143  m_sendEvent = Simulator::Schedule (Seconds (m_interval->GetValue ()),
144  &Sender::SendPacket, this);
145  }
146 
147  // end Sender::SendPacket
148 }
149 
150 
151 
152 
153 //----------------------------------------------------------------------
154 //-- Receiver
155 //------------------------------------------------------
156 TypeId
158 {
159  static TypeId tid = TypeId ("Receiver")
161  .AddConstructor<Receiver> ()
162  .AddAttribute ("Port", "Listening port.",
163  UintegerValue (1603),
164  MakeUintegerAccessor (&Receiver::m_port),
165  MakeUintegerChecker<uint32_t>())
166  ;
167  return tid;
168 }
169 
171  m_calc (0),
172  m_delay (0)
173 {
175  m_socket = 0;
176 }
177 
179 {
181 }
182 
183 void
185 {
187 
188  m_socket = 0;
189  // chain up
190  Application::DoDispose ();
191 }
192 
193 void
195 {
197 
198  if (m_socket == 0) {
200  (UdpSocketFactory::GetTypeId ());
201  m_socket = socketFactory->CreateSocket ();
202  InetSocketAddress local =
203  InetSocketAddress (Ipv4Address::GetAny (), m_port);
204  m_socket->Bind (local);
205  }
206 
208 
209  // end Receiver::StartApplication
210 }
211 
212 void
214 {
216 
217  if (m_socket != 0) {
219  }
220 
221  // end Receiver::StopApplication
222 }
223 
224 void
226 {
227  m_calc = calc;
228  // end Receiver::SetCounter
229 }
230 void
232 {
233  m_delay = delay;
234  // end Receiver::SetDelayTracker
235 }
236 
237 void
239 {
240  // NS_LOG_FUNCTION (this << socket << packet << from);
241 
242  Ptr<Packet> packet;
243  Address from;
244  while ((packet = socket->RecvFrom (from))) {
245  if (InetSocketAddress::IsMatchingType (from)) {
246  NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " <<
247  InetSocketAddress::ConvertFrom (from).GetIpv4 ());
248  }
249 
250  TimestampTag timestamp;
251  // Should never not be found since the sender is adding it, but
252  // you never know.
253  if (packet->FindFirstMatchingByteTag (timestamp)) {
254  Time tx = timestamp.GetTimestamp ();
255 
256  if (m_delay != 0) {
257  m_delay->Update (Simulator::Now () - tx);
258  }
259  }
260 
261  if (m_calc != 0) {
262  m_calc->Update ();
263  }
264 
265  // end receiving packets
266  }
267 
268  // end Receiver::Receive
269 }
270 
271 
272 
273 
274 //----------------------------------------------------------------------
275 //-- TimestampTag
276 //------------------------------------------------------
277 TypeId
279 {
280  static TypeId tid = TypeId ("TimestampTag")
281  .SetParent<Tag> ()
282  .AddConstructor<TimestampTag> ()
283  .AddAttribute ("Timestamp",
284  "Some momentous point in time!",
286  MakeTimeAccessor (&TimestampTag::GetTimestamp),
287  MakeTimeChecker ())
288  ;
289  return tid;
290 }
291 TypeId
293 {
294  return GetTypeId ();
295 }
296 
297 uint32_t
299 {
300  return 8;
301 }
302 void
304 {
305  int64_t t = m_timestamp.GetNanoSeconds ();
306  i.Write ((const uint8_t *)&t, 8);
307 }
308 void
310 {
311  int64_t t;
312  i.Read ((uint8_t *)&t, 8);
313  m_timestamp = NanoSeconds (t);
314 }
315 
316 void
318 {
319  m_timestamp = time;
320 }
321 Time
323 {
324  return m_timestamp;
325 }
326 
327 void
328 TimestampTag::Print (std::ostream &os) const
329 {
330  os << "t=" << m_timestamp;
331 }
static TypeId GetTypeId(void)
Get the type ID.
bool FindFirstMatchingByteTag(Tag &tag) const
Definition: packet.cc:824
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SendPacket()
an Inet address class
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Ipv4Address m_destAddr
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
hold variables of type string
Definition: string.h:19
virtual void StopApplication(void)
Application specific shutdown code.
uint32_t GetSize(void) const
Definition: packet.h:650
virtual Ptr< Socket > CreateSocket(void)=0
#define NS_LOG_INFO(msg)
Definition: log.h:298
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
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
static TypeId GetTypeId(void)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Object to create transport layer instances that provide a socket API to applications.
virtual uint32_t GetSerializedSize(void) const
a polymophic address class
Definition: address.h:86
static TypeId GetTypeId(void)
uint32_t m_numPkts
The base class for all ns3 applications.
Definition: application.h:61
A class for an empty attribute value.
Definition: attribute.h:222
Hold an unsigned integer type.
Definition: uinteger.h:46
Ptr< ConstantRandomVariable > m_interval
uint32_t m_destPort
Ptr< Node > GetNode() const
Definition: application.cc:104
uint32_t m_pktSize
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual void Serialize(TagBuffer i) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:83
tag a set of bytes in a packet
Definition: tag.h:36
virtual void StopApplication(void)
Application specific shutdown code.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void StartApplication(void)
Application specific startup code.
static TypeId GetTypeId(void)
virtual ~Receiver()
virtual void Deserialize(TagBuffer i)
void SetDelayTracker(Ptr< TimeMinMaxAvgTotalCalculator > delay)
Time GetTimestamp(void) const
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
hold objects of type ns3::Ipv4Address
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
Ptr< Socket > m_socket
int64_t GetNanoSeconds(void) const
Definition: nstime.h:299
uint32_t m_port
read and write tag data
Definition: tag-buffer.h:51
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
void Receive(Ptr< Socket > socket)
virtual TypeId GetInstanceTypeId(void) const
void Update()
Increments count by 1.
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
TracedCallback< Ptr< const Packet > > m_txTrace
void Print(std::ostream &os) const
void SetTimestamp(Time time)
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< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void StartApplication(void)
Application specific startup code.
virtual ~Sender()
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:808
Ptr< CounterCalculator<> > m_calc
void SetCounter(Ptr< CounterCalculator<> > calc)