A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-server-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9/*
10 * This example creates a simple network in which all LoRaWAN components are
11 * simulated: end devices, some gateways and a network server.
12 * Two end devices are already configured to send unconfirmed and confirmed messages respectively.
13 */
14
15#include "ns3/core-module.h"
16#include "ns3/lorawan-module.h"
17#include "ns3/mobility-helper.h"
18#include "ns3/point-to-point-helper.h"
19
20using namespace ns3;
21using namespace lorawan;
22
23NS_LOG_COMPONENT_DEFINE("NetworkServerExample");
24
25int
26main(int argc, char* argv[])
27{
28 bool verbose = false;
29
30 CommandLine cmd(__FILE__);
31 cmd.AddValue("verbose", "Whether to print output or not", verbose);
32 cmd.Parse(argc, argv);
33
34 // Logging
35 //////////
36
37 LogComponentEnable("NetworkServerExample", LOG_LEVEL_ALL);
38 LogComponentEnable("NetworkServer", LOG_LEVEL_ALL);
39 LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
40 // LogComponentEnable("LoraFrameHeader", LOG_LEVEL_ALL);
41 // LogComponentEnable("LorawanMacHeader", LOG_LEVEL_ALL);
42 // LogComponentEnable("MacCommand", LOG_LEVEL_ALL);
43 // LogComponentEnable("GatewayLoraPhy", LOG_LEVEL_ALL);
44 // LogComponentEnable("LoraPhy", LOG_LEVEL_ALL);
45 // LogComponentEnable("LoraChannel", LOG_LEVEL_ALL);
46 // LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
47 // LogComponentEnable("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
48 LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
49 LogComponentEnable("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
50 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
51 // LogComponentEnable("PointToPointNetDevice", LOG_LEVEL_ALL);
52 // LogComponentEnable ("Forwarder", LOG_LEVEL_ALL);
53 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
54 // LogComponentEnable ("DeviceStatus", LOG_LEVEL_ALL);
55 // LogComponentEnable ("GatewayStatus", LOG_LEVEL_ALL);
59
60 // Create a simple wireless channel
61 ///////////////////////////////////
62
64 loss->SetPathLossExponent(3.76);
65 loss->SetReference(1, 7.7);
66
68
70
71 // Helpers
72 //////////
73
74 // End device mobility
75 MobilityHelper mobilityEd;
76 MobilityHelper mobilityGw;
78 positionAllocEd->Add(Vector(6000.0, 0.0, 0.0));
79 positionAllocEd->Add(Vector(0.0, 100.0, 0.0));
80 mobilityEd.SetPositionAllocator(positionAllocEd);
81 mobilityEd.SetMobilityModel("ns3::ConstantPositionMobilityModel");
82
83 // Gateway mobility
85 positionAllocGw->Add(Vector(0.0, 0.0, 0.0));
86 positionAllocGw->Add(Vector(-2000.0, 0.0, 0.0));
87 positionAllocGw->Add(Vector(500.0, 0.0, 0.0));
88 mobilityGw.SetPositionAllocator(positionAllocGw);
89 mobilityGw.SetMobilityModel("ns3::ConstantPositionMobilityModel");
90
91 // Create the LoraPhyHelper
92 LoraPhyHelper phyHelper = LoraPhyHelper();
93 phyHelper.SetChannel(channel);
94
95 // Create the LorawanMacHelper
97
98 // Create the LoraHelper
99 LoraHelper helper = LoraHelper();
100
101 // Create end devices
102 /////////////
103
104 NodeContainer endDevices;
105 endDevices.Create(2);
106 mobilityEd.Install(endDevices);
107
108 // Create a LoraDeviceAddressGenerator
109 uint8_t nwkId = 54;
110 uint32_t nwkAddr = 1864;
113
114 // Create the LoraNetDevices of the end devices
117 macHelper.SetAddressGenerator(addrGen);
119 helper.Install(phyHelper, macHelper, endDevices);
120
121 // Set message type (Default is unconfirmed)
122 Ptr<LorawanMac> edMac1 = DynamicCast<LoraNetDevice>(endDevices.Get(1)->GetDevice(0))->GetMac();
124 edLorawanMac1->SetMType(LorawanMacHeader::CONFIRMED_DATA_UP);
125
126 // Install applications in end devices
128 oneShotHelper.SetSendTime(Seconds(4));
129 oneShotHelper.Install(endDevices.Get(0));
130 oneShotHelper.SetSendTime(Seconds(10));
131 oneShotHelper.Install(endDevices.Get(1));
132 // oneShotHelper.SetSendTime (Seconds (8));
133 // oneShotHelper.Install(endDevices.Get (1));
134 // oneShotHelper.SetSendTime (Seconds (12));
135 // oneShotHelper.Install(endDevices.Get (2));
136
137 ////////////////
138 // Create gateways //
139 ////////////////
140
141 NodeContainer gateways;
142 gateways.Create(1);
143 mobilityGw.Install(gateways);
144
145 // Create the LoraNetDevices of the gateways
148 helper.Install(phyHelper, macHelper, gateways);
149
150 // Set spreading factors up
151 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
152
153 ////////////
154 // Create network serverNS
155 ////////////
156
157 Ptr<Node> networkServer = CreateObject<Node>();
158
159 // PointToPoint links between gateways and server
161 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
162 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
163 // Store network server app registration details for later
164 P2PGwRegistration_t gwRegistration;
165 for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw)
166 {
167 auto container = p2p.Install(networkServer, *gw);
168 auto serverP2PNetDev = DynamicCast<PointToPointNetDevice>(container.Get(0));
169 gwRegistration.emplace_back(serverP2PNetDev, *gw);
170 }
171
172 // Install the NetworkServer application on the network server
173 NetworkServerHelper networkServerHelper;
174 networkServerHelper.SetGatewaysP2P(gwRegistration);
175 networkServerHelper.SetEndDevices(endDevices);
176 networkServerHelper.Install(networkServer);
177
178 // Install the Forwarder application on the gateways
179 ForwarderHelper forwarderHelper;
180 forwarderHelper.Install(gateways);
181
182 // Start simulation
186
187 return 0;
188}
Parse command-line arguments.
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition node.cc:138
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Hold variables of type string.
Definition string.h:45
This class can be used to install Forwarder applications on a set of gateways.
ApplicationContainer Install(NodeContainer c) const
Install a Forwarder application on each node of the input container configured with all the attribute...
Helps to create LoraNetDevice objects.
Definition lora-helper.h:34
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Helper to install LoraPhy instances on multiple Nodes.
void SetDeviceType(enum DeviceType dt)
Set the kind of PHY this helper will create.
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel to connect the PHYs to.
Helper class for configuring and installing the LorawanMac class on devices and gateways.
void SetDeviceType(enum DeviceType dt)
Set the kind of MAC this helper will create.
void SetAddressGenerator(Ptr< LoraDeviceAddressGenerator > addrGen)
Set the address generator to use for creation of these nodes.
static std::vector< int > SetSpreadingFactorsUp(NodeContainer endDevices, NodeContainer gateways, Ptr< LoraChannel > channel)
Initialize the end devices' data rate parameter.
void SetRegion(enum Regions region)
Set the region in which the device is to operate.
This class can install a NetworkServer application on a node.
void SetGatewaysP2P(const P2PGwRegistration_t &registration)
Register gateways connected with point-to-point to this network server.
void SetEndDevices(NodeContainer endDevices)
Set which end devices will be managed by this network server.
ApplicationContainer Install(Ptr< Node > node)
Create one lorawan network server application on the Node.
This class can be used to install OneShotSender applications on multiple nodes at once.
void SetSendTime(Time sendTime)
Set the send time of the applications.
ApplicationContainer Install(NodeContainer c) const
Install a OneShotSender application on each node of the input container configured with all the attri...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
std::list< std::pair< Ptr< PointToPointNetDevice >, Ptr< Node > > > P2PGwRegistration_t
Store network server app registration details for gateway nodes having a P2P link with the network se...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:279
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:112
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:297
channel
Definition third.py:77
bool verbose