From e7964f35a8f2459d81c9e98c13d9bf020dcdef83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Thu, 25 Apr 2024 08:16:53 +0200 Subject: [PATCH] applications: Add common class for source applications Proposed by Sharan Naribole --- src/applications/CMakeLists.txt | 2 + src/applications/model/source-application.cc | 77 ++++++++++++++++++++ src/applications/model/source-application.h | 66 +++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 src/applications/model/source-application.cc create mode 100644 src/applications/model/source-application.h diff --git a/src/applications/CMakeLists.txt b/src/applications/CMakeLists.txt index 21ba0aa6d..3afa02f3b 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/source-application.cc model/three-gpp-http-client.cc model/three-gpp-http-header.cc model/three-gpp-http-server.cc @@ -39,6 +40,7 @@ build_lib( model/seq-ts-echo-header.h model/seq-ts-header.h model/seq-ts-size-header.h + model/source-application.h model/three-gpp-http-client.h model/three-gpp-http-header.h model/three-gpp-http-server.h diff --git a/src/applications/model/source-application.cc b/src/applications/model/source-application.cc new file mode 100644 index 000000000..a46689f40 --- /dev/null +++ b/src/applications/model/source-application.cc @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING + * + * SPDX-License-Identifier: GPL-2.0-only + * + * Author: Sébastien Deronne + */ + +#include "source-application.h" + +#include "ns3/log.h" +#include "ns3/uinteger.h" + +namespace ns3 +{ + +NS_LOG_COMPONENT_DEFINE("SourceApplication"); + +NS_OBJECT_ENSURE_REGISTERED(SourceApplication); + +TypeId +SourceApplication::GetTypeId() +{ + static TypeId tid = + TypeId("ns3::SourceApplication") + .SetParent() + .SetGroupName("Applications") + .AddAttribute( + "Remote", + "The address of the destination, made of the remote IP address and the " + "destination port", + AddressValue(), + MakeAddressAccessor(&SourceApplication::SetRemote, &SourceApplication::GetRemote), + MakeAddressChecker()) + .AddAttribute("Local", + "The Address on which to bind the socket. If not set, it is generated " + "automatically when needed by the application.", + AddressValue(), + MakeAddressAccessor(&SourceApplication::m_local), + MakeAddressChecker()) + .AddAttribute("Tos", + "The Type of Service used to send IPv4 packets. " + "All 8 bits of the TOS byte are set (including ECN bits).", + UintegerValue(0), + MakeUintegerAccessor(&SourceApplication::m_tos), + MakeUintegerChecker()); + return tid; +} + +SourceApplication::SourceApplication() + : m_peer{} +{ + NS_LOG_FUNCTION(this); +} + +SourceApplication::~SourceApplication() +{ + NS_LOG_FUNCTION(this); +} + +void +SourceApplication::SetRemote(const Address& addr) +{ + NS_LOG_FUNCTION(this << addr); + if (!addr.IsInvalid()) + { + m_peer = addr; + } +} + +Address +SourceApplication::GetRemote() const +{ + return m_peer; +} + +} // Namespace ns3 diff --git a/src/applications/model/source-application.h b/src/applications/model/source-application.h new file mode 100644 index 000000000..464fa0135 --- /dev/null +++ b/src/applications/model/source-application.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING + * + * SPDX-License-Identifier: GPL-2.0-only + * + * Author: Sébastien Deronne + */ + +#ifndef SOURCE_APPLICATION_H +#define SOURCE_APPLICATION_H + +#include "ns3/address.h" +#include "ns3/application.h" + +namespace ns3 +{ + +/** + * \ingroup applications + * \brief Base class for source applications. + * + * This class can be used as a base class for source applications. + * A source application is one that primarily sources new data towards a single remote client + * address and port, and may also receive data (such as an HTTP server). + * + * The main purpose of this base class application public API is to provide a uniform way to + * configure remote and local addresses. + * + * Unlike the SinkApplication, the SourceApplication does not expose an individual Port attribute. + * Instead, the port values are embedded in the Local and Remote address attributes, which should be + * configured to an InetSocketAddress or Inet6SocketAddress value that contains the desired port + * number. + */ +class SourceApplication : public Application +{ + public: + /** + * \brief Get the type ID. + * \return the object TypeId + */ + static TypeId GetTypeId(); + + SourceApplication(); + ~SourceApplication() override; + + /** + * \brief set the remote address + * \param addr remote address + */ + virtual void SetRemote(const Address& addr); + + /** + * \brief get the remote address + * \return the remote address + */ + Address GetRemote() const; + + protected: + Address m_peer; //!< Peer address + Address m_local; //!< Local address to bind to + uint8_t m_tos; //!< The packets Type of Service +}; + +} // namespace ns3 + +#endif /* SOURCE_APPLICATION_H */