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  m_socket->Bind();
132  }
133  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
134  {
135  m_socket->Bind6();
137  }
139  {
140  m_socket->Bind ();
142  }
144  {
145  m_socket->Bind6 ();
147  }
148  else
149  {
150  NS_ASSERT_MSG (false, "Incompatible address type: " << m_peerAddress);
151  }
152  }
153 
155  m_socket->SetAllowBroadcast (true);
156  ScheduleTransmit (Seconds (0.));
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this);
163 
164  if (m_socket != 0)
165  {
166  m_socket->Close ();
168  m_socket = 0;
169  }
170 
172 }
173 
174 void
175 UdpEchoClient::SetDataSize (uint32_t dataSize)
176 {
177  NS_LOG_FUNCTION (this << dataSize);
178 
179  //
180  // If the client is setting the echo packet data size this way, we infer
181  // that she doesn't care about the contents of the packet at all, so
182  // neither will we.
183  //
184  delete [] m_data;
185  m_data = 0;
186  m_dataSize = 0;
187  m_size = dataSize;
188 }
189 
190 uint32_t
192 {
193  NS_LOG_FUNCTION (this);
194  return m_size;
195 }
196 
197 void
198 UdpEchoClient::SetFill (std::string fill)
199 {
200  NS_LOG_FUNCTION (this << fill);
201 
202  uint32_t dataSize = fill.size () + 1;
203 
204  if (dataSize != m_dataSize)
205  {
206  delete [] m_data;
207  m_data = new uint8_t [dataSize];
208  m_dataSize = dataSize;
209  }
210 
211  memcpy (m_data, fill.c_str (), dataSize);
212 
213  //
214  // Overwrite packet size attribute.
215  //
216  m_size = dataSize;
217 }
218 
219 void
220 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
221 {
222  NS_LOG_FUNCTION (this << fill << dataSize);
223  if (dataSize != m_dataSize)
224  {
225  delete [] m_data;
226  m_data = new uint8_t [dataSize];
227  m_dataSize = dataSize;
228  }
229 
230  memset (m_data, fill, dataSize);
231 
232  //
233  // Overwrite packet size attribute.
234  //
235  m_size = dataSize;
236 }
237 
238 void
239 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
240 {
241  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
242  if (dataSize != m_dataSize)
243  {
244  delete [] m_data;
245  m_data = new uint8_t [dataSize];
246  m_dataSize = dataSize;
247  }
248 
249  if (fillSize >= dataSize)
250  {
251  memcpy (m_data, fill, dataSize);
252  m_size = dataSize;
253  return;
254  }
255 
256  //
257  // Do all but the final fill.
258  //
259  uint32_t filled = 0;
260  while (filled + fillSize < dataSize)
261  {
262  memcpy (&m_data[filled], fill, fillSize);
263  filled += fillSize;
264  }
265 
266  //
267  // Last fill may be partial
268  //
269  memcpy (&m_data[filled], fill, dataSize - filled);
270 
271  //
272  // Overwrite packet size attribute.
273  //
274  m_size = dataSize;
275 }
276 
277 void
279 {
280  NS_LOG_FUNCTION (this << dt);
282 }
283 
284 void
286 {
287  NS_LOG_FUNCTION (this);
288 
290 
291  Ptr<Packet> p;
292  if (m_dataSize)
293  {
294  //
295  // If m_dataSize is non-zero, we have a data buffer of the same size that we
296  // are expected to copy and send. This state of affairs is created if one of
297  // the Fill functions is called. In this case, m_size must have been set
298  // to agree with m_dataSize
299  //
300  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
301  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
302  p = Create<Packet> (m_data, m_dataSize);
303  }
304  else
305  {
306  //
307  // If m_dataSize is zero, the client has indicated that it doesn't care
308  // about the data itself either by specifying the data size by setting
309  // the corresponding attribute or by not calling a SetFill function. In
310  // this case, we don't worry about it either. But we do allow m_size
311  // to have a value different from the (zero) m_dataSize.
312  //
313  p = Create<Packet> (m_size);
314  }
315  // call to the trace sinks before the packet is actually sent,
316  // so that tags added to the packet can be sent as well
317  m_txTrace (p);
318  m_socket->Send (p);
319 
320  ++m_sent;
321 
323  {
324  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
326  }
328  {
329  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
331  }
333  {
334  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
336  }
338  {
339  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
341  }
342 
343  if (m_sent < m_count)
344  {
346  }
347 }
348 
349 void
351 {
352  NS_LOG_FUNCTION (this << socket);
353  Ptr<Packet> packet;
354  Address from;
355  while ((packet = socket->RecvFrom (from)))
356  {
358  {
359  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
360  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
362  }
363  else if (Inet6SocketAddress::IsMatchingType (from))
364  {
365  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
366  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
368  }
369  }
370 }
371 
372 } // 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:44
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:792
#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:321
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:1238
The base class for all ns3 applications.
Definition: application.h:60
AttributeValue implementation for Time.
Definition: nstime.h:957
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:958
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
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:895
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:904
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:813
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.