From 69cb2e32b3cf9934848772362bed58b670359192 Mon Sep 17 00:00:00 2001 From: Alessandro Aimi Date: Wed, 20 Mar 2024 15:35:04 +0100 Subject: [PATCH] Expose p2p connection of gateways and server (#151) * Expose p2p connection of gateways to server (#150) * Expose gw registration to simplify NetworkServerHelper::Install + remove NodeContainer install of NetworkServerHelper + add P2PGwRegistration_t helper structure to facilitate independent instantiation of P2P and server app * Directly pass P2PGwRegistration_t to NetworkServerHelper --- examples/adr-example.cc | 22 ++++++++++--- examples/aloha-throughput.cc | 18 +++++++++-- examples/complete-network-example.cc | 18 +++++++++-- examples/frame-counter-update.cc | 18 +++++++++-- examples/network-server-example.cc | 20 +++++++++--- helper/forwarder-helper.cc | 25 ++++++++------- helper/network-server-helper.cc | 46 ++++++++++------------------ helper/network-server-helper.h | 28 ++++++++++++----- test/utilities.cc | 25 ++++++++++++--- 9 files changed, 147 insertions(+), 73 deletions(-) diff --git a/examples/adr-example.cc b/examples/adr-example.cc index 5a8583b4ff..6c61d89d6e 100644 --- a/examples/adr-example.cc +++ b/examples/adr-example.cc @@ -269,16 +269,28 @@ main(int argc, char* argv[]) // Create NS //////////// - NodeContainer networkServers; - networkServers.Create(1); + Ptr networkServer = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(networkServer, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } // Install the NetworkServer application on the network server NetworkServerHelper networkServerHelper; - networkServerHelper.SetGateways(gateways); - networkServerHelper.SetEndDevices(endDevices); networkServerHelper.EnableAdr(adrEnabled); networkServerHelper.SetAdr(adrType); - networkServerHelper.Install(networkServers); + networkServerHelper.SetGatewaysP2P(gwRegistration); + networkServerHelper.SetEndDevices(endDevices); + networkServerHelper.Install(networkServer); // Install the Forwarder application on the gateways ForwarderHelper forwarderHelper; diff --git a/examples/aloha-throughput.cc b/examples/aloha-throughput.cc index 515d0db32f..551141df3d 100644 --- a/examples/aloha-throughput.cc +++ b/examples/aloha-throughput.cc @@ -295,12 +295,24 @@ main(int argc, char* argv[]) ***************************/ // Create the NS node - NodeContainer networkServer; - networkServer.Create(1); + Ptr networkServer = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(networkServer, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } // Create a NS for the network + nsHelper.SetGatewaysP2P(gwRegistration); nsHelper.SetEndDevices(endDevices); - nsHelper.SetGateways(gateways); nsHelper.Install(networkServer); // Create a forwarder for each gateway diff --git a/examples/complete-network-example.cc b/examples/complete-network-example.cc index 665a1868e1..ee02037070 100644 --- a/examples/complete-network-example.cc +++ b/examples/complete-network-example.cc @@ -320,12 +320,24 @@ main(int argc, char* argv[]) ***************************/ // Create the NS node - NodeContainer networkServer; - networkServer.Create(1); + Ptr networkServer = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(networkServer, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } // Create a NS for the network + nsHelper.SetGatewaysP2P(gwRegistration); nsHelper.SetEndDevices(endDevices); - nsHelper.SetGateways(gateways); nsHelper.Install(networkServer); // Create a forwarder for each gateway diff --git a/examples/frame-counter-update.cc b/examples/frame-counter-update.cc index 8a4dbba6de..ff1d3f54d7 100644 --- a/examples/frame-counter-update.cc +++ b/examples/frame-counter-update.cc @@ -249,12 +249,24 @@ main(int argc, char* argv[]) ***************************/ // Create the NS node - NodeContainer networkServer; - networkServer.Create(1); + Ptr networkServer = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(networkServer, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } // Create a NS for the network + nsHelper.SetGatewaysP2P(gwRegistration); nsHelper.SetEndDevices(endDevices); - nsHelper.SetGateways(gateways); nsHelper.Install(networkServer); // Create a forwarder for each gateway diff --git a/examples/network-server-example.cc b/examples/network-server-example.cc index e86408bc1a..4f50e319d2 100644 --- a/examples/network-server-example.cc +++ b/examples/network-server-example.cc @@ -179,14 +179,26 @@ main(int argc, char* argv[]) // Create NS //////////// - NodeContainer networkServers; - networkServers.Create(1); + Ptr networkServer = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(networkServer, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } // Install the NetworkServer application on the network server NetworkServerHelper networkServerHelper; - networkServerHelper.SetGateways(gateways); + networkServerHelper.SetGatewaysP2P(gwRegistration); networkServerHelper.SetEndDevices(endDevices); - networkServerHelper.Install(networkServers); + networkServerHelper.Install(networkServer); // Install the Forwarder application on the gateways ForwarderHelper forwarderHelper; diff --git a/helper/forwarder-helper.cc b/helper/forwarder-helper.cc index 06b71ff3e7..06490f5f37 100644 --- a/helper/forwarder-helper.cc +++ b/helper/forwarder-helper.cc @@ -15,6 +15,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Davide Magrin + * + * Modified by: Alessandro Aimi */ #include "forwarder-helper.h" @@ -22,6 +24,7 @@ #include "ns3/double.h" #include "ns3/forwarder.h" #include "ns3/log.h" +#include "ns3/lora-net-device.h" #include "ns3/random-variable-stream.h" #include "ns3/simulator.h" #include "ns3/string.h" @@ -71,6 +74,8 @@ Ptr ForwarderHelper::InstallPriv(Ptr node) const { NS_LOG_FUNCTION(this << node); + NS_ASSERT_MSG(node->GetNDevices() == 2, + "NDevices != 2, the node must have a LoraNetDevice and a PointToPointNetDevice"); Ptr app = m_factory.Create(); @@ -80,22 +85,16 @@ ForwarderHelper::InstallPriv(Ptr node) const // Link the Forwarder to the NetDevices for (uint32_t i = 0; i < node->GetNDevices(); i++) { - Ptr currentNetDevice = node->GetDevice(i); - if (currentNetDevice->GetObject()) + Ptr currNetDev = node->GetDevice(i); + if (auto loraNetDev = DynamicCast(currNetDev); loraNetDev) { - Ptr loraNetDevice = currentNetDevice->GetObject(); - app->SetLoraNetDevice(loraNetDevice); - loraNetDevice->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromLora, app)); + app->SetLoraNetDevice(loraNetDev); + loraNetDev->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromLora, app)); } - else if (currentNetDevice->GetObject()) + else if (auto p2pNetDev = DynamicCast(currNetDev); p2pNetDev) { - Ptr pointToPointNetDevice = - currentNetDevice->GetObject(); - - app->SetPointToPointNetDevice(pointToPointNetDevice); - - pointToPointNetDevice->SetReceiveCallback( - MakeCallback(&Forwarder::ReceiveFromPointToPoint, app)); + app->SetPointToPointNetDevice(p2pNetDev); + p2pNetDev->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromPointToPoint, app)); } else { diff --git a/helper/network-server-helper.cc b/helper/network-server-helper.cc index b1300709fc..68eee0eeb0 100644 --- a/helper/network-server-helper.cc +++ b/helper/network-server-helper.cc @@ -15,6 +15,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Davide Magrin + * + * Modified by: Alessandro Aimi */ #include "network-server-helper.h" @@ -23,6 +25,7 @@ #include "ns3/double.h" #include "ns3/log.h" #include "ns3/network-controller-components.h" +#include "ns3/point-to-point-channel.h" #include "ns3/simulator.h" #include "ns3/string.h" #include "ns3/trace-source-accessor.h" @@ -35,10 +38,9 @@ namespace lorawan NS_LOG_COMPONENT_DEFINE("NetworkServerHelper"); NetworkServerHelper::NetworkServerHelper() + : m_adrEnabled(false) { m_factory.SetTypeId("ns3::NetworkServer"); - p2pHelper.SetDeviceAttribute("DataRate", StringValue("5Mbps")); - p2pHelper.SetChannelAttribute("Delay", StringValue("2ms")); SetAdr("ns3::AdrComponent"); } @@ -53,9 +55,15 @@ NetworkServerHelper::SetAttribute(std::string name, const AttributeValue& value) } void -NetworkServerHelper::SetGateways(NodeContainer gateways) +NetworkServerHelper::SetGatewaysP2P(const P2PGwRegistration_t& registration) { - m_gateways = gateways; + for (const auto& [serverP2PNetDev, gwNode] : registration) + { + NS_ASSERT_MSG( + serverP2PNetDev->GetNode()->GetId() != gwNode->GetId(), + "wrong P2P NetDevice detected, please provide the one on the NS's side instead"); + m_gatewayRegistrationList.emplace_back(serverP2PNetDev, gwNode); + } } void @@ -70,44 +78,22 @@ NetworkServerHelper::Install(Ptr node) return ApplicationContainer(InstallPriv(node)); } -ApplicationContainer -NetworkServerHelper::Install(NodeContainer c) -{ - ApplicationContainer apps; - for (auto i = c.Begin(); i != c.End(); ++i) - { - apps.Add(InstallPriv(*i)); - } - - return apps; -} - Ptr NetworkServerHelper::InstallPriv(Ptr node) { NS_LOG_FUNCTION(this << node); + NS_ASSERT_MSG(node->GetNDevices() > 0, "No gateways connected to provided node"); Ptr app = m_factory.Create(); app->SetNode(node); node->AddApplication(app); - // Cycle on each gateway - for (auto i = m_gateways.Begin(); i != m_gateways.End(); i++) - { - // Add the connections with the gateway - // Create a PointToPoint link between gateway and NS - NetDeviceContainer container = p2pHelper.Install(node, *i); - - // Add the gateway to the NS list - app->AddGateway(*i, container.Get(0)); - } - - // Link the NetworkServer to its NetDevices - for (uint32_t i = 0; i < node->GetNDevices(); i++) + // Connect the net devices receive callback to the app and register the respective gateway + for (const auto& [currentNetDevice, gwNode] : m_gatewayRegistrationList) { - Ptr currentNetDevice = node->GetDevice(i); currentNetDevice->SetReceiveCallback(MakeCallback(&NetworkServer::Receive, app)); + app->AddGateway(gwNode, currentNetDevice); } // Add the end devices diff --git a/helper/network-server-helper.h b/helper/network-server-helper.h index 166451a2d7..dd9e0ff362 100644 --- a/helper/network-server-helper.h +++ b/helper/network-server-helper.h @@ -15,6 +15,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Davide Magrin + * + * Modified by: Alessandro Aimi */ #ifndef NETWORK_SERVER_HELPER_H @@ -37,6 +39,15 @@ namespace ns3 namespace lorawan { +/** + * Store NS app registration details for gateway nodes having a P2P link with the NS. + * + * For each gateway, store in a pair: + * - The Point-to-point net device of the network server; + * - The gateway node connected to the P2P net device. + */ +typedef std::list, Ptr>> P2PGwRegistration_t; + /** * This class can install Network Server applications on multiple nodes at once. */ @@ -49,14 +60,18 @@ class NetworkServerHelper void SetAttribute(std::string name, const AttributeValue& value); - ApplicationContainer Install(NodeContainer c); - ApplicationContainer Install(Ptr node); /** - * Set which gateways will need to be connected to this NS. + * Register gateways connected with point-to-point to this NS. + * + * \remark For the moment, only P2P connections are supported. + * + * \param registration The gateways registration data. + * + * \see ns3::lorawan::P2PGwRegistration_t */ - void SetGateways(NodeContainer gateways); + void SetGatewaysP2P(const P2PGwRegistration_t& registration); /** * Set which end devices will be managed by this NS. @@ -81,12 +96,11 @@ class NetworkServerHelper ObjectFactory m_factory; - NodeContainer m_gateways; //!< Set of gateways to connect to this NS + std::list, Ptr>> + m_gatewayRegistrationList; //!< List of gateway nodes to register to this NS net devices NodeContainer m_endDevices; //!< Set of endDevices to connect to this NS - PointToPointHelper p2pHelper; //!< Helper to create PointToPoint links - bool m_adrEnabled; ObjectFactory m_adrSupportFactory; diff --git a/test/utilities.cc b/test/utilities.cc index 706e81773b..683ce5b70d 100644 --- a/test/utilities.cc +++ b/test/utilities.cc @@ -95,12 +95,27 @@ CreateGateways(int nGateways, MobilityHelper mobility, Ptr channel) Ptr CreateNetworkServer(NodeContainer endDevices, NodeContainer gateways) { - // Create the NetworkServer - NetworkServerHelper networkServerHelper = NetworkServerHelper(); - networkServerHelper.SetEndDevices(endDevices); - networkServerHelper.SetGateways(gateways); + // Create the NetworkServer node Ptr nsNode = CreateObject(); - networkServerHelper.Install(nsNode); // This connects NS and GWs + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + // Store NS app registration details for later + P2PGwRegistration_t gwRegistration; + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + auto container = p2p.Install(nsNode, *gw); + auto serverP2PNetDev = DynamicCast(container.Get(0)); + gwRegistration.emplace_back(serverP2PNetDev, *gw); + } + + // Install server application + NetworkServerHelper networkServerHelper; + networkServerHelper.SetGatewaysP2P(gwRegistration); + networkServerHelper.SetEndDevices(endDevices); + networkServerHelper.Install(nsNode); // Install a forwarder on the gateways ForwarderHelper forwarderHelper;