A Discrete-Event Network Simulator
API
udp-trace-client.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
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  * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 #include "ns3/log.h"
22 #include "ns3/ipv4-address.h"
23 #include "ns3/nstime.h"
24 #include "ns3/inet-socket-address.h"
25 #include "ns3/inet6-socket-address.h"
26 #include "ns3/socket.h"
27 #include "ns3/simulator.h"
28 #include "ns3/socket-factory.h"
29 #include "ns3/packet.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/string.h"
32 #include "seq-ts-header.h"
33 #include "udp-trace-client.h"
34 #include <cstdlib>
35 #include <cstdio>
36 #include <fstream>
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("UdpTraceClient");
41 
42 NS_OBJECT_ENSURE_REGISTERED (UdpTraceClient);
43 
47 struct UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
48  { 0, 534, 'I'},
49  { 40, 1542, 'P'},
50  { 120, 134, 'B'},
51  { 80, 390, 'B'},
52  { 240, 765, 'P'},
53  { 160, 407, 'B'},
54  { 200, 504, 'B'},
55  { 360, 903, 'P'},
56  { 280, 421, 'B'},
57  { 320, 587, 'B'}
58 };
59 
60 TypeId
62 {
63  static TypeId tid = TypeId ("ns3::UdpTraceClient")
65  .AddConstructor<UdpTraceClient> ()
66  .AddAttribute ("RemoteAddress",
67  "The destination Address of the outbound packets",
68  AddressValue (),
71  .AddAttribute ("RemotePort",
72  "The destination port of the outbound packets",
73  UintegerValue (100),
75  MakeUintegerChecker<uint16_t> ())
76  .AddAttribute ("MaxPacketSize",
77  "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
78  UintegerValue (1024),
80  MakeUintegerChecker<uint32_t> ())
81  .AddAttribute ("TraceFilename",
82  "Name of file to load a trace from. By default, uses a hardcoded trace.",
83  StringValue (""),
86 
87  ;
88  return tid;
89 }
90 
92 {
93  NS_LOG_FUNCTION (this);
94  m_sent = 0;
95  m_socket = 0;
96  m_sendEvent = EventId ();
97  m_maxPacketSize = 1400;
98 }
99 
101  char *traceFile)
102 {
103  NS_LOG_FUNCTION (this);
104  m_sent = 0;
105  m_socket = 0;
106  m_sendEvent = EventId ();
107  m_peerAddress = ip;
108  m_peerPort = port;
109  m_currentEntry = 0;
110  m_maxPacketSize = 1400;
111  if (traceFile != NULL)
112  {
113  SetTraceFile (traceFile);
114  }
115 }
116 
118 {
119  NS_LOG_FUNCTION (this);
120  m_entries.clear ();
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this << ip << port);
127  m_entries.clear ();
128  m_peerAddress = ip;
129  m_peerPort = port;
130 }
131 
132 void
134 {
135  NS_LOG_FUNCTION (this << ip << port);
136  m_entries.clear ();
137  m_peerAddress = Address (ip);
138  m_peerPort = port;
139 }
140 
141 void
143 {
144  NS_LOG_FUNCTION (this << ip << port);
145  m_entries.clear ();
146  m_peerAddress = Address (ip);
147  m_peerPort = port;
148 }
149 
150 void
151 UdpTraceClient::SetTraceFile (std::string traceFile)
152 {
153  NS_LOG_FUNCTION (this << traceFile);
154  if (traceFile == "")
155  {
156  LoadDefaultTrace ();
157  }
158  else
159  {
160  LoadTrace (traceFile);
161  }
162 }
163 
164 void
165 UdpTraceClient::SetMaxPacketSize (uint16_t maxPacketSize)
166 {
167  NS_LOG_FUNCTION (this << maxPacketSize);
168  m_maxPacketSize = maxPacketSize;
169 }
170 
171 
173 {
174  NS_LOG_FUNCTION (this);
175  return m_maxPacketSize;
176 }
177 
178 
179 void
181 {
182  NS_LOG_FUNCTION (this);
184 }
185 
186 void
187 UdpTraceClient::LoadTrace (std::string filename)
188 {
189  NS_LOG_FUNCTION (this << filename);
190  uint32_t time, index, size, prevTime = 0;
191  char frameType;
192  TraceEntry entry;
193  std::ifstream ifTraceFile;
194  ifTraceFile.open (filename.c_str (), std::ifstream::in);
195  m_entries.clear ();
196  if (!ifTraceFile.good ())
197  {
198  LoadDefaultTrace ();
199  }
200  while (ifTraceFile.good ())
201  {
202  ifTraceFile >> index >> frameType >> time >> size;
203  if (frameType == 'B')
204  {
205  entry.timeToSend = 0;
206  }
207  else
208  {
209  entry.timeToSend = time - prevTime;
210  prevTime = time;
211  }
212  entry.packetSize = size;
213  entry.frameType = frameType;
214  m_entries.push_back (entry);
215  }
216  ifTraceFile.close ();
217  m_currentEntry = 0;
218 }
219 
220 void
222 {
223  NS_LOG_FUNCTION (this);
224  uint32_t prevTime = 0;
225  for (uint32_t i = 0; i < (sizeof (g_defaultEntries) / sizeof (struct TraceEntry)); i++)
226  {
227  struct TraceEntry entry = g_defaultEntries[i];
228  if (entry.frameType == 'B')
229  {
230  entry.timeToSend = 0;
231  }
232  else
233  {
234  uint32_t tmp = entry.timeToSend;
235  entry.timeToSend -= prevTime;
236  prevTime = tmp;
237  }
238  m_entries.push_back (entry);
239  }
240  m_currentEntry = 0;
241 }
242 
243 void
245 {
246  NS_LOG_FUNCTION (this);
247 
248  if (m_socket == 0)
249  {
250  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
253  {
254  m_socket->Bind ();
256  }
257  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
258  {
259  m_socket->Bind6 ();
261  }
262  }
265 }
266 
267 void
269 {
270  NS_LOG_FUNCTION (this);
272 }
273 
274 void
276 {
277  NS_LOG_FUNCTION (this << size);
278  Ptr<Packet> p;
279  uint32_t packetSize;
280  if (size>12)
281  {
282  packetSize = size - 12; // 12 is the size of the SeqTsHeader
283  }
284  else
285  {
286  packetSize = 0;
287  }
288  p = Create<Packet> (packetSize);
289  SeqTsHeader seqTs;
290  seqTs.SetSeq (m_sent);
291  p->AddHeader (seqTs);
292 
293  std::stringstream addressString;
295  {
296  addressString << Ipv4Address::ConvertFrom (m_peerAddress);
297  }
298  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
299  {
300  addressString << Ipv6Address::ConvertFrom (m_peerAddress);
301  }
302  else
303  {
304  addressString << m_peerAddress;
305  }
306 
307  if ((m_socket->Send (p)) >= 0)
308  {
309  ++m_sent;
310  NS_LOG_INFO ("Sent " << size << " bytes to "
311  << addressString.str ());
312  }
313  else
314  {
315  NS_LOG_INFO ("Error while sending " << size << " bytes to "
316  << addressString.str ());
317  }
318 }
319 
320 void
322 {
323  NS_LOG_FUNCTION (this);
324 
326  Ptr<Packet> p;
327  struct TraceEntry *entry = &m_entries[m_currentEntry];
328  do
329  {
330  for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
331  {
333  }
334 
335  uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
336  SendPacket (sizetosend);
337 
338  m_currentEntry++;
339  m_currentEntry %= m_entries.size ();
340  entry = &m_entries[m_currentEntry];
341  }
342  while (entry->timeToSend == 0);
344 }
345 
346 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
an Inet address class
void LoadDefaultTrace(void)
Load the default trace.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#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.
Hold variables of type string.
Definition: string.h:41
uint32_t packetSize
Size of the frame.
#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
virtual void StartApplication(void)
Application specific startup code.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:867
#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:1436
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:819
Address m_peerAddress
Remote peer address.
virtual void StopApplication(void)
Application specific shutdown code.
void SetTraceFile(std::string filename)
Set the trace file to be used by the application.
EventId m_sendEvent
Event to send the next packet.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
uint32_t timeToSend
Time to send the frame.
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
The base class for all ns3 applications.
Definition: application.h:60
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t GetMaxPacketSize(void)
Return the maximum packet size.
static struct TraceEntry g_defaultEntries[]
Default trace to send.
Ptr< Node > GetNode() const
Definition: application.cc:103
char frameType
Frame type (I, P or B)
An Inet6 address class.
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
std::vector< struct TraceEntry > m_entries
Entries in the trace to send.
static bool IsMatchingType(const Address &address)
static TypeId GetTypeId(void)
Get the type ID.
Introspection did not find any typical Config paths.
Definition: seq-ts-header.h:36
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:82
void LoadTrace(std::string filename)
Load a trace file.
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.
uint32_t m_sent
Counter for sent packets.
Ptr< Socket > m_socket
Socket.
void SetMaxPacketSize(uint16_t maxPacketSize)
Set the maximum packet size.
void Send(void)
Send a packet.
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
Default trace to send.
Entry to send.
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
AttributeValue implementation for Address.
Definition: address.h:278
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
An identifier for simulation events.
Definition: event-id.h:53
uint16_t m_peerPort
Remote peer port.
void SetSeq(uint32_t seq)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
uint32_t m_currentEntry
Current entry index.
static const uint32_t packetSize
static Ipv4Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
Destructor implementation.
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: string.h:42
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SendPacket(uint32_t size)
Send a packet of a given size.
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:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void SetRemote(Ipv4Address ip, uint16_t port)
set the remote address and port
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
static TypeId LookupByName(std::string name)
Definition: type-id.cc:556
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.