A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sink-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
9#include "sink-application.h"
10
11#include "ns3/log.h"
12#include "ns3/socket.h"
13#include "ns3/uinteger.h"
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("SinkApplication");
19
21
24{
25 static TypeId tid =
26 TypeId("ns3::SinkApplication")
28 .SetGroupName("Applications")
29 .AddAttribute(
30 "Local",
31 "The Address on which to Bind the rx socket. "
32 "If it is not specified, it will listen to any address.",
36 .AddAttribute(
37 "Port",
38 "Port on which the application listens for incoming packets.",
42 .AddTraceSource("Rx",
43 "A packet has been received",
45 "ns3::Packet::AddressTracedCallback")
46 .AddTraceSource("RxWithoutAddress",
47 "A packet has been received from a given address",
49 "ns3::Packet::TracedCallback");
50 return tid;
51}
52
54 : m_port{defaultPort}
55{
56 NS_LOG_FUNCTION(this << defaultPort);
57}
58
63
64void
66{
67 NS_LOG_FUNCTION(this);
68 m_socket = nullptr;
69 m_socket6 = nullptr;
71}
72
73void
75{
76 NS_LOG_FUNCTION(this << addr);
77 m_local = addr;
78}
79
82{
83 return m_local;
84}
85
86void
88{
89 NS_LOG_FUNCTION(this << port);
90 if (port == INVALID_PORT)
91 {
92 return;
93 }
94 m_port = port;
95}
96
99{
100 return m_port;
101}
102
103void
105{
106 NS_LOG_FUNCTION(this);
107
108 // note: it is currently not possible to restart an application
109
111 if (m_local.IsInvalid() && !m_socket6)
112 {
113 // local address is not specified, so create another socket to also listen to all IPv6
114 // addresses
116 }
117
119}
120
121void
128
129bool
131{
132 NS_LOG_FUNCTION(this);
133 auto ret = CloseSocket(m_socket);
134 ret = CloseSocket(m_socket6) && ret;
135 return ret;
136}
137
138bool
140{
141 NS_LOG_FUNCTION(this << socket);
142 if (socket)
143 {
144 const auto ret = socket->Close();
145 socket->SetAcceptCallback(MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
146 MakeNullCallback<void, Ptr<Socket>, const Address&>());
147 socket->SetCloseCallbacks(MakeNullCallback<void, Ptr<Socket>>(),
149 socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
150 socket->SetSendCallback(MakeNullCallback<void, Ptr<Socket>, uint32_t>());
151 return (ret == 0);
152 }
153 return true;
154}
155
156void
161
162void
167
168} // Namespace ns3
a polymophic address class
Definition address.h:111
AttributeValue implementation for Address.
Definition address.h:357
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Base class for sink applications.
static constexpr uint32_t INVALID_PORT
invalid port
virtual void SetLocal(const Address &addr)
set the local address
ns3::TracedCallback< Ptr< const Packet > > m_rxTraceWithoutAddress
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified).
void StopApplication() override
Application specific shutdown code.
void DoDispose() override
Destructor implementation.
TypeId m_protocolTid
Protocol TypeId value.
SinkApplication(uint16_t defaultPort=0)
Constructor.
virtual void DoStartApplication()
Application specific startup code for child subclasses.
Address GetLocal() const
get the local address
uint32_t GetPort() const
get the server port
virtual void DoStopApplication()
Application specific shutdown code for child subclasses.
void StartApplication() override
Application specific startup code.
static TypeId GetTypeId()
Get the type ID.
Address m_local
Local address to bind to (address and port).
Ptr< Socket > m_socket
Socket (IPv4 or IPv6, depending on local address).
bool CloseSocket(Ptr< Socket > socket)
Close the socket.
uint32_t m_port
Local port to bind to.
virtual void SetPort(uint32_t port)
set the server port
bool CloseAllSockets()
Close all the sockets.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:193
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:357
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_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.