A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
source-application.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
7 */
8
10
11#include "ns3/inet-socket-address.h"
12#include "ns3/inet6-socket-address.h"
13#include "ns3/log.h"
14#include "ns3/packet-socket-address.h"
15#include "ns3/socket.h"
16#include "ns3/uinteger.h"
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("SourceApplication");
22
24
27{
28 static TypeId tid =
29 TypeId("ns3::SourceApplication")
31 .SetGroupName("Applications")
32 .AddAttribute(
33 "Remote",
34 "The address of the destination, made of the remote IP address and the "
35 "destination port",
39 .AddAttribute("Local",
40 "The Address on which to bind the socket. If not set, it is generated "
41 "automatically when needed by the application.",
45 .AddAttribute("Tos",
46 "The Type of Service used to send IPv4 packets. "
47 "All 8 bits of the TOS byte are set (including ECN bits).",
51 .AddTraceSource("Tx",
52 "A packet is sent",
54 "ns3::Packet::TracedCallback")
55 .AddTraceSource("ConnectionSucceeded",
56 "Succeeded to establish connection",
58 "ns3::SourceApplication::ConnectionEventCallback")
59 .AddTraceSource("ConnectionFailed",
60 "Failed to establish connection",
62 "ns3::SourceApplication::ConnectionEventCallback");
63 return tid;
64}
65
67 : m_allowPacketSocket{allowPacketSocket}
68{
69 NS_LOG_FUNCTION(this);
70}
71
76
77void
85
86void
88{
89 NS_LOG_FUNCTION(this << addr);
90 if (!addr.IsInvalid())
91 {
92 m_peer = addr;
93 }
94}
95
98{
99 return m_peer;
100}
101
104{
105 return m_socket;
106}
107
108void
110{
111 NS_LOG_FUNCTION(this);
112
113 // note: it is currently not possible to restart an application
114
115 NS_ABORT_MSG_IF(m_peer.IsInvalid(), "Remote address not properly set");
116 if (!m_local.IsInvalid())
117 {
122 "Incompatible peer and local address IP version");
123 }
124
128
129 int ret{-1};
132 {
133 ret = m_socket->Bind();
134 }
136 {
137 ret = m_socket->Bind6();
138 }
139 else
140 {
141 NS_FATAL_ERROR("Incompatible address type: " << m_peer);
142 }
143 if (ret == -1)
144 {
145 NS_FATAL_ERROR("Failed to bind socket");
146 }
147
149 {
150 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
151 }
152
153 m_socket->Connect(m_peer);
154
155 CancelEvents();
156
158}
159
160void
168
169bool
171{
172 m_connected = false;
173 if (m_socket)
174 {
175 const auto ret = m_socket->Close();
176 m_socket->SetConnectCallback(MakeNullCallback<void, Ptr<Socket>>(),
178 m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
179 return (ret == 0);
180 }
181 return true;
182}
183
184void
192
193void
195{
196 NS_LOG_FUNCTION(this << socket);
197 m_connected = false;
198 DoConnectionFailed(socket);
200}
201
202void
207
208void
213
214void
219
220void
225
226} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
AttributeValue implementation for Address.
Definition address.h:275
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
static bool IsMatchingType(const Address &addr)
If the address match.
static bool IsMatchingType(const Address &address)
static bool IsMatchingType(const Address &address)
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
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
Base class for source applications.
TracedCallback< Ptr< Socket >, const Address &, const Address & > m_connectionFailure
Traced Callback: connection failure event.
TypeId m_protocolTid
Protocol TypeId value.
void StopApplication() override
Application specific shutdown code.
Address m_local
Local address to bind to.
Address GetRemote() const
get the remote address
SourceApplication(bool allowPacketSocket=true)
Constructor.
void ConnectionFailed(Ptr< Socket > socket)
Handle a Connection Failed event.
Ptr< Socket > m_socket
Socket.
TracedCallback< Ptr< Socket >, const Address &, const Address & > m_connectionSuccess
Traced Callback: connection success event.
void DoDispose() override
Destructor implementation.
Ptr< Socket > GetSocket() const
Get the socket this application is attached to.
void ConnectionSucceeded(Ptr< Socket > socket)
Handle a Connection Succeed event.
virtual void DoConnectionFailed(Ptr< Socket > socket)
Application specific code for child subclasses upon a Connection Failed event.
virtual void DoStopApplication()
Application specific shutdown code for child subclasses.
virtual void SetRemote(const Address &addr)
set the remote address
virtual void CancelEvents()=0
Cancel all pending events.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: transmitted packets.
uint8_t m_tos
The packets Type of Service.
void StartApplication() override
Application specific startup code.
bool m_connected
flag whether socket is connected
bool m_allowPacketSocket
Allow use of packet socket.
static TypeId GetTypeId()
Get the type ID.
Address m_peer
Peer address.
bool CloseSocket()
Close the socket.
virtual void DoStartApplication()
Application specific startup code for child subclasses.
virtual void DoConnectionSucceeded(Ptr< Socket > socket)
Application specific code for child subclasses upon a Connection Succeed event.
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:169
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:275
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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:35
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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
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.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684