A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
udp-echo-client.cc
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
38 UdpEchoClient::GetTypeId (void)
39 {
40  static TypeId tid = TypeId ("ns3::UdpEchoClient")
41  .SetParent<Application> ()
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 
74 UdpEchoClient::UdpEchoClient ()
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 
84 UdpEchoClient::~UdpEchoClient()
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
95 UdpEchoClient::SetRemote (Address ip, uint16_t port)
96 {
97  NS_LOG_FUNCTION (this << ip << port);
98  m_peerAddress = ip;
99  m_peerPort = port;
100 }
101 
102 void
103 UdpEchoClient::SetRemote (Ipv4Address ip, uint16_t port)
104 {
105  NS_LOG_FUNCTION (this << ip << port);
106  m_peerAddress = Address (ip);
107  m_peerPort = port;
108 }
109 
110 void
111 UdpEchoClient::SetRemote (Ipv6Address ip, uint16_t port)
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");
133  m_socket = Socket::CreateSocket (GetNode (), tid);
134  if (Ipv4Address::IsMatchingType(m_peerAddress) == true)
135  {
136  m_socket->Bind();
137  m_socket->Connect (InetSocketAddress (Ipv4Address::ConvertFrom(m_peerAddress), m_peerPort));
138  }
139  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
140  {
141  m_socket->Bind6();
142  m_socket->Connect (Inet6SocketAddress (Ipv6Address::ConvertFrom(m_peerAddress), m_peerPort));
143  }
144  }
145 
146  m_socket->SetRecvCallback (MakeCallback (&UdpEchoClient::HandleRead, this));
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 ();
159  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
160  m_socket = 0;
161  }
162 
163  Simulator::Cancel (m_sendEvent);
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
270 UdpEchoClient::ScheduleTransmit (Time dt)
271 {
272  NS_LOG_FUNCTION (this << dt);
273  m_sendEvent = Simulator::Schedule (dt, &UdpEchoClient::Send, this);
274 }
275 
276 void
277 UdpEchoClient::Send (void)
278 {
279  NS_LOG_FUNCTION (this);
280 
281  NS_ASSERT (m_sendEvent.IsExpired ());
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 she doesn't care
300  // about the data itself either by specifying the data size by setting
301  // the corresponding atribute 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 
314  if (Ipv4Address::IsMatchingType (m_peerAddress))
315  {
316  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
317  Ipv4Address::ConvertFrom (m_peerAddress) << " port " << m_peerPort);
318  }
319  else if (Ipv6Address::IsMatchingType (m_peerAddress))
320  {
321  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
322  Ipv6Address::ConvertFrom (m_peerAddress) << " port " << m_peerPort);
323  }
324 
325  if (m_sent < m_count)
326  {
327  ScheduleTransmit (m_interval);
328  }
329 }
330 
331 void
332 UdpEchoClient::HandleRead (Ptr<Socket> socket)
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.
keep track of time unit.
Definition: nstime.h:149
an Inet address class
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
TracedCallback< Ptr< const Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual void DoDispose(void)
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_INFO(msg)
Definition: log.h:264
static void Cancel(const EventId &id)
Definition: simulator.cc:267
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
void SetRemote(Address ip, uint16_t port)
virtual void StopApplication(void)
Application specific shutdown code.
a polymophic address class
Definition: address.h:86
Ptr< Node > GetNode() const
Definition: application.cc:103
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
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)
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)
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)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Callback< R > MakeNullCallback(void)
Definition: callback.h:781
static Time Now(void)
Definition: simulator.cc:179
void SetDataSize(uint32_t dataSize)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
uint32_t GetDataSize(void) const
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
uint16_t GetPort(void) const
Get the port.
uint16_t GetPort(void) const
static Ipv4Address ConvertFrom(const Address &address)
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
Definition: event-id.cc:53
a unique identifier for an interface.
Definition: type-id.h:44
static bool IsMatchingType(const Address &address)
void SetFill(std::string fill)
static TypeId LookupByName(std::string name)
Definition: type-id.cc:415
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.