Skip to content

Commit

Permalink
Expose p2p connection of gateways and server (#151)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
non-det-alle authored Mar 20, 2024
1 parent 5ce32fa commit 69cb2e3
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 73 deletions.
22 changes: 17 additions & 5 deletions examples/adr-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,28 @@ main(int argc, char* argv[])
// Create NS
////////////

NodeContainer networkServers;
networkServers.Create(1);
Ptr<Node> networkServer = CreateObject<Node>();

// 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<PointToPointNetDevice>(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;
Expand Down
18 changes: 15 additions & 3 deletions examples/aloha-throughput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,24 @@ main(int argc, char* argv[])
***************************/

// Create the NS node
NodeContainer networkServer;
networkServer.Create(1);
Ptr<Node> networkServer = CreateObject<Node>();

// 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<PointToPointNetDevice>(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
Expand Down
18 changes: 15 additions & 3 deletions examples/complete-network-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,24 @@ main(int argc, char* argv[])
***************************/

// Create the NS node
NodeContainer networkServer;
networkServer.Create(1);
Ptr<Node> networkServer = CreateObject<Node>();

// 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<PointToPointNetDevice>(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
Expand Down
18 changes: 15 additions & 3 deletions examples/frame-counter-update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,24 @@ main(int argc, char* argv[])
***************************/

// Create the NS node
NodeContainer networkServer;
networkServer.Create(1);
Ptr<Node> networkServer = CreateObject<Node>();

// 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<PointToPointNetDevice>(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
Expand Down
20 changes: 16 additions & 4 deletions examples/network-server-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,26 @@ main(int argc, char* argv[])
// Create NS
////////////

NodeContainer networkServers;
networkServers.Create(1);
Ptr<Node> networkServer = CreateObject<Node>();

// 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<PointToPointNetDevice>(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;
Expand Down
25 changes: 12 additions & 13 deletions helper/forwarder-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Davide Magrin <[email protected]>
*
* Modified by: Alessandro Aimi <[email protected]>
*/

#include "forwarder-helper.h"

#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"
Expand Down Expand Up @@ -71,6 +74,8 @@ Ptr<Application>
ForwarderHelper::InstallPriv(Ptr<Node> 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<Forwarder> app = m_factory.Create<Forwarder>();

Expand All @@ -80,22 +85,16 @@ ForwarderHelper::InstallPriv(Ptr<Node> node) const
// Link the Forwarder to the NetDevices
for (uint32_t i = 0; i < node->GetNDevices(); i++)
{
Ptr<NetDevice> currentNetDevice = node->GetDevice(i);
if (currentNetDevice->GetObject<LoraNetDevice>())
Ptr<NetDevice> currNetDev = node->GetDevice(i);
if (auto loraNetDev = DynamicCast<LoraNetDevice>(currNetDev); loraNetDev)
{
Ptr<LoraNetDevice> loraNetDevice = currentNetDevice->GetObject<LoraNetDevice>();
app->SetLoraNetDevice(loraNetDevice);
loraNetDevice->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromLora, app));
app->SetLoraNetDevice(loraNetDev);
loraNetDev->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromLora, app));
}
else if (currentNetDevice->GetObject<PointToPointNetDevice>())
else if (auto p2pNetDev = DynamicCast<PointToPointNetDevice>(currNetDev); p2pNetDev)
{
Ptr<PointToPointNetDevice> pointToPointNetDevice =
currentNetDevice->GetObject<PointToPointNetDevice>();

app->SetPointToPointNetDevice(pointToPointNetDevice);

pointToPointNetDevice->SetReceiveCallback(
MakeCallback(&Forwarder::ReceiveFromPointToPoint, app));
app->SetPointToPointNetDevice(p2pNetDev);
p2pNetDev->SetReceiveCallback(MakeCallback(&Forwarder::ReceiveFromPointToPoint, app));
}
else
{
Expand Down
46 changes: 16 additions & 30 deletions helper/network-server-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Davide Magrin <[email protected]>
*
* Modified by: Alessandro Aimi <[email protected]>
*/

#include "network-server-helper.h"
Expand All @@ -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"
Expand All @@ -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");
}

Expand All @@ -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
Expand All @@ -70,44 +78,22 @@ NetworkServerHelper::Install(Ptr<Node> 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<Application>
NetworkServerHelper::InstallPriv(Ptr<Node> node)
{
NS_LOG_FUNCTION(this << node);
NS_ASSERT_MSG(node->GetNDevices() > 0, "No gateways connected to provided node");

Ptr<NetworkServer> app = m_factory.Create<NetworkServer>();

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<NetDevice> currentNetDevice = node->GetDevice(i);
currentNetDevice->SetReceiveCallback(MakeCallback(&NetworkServer::Receive, app));
app->AddGateway(gwNode, currentNetDevice);
}

// Add the end devices
Expand Down
28 changes: 21 additions & 7 deletions helper/network-server-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Davide Magrin <[email protected]>
*
* Modified by: Alessandro Aimi <[email protected]>
*/

#ifndef NETWORK_SERVER_HELPER_H
Expand All @@ -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<std::pair<Ptr<PointToPointNetDevice>, Ptr<Node>>> P2PGwRegistration_t;

/**
* This class can install Network Server applications on multiple nodes at once.
*/
Expand All @@ -49,14 +60,18 @@ class NetworkServerHelper

void SetAttribute(std::string name, const AttributeValue& value);

ApplicationContainer Install(NodeContainer c);

ApplicationContainer Install(Ptr<Node> 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.
Expand All @@ -81,12 +96,11 @@ class NetworkServerHelper

ObjectFactory m_factory;

NodeContainer m_gateways; //!< Set of gateways to connect to this NS
std::list<std::pair<Ptr<NetDevice>, Ptr<Node>>>
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;
Expand Down
25 changes: 20 additions & 5 deletions test/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,27 @@ CreateGateways(int nGateways, MobilityHelper mobility, Ptr<LoraChannel> channel)
Ptr<Node>
CreateNetworkServer(NodeContainer endDevices, NodeContainer gateways)
{
// Create the NetworkServer
NetworkServerHelper networkServerHelper = NetworkServerHelper();
networkServerHelper.SetEndDevices(endDevices);
networkServerHelper.SetGateways(gateways);
// Create the NetworkServer node
Ptr<Node> nsNode = CreateObject<Node>();
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<PointToPointNetDevice>(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;
Expand Down

0 comments on commit 69cb2e3

Please sign in to comment.