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  .SetGroupName("Applications")
44  .AddConstructor<UdpEchoClient> ()
45  .AddAttribute ("MaxPackets",
46  "The maximum number of packets the application will send",
47  UintegerValue (100),
49  MakeUintegerChecker<uint32_t> ())
50  .AddAttribute ("Interval",
51  "The time to wait between packets",
52  TimeValue (Seconds (1.0)),
54  MakeTimeChecker ())
55  .AddAttribute ("RemoteAddress",
56  "The destination Address of the outbound packets",
57  AddressValue (),
60  .AddAttribute ("RemotePort",
61  "The destination port of the outbound packets",
62  UintegerValue (0),
64  MakeUintegerChecker<uint16_t> ())
65  .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
66  UintegerValue (100),
69  MakeUintegerChecker<uint32_t> ())
70  .AddTraceSource ("Tx", "A new packet is created and is sent",
72  "ns3::Packet::TracedCallback")
73  ;
74  return tid;
75 }
76 
78 {
79  NS_LOG_FUNCTION (this);
80  m_sent = 0;
81  m_socket = 0;
82  m_sendEvent = EventId ();
83  m_data = 0;
84  m_dataSize = 0;
85 }
86 
88 {
89  NS_LOG_FUNCTION (this);
90  m_socket = 0;
91 
92  delete [] m_data;
93  m_data = 0;
94  m_dataSize = 0;
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this << ip << port);
101  m_peerAddress = ip;
102  m_peerPort = port;
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION (this << addr);
109  m_peerAddress = addr;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this);
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION (this);
123 
124  if (m_socket == 0)
125  {
126  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
129  {
130  if (m_socket->Bind () == -1)
131  {
132  NS_FATAL_ERROR ("Failed to bind socket");
133  }
135  }
136  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
137  {
138  if (m_socket->Bind6 () == -1)
139  {
140  NS_FATAL_ERROR ("Failed to bind socket");
141  }
143  }
145  {
146  if (m_socket->Bind () == -1)
147  {
148  NS_FATAL_ERROR ("Failed to bind socket");
149  }
151  }
153  {
154  if (m_socket->Bind6 () == -1)
155  {
156  NS_FATAL_ERROR ("Failed to bind socket");
157  }
159  }
160  else
161  {
162  NS_ASSERT_MSG (false, "Incompatible address type: " << m_peerAddress);
163  }
164  }
165 
167  m_socket->SetAllowBroadcast (true);
168  ScheduleTransmit (Seconds (0.));
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this);
175 
176  if (m_socket != 0)
177  {
178  m_socket->Close ();
180  m_socket = 0;
181  }
182 
184 }
185 
186 void
187 UdpEchoClient::SetDataSize (uint32_t dataSize)
188 {
189  NS_LOG_FUNCTION (this << dataSize);
190 
191  //
192  // If the client is setting the echo packet data size this way, we infer
193  // that she doesn't care about the contents of the packet at all, so
194  // neither will we.
195  //
196  delete [] m_data;
197  m_data = 0;
198  m_dataSize = 0;
199  m_size = dataSize;
200 }
201 
202 uint32_t
204 {
205  NS_LOG_FUNCTION (this);
206  return m_size;
207 }
208 
209 void
210 UdpEchoClient::SetFill (std::string fill)
211 {
212  NS_LOG_FUNCTION (this << fill);
213 
214  uint32_t dataSize = fill.size () + 1;
215 
216  if (dataSize != m_dataSize)
217  {
218  delete [] m_data;
219  m_data = new uint8_t [dataSize];
220  m_dataSize = dataSize;
221  }
222 
223  memcpy (m_data, fill.c_str (), dataSize);
224 
225  //
226  // Overwrite packet size attribute.
227  //
228  m_size = dataSize;
229 }
230 
231 void
232 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
233 {
234  NS_LOG_FUNCTION (this << fill << dataSize);
235  if (dataSize != m_dataSize)
236  {
237  delete [] m_data;
238  m_data = new uint8_t [dataSize];
239  m_dataSize = dataSize;
240  }
241 
242  memset (m_data, fill, dataSize);
243 
244  //
245  // Overwrite packet size attribute.
246  //
247  m_size = dataSize;
248 }
249 
250 void
251 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
252 {
253  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
254  if (dataSize != m_dataSize)
255  {
256  delete [] m_data;
257  m_data = new uint8_t [dataSize];
258  m_dataSize = dataSize;
259  }
260 
261  if (fillSize >= dataSize)
262  {
263  memcpy (m_data, fill, dataSize);
264  m_size = dataSize;
265  return;
266  }
267 
268  //
269  // Do all but the final fill.
270  //
271  uint32_t filled = 0;
272  while (filled + fillSize < dataSize)
273  {
274  memcpy (&m_data[filled], fill, fillSize);
275  filled += fillSize;
276  }
277 
278  //
279  // Last fill may be partial
280  //
281  memcpy (&m_data[filled], fill, dataSize - filled);
282 
283  //
284  // Overwrite packet size attribute.
285  //
286  m_size = dataSize;
287 }
288 
289 void
291 {
292  NS_LOG_FUNCTION (this << dt);
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION (this);
300 
302 
303  Ptr<Packet> p;
304  if (m_dataSize)
305  {
306  //
307  // If m_dataSize is non-zero, we have a data buffer of the same size that we
308  // are expected to copy and send. This state of affairs is created if one of
309  // the Fill functions is called. In this case, m_size must have been set
310  // to agree with m_dataSize
311  //
312  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
313  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
314  p = Create<Packet> (m_data, m_dataSize);
315  }
316  else
317  {
318  //
319  // If m_dataSize is zero, the client has indicated that it doesn't care
320  // about the data itself either by specifying the data size by setting
321  // the corresponding attribute or by not calling a SetFill function. In
322  // this case, we don't worry about it either. But we do allow m_size
323  // to have a value different from the (zero) m_dataSize.
324  //
325  p = Create<Packet> (m_size);
326  }
327  // call to the trace sinks before the packet is actually sent,
328  // so that tags added to the packet can be sent as well
329  m_txTrace (p);
330  m_socket->Send (p);
331 
332  ++m_sent;
333 
335  {
336  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
338  }
340  {
341  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
343  }
345  {
346  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
348  }
350  {
351  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
353  }
354 
355  if (m_sent < m_count)
356  {
358  }
359 }
360 
361 void
363 {
364  NS_LOG_FUNCTION (this << socket);
365  Ptr<Packet> packet;
366  Address from;
367  while ((packet = socket->RecvFrom (from)))
368  {
370  {
371  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
372  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
374  }
375  else if (Inet6SocketAddress::IsMatchingType (from))
376  {
377  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
378  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
380  }
381  }
382 }
383 
384 } // 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:102
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:45
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
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:67
#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:796
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
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:346
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
void SetRemote(Address ip, uint16_t port)
set the remote address and port
virtual void StopApplication(void)
Application specific shutdown code.
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:446
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
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
The base class for all ns3 applications.
Definition: application.h:60
AttributeValue implementation for Time.
Definition: nstime.h:1055
uint8_t * m_data
packet payload data
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< Node > GetNode() const
Definition: application.cc:104
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:71
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:83
A Udp Echo client.
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:1056
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
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:90
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:993
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:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
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)
Get a TypeId by name.
Definition: type-id.cc:823
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.