A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_OBJECT_ENSURE_REGISTERED (UdpEchoClient);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::UdpEchoClient")
42  .AddConstructor<UdpEchoClient> ()
43  .AddAttribute ("MaxPackets",
44  "The maximum number of packets the application will send",
45  UintegerValue (100),
46  MakeUintegerAccessor (&UdpEchoClient::m_count),
47  MakeUintegerChecker<uint32_t> ())
48  .AddAttribute ("Interval",
49  "The time to wait between packets",
50  TimeValue (Seconds (1.0)),
51  MakeTimeAccessor (&UdpEchoClient::m_interval),
52  MakeTimeChecker ())
53  .AddAttribute ("RemoteAddress",
54  "The destination Address of the outbound packets",
55  AddressValue (),
56  MakeAddressAccessor (&UdpEchoClient::m_peerAddress),
57  MakeAddressChecker ())
58  .AddAttribute ("RemotePort",
59  "The destination port of the outbound packets",
60  UintegerValue (0),
61  MakeUintegerAccessor (&UdpEchoClient::m_peerPort),
62  MakeUintegerChecker<uint16_t> ())
63  .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
64  UintegerValue (100),
65  MakeUintegerAccessor (&UdpEchoClient::SetDataSize,
67  MakeUintegerChecker<uint32_t> ())
68  .AddTraceSource ("Tx", "A new packet is created and is sent",
70  ;
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION (this);
77  m_sent = 0;
78  m_socket = 0;
79  m_sendEvent = EventId ();
80  m_data = 0;
81  m_dataSize = 0;
82 }
83 
85 {
86  NS_LOG_FUNCTION (this);
87  m_socket = 0;
88 
89  delete [] m_data;
90  m_data = 0;
91  m_dataSize = 0;
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION (this << ip << port);
98  m_peerAddress = ip;
99  m_peerPort = port;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this << ip << port);
106  m_peerAddress = Address (ip);
107  m_peerPort = port;
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION (this << ip << port);
114  m_peerAddress = Address (ip);
115  m_peerPort = port;
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION (this);
123 }
124 
125 void
127 {
128  NS_LOG_FUNCTION (this);
129 
130  if (m_socket == 0)
131  {
132  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
135  {
136  m_socket->Bind();
138  }
139  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
140  {
141  m_socket->Bind6();
143  }
144  }
145 
147 
148  ScheduleTransmit (Seconds (0.));
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this);
155 
156  if (m_socket != 0)
157  {
158  m_socket->Close ();
160  m_socket = 0;
161  }
162 
164 }
165 
166 void
167 UdpEchoClient::SetDataSize (uint32_t dataSize)
168 {
169  NS_LOG_FUNCTION (this << dataSize);
170 
171  //
172  // If the client is setting the echo packet data size this way, we infer
173  // that she doesn't care about the contents of the packet at all, so
174  // neither will we.
175  //
176  delete [] m_data;
177  m_data = 0;
178  m_dataSize = 0;
179  m_size = dataSize;
180 }
181 
182 uint32_t
184 {
185  NS_LOG_FUNCTION (this);
186  return m_size;
187 }
188 
189 void
190 UdpEchoClient::SetFill (std::string fill)
191 {
192  NS_LOG_FUNCTION (this << fill);
193 
194  uint32_t dataSize = fill.size () + 1;
195 
196  if (dataSize != m_dataSize)
197  {
198  delete [] m_data;
199  m_data = new uint8_t [dataSize];
200  m_dataSize = dataSize;
201  }
202 
203  memcpy (m_data, fill.c_str (), dataSize);
204 
205  //
206  // Overwrite packet size attribute.
207  //
208  m_size = dataSize;
209 }
210 
211 void
212 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
213 {
214  NS_LOG_FUNCTION (this << fill << dataSize);
215  if (dataSize != m_dataSize)
216  {
217  delete [] m_data;
218  m_data = new uint8_t [dataSize];
219  m_dataSize = dataSize;
220  }
221 
222  memset (m_data, fill, dataSize);
223 
224  //
225  // Overwrite packet size attribute.
226  //
227  m_size = dataSize;
228 }
229 
230 void
231 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
232 {
233  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
234  if (dataSize != m_dataSize)
235  {
236  delete [] m_data;
237  m_data = new uint8_t [dataSize];
238  m_dataSize = dataSize;
239  }
240 
241  if (fillSize >= dataSize)
242  {
243  memcpy (m_data, fill, dataSize);
244  m_size = dataSize;
245  return;
246  }
247 
248  //
249  // Do all but the final fill.
250  //
251  uint32_t filled = 0;
252  while (filled + fillSize < dataSize)
253  {
254  memcpy (&m_data[filled], fill, fillSize);
255  filled += fillSize;
256  }
257 
258  //
259  // Last fill may be partial
260  //
261  memcpy (&m_data[filled], fill, dataSize - filled);
262 
263  //
264  // Overwrite packet size attribute.
265  //
266  m_size = dataSize;
267 }
268 
269 void
271 {
272  NS_LOG_FUNCTION (this << dt);
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION (this);
280 
282 
283  Ptr<Packet> p;
284  if (m_dataSize)
285  {
286  //
287  // If m_dataSize is non-zero, we have a data buffer of the same size that we
288  // are expected to copy and send. This state of affairs is created if one of
289  // the Fill functions is called. In this case, m_size must have been set
290  // to agree with m_dataSize
291  //
292  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
293  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
294  p = Create<Packet> (m_data, m_dataSize);
295  }
296  else
297  {
298  //
299  // If m_dataSize is zero, the client has indicated that it doesn't care
300  // about the data itself either by specifying the data size by setting
301  // the corresponding attribute or by not calling a SetFill function. In
302  // this case, we don't worry about it either. But we do allow m_size
303  // to have a value different from the (zero) m_dataSize.
304  //
305  p = Create<Packet> (m_size);
306  }
307  // call to the trace sinks before the packet is actually sent,
308  // so that tags added to the packet can be sent as well
309  m_txTrace (p);
310  m_socket->Send (p);
311 
312  ++m_sent;
313 
315  {
316  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
318  }
320  {
321  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
323  }
324 
325  if (m_sent < m_count)
326  {
328  }
329 }
330 
331 void
333 {
334  NS_LOG_FUNCTION (this << socket);
335  Ptr<Packet> packet;
336  Address from;
337  while ((packet = socket->RecvFrom (from)))
338  {
340  {
341  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
342  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
344  }
345  else if (Inet6SocketAddress::IsMatchingType (from))
346  {
347  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
348  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
350  }
351  }
352 }
353 
354 } // 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 the class in the ns-3 factory.
Definition: object-base.h:38
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
Ptr< Socket > m_socket
Socket.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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
Callback< R > MakeNullCallback(void)
Definition: callback.h:1429
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:825
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:86
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
The base class for all ns3 applications.
Definition: application.h:60
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
uint8_t * m_data
packet payload data
Hold an unsigned integer type.
Definition: uinteger.h:46
Ptr< Node > GetNode() const
Definition: application.cc:103
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1283
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
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.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
hold objects of type ns3::Address
EventId m_sendEvent
Event to send the next packet.
an identifier for simulation events.
Definition: event-id.h:46
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:845
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:53
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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:535
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.