A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-server.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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 * Authors: Davide Magrin <magrinda@dei.unipd.it>
18 * Martina Capuzzo <capuzzom@dei.unipd.it>
19 */
20
21#include "network-server.h"
22
24#include "lora-device-address.h"
25#include "lora-frame-header.h"
26#include "lorawan-mac-header.h"
27#include "mac-command.h"
28#include "network-status.h"
29
30#include "ns3/net-device.h"
31#include "ns3/node-container.h"
32#include "ns3/packet.h"
33#include "ns3/point-to-point-net-device.h"
34
35namespace ns3
36{
37namespace lorawan
38{
39
40NS_LOG_COMPONENT_DEFINE("NetworkServer");
41
42NS_OBJECT_ENSURE_REGISTERED(NetworkServer);
43
44TypeId
46{
47 static TypeId tid =
48 TypeId("ns3::NetworkServer")
50 .AddConstructor<NetworkServer>()
51 .AddTraceSource(
52 "ReceivedPacket",
53 "Trace source that is fired when a packet arrives at the network server",
55 "ns3::Packet::TracedCallback")
56 .SetGroupName("lorawan");
57 return tid;
58}
59
61 : m_status(Create<NetworkStatus>()),
62 m_controller(Create<NetworkController>(m_status)),
63 m_scheduler(Create<NetworkScheduler>(m_status, m_controller))
64{
66}
67
69{
71}
72
73void
75{
77}
78
79void
81{
83}
84
85void
87{
88 NS_LOG_FUNCTION(this << gateway);
89
90 // Get the PointToPointNetDevice
91 Ptr<PointToPointNetDevice> p2pNetDevice;
92 for (uint32_t i = 0; i < gateway->GetNDevices(); i++)
93 {
94 p2pNetDevice = gateway->GetDevice(i)->GetObject<PointToPointNetDevice>();
95 if (p2pNetDevice)
96 {
97 // We found a p2pNetDevice on the gateway
98 break;
99 }
100 }
101
102 // Get the gateway's LoRa MAC layer (assumes gateway's MAC is configured as first device)
104 gateway->GetDevice(0)->GetObject<LoraNetDevice>()->GetMac()->GetObject<GatewayLorawanMac>();
105 NS_ASSERT(gwMac);
106
107 // Get the Address
108 Address gatewayAddress = p2pNetDevice->GetAddress();
109
110 // Create new gatewayStatus
111 Ptr<GatewayStatus> gwStatus = Create<GatewayStatus>(gatewayAddress, netDevice, gwMac);
112
113 m_status->AddGateway(gatewayAddress, gwStatus);
114}
115
116void
118{
120
121 // For each node in the container, call the function to add that single node
123 for (it = nodes.Begin(); it != nodes.End(); it++)
124 {
125 AddNode(*it);
126 }
127}
128
129void
131{
132 NS_LOG_FUNCTION(this << node);
133
134 // Get the LoraNetDevice
135 Ptr<LoraNetDevice> loraNetDevice;
136 for (uint32_t i = 0; i < node->GetNDevices(); i++)
137 {
138 loraNetDevice = node->GetDevice(i)->GetObject<LoraNetDevice>();
139 if (loraNetDevice)
140 {
141 // We found a LoraNetDevice on the node
142 break;
143 }
144 }
145
146 // Get the MAC
147 Ptr<ClassAEndDeviceLorawanMac> edLorawanMac =
148 loraNetDevice->GetMac()->GetObject<ClassAEndDeviceLorawanMac>();
149
150 // Update the NetworkStatus about the existence of this node
151 m_status->AddNode(edLorawanMac);
152}
153
154bool
156 Ptr<const Packet> packet,
157 uint16_t protocol,
158 const Address& address)
159{
160 NS_LOG_FUNCTION(this << packet << protocol << address);
161
162 // Create a copy of the packet
163 Ptr<Packet> myPacket = packet->Copy();
164
165 // Fire the trace source
166 m_receivedPacket(packet);
167
168 // Inform the scheduler of the newly arrived packet
169 m_scheduler->OnReceivedPacket(packet);
170
171 // Inform the status of the newly arrived packet
172 m_status->OnReceivedPacket(packet, address);
173
174 // Inform the controller of the newly arrived packet
175 m_controller->OnNewPacket(packet);
176
177 return true;
178}
179
180void
182{
183 NS_LOG_FUNCTION(this << component);
184
185 m_controller->Install(component);
186}
187
190{
191 return m_status;
192}
193
194} // namespace lorawan
195} // namespace ns3
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
keep track of a set of node pointers.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
A Device for a Point to Point Network Link.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Class representing the MAC layer of a Class A LoRaWAN device.
Class representing the MAC layer of a LoRaWAN gateway.
Hold together all LoRa related objects.
This class collects a series of components that deal with various aspects of managing the network,...
Network server component in charge of scheduling downling packets onto devices' reception windows.
void AddNode(Ptr< Node > node)
Inform the NetworkServer application that this node is connected to the network.
NetworkServer()
Default constructor.
Ptr< NetworkStatus > GetNetworkStatus()
Get the NetworkStatus object of this NetworkServer application.
void AddGateway(Ptr< Node > gateway, Ptr< NetDevice > netDevice)
Add the gateway to the list of gateways connected to this network server.
TracedCallback< Ptr< const Packet > > m_receivedPacket
The ReceivedPacket trace source.
bool Receive(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &sender)
Receive a packet from a gateway.
void StopApplication() override
Stop the network server application.
static TypeId GetTypeId()
Register this type.
Ptr< NetworkScheduler > m_scheduler
Ptr to the NetworkScheduler object.
Ptr< NetworkStatus > m_status
Ptr to the NetworkStatus object.
void AddNodes(NodeContainer nodes)
Inform the NetworkServer application that these nodes are connected to the network.
~NetworkServer() override
Destructor.
void AddComponent(Ptr< NetworkControllerComponent > component)
Add a NetworkControllerComponent to this NetworkServer application.
void StartApplication() override
Start the network server application.
Ptr< NetworkController > m_controller
Ptr to the NetworkController object.
This class represents the knowledge about the state of the network that is available at the network s...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:447
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.