A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-socket-client.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Universita' di Firenze
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
10
13#include "packet-socket.h"
14
15#include "ns3/abort.h"
16#include "ns3/log.h"
17#include "ns3/nstime.h"
18#include "ns3/packet.h"
19#include "ns3/simulator.h"
20#include "ns3/socket-factory.h"
21#include "ns3/socket.h"
22#include "ns3/uinteger.h"
23
24#include <cstdio>
25#include <cstdlib>
26
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("PacketSocketClient");
31
33
35PacketSocketClient::GetTypeId()
36{
37 static TypeId tid =
38 TypeId("ns3::PacketSocketClient")
40 .SetGroupName("Network")
41 .AddConstructor<PacketSocketClient>()
42 .AddAttribute(
43 "MaxPackets",
44 "The maximum number of packets the application will send (zero means infinite)",
45 UintegerValue(100),
46 MakeUintegerAccessor(&PacketSocketClient::m_maxPackets),
48 .AddAttribute("Interval",
49 "The time to wait between packets",
51 MakeTimeAccessor(&PacketSocketClient::m_interval),
53 .AddAttribute("PacketSize",
54 "Size of packets generated (bytes).",
55 UintegerValue(1024),
56 MakeUintegerAccessor(&PacketSocketClient::m_size),
58 .AddAttribute("Priority",
59 "Priority assigned to the packets generated.",
61 MakeUintegerAccessor(&PacketSocketClient::SetPriority,
62 &PacketSocketClient::GetPriority),
64 .AddTraceSource("Tx",
65 "A packet has been sent",
66 MakeTraceSourceAccessor(&PacketSocketClient::m_txTrace),
67 "ns3::Packet::AddressTracedCallback");
68 return tid;
69}
70
71PacketSocketClient::PacketSocketClient()
72{
73 NS_LOG_FUNCTION(this);
74 m_sent = 0;
75 m_socket = nullptr;
77 m_peerAddressSet = false;
78}
79
80PacketSocketClient::~PacketSocketClient()
81{
82 NS_LOG_FUNCTION(this);
83}
84
85void
86PacketSocketClient::SetRemote(PacketSocketAddress addr)
87{
88 NS_LOG_FUNCTION(this << addr);
89 m_peerAddress = addr;
90 m_peerAddressSet = true;
91}
92
93void
94PacketSocketClient::DoDispose()
95{
96 NS_LOG_FUNCTION(this);
98}
99
100void
101PacketSocketClient::SetPriority(uint8_t priority)
102{
103 m_priority = priority;
104 if (m_socket)
105 {
106 m_socket->SetPriority(priority);
107 }
108}
109
110uint8_t
111PacketSocketClient::GetPriority() const
112{
113 return m_priority;
114}
115
116void
117PacketSocketClient::StartApplication()
118{
119 NS_LOG_FUNCTION(this);
120 NS_ASSERT_MSG(m_peerAddressSet, "Peer address not set");
121
122 if (!m_socket)
123 {
124 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
125 m_socket = Socket::CreateSocket(GetNode(), tid);
126
127 m_socket->Bind(m_peerAddress);
128 m_socket->Connect(m_peerAddress);
129
130 if (m_priority)
131 {
132 m_socket->SetPriority(m_priority);
133 }
134 }
135
136 m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
137 m_sendEvent = Simulator::ScheduleNow(&PacketSocketClient::Send, this);
138}
139
140void
141PacketSocketClient::StopApplication()
142{
143 NS_LOG_FUNCTION(this);
145 m_socket->Close();
146}
147
148void
149PacketSocketClient::Send()
150{
151 NS_LOG_FUNCTION(this);
152 NS_ASSERT(m_sendEvent.IsExpired());
153
155
156 std::stringstream peerAddressStringStream;
157 peerAddressStringStream << PacketSocketAddress::ConvertFrom(m_peerAddress);
158
159 if ((m_socket->Send(p)) >= 0)
160 {
162 NS_LOG_INFO("TraceDelay TX " << m_size << " bytes to " << peerAddressStringStream.str()
163 << " Uid: " << p->GetUid()
164 << " Time: " << (Simulator::Now()).GetSeconds());
165 }
166 else
167 {
168 NS_LOG_INFO("Error while sending " << m_size << " bytes to "
169 << peerAddressStringStream.str());
170 }
171 m_sent++;
172
173 if ((m_sent < m_maxPackets) || (m_maxPackets == 0))
174 {
175 if (m_interval.IsZero())
176 {
178 m_maxPackets == 0,
179 "Generating infinite packets at the same time does not seem to be a good idea");
180 Send();
181 }
182 else
183 {
184 m_sendEvent = Simulator::Schedule(m_interval, &PacketSocketClient::Send, this);
185 }
186 }
187}
188
189} // Namespace ns3
The base class for all ns3 applications.
Definition application.h:51
void DoDispose() override
Destructor implementation.
An identifier for simulation events.
Definition event-id.h:44
static PacketSocketAddress ConvertFrom(const Address &address)
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
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
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
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
a unique identifier for an interface.
Definition type-id.h:49
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
Hold an unsigned integer type.
Definition uinteger.h:34
#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_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:439
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1457
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
PacketSocketAddress m_peerAddress
Remote peer address.
Time m_interval
Packet inter-send time.
uint32_t m_sent
Counter for sent packets.
EventId m_sendEvent
Event to send the next packet.
bool m_peerAddressSet
Sanity check.
void Send()
Send a packet.
TracedCallback< Ptr< const Packet >, const Address & > m_txTrace
Traced Callback: sent packets, source address.
uint8_t m_priority
Priority of the sent packets.
Ptr< Socket > m_socket
Socket.
uint32_t m_size
Size of the sent packet.
uint32_t m_maxPackets
Maximum number of packets the application will send.
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1477