internet-apps: Simplify helpers

This commit is contained in:
Sébastien Deronne
2024-02-05 20:39:51 +01:00
parent 6b1afa1a5d
commit 188720309c
6 changed files with 30 additions and 245 deletions

View File

@@ -24,62 +24,19 @@
#include "ping-helper.h"
#include "ns3/names.h"
#include "ns3/ping.h"
namespace ns3
{
PingHelper::PingHelper()
: ApplicationHelper("ns3::Ping")
{
m_factory.SetTypeId("ns3::Ping");
}
PingHelper::PingHelper(Address remote, Address local)
PingHelper::PingHelper(const Address& remote, const Address& local)
: ApplicationHelper("ns3::Ping")
{
m_factory.SetTypeId("ns3::Ping");
m_factory.Set("Destination", AddressValue(remote));
m_factory.Set("InterfaceAddress", AddressValue(local));
}
void
PingHelper::SetAttribute(std::string name, const AttributeValue& value)
{
m_factory.Set(name, value);
}
ApplicationContainer
PingHelper::Install(Ptr<Node> node) const
{
return ApplicationContainer(InstallPriv(node));
}
ApplicationContainer
PingHelper::Install(std::string nodeName) const
{
Ptr<Node> node = Names::Find<Node>(nodeName);
return ApplicationContainer(InstallPriv(node));
}
ApplicationContainer
PingHelper::Install(NodeContainer c) const
{
ApplicationContainer apps;
for (auto i = c.Begin(); i != c.End(); ++i)
{
apps.Add(InstallPriv(*i));
}
return apps;
}
Ptr<Application>
PingHelper::InstallPriv(Ptr<Node> node) const
{
Ptr<Ping> app = m_factory.Create<Ping>();
node->AddApplication(app);
return app;
}
} // namespace ns3

View File

@@ -25,14 +25,8 @@
#ifndef PING_HELPER_H
#define PING_HELPER_H
#include "ns3/application-container.h"
#include "ns3/ipv4-address.h"
#include "ns3/ipv6-address.h"
#include "ns3/node-container.h"
#include "ns3/object-factory.h"
#include "ns3/ping.h"
#include <stdint.h>
#include <ns3/application-helper.h>
namespace ns3
{
@@ -44,7 +38,7 @@ namespace ns3
* This class creates one or multiple instances of ns3::Ping and associates
* it/them to one/multiple node(s).
*/
class PingHelper
class PingHelper : public ApplicationHelper
{
public:
/**
@@ -60,55 +54,7 @@ class PingHelper
* \param remote The address which should be pinged
* \param local The source address
*/
PingHelper(Address remote, Address local = Address());
/**
* Install a Ping application on each Node in the provided NodeContainer.
*
* \param nodes The NodeContainer containing all of the nodes to get a Ping
* application.
*
* \returns A list of Ping applications, one for each input node
*/
ApplicationContainer Install(NodeContainer nodes) const;
/**
* Install a Ping application on the provided Node. The Node is specified
* directly by a Ptr<Node>
*
* \param node The node to install the Ping application on.
*
* \returns An ApplicationContainer holding the Ping application created.
*/
ApplicationContainer Install(Ptr<Node> node) const;
/**
* Install a Ping application on the provided Node. The Node is specified
* by a string that must have previously been associated with a Node using the
* Object Name Service.
*
* \param nodeName The node to install the Ping application on.
*
* \returns An ApplicationContainer holding the Ping application created.
*/
ApplicationContainer Install(std::string nodeName) const;
/**
* \brief Configure ping applications attribute
* \param name attribute's name
* \param value attribute's value
*/
void SetAttribute(std::string name, const AttributeValue& value);
private:
/**
* \brief Do the actual application installation in the node
* \param node the node
* \returns a Smart pointer to the installed application
*/
Ptr<Application> InstallPriv(Ptr<Node> node) const;
/// Object factory
ObjectFactory m_factory;
PingHelper(const Address& remote, const Address& local = Address());
};
} // namespace ns3

View File

