diff --git a/src/applications/CMakeLists.txt b/src/applications/CMakeLists.txt index 3afa02f3b..c7e3437a1 100644 --- a/src/applications/CMakeLists.txt +++ b/src/applications/CMakeLists.txt @@ -15,6 +15,7 @@ build_lib( model/seq-ts-echo-header.cc model/seq-ts-header.cc model/seq-ts-size-header.cc + model/sink-application.cc model/source-application.cc model/three-gpp-http-client.cc model/three-gpp-http-header.cc @@ -40,6 +41,7 @@ build_lib( model/seq-ts-echo-header.h model/seq-ts-header.h model/seq-ts-size-header.h + model/sink-application.h model/source-application.h model/three-gpp-http-client.h model/three-gpp-http-header.h diff --git a/src/applications/model/sink-application.cc b/src/applications/model/sink-application.cc new file mode 100644 index 000000000..ed2f86947 --- /dev/null +++ b/src/applications/model/sink-application.cc @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING + * + * SPDX-License-Identifier: GPL-2.0-only + * + * Author: Sébastien Deronne + */ + +#include "sink-application.h" + +#include "ns3/log.h" +#include "ns3/uinteger.h" + +namespace ns3 +{ + +NS_LOG_COMPONENT_DEFINE("SinkApplication"); + +NS_OBJECT_ENSURE_REGISTERED(SinkApplication); + +TypeId +SinkApplication::GetTypeId() +{ + static TypeId tid = + TypeId("ns3::SinkApplication") + .SetParent() + .SetGroupName("Applications") + .AddAttribute( + "Local", + "The Address on which to Bind the rx socket. " + "If it is not specified, it will listen to any address.", + AddressValue(), + MakeAddressAccessor(&SinkApplication::SetLocal, &SinkApplication::GetLocal), + MakeAddressChecker()) + .AddAttribute( + "Port", + "Port on which the application listens for incoming packets.", + UintegerValue(INVALID_PORT), + MakeUintegerAccessor(&SinkApplication::SetPort, &SinkApplication::GetPort), + MakeUintegerChecker()); + return tid; +} + +SinkApplication::SinkApplication(uint16_t defaultPort) + : m_port{defaultPort} +{ + NS_LOG_FUNCTION(this << defaultPort); +} + +SinkApplication::~SinkApplication() +{ + NS_LOG_FUNCTION(this); +} + +void +SinkApplication::SetLocal(const Address& addr) +{ + NS_LOG_FUNCTION(this << addr); + m_local = addr; +} + +Address +SinkApplication::GetLocal() const +{ + return m_local; +} + +void +SinkApplication::SetPort(uint32_t port) +{ + NS_LOG_FUNCTION(this << port); + if (port == INVALID_PORT) + { + return; + } + m_port = port; +} + +uint32_t +SinkApplication::GetPort() const +{ + return m_port; +} + +} // Namespace ns3 diff --git a/src/applications/model/sink-application.h b/src/applications/model/sink-application.h new file mode 100644 index 000000000..ae1b6da44 --- /dev/null +++ b/src/applications/model/sink-application.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING + * + * SPDX-License-Identifier: GPL-2.0-only + * + * Author: Sébastien Deronne + */ + +#ifndef SINK_APPLICATION_H +#define SINK_APPLICATION_H + +#include "ns3/address.h" +#include "ns3/application.h" + +#include + +namespace ns3 +{ + +/** + * \ingroup applications + * \brief Base class for sink applications. + * + * This class can be used as a base class for sink applications. + * A sink application is an application that is primarily used to only receive or echo packets. + * + * The main purpose of this base class application public API is to hold attributes for the local + * (IPv4 or IPv6) address and port to bind to. + * + * There are three ways that the port value can be configured. First, and most typically, through + * the use of a socket address (InetSocketAddress or Inet6SocketAddress) that is configured as the + * Local address to bind to. Second, through direct configuration of the Port attribute. Third, + * through the use of an optional constructor argument. If multiple of these port configuration + * methods are used, it is up to subclass definition which one takes precedence; in the existing + * subclasses in this directory, the port value configured in the Local socket address (if a socket + * address is configured there) will take precedence. + */ +class SinkApplication : public Application +{ + public: + /** + * \brief Get the type ID. + * \return the object TypeId + */ + static TypeId GetTypeId(); + + /** + * Constructor + * + * \param defaultPort the default port number + */ + SinkApplication(uint16_t defaultPort = 0); + ~SinkApplication() override; + + static constexpr uint32_t INVALID_PORT{std::numeric_limits::max()}; //!< invalid port + + protected: + Address m_local; //!< Local address to bind to (address and port) + uint32_t m_port; //!< Local port to bind to + + private: + /** + * \brief set the local address + * \param addr local address + */ + virtual void SetLocal(const Address& addr); + + /** + * \brief get the local address + * \return the local address + */ + Address GetLocal() const; + + /** + * \brief set the server port + * \param port server port + */ + virtual void SetPort(uint32_t port); + + /** + * \brief get the server port + * \return the server port + */ + uint32_t GetPort() const; +}; + +} // namespace ns3 + +#endif /* SINK_APPLICATION_H */