A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-trace-client.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9#include "udp-trace-client.h"
10
11#include "seq-ts-header.h"
12
13#include "ns3/boolean.h"
14#include "ns3/inet-socket-address.h"
15#include "ns3/inet6-socket-address.h"
16#include "ns3/ipv4-address.h"
17#include "ns3/log.h"
18#include "ns3/nstime.h"
19#include "ns3/packet.h"
20#include "ns3/simulator.h"
21#include "ns3/socket-factory.h"
22#include "ns3/socket.h"
23#include "ns3/string.h"
24#include "ns3/uinteger.h"
25
26#include <cstdio>
27#include <cstdlib>
28#include <fstream>
29
30namespace ns3
31{
32
33NS_LOG_COMPONENT_DEFINE("UdpTraceClient");
34
35NS_OBJECT_ENSURE_REGISTERED(UdpTraceClient);
36
37/**
38 * \brief Default trace to send
39 */
40UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
41 {0, 534, 'I'},
42 {40, 1542, 'P'},
43 {120, 134, 'B'},
44 {80, 390, 'B'},
45 {240, 765, 'P'},
46 {160, 407, 'B'},
47 {200, 504, 'B'},
48 {360, 903, 'P'},
49 {280, 421, 'B'},
50 {320, 587, 'B'},
51};
52
53TypeId
55{
56 static TypeId tid =
57 TypeId("ns3::UdpTraceClient")
59 .SetGroupName("Applications")
60 .AddConstructor<UdpTraceClient>()
61 .AddAttribute("RemoteAddress",
62 "The destination Address of the outbound packets",
66 .AddAttribute("RemotePort",
67 "The destination port of the outbound packets",
68 UintegerValue(100),
71 .AddAttribute("Tos",
72 "The Type of Service used to send IPv4 packets. "
73 "All 8 bits of the TOS byte are set (including ECN bits).",
77 .AddAttribute("MaxPacketSize",
78 "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
79 UintegerValue(1024),
82 .AddAttribute("TraceFilename",
83 "Name of file to load a trace from. By default, uses a hardcoded trace.",
84 StringValue(""),
87 .AddAttribute("TraceLoop",
88 "Loops through the trace file, starting again once it is over.",
89 BooleanValue(true),
92
93 ;
94 return tid;
95}
96
98{
99 NS_LOG_FUNCTION(this);
100 m_sent = 0;
101 m_socket = nullptr;
103 m_maxPacketSize = 1400;
104}
105
106UdpTraceClient::UdpTraceClient(Ipv4Address ip, uint16_t port, char* traceFile)
107{
108 NS_LOG_FUNCTION(this);
109 m_sent = 0;
110 m_socket = nullptr;
112 m_peerAddress = ip;
114 m_currentEntry = 0;
115 m_maxPacketSize = 1400;
116 if (traceFile != nullptr)
117 {
118 SetTraceFile(traceFile);
119 }
120}
121
127
128void
130{
131 NS_LOG_FUNCTION(this << ip << port);
132 m_entries.clear();
133 m_peerAddress = ip;
135}
136
137void
139{
140 NS_LOG_FUNCTION(this << addr);
141 m_entries.clear();
142 m_peerAddress = addr;
143}
144
145void
146UdpTraceClient::SetTraceFile(std::string traceFile)
147{
148 NS_LOG_FUNCTION(this << traceFile);
149 if (traceFile.empty())
150 {
152 }
153 else
154 {
155 LoadTrace(traceFile);
156 }
157}
158
159void
160UdpTraceClient::SetMaxPacketSize(uint16_t maxPacketSize)
161{
162 NS_LOG_FUNCTION(this << maxPacketSize);
163 m_maxPacketSize = maxPacketSize;
164}
165
166uint16_t
172
173void
174UdpTraceClient::LoadTrace(std::string filename)
175{
176 NS_LOG_FUNCTION(this << filename);
177 uint32_t time = 0;
178 uint32_t index = 0;
179 uint32_t oldIndex = 0;
180 uint32_t size = 0;
181 uint32_t prevTime = 0;
182 char frameType;
183 TraceEntry entry;
184 std::ifstream ifTraceFile;
185 ifTraceFile.open(filename, std::ifstream::in);
186 m_entries.clear();
187 if (!ifTraceFile.good())
188 {
190 }
191 while (ifTraceFile.good())
192 {
193 ifTraceFile >> index >> frameType >> time >> size;
194 if (index == oldIndex)
195 {
196 continue;
197 }
198 if (frameType == 'B')
199 {
200 entry.timeToSend = 0;
201 }
202 else
203 {
204 entry.timeToSend = time - prevTime;
205 prevTime = time;
206 }
207 entry.packetSize = size;
208 entry.frameType = frameType;
209 m_entries.push_back(entry);
210 oldIndex = index;
211 }
212 ifTraceFile.close();
213 NS_ASSERT_MSG(prevTime != 0, "A trace file can not contain B frames only.");
214 m_currentEntry = 0;
215}
216
217void
219{
220 NS_LOG_FUNCTION(this);
221 uint32_t prevTime = 0;
222 for (uint32_t i = 0; i < (sizeof(g_defaultEntries) / sizeof(TraceEntry)); i++)
223 {
224 TraceEntry entry = g_defaultEntries[i];
225 if (entry.frameType == 'B')
226 {
227 entry.timeToSend = 0;
228 }
229 else
230 {
231 uint32_t tmp = entry.timeToSend;
232 entry.timeToSend -= prevTime;
233 prevTime = tmp;
234 }
235 m_entries.push_back(entry);
236 }
237 m_currentEntry = 0;
238}
239
240void
242{
243 NS_LOG_FUNCTION(this);
244
245 if (!m_socket)
246 {
247 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
249 NS_ABORT_MSG_IF(m_peerAddress.IsInvalid(), "'RemoteAddress' attribute not properly set");
251 {
252 if (m_socket->Bind() == -1)
253 {
254 NS_FATAL_ERROR("Failed to bind socket");
255 }
256 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
259 }
261 {
262 if (m_socket->Bind6() == -1)
263 {
264 NS_FATAL_ERROR("Failed to bind socket");
265 }
268 }
270 {
271 if (m_socket->Bind() == -1)
272 {
273 NS_FATAL_ERROR("Failed to bind socket");
274 }
275 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
277 }
279 {
280 if (m_socket->Bind6() == -1)
281 {
282 NS_FATAL_ERROR("Failed to bind socket");
283 }
285 }
286 else
287 {
288 NS_ASSERT_MSG(false, "Incompatible address type: " << m_peerAddress);
289 }
290 }
294}
295
296void
302
303void
305{
306 NS_LOG_FUNCTION(this << size);
307 Ptr<Packet> p;
309 if (size > 12)
310 {
311 packetSize = size - 12; // 12 is the size of the SeqTsHeader
312 }
313 else
314 {
315 packetSize = 0;
316 }
318 SeqTsHeader seqTs;
319 seqTs.SetSeq(m_sent);
320 p->AddHeader(seqTs);
321
322 std::stringstream addressString;
324 {
325 addressString << Ipv4Address::ConvertFrom(m_peerAddress);
326 }
328 {
329 addressString << Ipv6Address::ConvertFrom(m_peerAddress);
330 }
331 else
332 {
333 addressString << m_peerAddress;
334 }
335
336 if ((m_socket->Send(p)) >= 0)
337 {
338 ++m_sent;
339 NS_LOG_INFO("Sent " << size << " bytes to " << addressString.str());
340 }
341 else
342 {
343 NS_LOG_INFO("Error while sending " << size << " bytes to " << addressString.str());
344 }
345}
346
347void
349{
350 NS_LOG_FUNCTION(this);
351
353
354 bool cycled = false;
355 Ptr<Packet> p;
357 do
358 {
359 for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
360 {
362 }
363
364 uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
365 SendPacket(sizetosend);
366
368 if (m_currentEntry >= m_entries.size())
369 {
370 m_currentEntry = 0;
371 cycled = true;
372 }
373 entry = &m_entries[m_currentEntry];
374 } while (entry->timeToSend == 0);
375
376 if (!cycled || m_traceLoop)
377 {
380 }
381}
382
383void
385{
386 m_traceLoop = traceLoop;
387}
388
389} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
The base class for all ns3 applications.
Definition application.h:51
Ptr< Node > GetNode() const
An identifier for simulation events.
Definition event-id.h:45
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
An Inet6 address class.
static bool IsMatchingType(const Address &addr)
If the address match.
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4 addresses are stored in host order in this class.
static Ipv4Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Smart pointer class similar to boost::intrusive_ptr.
Packet header to carry sequence number and timestamp.
void SetSeq(uint32_t seq)
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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:274
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition socket.cc:423
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
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:61
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
A trace based streamer.
EventId m_sendEvent
Event to send the next packet.
void StopApplication() override
Application specific shutdown code.
uint8_t m_tos
The packets Type of Service.
static TypeId GetTypeId()
Get the type ID.
void Send()
Send a packet.
uint16_t m_peerPort
Remote peer port.
void SetTraceLoop(bool traceLoop)
Set the trace loop flag.
uint32_t m_sent
Counter for sent packets.
void SetRemote(Address ip, uint16_t port)
set the remote address and port
Address m_peerAddress
Remote peer address.
std::vector< TraceEntry > m_entries
Entries in the trace to send.
uint32_t m_currentEntry
Current entry index.
void SetTraceFile(std::string filename)
Set the trace file to be used by the application.
void LoadTrace(std::string filename)
Load a trace file.
void SetMaxPacketSize(uint16_t maxPacketSize)
Set the maximum packet size.
void StartApplication() override
Application specific startup code.
void SendPacket(uint32_t size)
Send a packet of a given size.
bool m_traceLoop
Loop through the trace file.
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
static TraceEntry g_defaultEntries[]
Default trace to send.
uint16_t GetMaxPacketSize()
Return the maximum packet size.
void LoadDefaultTrace()
Load the default trace.
Ptr< Socket > m_socket
Socket.
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:169
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition string.h:46
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Definition address.h:275
Entry to send.
uint32_t timeToSend
Time to send the frame.
uint32_t packetSize
Size of the frame.
char frameType
Frame type (I, P or B)
Time prevTime
static const uint32_t packetSize
Packet size generated at the AP.