@@ -31,12 +31,14 @@ namespace ns3
NS_LOG_COMPONENT_DEFINE("RadvdHelper");
RadvdHelper::RadvdHelper()
: ApplicationHelper(Radvd::GetTypeId())
{
m_factory.SetTypeId(Radvd::GetTypeId());
}
void
RadvdHelper::AddAnnouncedPrefix(uint32_t interface, Ipv6Address prefix, uint32_t prefixLength)
RadvdHelper::AddAnnouncedPrefix(uint32_t interface,
const Ipv6Address& prefix,
uint32_t prefixLength)
{
NS_LOG_FUNCTION(this << int(interface) << prefix << int(prefixLength));
if (prefixLength != 64)
@@ -109,27 +111,19 @@ RadvdHelper::ClearPrefixes()
m_radvdInterfaces.clear();
}
void
RadvdHelper::SetAttribute(std::string name, const AttributeValue& value)
Ptr<Application>
RadvdHelper::DoInstall(Ptr<Node> node)
{
m_factory.Set(name, value);
}
ApplicationContainer
RadvdHelper::Install(Ptr<Node> node)
{
ApplicationContainer apps;
Ptr<Radvd> radvd = m_factory.Create<Radvd>();
for (auto iter = m_radvdInterfaces.begin(); iter != m_radvdInterfaces.end(); iter++)
auto radvd = m_factory.Create<Radvd>();
for (auto [index, interface] : m_radvdInterfaces)
{
if (!iter->second->GetPrefixes().empty())
if (!interface->GetPrefixes().empty())
{
radvd->AddConfiguration(iter->second);
radvd->AddConfiguration(interface);
}
}
node->AddApplication(radvd);
apps.Add(radvd);
return apps;
return radvd;
}
} /* namespace ns3 */

View File

