From 1c9565bb408ae8967d7d89192aa931f551cfb396 Mon Sep 17 00:00:00 2001 From: Alessandro Aimi Date: Fri, 9 Feb 2024 21:27:24 +0100 Subject: [PATCH] Expose p2p connection of gateways to server (#150) --- examples/adr-example.cc | 10 ++++++- examples/aloha-throughput.cc | 10 ++++++- examples/complete-network-example.cc | 10 ++++++- examples/frame-counter-update.cc | 10 ++++++- examples/network-server-example.cc | 10 ++++++- helper/forwarder-helper.cc | 25 ++++++++--------- helper/forwarder-helper.h | 2 ++ helper/network-server-helper.cc | 42 +++++++++++++++------------- helper/network-server-helper.h | 11 ++------ test/utilities.cc | 18 +++++++++--- 10 files changed, 97 insertions(+), 51 deletions(-) diff --git a/examples/adr-example.cc b/examples/adr-example.cc index 5a8583b4ff..af8d255091 100644 --- a/examples/adr-example.cc +++ b/examples/adr-example.cc @@ -272,9 +272,17 @@ main(int argc, char* argv[]) NodeContainer networkServers; networkServers.Create(1); + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(networkServers.Get(0), *gw); + } + // Install the NetworkServer application on the network server NetworkServerHelper networkServerHelper; - networkServerHelper.SetGateways(gateways); networkServerHelper.SetEndDevices(endDevices); networkServerHelper.EnableAdr(adrEnabled); networkServerHelper.SetAdr(adrType); diff --git a/examples/aloha-throughput.cc b/examples/aloha-throughput.cc index 515d0db32f..fe2e021460 100644 --- a/examples/aloha-throughput.cc +++ b/examples/aloha-throughput.cc @@ -298,9 +298,17 @@ main(int argc, char* argv[]) NodeContainer networkServer; networkServer.Create(1); + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(networkServer.Get(0), *gw); + } + // Create a NS for the network 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..e9509fae05 100644 --- a/examples/complete-network-example.cc +++ b/examples/complete-network-example.cc @@ -323,9 +323,17 @@ main(int argc, char* argv[]) NodeContainer networkServer; networkServer.Create(1); + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(networkServer.Get(0), *gw); + } + // Create a NS for the network 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..2dd5f12ee1 100644 --- a/examples/frame-counter-update.cc +++ b/examples/frame-counter-update.cc @@ -252,9 +252,17 @@ main(int argc, char* argv[]) NodeContainer networkServer; networkServer.Create(1); + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(networkServer.Get(0), *gw); + } + // Create a NS for the network 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..632567625f 100644 --- a/examples/network-server-example.cc +++ b/examples/network-server-example.cc @@ -182,9 +182,17 @@ main(int argc, char* argv[]) NodeContainer networkServers; networkServers.Create(1); + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(networkServers.Get(0), *gw); + } + // Install the NetworkServer application on the network server NetworkServerHelper networkServerHelper; - networkServerHelper.SetGateways(gateways); networkServerHelper.SetEndDevices(endDevices); networkServerHelper.Install(networkServers); diff --git a/helper/forwarder-helper.cc b/helper/forwarder-helper.cc index 06b71ff3e7..62cbeeeb5c 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: Alessandro Aimi (12/02/2024) */ #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/forwarder-helper.h b/helper/forwarder-helper.h index b137daace4..1ddf23608f 100644 --- a/helper/forwarder-helper.h +++ b/helper/forwarder-helper.h @@ -15,6 +15,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Davide Magrin + * + * Modified: Alessandro Aimi (12/02/2024) */ #ifndef FORWARDER_HELPER_H diff --git a/helper/network-server-helper.cc b/helper/network-server-helper.cc index b1300709fc..997f0b167d 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: Alessandro Aimi (12/02/2024) */ #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"); } @@ -52,12 +54,6 @@ NetworkServerHelper::SetAttribute(std::string name, const AttributeValue& value) m_factory.Set(name, value); } -void -NetworkServerHelper::SetGateways(NodeContainer gateways) -{ - m_gateways = gateways; -} - void NetworkServerHelper::SetEndDevices(NodeContainer endDevices) { @@ -86,28 +82,34 @@ 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++) { + // Link the NetworkServer app to its NetDevices Ptr currentNetDevice = node->GetDevice(i); currentNetDevice->SetReceiveCallback(MakeCallback(&NetworkServer::Receive, app)); + + // Register gateways + Ptr channel = currentNetDevice->GetChannel(); + NS_ASSERT_MSG(DynamicCast(channel), + "Connection with gateways is not PointToPoint"); + for (uint32_t j = 0; j < channel->GetNDevices(); ++j) + { + Ptr gwNode = channel->GetDevice(j)->GetNode(); + // Point to point, so channel only holds 2 devices + if (gwNode->GetId() != node->GetId()) + { + // Add the gateway to the NS list + app->AddGateway(gwNode, currentNetDevice); + break; + } + } } // Add the end devices diff --git a/helper/network-server-helper.h b/helper/network-server-helper.h index 166451a2d7..67a4ee802c 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: Alessandro Aimi (12/02/2024) */ #ifndef NETWORK_SERVER_HELPER_H @@ -53,11 +55,6 @@ class NetworkServerHelper ApplicationContainer Install(Ptr node); - /** - * Set which gateways will need to be connected to this NS. - */ - void SetGateways(NodeContainer gateways); - /** * Set which end devices will be managed by this NS. */ @@ -81,12 +78,8 @@ class NetworkServerHelper ObjectFactory m_factory; - NodeContainer m_gateways; //!< Set of gateways to connect to this NS - 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..84b445e24e 100644 --- a/test/utilities.cc +++ b/test/utilities.cc @@ -95,12 +95,22 @@ CreateGateways(int nGateways, MobilityHelper mobility, Ptr channel) Ptr CreateNetworkServer(NodeContainer endDevices, NodeContainer gateways) { - // Create the NetworkServer + // Create the NetworkServer node + Ptr nsNode = CreateObject(); + + // PointToPoint links between gateways and server + PointToPointHelper p2p; + p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); + p2p.SetChannelAttribute("Delay", StringValue("2ms")); + for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw) + { + p2p.Install(nsNode, *gw); + } + + // Install server application NetworkServerHelper networkServerHelper = NetworkServerHelper(); networkServerHelper.SetEndDevices(endDevices); - networkServerHelper.SetGateways(gateways); - Ptr nsNode = CreateObject(); - networkServerHelper.Install(nsNode); // This connects NS and GWs + networkServerHelper.Install(nsNode); // Install a forwarder on the gateways ForwarderHelper forwarderHelper;