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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20/*
21 * This example creates a simple network in which all LoRaWAN components are
22 * simulated: end devices, some gateways and a network server.
23 * Two end devices are already configured to send unconfirmed and confirmed messages respectively.
24 */
25
26#include "ns3/command-line.h"
27#include "ns3/core-module.h"
28#include "ns3/forwarder-helper.h"
29#include "ns3/gateway-lora-phy.h"
30#include "ns3/log.h"
31#include "ns3/lora-channel.h"
32#include "ns3/lora-device-address-generator.h"
33#include "ns3/lora-helper.h"
34#include "ns3/lora-phy-helper.h"
35#include "ns3/lorawan-mac-helper.h"
36#include "ns3/mobility-helper.h"
37#include "ns3/network-module.h"
38#include "ns3/network-server-helper.h"
39#include "ns3/one-shot-sender-helper.h"
40#include "ns3/periodic-sender-helper.h"
41#include "ns3/periodic-sender.h"
42#include "ns3/point-to-point-module.h"
43#include "ns3/string.h"
44
45using namespace ns3;
46using namespace lorawan;
47
48NS_LOG_COMPONENT_DEFINE("NetworkServerExample");
49
50int
51main(int argc, char* argv[])
52{
53 bool verbose = false;
54
55 CommandLine cmd(__FILE__);
56 cmd.AddValue("verbose", "Whether to print output or not", verbose);
57 cmd.Parse(argc, argv);
58
59 // Logging
60 //////////
61
62 LogComponentEnable("NetworkServerExample", LOG_LEVEL_ALL);
63 LogComponentEnable("NetworkServer", LOG_LEVEL_ALL);
64 LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
65 // LogComponentEnable("LoraFrameHeader", LOG_LEVEL_ALL);
66 // LogComponentEnable("LorawanMacHeader", LOG_LEVEL_ALL);
67 // LogComponentEnable("MacCommand", LOG_LEVEL_ALL);
68 // LogComponentEnable("GatewayLoraPhy", LOG_LEVEL_ALL);
69 // LogComponentEnable("LoraPhy", LOG_LEVEL_ALL);
70 // LogComponentEnable("LoraChannel", LOG_LEVEL_ALL);
71 // LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
72 // LogComponentEnable("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
73 LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
74 LogComponentEnable("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
75 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
76 // LogComponentEnable("PointToPointNetDevice", LOG_LEVEL_ALL);
77 // LogComponentEnable ("Forwarder", LOG_LEVEL_ALL);
78 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
79 // LogComponentEnable ("DeviceStatus", LOG_LEVEL_ALL);
80 // LogComponentEnable ("GatewayStatus", LOG_LEVEL_ALL);
84
85 // Create a simple wireless channel
86 ///////////////////////////////////
87
88 Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel>();
89 loss->SetPathLossExponent(3.76);
90 loss->SetReference(1, 7.7);
91
92 Ptr<PropagationDelayModel> delay = CreateObject<ConstantSpeedPropagationDelayModel>();
93
94 Ptr<LoraChannel> channel = CreateObject<LoraChannel>(loss, delay);
95
96 // Helpers
97 //////////
98
99 // End device mobility
100 MobilityHelper mobilityEd;
101 MobilityHelper mobilityGw;
102 Ptr<ListPositionAllocator> positionAllocEd = CreateObject<ListPositionAllocator>();
103 positionAllocEd->Add(Vector(6000.0, 0.0, 0.0));
104 positionAllocEd->Add(Vector(0.0, 100.0, 0.0));
105 mobilityEd.SetPositionAllocator(positionAllocEd);
106 mobilityEd.SetMobilityModel("ns3::ConstantPositionMobilityModel");
107
108 // Gateway mobility
109 Ptr<ListPositionAllocator> positionAllocGw = CreateObject<ListPositionAllocator>();
110 positionAllocGw->Add(Vector(0.0, 0.0, 0.0));
111 positionAllocGw->Add(Vector(-2000.0, 0.0, 0.0));
112 positionAllocGw->Add(Vector(500.0, 0.0, 0.0));
113 mobilityGw.SetPositionAllocator(positionAllocGw);
114 mobilityGw.SetMobilityModel("ns3::ConstantPositionMobilityModel");
115
116 // Create the LoraPhyHelper
117 LoraPhyHelper phyHelper = LoraPhyHelper();
118 phyHelper.SetChannel(channel);
119
120 // Create the LorawanMacHelper
122
123 // Create the LoraHelper
124 LoraHelper helper = LoraHelper();
125
126 // Create end devices
127 /////////////
128
129 NodeContainer endDevices;
130 endDevices.Create(2);
131 mobilityEd.Install(endDevices);
132
133 // Create a LoraDeviceAddressGenerator
134 uint8_t nwkId = 54;
135 uint32_t nwkAddr = 1864;
137 CreateObject<LoraDeviceAddressGenerator>(nwkId, nwkAddr);
138
139 // Create the LoraNetDevices of the end devices
142 macHelper.SetAddressGenerator(addrGen);
144 helper.Install(phyHelper, macHelper, endDevices);
145
146 // Set message type (Default is unconfirmed)
147 Ptr<LorawanMac> edMac1 = endDevices.Get(1)->GetDevice(0)->GetObject<LoraNetDevice>()->GetMac();
148 Ptr<ClassAEndDeviceLorawanMac> edLorawanMac1 = edMac1->GetObject<ClassAEndDeviceLorawanMac>();
149 edLorawanMac1->SetMType(LorawanMacHeader::CONFIRMED_DATA_UP);
150
151 // Install applications in end devices
153 oneShotHelper.SetSendTime(Seconds(4));
154 oneShotHelper.Install(endDevices.Get(0));
155 oneShotHelper.SetSendTime(Seconds(10));
156 oneShotHelper.Install(endDevices.Get(1));
157 // oneShotHelper.SetSendTime (Seconds (8));
158 // oneShotHelper.Install(endDevices.Get (1));
159 // oneShotHelper.SetSendTime (Seconds (12));
160 // oneShotHelper.Install(endDevices.Get (2));
161
162 ////////////////
163 // Create gateways //
164 ////////////////
165
166 NodeContainer gateways;
167 gateways.Create(1);
168 mobilityGw.Install(gateways);
169
170 // Create the LoraNetDevices of the gateways
173 helper.Install(phyHelper, macHelper, gateways);
174
175 // Set spreading factors up
176 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
177
178 ////////////
179 // Create network serverNS
180 ////////////
181
182 Ptr<Node> networkServer = CreateObject<Node>();
183
184 // PointToPoint links between gateways and server
186 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
187 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
188 // Store network server app registration details for later
189 P2PGwRegistration_t gwRegistration;
190 for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw)
191 {
192 auto container = p2p.Install(networkServer, *gw);
193 auto serverP2PNetDev = DynamicCast<PointToPointNetDevice>(container.Get(0));
194 gwRegistration.emplace_back(serverP2PNetDev, *gw);
195 }
196
197 // Install the NetworkServer application on the network server
198 NetworkServerHelper networkServerHelper;
199 networkServerHelper.SetGatewaysP2P(gwRegistration);
200 networkServerHelper.SetEndDevices(endDevices);
201 networkServerHelper.Install(networkServer);
202
203 // Install the Forwarder application on the gateways
204 ForwarderHelper forwarderHelper;
205 forwarderHelper.Install(gateways);
206
207 // Start simulation
211
212 return 0;
213}
Parse command-line arguments.
Definition: command-line.h:232
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:149
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Class representing the MAC layer of a Class A LoRaWAN device.
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:48
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Definition: lora-helper.cc:44
Hold together all LoRa related objects.
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:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:302
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88
bool verbose