A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ;
44 
48 struct UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
49  { 0, 534, 'I'},
50  { 40, 1542, 'P'},
51  { 120, 134, 'B'},
52  { 80, 390, 'B'},
53  { 240, 765, 'P'},
54  { 160, 407, 'B'},
55  { 200, 504, 'B'},
56  { 360, 903, 'P'},
57  { 280, 421, 'B'},
58  { 320, 587, 'B'}
59 };
60 
61 TypeId
63 {
64  static TypeId tid = TypeId ("ns3::UdpTraceClient")
66  .AddConstructor<UdpTraceClient> ()
67  .AddAttribute ("RemoteAddress",
68  "The destination Address of the outbound packets",
69  AddressValue (),
70  MakeAddressAccessor (&UdpTraceClient::m_peerAddress),
71  MakeAddressChecker ())
72  .AddAttribute ("RemotePort",
73  "The destination port of the outbound packets",
74  UintegerValue (100),
75  MakeUintegerAccessor (&UdpTraceClient::m_peerPort),
76  MakeUintegerChecker<uint16_t> ())
77  .AddAttribute ("MaxPacketSize",
78  "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
79  UintegerValue (1024),
80  MakeUintegerAccessor (&UdpTraceClient::m_maxPacketSize),
81  MakeUintegerChecker<uint32_t> ())
82  .AddAttribute ("TraceFilename",
83  "Name of file to load a trace from. By default, uses a hardcoded trace.",
84  StringValue (""),
85  MakeStringAccessor (&UdpTraceClient::SetTraceFile),
86  MakeStringChecker ())
87 
88  ;
89  return tid;
90 }
91 
93 {
94  NS_LOG_FUNCTION (this);
95  m_sent = 0;
96  m_socket = 0;
97  m_sendEvent = EventId ();
98  m_maxPacketSize = 1400;
99 }
100 
102  char *traceFile)
103 {
104  NS_LOG_FUNCTION (this);
105  m_sent = 0;
106  m_socket = 0;
107  m_sendEvent = EventId ();
108  m_peerAddress = ip;
109  m_peerPort = port;
110  m_currentEntry = 0;
111  m_maxPacketSize = 1400;
112  if (traceFile != NULL)
113  {
114  SetTraceFile (traceFile);
115  }
116 }
117 
119 {
120  NS_LOG_FUNCTION (this);
121  m_entries.clear ();
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this << ip << port);
128  m_entries.clear ();
129  m_peerAddress = ip;
130  m_peerPort = port;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this << ip << port);
137  m_entries.clear ();
138  m_peerAddress = Address (ip);
139  m_peerPort = port;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this << ip << port);
146  m_entries.clear ();
147  m_peerAddress = Address (ip);
148  m_peerPort = port;
149 }
150 
151 void
152 UdpTraceClient::SetTraceFile (std::string traceFile)
153 {
154  NS_LOG_FUNCTION (this << traceFile);
155  if (traceFile == "")
156  {
157  LoadDefaultTrace ();
158  }
159  else
160  {
161  LoadTrace (traceFile);
162  }
163 }
164 
165 void
166 UdpTraceClient::SetMaxPacketSize (uint16_t maxPacketSize)
167 {
168  NS_LOG_FUNCTION (this << maxPacketSize);
169  m_maxPacketSize = maxPacketSize;
170 }
171 
172 
174 {
175  NS_LOG_FUNCTION (this);
176  return m_maxPacketSize;
177 }
178 
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
185 }
186 
187 void
188 UdpTraceClient::LoadTrace (std::string filename)
189 {
190  NS_LOG_FUNCTION (this << filename);
191  uint32_t time, index, prevTime = 0;
192  uint16_t size;
193  char frameType;
194  TraceEntry entry;
195  std::ifstream ifTraceFile;
196  ifTraceFile.open (filename.c_str (), std::ifstream::in);
197  m_entries.clear ();
198  if (!ifTraceFile.good ())
199  {
200  LoadDefaultTrace ();
201  }
202  while (ifTraceFile.good ())
203  {
204  ifTraceFile >> index >> frameType >> time >> size;
205  if (frameType == 'B')
206  {
207  entry.timeToSend = 0;
208  }
209  else
210  {
211  entry.timeToSend = time - prevTime;
212  prevTime = time;
213  }
214  entry.packetSize = size;
215  entry.frameType = frameType;
216  m_entries.push_back (entry);
217  }
218  ifTraceFile.close ();
219  m_currentEntry = 0;
220 }
221 
222 void
224 {
225  NS_LOG_FUNCTION (this);
226  uint32_t prevTime = 0;
227  for (uint32_t i = 0; i < (sizeof (g_defaultEntries) / sizeof (struct TraceEntry)); i++)
228  {
229  struct TraceEntry entry = g_defaultEntries[i];
230  if (entry.frameType == 'B')
231  {
232  entry.timeToSend = 0;
233  }
234  else
235  {
236  uint32_t tmp = entry.timeToSend;
237  entry.timeToSend -= prevTime;
238  prevTime = tmp;
239  }
240  m_entries.push_back (entry);
241  }
242  m_currentEntry = 0;
243 }
244 
245 void
247 {
248  NS_LOG_FUNCTION (this);
249 
250  if (m_socket == 0)
251  {
252  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
255  {
256  m_socket->Bind ();
258  }
259  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
260  {
261  m_socket->Bind6 ();
263  }
264  }
266  m_sendEvent = Simulator::Schedule (Seconds (0.0), &UdpTraceClient::Send, this);
267 }
268 
269 void
271 {
272  NS_LOG_FUNCTION (this);
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION (this << size);
280  Ptr<Packet> p;
281  uint32_t packetSize;
282  if (size>12)
283  {
284  packetSize = size - 12; // 12 is the size of the SeqTsHeader
285  }
286  else
287  {
288  packetSize = 0;
289  }
290  p = Create<Packet> (packetSize);
291  SeqTsHeader seqTs;
292  seqTs.SetSeq (m_sent);
293  p->AddHeader (seqTs);
294 
295  std::stringstream addressString;
297  {
298  addressString << Ipv4Address::ConvertFrom (m_peerAddress);
299  }
300  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
301  {
302  addressString << Ipv6Address::ConvertFrom (m_peerAddress);
303  }
304  else
305  {
306  addressString << m_peerAddress;
307  }
308 
309  if ((m_socket->Send (p)) >= 0)
310  {
311  ++m_sent;
312  NS_LOG_INFO ("Sent " << size << " bytes to "
313  << addressString.str ());
314  }
315  else
316  {
317  NS_LOG_INFO ("Error while sending " << size << " bytes to "
318  << addressString.str ());
319  }
320 }
321 
322 void
324 {
325  NS_LOG_FUNCTION (this);
326 
328  Ptr<Packet> p;
329  struct TraceEntry *entry = &m_entries[m_currentEntry];
330  do
331  {
332  for (int i = 0; i < entry->packetSize / m_maxPacketSize; i++)
333  {
335  }
336 
337  uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
338  SendPacket (sizetosend);
339 
340  m_currentEntry++;
341  m_currentEntry %= m_entries.size ();
342  entry = &m_entries[m_currentEntry];
343  }
344  while (entry->timeToSend == 0);
345  m_sendEvent = Simulator::Schedule (MilliSeconds (entry->timeToSend), &UdpTraceClient::Send, this);
346 }
347 
348 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
an Inet address class
void LoadDefaultTrace(void)
Load the default trace.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
hold variables of type string
Definition: string.h:19
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void StartApplication(void)
Application specific startup code.
#define NS_LOG_INFO(msg)
Definition: log.h:298
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:1395
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:824
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:86
uint32_t timeToSend
Time to send the frame.
The base class for all ns3 applications.
Definition: application.h:61
uint16_t packetSize
Size of the frame.
Hold an unsigned integer type.
Definition: uinteger.h:46
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:104
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: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
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.
Doxygen 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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:83
void LoadTrace(std::string filename)
Load a trace file.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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.
Default trace to send.
Entry to send.
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
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
an identifier for simulation events.
Definition: event-id.h:46
uint16_t m_peerPort
Remote peer port.
void SetSeq(uint32_t seq)
uint32_t m_currentEntry
Current entry index.
static Ipv4Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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:53
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
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:536
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.