A Discrete-Event Network Simulator
API
udp-echo-client.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 #include "ns3/log.h"
19 #include "ns3/ipv4-address.h"
20 #include "ns3/ipv6-address.h"
21 #include "ns3/nstime.h"
22 #include "ns3/inet-socket-address.h"
23 #include "ns3/inet6-socket-address.h"
24 #include "ns3/socket.h"
25 #include "ns3/simulator.h"
26 #include "ns3/socket-factory.h"
27 #include "ns3/packet.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/trace-source-accessor.h"
30 #include "udp-echo-client.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("UdpEchoClientApplication");
35 
36 NS_OBJECT_ENSURE_REGISTERED (UdpEchoClient);
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::UdpEchoClient")
43  .AddConstructor<UdpEchoClient> ()
44  .AddAttribute ("MaxPackets",
45  "The maximum number of packets the application will send",
46  UintegerValue (100),
48  MakeUintegerChecker<uint32_t> ())
49  .AddAttribute ("Interval",
50  "The time to wait between packets",
51  TimeValue (Seconds (1.0)),
53  MakeTimeChecker ())
54  .AddAttribute ("RemoteAddress",
55  "The destination Address of the outbound packets",
56  AddressValue (),
59  .AddAttribute ("RemotePort",
60  "The destination port of the outbound packets",
61  UintegerValue (0),
63  MakeUintegerChecker<uint16_t> ())
64  .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
65  UintegerValue (100),
68  MakeUintegerChecker<uint32_t> ())
69  .AddTraceSource ("Tx", "A new packet is created and is sent",
71  "ns3::Packet::TracedCallback")
72  ;
73  return tid;
74 }
75 
77 {
78  NS_LOG_FUNCTION (this);
79  m_sent = 0;
80  m_socket = 0;
81  m_sendEvent = EventId ();
82  m_data = 0;
83  m_dataSize = 0;
84 }
85 
87 {
88  NS_LOG_FUNCTION (this);
89  m_socket = 0;
90 
91  delete [] m_data;
92  m_data = 0;
93  m_dataSize = 0;
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION (this << ip << port);
100  m_peerAddress = ip;
101  m_peerPort = port;
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION (this << ip << port);
108  m_peerAddress = Address (ip);
109  m_peerPort = port;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << ip << port);
116  m_peerAddress = Address (ip);
117  m_peerPort = port;
118 }
119 
120 void
122 {
123  NS_LOG_FUNCTION (this);
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION (this);
131 
132  if (m_socket == 0)
133  {
134  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
137  {
138  m_socket->Bind();
140  }
141  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
142  {
143  m_socket->Bind6();
145  }
146  }
147 
149 
150  ScheduleTransmit (Seconds (0.));
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this);
157 
158  if (m_socket != 0)
159  {
160  m_socket->Close ();
162  m_socket = 0;
163  }
164 
166 }
167 
168 void
169 UdpEchoClient::SetDataSize (uint32_t dataSize)
170 {
171  NS_LOG_FUNCTION (this << dataSize);
172 
173  //
174  // If the client is setting the echo packet data size this way, we infer
175  // that she doesn't care about the contents of the packet at all, so
176  // neither will we.
177  //
178  delete [] m_data;
179  m_data = 0;
180  m_dataSize = 0;
181  m_size = dataSize;
182 }
183 
184 uint32_t
186 {
187  NS_LOG_FUNCTION (this);
188  return m_size;
189 }
190 
191 void
192 UdpEchoClient::SetFill (std::string fill)
193 {
194  NS_LOG_FUNCTION (this << fill);
195 
196  uint32_t dataSize = fill.size () + 1;
197 
198  if (dataSize != m_dataSize)
199  {
200  delete [] m_data;
201  m_data = new uint8_t [dataSize];
202  m_dataSize = dataSize;
203  }
204 
205  memcpy (m_data, fill.c_str (), dataSize);
206 
207  //
208  // Overwrite packet size attribute.
209  //
210  m_size = dataSize;
211 }
212 
213 void
214 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
215 {
216  NS_LOG_FUNCTION (this << fill << dataSize);
217  if (dataSize != m_dataSize)
218  {
219  delete [] m_data;
220  m_data = new uint8_t [dataSize];
221  m_dataSize = dataSize;
222  }
223 
224  memset (m_data, fill, dataSize);
225 
226  //
227  // Overwrite packet size attribute.
228  //
229  m_size = dataSize;
230 }
231 
232 void
233 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
234 {
235  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
236  if (dataSize != m_dataSize)
237  {
238  delete [] m_data;
239  m_data = new uint8_t [dataSize];
240  m_dataSize = dataSize;
241  }
242 
243  if (fillSize >= dataSize)
244  {
245  memcpy (m_data, fill, dataSize);
246  m_size = dataSize;
247  return;
248  }
249 
250  //
251  // Do all but the final fill.
252  //
253  uint32_t filled = 0;
254  while (filled + fillSize < dataSize)
255  {
256  memcpy (&m_data[filled], fill, fillSize);
257  filled += fillSize;
258  }
259 
260  //
261  // Last fill may be partial
262  //
263  memcpy (&m_data[filled], fill, dataSize - filled);
264 
265  //
266  // Overwrite packet size attribute.
267  //
268  m_size = dataSize;
269 }
270 
271 void
273 {
274  NS_LOG_FUNCTION (this << dt);
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION (this);
282 
284 
285  Ptr<Packet> p;
286  if (m_dataSize)
287  {
288  //
289  // If m_dataSize is non-zero, we have a data buffer of the same size that we
290  // are expected to copy and send. This state of affairs is created if one of
291  // the Fill functions is called. In this case, m_size must have been set
292  // to agree with m_dataSize
293  //
294  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
295  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
296  p = Create<Packet> (m_data, m_dataSize);
297  }
298  else
299  {
300  //
301  // If m_dataSize is zero, the client has indicated that it doesn't care
302  // about the data itself either by specifying the data size by setting
303  // the corresponding attribute or by not calling a SetFill function. In
304  // this case, we don't worry about it either. But we do allow m_size
305  // to have a value different from the (zero) m_dataSize.
306  //
307  p = Create<Packet> (m_size);
308  }
309  // call to the trace sinks before the packet is actually sent,
310  // so that tags added to the packet can be sent as well
311  m_txTrace (p);
312  m_socket->Send (p);
313 
314  ++m_sent;
315 
317  {
318  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
320  }
322  {
323  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
325  }
326 
327  if (m_sent < m_count)
328  {
330  }
331 }
332 
333 void
335 {
336  NS_LOG_FUNCTION (this << socket);
337  Ptr<Packet> packet;
338  Address from;
339  while ((packet = socket->RecvFrom (from)))
340  {
342  {
343  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
344  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
346  }
347  else if (Inet6SocketAddress::IsMatchingType (from))
348  {
349  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
350  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
352  }
353  }
354 }
355 
356 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
an Inet address class
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TracedCallback< Ptr< const Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
uint32_t m_dataSize
packet payload size (must be equal to m_size)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
Ptr< Socket > m_socket
Socket.
virtual void DoDispose(void)
Destructor implementation.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
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:311
Callback< R > MakeNullCallback(void)
Definition: callback.h:1436
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:819
virtual void StopApplication(void)
Application specific shutdown code.
void SetRemote(Ipv4Address ip, uint16_t port)
set the remote address and port
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:439
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: address.h:278
The base class for all ns3 applications.
Definition: application.h:60
AttributeValue implementation for Time.
Definition: nstime.h:921
uint8_t * m_data
packet payload data
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< Node > GetNode() const
Definition: application.cc:103
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:70
static bool IsMatchingType(const Address &address)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:82
virtual void StartApplication(void)
Application specific startup code.
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.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint32_t m_count
Maximum number of packets the application will send.
static TypeId GetTypeId(void)
Get the type ID.
Time m_interval
Packet inter-send time.
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
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:922
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void SetDataSize(uint32_t dataSize)
Set the data size of the packet (the number of bytes that are sent as data to the server)...
void Send(void)
Send a packet.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
AttributeValue implementation for Address.
Definition: address.h:278
EventId m_sendEvent
Event to send the next packet.
An identifier for simulation events.
Definition: event-id.h:53
uint32_t GetDataSize(void) const
Get the number of data bytes that will be sent to the server.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Address m_peerAddress
Remote peer address.
static bool IsMatchingType(const Address &addr)
If the address match.
uint32_t m_sent
Counter for sent packets.
uint16_t GetPort(void) const
Get the port.
uint16_t GetPort(void) const
static Ipv4Address ConvertFrom(const Address &address)
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.
uint32_t m_size
Size of the sent packet.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
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:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
static bool IsMatchingType(const Address &address)
uint16_t m_peerPort
Remote peer port.
void SetFill(std::string fill)
Set the data fill of the packet (what is sent as data to the server) to the zero-terminated contents ...
static TypeId LookupByName(std::string name)
Definition: type-id.cc:556
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.