A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-scheduler.cc
Go to the documentation of this file.
1#include "network-scheduler.h"
2
3namespace ns3
4{
5namespace lorawan
6{
7
8NS_LOG_COMPONENT_DEFINE("NetworkScheduler");
9
10NS_OBJECT_ENSURE_REGISTERED(NetworkScheduler);
11
12TypeId
14{
15 static TypeId tid =
16 TypeId("ns3::NetworkScheduler")
18 .AddConstructor<NetworkScheduler>()
19 .AddTraceSource("ReceiveWindowOpened",
20 "Trace source that is fired when a receive window opportunity happens.",
22 "ns3::Packet::TracedCallback")
23 .SetGroupName("lorawan");
24 return tid;
25}
26
28{
29}
30
32 : m_status(status),
33 m_controller(controller)
34{
35}
36
38{
39}
40
41void
43{
44 NS_LOG_FUNCTION(packet);
45
46 // Get the current packet's frame counter
47 Ptr<Packet> packetCopy = packet->Copy();
48 LorawanMacHeader receivedMacHdr;
49 packetCopy->RemoveHeader(receivedMacHdr);
50 LoraFrameHeader receivedFrameHdr;
51 receivedFrameHdr.SetAsUplink();
52 packetCopy->RemoveHeader(receivedFrameHdr);
53
54 // Need to decide whether to schedule a receive window
55 if (!m_status->GetEndDeviceStatus(packet)->HasReceiveWindowOpportunityScheduled())
56 {
57 // Extract the address
58 LoraDeviceAddress deviceAddress = receivedFrameHdr.GetAddress();
59
60 // Schedule OnReceiveWindowOpportunity event
61 m_status->GetEndDeviceStatus(packet)->SetReceiveWindowOpportunity(
64 this,
65 deviceAddress,
66 1)); // This will be the first receive window
67 }
68}
69
70void
72{
73 NS_LOG_FUNCTION(deviceAddress);
74
75 NS_LOG_DEBUG("Opening receive window number " << window << " for device " << deviceAddress);
76
77 // Check whether we can send a reply to the device, again by using
78 // NetworkStatus
79 Address gwAddress = m_status->GetBestGatewayForDevice(deviceAddress, window);
80
81 if (gwAddress == Address() && window == 1)
82 {
83 NS_LOG_DEBUG("No suitable gateway found for first window.");
84
85 // No suitable gateway was found, but there's still hope to find one for the
86 // second window.
87 // Schedule another OnReceiveWindowOpportunity event
88 m_status->GetEndDeviceStatus(deviceAddress)
89 ->SetReceiveWindowOpportunity(
92 this,
93 deviceAddress,
94 2)); // This will be the second receive window
95 }
96 else if (gwAddress == Address() && window == 2)
97 {
98 // No suitable gateway was found and this was our last opportunity
99 // Simply give up.
100 NS_LOG_DEBUG("Giving up on reply: no suitable gateway was found "
101 << "on the second receive window");
102
103 // Reset the reply
104 // XXX Should we reset it here or keep it for the next opportunity?
105 m_status->GetEndDeviceStatus(deviceAddress)->RemoveReceiveWindowOpportunity();
106 m_status->GetEndDeviceStatus(deviceAddress)->InitializeReply();
107 }
108 else
109 {
110 // A gateway was found
111
112 NS_LOG_DEBUG("Found available gateway with address: " << gwAddress);
113
114 m_controller->BeforeSendingReply(m_status->GetEndDeviceStatus(deviceAddress));
115
116 // Check whether this device needs a response by querying m_status
117 bool needsReply = m_status->NeedsReply(deviceAddress);
118
119 if (needsReply)
120 {
121 NS_LOG_INFO("A reply is needed");
122
123 // Send the reply through that gateway
124 m_status->SendThroughGateway(m_status->GetReplyForDevice(deviceAddress, window),
125 gwAddress);
126
127 // Reset the reply
128 m_status->GetEndDeviceStatus(deviceAddress)->RemoveReceiveWindowOpportunity();
129 m_status->GetEndDeviceStatus(deviceAddress)->InitializeReply();
130 }
131 }
132}
133} // namespace lorawan
134} // namespace ns3
a polymophic address class
Definition: address.h:101
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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.
~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:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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.