A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-scheduler.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#include "network-scheduler.h"
10
11#include "network-controller.h"
12#include "network-status.h"
13
14#include "ns3/simulator.h"
15
16namespace ns3
17{
18namespace lorawan
19{
20
21NS_LOG_COMPONENT_DEFINE("NetworkScheduler");
22
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::NetworkScheduler")
31 .AddConstructor<NetworkScheduler>()
32 .AddTraceSource("ReceiveWindowOpened",
33 "Trace source that is fired when a receive window opportunity happens.",
35 "ns3::Packet::TracedCallback")
36 .SetGroupName("lorawan");
37 return tid;
38}
39
43
49
53
54void
56{
57 NS_LOG_FUNCTION(packet);
58
59 // Get the current packet's frame counter
60 Ptr<Packet> packetCopy = packet->Copy();
61 LorawanMacHeader receivedMacHdr;
62 packetCopy->RemoveHeader(receivedMacHdr);
63 LoraFrameHeader receivedFrameHdr;
64 receivedFrameHdr.SetAsUplink();
65 packetCopy->RemoveHeader(receivedFrameHdr);
66
67 // Need to decide whether to schedule a receive window
68 if (!m_status->GetEndDeviceStatus(packet)->HasReceiveWindowOpportunityScheduled())
69 {
70 // Extract the address
71 LoraDeviceAddress deviceAddress = receivedFrameHdr.GetAddress();
72
73 // Schedule OnReceiveWindowOpportunity event
74 m_status->GetEndDeviceStatus(packet)->SetReceiveWindowOpportunity(
77 this,
78 deviceAddress,
79 1)); // This will be the first receive window
80 }
81}
82
83void
85{
86 NS_LOG_FUNCTION(deviceAddress);
87
88 NS_LOG_DEBUG("Opening receive window number " << window << " for device " << deviceAddress);
89
90 // Check whether we can send a reply to the device, again by using
91 // NetworkStatus
92 Address gwAddress = m_status->GetBestGatewayForDevice(deviceAddress, window);
93
94 if (gwAddress == Address() && window == 1)
95 {
96 NS_LOG_DEBUG("No suitable gateway found for first window.");
97
98 // No suitable gateway was found, but there's still hope to find one for the
99 // second window.
100 // Schedule another OnReceiveWindowOpportunity event
101 m_status->GetEndDeviceStatus(deviceAddress)
102 ->SetReceiveWindowOpportunity(
105 this,
106 deviceAddress,
107 2)); // This will be the second receive window
108 }
109 else if (gwAddress == Address() && window == 2)
110 {
111 // No suitable gateway was found and this was our last opportunity
112 // Simply give up.
113 NS_LOG_DEBUG("Giving up on reply: no suitable gateway was found "
114 << "on the second receive window");
115
116 // Reset the reply
117 // XXX Should we reset it here or keep it for the next opportunity?
118 m_status->GetEndDeviceStatus(deviceAddress)->RemoveReceiveWindowOpportunity();
119 m_status->GetEndDeviceStatus(deviceAddress)->InitializeReply();
120 }
121 else
122 {
123 // A gateway was found
124
125 NS_LOG_DEBUG("Found available gateway with address: " << gwAddress);
126
127 m_controller->BeforeSendingReply(m_status->GetEndDeviceStatus(deviceAddress));
128
129 // Check whether this device needs a response by querying m_status
130 bool needsReply = m_status->NeedsReply(deviceAddress);
131
132 if (needsReply)
133 {
134 NS_LOG_INFO("A reply is needed");
135
136 // Send the reply through that gateway
137 m_status->SendThroughGateway(m_status->GetReplyForDevice(deviceAddress, window),
138 gwAddress);
139
140 // Reset the reply
141 m_status->GetEndDeviceStatus(deviceAddress)->RemoveReceiveWindowOpportunity();
142 m_status->GetEndDeviceStatus(deviceAddress)->InitializeReply();
143 }
144 }
145}
146
147} // namespace lorawan
148} // namespace ns3
a polymophic address class
Definition address.h:114
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
This class represents the device address of a LoraWAN end device.
This class represents the Frame header (FHDR) used in a LoraWAN network.
void SetAsUplink()
State that this is an uplink message.
LoraDeviceAddress GetAddress() const
Get this header's device address value.
This class represents the Mac header of a LoRaWAN packet.
Network server component in charge of scheduling downling packets onto devices' reception windows.
~NetworkScheduler() override
Destructor.
void OnReceivedPacket(Ptr< const Packet > packet)
Method called by NetworkServer application to inform the Scheduler of a newly arrived uplink packet.
Ptr< NetworkController > m_controller
A pointer to the NetworkController object.
Ptr< NetworkStatus > m_status
A pointer to the NetworkStatus object.
NetworkScheduler()
Default constructor.
static TypeId GetTypeId()
Register this type.
void OnReceiveWindowOpportunity(LoraDeviceAddress deviceAddress, int window)
Method that is scheduled after packet arrival in order to take action on sender's receive windows ope...
TracedCallback< Ptr< const Packet > > m_receiveWindowOpened
Trace callback source for reception windows openings.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.