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 << ip << port);
109  m_peerAddress = Address (ip);
110  m_peerPort = port;
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this << ip << port);
117  m_peerAddress = Address (ip);
118  m_peerPort = port;
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this);
132 
133  if (m_socket == 0)
134  {
135  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
138  {
139  m_socket->Bind();
141  }
142  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
143  {
144  m_socket->Bind6();
146  }
147  }
148 
150  m_socket->SetAllowBroadcast (true);
151  ScheduleTransmit (Seconds (0.));
152 }
153 
154 void
156 {
157  NS_LOG_FUNCTION (this);
158 
159  if (m_socket != 0)
160  {
161  m_socket->Close ();
163  m_socket = 0;
164  }
165 
167 }
168 
169 void
170 UdpEchoClient::SetDataSize (uint32_t dataSize)
171 {
172  NS_LOG_FUNCTION (this << dataSize);
173 
174  //
175  // If the client is setting the echo packet data size this way, we infer
176  // that she doesn't care about the contents of the packet at all, so
177  // neither will we.
178  //
179  delete [] m_data;
180  m_data = 0;
181  m_dataSize = 0;
182  m_size = dataSize;
183 }
184 
185 uint32_t
187 {
188  NS_LOG_FUNCTION (this);
189  return m_size;
190 }
191 
192 void
193 UdpEchoClient::SetFill (std::string fill)
194 {
195  NS_LOG_FUNCTION (this << fill);
196 
197  uint32_t dataSize = fill.size () + 1;
198 
199  if (dataSize != m_dataSize)
200  {
201  delete [] m_data;
202  m_data = new uint8_t [dataSize];
203  m_dataSize = dataSize;
204  }
205 
206  memcpy (m_data, fill.c_str (), dataSize);
207 
208  //
209  // Overwrite packet size attribute.
210  //
211  m_size = dataSize;
212 }
213 
214 void
215 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
216 {
217  NS_LOG_FUNCTION (this << fill << dataSize);
218  if (dataSize != m_dataSize)
219  {
220  delete [] m_data;
221  m_data = new uint8_t [dataSize];
222  m_dataSize = dataSize;
223  }
224 
225  memset (m_data, fill, dataSize);
226 
227  //
228  // Overwrite packet size attribute.
229  //
230  m_size = dataSize;
231 }
232 
233 void
234 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
235 {
236  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
237  if (dataSize != m_dataSize)
238  {
239  delete [] m_data;
240  m_data = new uint8_t [dataSize];
241  m_dataSize = dataSize;
242  }
243 
244  if (fillSize >= dataSize)
245  {
246  memcpy (m_data, fill, dataSize);
247  m_size = dataSize;
248  return;
249  }
250 
251  //
252  // Do all but the final fill.
253  //
254  uint32_t filled = 0;
255  while (filled + fillSize < dataSize)
256  {
257  memcpy (&m_data[filled], fill, fillSize);
258  filled += fillSize;
259  }
260 
261  //
262  // Last fill may be partial
263  //
264  memcpy (&m_data[filled], fill, dataSize - filled);
265 
266  //
267  // Overwrite packet size attribute.
268  //
269  m_size = dataSize;
270 }
271 
272 void
274 {
275  NS_LOG_FUNCTION (this << dt);
277 }
278 
279 void
281 {
282  NS_LOG_FUNCTION (this);
283 
285 
286  Ptr<Packet> p;
287  if (m_dataSize)
288  {
289  //
290  // If m_dataSize is non-zero, we have a data buffer of the same size that we
291  // are expected to copy and send. This state of affairs is created if one of
292  // the Fill functions is called. In this case, m_size must have been set
293  // to agree with m_dataSize
294  //
295  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
296  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
297  p = Create<Packet> (m_data, m_dataSize);
298  }
299  else
300  {
301  //
302  // If m_dataSize is zero, the client has indicated that it doesn't care
303  // about the data itself either by specifying the data size by setting
304  // the corresponding attribute or by not calling a SetFill function. In
305  // this case, we don't worry about it either. But we do allow m_size
306  // to have a value different from the (zero) m_dataSize.
307  //
308  p = Create<Packet> (m_size);
309  }
310  // call to the trace sinks before the packet is actually sent,
311  // so that tags added to the packet can be sent as well
312  m_txTrace (p);
313  m_socket->Send (p);
314 
315  ++m_sent;
316 
318  {
319  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
321  }
323  {
324  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
326  }
327 
328  if (m_sent < m_count)
329  {
331  }
332 }
333 
334 void
336 {
337  NS_LOG_FUNCTION (this << socket);
338  Ptr<Packet> packet;
339  Address from;
340  while ((packet = socket->RecvFrom (from)))
341  {
343  {
344  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
345  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
347  }
348  else if (Inet6SocketAddress::IsMatchingType (from))
349  {
350  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
351  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
353  }
354  }
355 }
356 
357 } // 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:786
#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:1626
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: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:1216
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:1480
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: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:90
Describes an IPv6 address.
Definition: ipv6-address.h:48
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: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:826
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:751
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.