@@ -20,13 +20,9 @@
#ifndef RADVD_HELPER_H
#define RADVD_HELPER_H
#include "ns3/application-container.h"
#include "ns3/ipv6-address.h"
#include "ns3/node-container.h"
#include "ns3/object-factory.h"
#include "ns3/radvd-interface.h"
#include <ns3/application-helper.h>
#include <list>
#include <map>
#include <stdint.h>
@@ -37,7 +33,7 @@ namespace ns3
* \ingroup radvd
* \brief Radvd application helper.
*/
class RadvdHelper
class RadvdHelper : public ApplicationHelper
{
public:
/**
@@ -51,7 +47,7 @@ class RadvdHelper
* \param prefix announced IPv6 prefix
* \param prefixLength announced IPv6 prefix length
*/
void AddAnnouncedPrefix(uint32_t interface, Ipv6Address prefix, uint32_t prefixLength);
void AddAnnouncedPrefix(uint32_t interface, const Ipv6Address& prefix, uint32_t prefixLength);
/**
* \brief Enable the router as default router for the interface.
@@ -80,25 +76,8 @@ class RadvdHelper
*/
void ClearPrefixes();
/**
* \brief Set some attributes.
* \param name attribute name
* \param value attribute value
*/
void SetAttribute(std::string name, const AttributeValue& value);
/**
* \brief Install the application in a Node.
* \param node the Node
* \return application container
*/
ApplicationContainer Install(Ptr<Node> node);
private:
/**
* \brief An object factory.
*/
ObjectFactory m_factory;
Ptr<Application> DoInstall(Ptr<Node> node) override;
/// Container: interface index, RadvdInterface
typedef std::map<uint32_t, Ptr<RadvdInterface>> RadvdInterfaceMap;

View File

@@ -21,67 +21,24 @@
#include "v4traceroute-helper.h"
#include "ns3/names.h"
#include "ns3/output-stream-wrapper.h"
#include "ns3/v4traceroute.h"
namespace ns3
{
V4TraceRouteHelper::V4TraceRouteHelper(Ipv4Address remote)
V4TraceRouteHelper::V4TraceRouteHelper(const Ipv4Address& remote)
: ApplicationHelper("ns3::V4TraceRoute")
{
m_factory.SetTypeId("ns3::V4TraceRoute");
m_factory.Set("Remote", Ipv4AddressValue(remote));
}
void
V4TraceRouteHelper::SetAttribute(std::string name, const AttributeValue& value)
{
m_factory.Set(name, value);
}
ApplicationContainer
V4TraceRouteHelper::Install(Ptr<Node> node) const
{
return ApplicationContainer(InstallPriv(node));
}
ApplicationContainer
V4TraceRouteHelper::Install(std::string nodeName) const
{
Ptr<Node> node = Names::Find<Node>(nodeName);
return ApplicationContainer(InstallPriv(node));
}
ApplicationContainer
V4TraceRouteHelper::Install(NodeContainer c) const
{
ApplicationContainer apps;
for (auto i = c.Begin(); i != c.End(); ++i)
{
apps.Add(InstallPriv(*i));
}
return apps;
}
Ptr<Application>
V4TraceRouteHelper::InstallPriv(Ptr<Node> node) const
{
Ptr<V4TraceRoute> app = m_factory.Create<V4TraceRoute>();
node->AddApplication(app);
return app;
}
void
V4TraceRouteHelper::PrintTraceRouteAt(Ptr<Node> node, Ptr<OutputStreamWrapper> stream)
{
Ptr<V4TraceRoute> trace;
for (uint32_t i = 0; i < node->GetNApplications(); ++i)
{
trace = node->GetApplication(i)->GetObject<V4TraceRoute>();
if (trace)
if (auto trace = node->GetApplication(i)->GetObject<V4TraceRoute>())
{
*stream->GetStream() << "Tracing Route from Node " << node->GetId() << "\n";
trace->Print(stream);

View File

@@ -22,14 +22,13 @@
#ifndef V4TRACEROUTE_HELPER_H
#define V4TRACEROUTE_HELPER_H
#include "ns3/application-container.h"
#include "ns3/node-container.h"
#include "ns3/object-factory.h"
#include "ns3/output-stream-wrapper.h"
#include <ns3/application-helper.h>
namespace ns3
{
class OutputStreamWrapper;
/**
* \ingroup v4traceroute
* \brief Create a IPv4 traceroute application and associate it to a node
@@ -37,7 +36,7 @@ namespace ns3
* This class creates one or multiple instances of ns3::V4TraceRoute and associates
* it/them to one/multiple node(s).
*/
class V4TraceRouteHelper
class V4TraceRouteHelper : public ApplicationHelper
{
public:
/**
@@ -46,61 +45,14 @@ class V4TraceRouteHelper
*
* \param remote The address which should be traced
*/
V4TraceRouteHelper(Ipv4Address remote);
V4TraceRouteHelper(const Ipv4Address& remote);
/**
* Install a TraceRoute application on each Node in the provided NodeContainer.
*
* \param nodes The NodeContainer containing all of the nodes to get a V4TraceRoute
* application.
*
* \returns A list of TraceRoute applications, one for each input node
*/
ApplicationContainer Install(NodeContainer nodes) const;
/**
* Install a TraceRoute application on the provided Node. The Node is specified
* directly by a Ptr<Node>
*
* \param node The node to install the V4TraceRouteApplication on.
*
* \returns An ApplicationContainer holding the TraceRoute application created.
*/
ApplicationContainer Install(Ptr<Node> node) const;
/**
* Install a TraceRoute application on the provided Node. The Node is specified
* by a string that must have previously been associated with a Node using the
* Object Name Service.
*
* \param nodeName The node to install the V4TraceRouteApplication on.
*
* \returns An ApplicationContainer holding the TraceRoute application created.
*/
ApplicationContainer Install(std::string nodeName) const;
/**
* \brief Configure traceRoute applications attribute
* \param name attribute's name
* \param value attribute's value
*/
void SetAttribute(std::string name, const AttributeValue& value);
/**
* \brief Print the resulting trace routes from given node.
* \param node The origin node where the traceroute is initiated.
* \param stream The outputstream used to print the resulting traced routes.
*/
static void PrintTraceRouteAt(Ptr<Node> node, Ptr<OutputStreamWrapper> stream);
private:
/**
* \brief Do the actual application installation in the node
* \param node the node
* \returns a Smart pointer to the installed application
*/
Ptr<Application> InstallPriv(Ptr<Node> node) const;
/// Object factory
ObjectFactory m_factory;
};
} // namespace ns3