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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18 * <amine.ismail@udcast.com>
19 */
20#include "udp-trace-client.h"
21
22#include "seq-ts-header.h"
23
24#include "ns3/boolean.h"
25#include "ns3/inet-socket-address.h"
26#include "ns3/inet6-socket-address.h"
27#include "ns3/ipv4-address.h"
28#include "ns3/log.h"
29#include "ns3/nstime.h"
30#include "ns3/packet.h"
31#include "ns3/simulator.h"
32#include "ns3/socket-factory.h"
33#include "ns3/socket.h"
34#include "ns3/string.h"
35#include "ns3/uinteger.h"
36
37#include <cstdio>
38#include <cstdlib>
39#include <fstream>
40
41namespace ns3
42{
43
44NS_LOG_COMPONENT_DEFINE("UdpTraceClient");
45
46NS_OBJECT_ENSURE_REGISTERED(UdpTraceClient);
47
48/**
49 * \brief Default trace to send
50 */
51UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
52 {0, 534, 'I'},
53 {40, 1542, 'P'},
54 {120, 134, 'B'},
55 {80, 390, 'B'},
56 {240, 765, 'P'},
57 {160, 407, 'B'},
58 {200, 504, 'B'},
59 {360, 903, 'P'},
60 {280, 421, 'B'},
61 {320, 587, 'B'},
62};
63
64TypeId
66{
67 static TypeId tid =
68 TypeId("ns3::UdpTraceClient")
70 .SetGroupName("Applications")
71 .AddConstructor<UdpTraceClient>()
72 .AddAttribute("RemoteAddress",
73 "The destination Address of the outbound packets",
77 .AddAttribute("RemotePort",
78 "The destination port of the outbound packets",
79 UintegerValue(100),
81 MakeUintegerChecker<uint16_t>())
82 .AddAttribute("Tos",
83 "The Type of Service used to send IPv4 packets. "
84 "All 8 bits of the TOS byte are set (including ECN bits).",
87 MakeUintegerChecker<uint8_t>())
88 .AddAttribute("MaxPacketSize",
89 "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
90 UintegerValue(1024),
92 MakeUintegerChecker<uint32_t>())
93 .AddAttribute("TraceFilename",
94 "Name of file to load a trace from. By default, uses a hardcoded trace.",
95 StringValue(""),
98 .AddAttribute("TraceLoop",
99 "Loops through the trace file, starting again once it is over.",
100 BooleanValue(true),
103
104 ;
105 return tid;
106}
107
109{
110 NS_LOG_FUNCTION(this);
111 m_sent = 0;
112 m_socket = nullptr;
114 m_maxPacketSize = 1400;
115}
116
117UdpTraceClient::UdpTraceClient(Ipv4Address ip, uint16_t port, char* traceFile)
118{
119 NS_LOG_FUNCTION(this);
120 m_sent = 0;
121 m_socket = nullptr;
123 m_peerAddress = ip;
125 m_currentEntry = 0;
126 m_maxPacketSize = 1400;
127 if (traceFile != nullptr)
128 {
129 SetTraceFile(traceFile);
130 }
131}
132
134{
135 NS_LOG_FUNCTION(this);
136 m_entries.clear();
137}
138
139void
141{
142 NS_LOG_FUNCTION(this << ip << port);
143 m_entries.clear();
144 m_peerAddress = ip;
146}
147
148void
150{
151 NS_LOG_FUNCTION(this << addr);
152 m_entries.clear();
153 m_peerAddress = addr;
154}
155
156void
157UdpTraceClient::SetTraceFile(std::string traceFile)
158{
159 NS_LOG_FUNCTION(this << traceFile);
160 if (traceFile.empty())
161 {
163 }
164 else
165 {
166 LoadTrace(traceFile);
167 }
168}
169
170void
171UdpTraceClient::SetMaxPacketSize(uint16_t maxPacketSize)
172{
173 NS_LOG_FUNCTION(this << maxPacketSize);
174 m_maxPacketSize = maxPacketSize;
175}
176
177uint16_t
179{
180 NS_LOG_FUNCTION(this);
181 return m_maxPacketSize;
182}
183
184void
185UdpTraceClient::LoadTrace(std::string filename)
186{
187 NS_LOG_FUNCTION(this << filename);
188 uint32_t time = 0;
189 uint32_t index = 0;
190 uint32_t oldIndex = 0;
191 uint32_t size = 0;
192 uint32_t prevTime = 0;
193 char frameType;
194 TraceEntry entry;
195 std::ifstream ifTraceFile;
196 ifTraceFile.open(filename, std::ifstream::in);
197 m_entries.clear();
198 if (!ifTraceFile.good())
199 {
201 }
202 while (ifTraceFile.good())
203 {
204 ifTraceFile >> index >> frameType >> time >> size;
205 if (index == oldIndex)
206 {
207 continue;
208 }
209 if (frameType == 'B')
210 {
211 entry.timeToSend = 0;
212 }
213 else
214 {
215 entry.timeToSend = time - prevTime;
216 prevTime = time;
217 }
218 entry.packetSize = size;
219 entry.frameType = frameType;
220 m_entries.push_back(entry);
221 oldIndex = index;
222 }
223 ifTraceFile.close();
224 NS_ASSERT_MSG(prevTime != 0, "A trace file can not contain B frames only.");
225 m_currentEntry = 0;
226}
227
228void
230{
231 NS_LOG_FUNCTION(this);
232 uint32_t prevTime = 0;
233 for (uint32_t i = 0; i < (sizeof(g_defaultEntries) / sizeof(TraceEntry)); i++)
234 {
235 TraceEntry entry = g_defaultEntries[i];
236 if (entry.frameType == 'B')
237 {
238 entry.timeToSend = 0;
239 }
240 else
241 {
242 uint32_t tmp = entry.timeToSend;
243 entry.timeToSend -= prevTime;
244 prevTime = tmp;
245 }
246 m_entries.push_back(entry);
247 }
248 m_currentEntry = 0;
249}
250
251void
253{
254 NS_LOG_FUNCTION(this);
255
256 if (!m_socket)
257 {
258 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
260 NS_ABORT_MSG_IF(m_peerAddress.IsInvalid(), "'RemoteAddress' attribute not properly set");
262 {
263 if (m_socket->Bind() == -1)
264 {
265 NS_FATAL_ERROR("Failed to bind socket");
266 }
267 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
270 }
272 {
273 if (m_socket->Bind6() == -1)
274 {
275 NS_FATAL_ERROR("Failed to bind socket");
276 }
279 }
281 {
282 if (m_socket->Bind() == -1)
283 {
284 NS_FATAL_ERROR("Failed to bind socket");
285 }
286 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
288 }
290 {
291 if (m_socket->Bind6() == -1)
292 {
293 NS_FATAL_ERROR("Failed to bind socket");
294 }
296 }
297 else
298 {
299 NS_ASSERT_MSG(false, "Incompatible address type: " << m_peerAddress);
300 }
301 }
305}
306
307void
309{
310 NS_LOG_FUNCTION(this);
312}
313
314void
316{
317 NS_LOG_FUNCTION(this << size);
318 Ptr<Packet> p;
320 if (size > 12)
321 {
322 packetSize = size - 12; // 12 is the size of the SeqTsHeader
323 }
324 else
325 {
326 packetSize = 0;
327 }
328 p = Create<Packet>(packetSize);
329 SeqTsHeader seqTs;
330 seqTs.SetSeq(m_sent);
331 p->AddHeader(seqTs);
332
333 std::stringstream addressString;
335 {
336 addressString << Ipv4Address::ConvertFrom(m_peerAddress);
337 }
339 {
340 addressString << Ipv6Address::ConvertFrom(m_peerAddress);
341 }
342 else
343 {
344 addressString << m_peerAddress;
345 }
346
347 if ((m_socket->Send(p)) >= 0)
348 {
349 ++m_sent;
350 NS_LOG_INFO("Sent " << size << " bytes to " << addressString.str());
351 }
352 else
353 {
354 NS_LOG_INFO("Error while sending " << size << " bytes to " << addressString.str());
355 }
356}
357
358void
360{
361 NS_LOG_FUNCTION(this);
362
364
365 bool cycled = false;
366 Ptr<Packet> p;
368 do
369 {
370 for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
371 {
373 }
374
375 uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
376 SendPacket(sizetosend);
377
379 if (m_currentEntry >= m_entries.size())
380 {
381 m_currentEntry = 0;
382 cycled = true;
383 }
384 entry = &m_entries[m_currentEntry];
385 } while (entry->timeToSend == 0);
386
387 if (!cycled || m_traceLoop)
388 {
391 }
392}
393
394void
396{
397 m_traceLoop = traceLoop;
398}
399
400} // Namespace ns3
a polymophic address class
Definition: address.h:101
bool IsInvalid() const
Definition: address.cc:71
AttributeValue implementation for Address.
Definition: address.h:286
The base class for all ns3 applications.
Definition: application.h:62
Ptr< Node > GetNode() const
Definition: application.cc:108
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
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.
Definition: ipv4-address.h:42
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.
Definition: ptr.h:77
Packet header to carry sequence number and timestamp.
Definition: seq-ts-header.h:45
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:571
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:285
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:434
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: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:72
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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:45
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
Ptr< const AttributeChecker > MakeAddressChecker()
Definition: address.cc:180
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Definition: address.h:286
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition: string.h:57
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:749
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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.