A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
periodic-sender.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#include "periodic-sender.h"
21
22#include "lora-net-device.h"
23
24#include "ns3/double.h"
25#include "ns3/log.h"
26#include "ns3/pointer.h"
27#include "ns3/string.h"
28
29namespace ns3
30{
31namespace lorawan
32{
33
34NS_LOG_COMPONENT_DEFINE("PeriodicSender");
35
36NS_OBJECT_ENSURE_REGISTERED(PeriodicSender);
37
38TypeId
40{
41 static TypeId tid = TypeId("ns3::PeriodicSender")
43 .AddConstructor<PeriodicSender>()
44 .SetGroupName("lorawan")
45 .AddAttribute("Interval",
46 "The interval between packet sends of this app",
51 // .AddAttribute ("PacketSizeRandomVariable", "The random variable that determines the shape of
52 // the packet size, in bytes",
53 // StringValue ("ns3::UniformRandomVariable[Min=0,Max=10]"),
54 // MakePointerAccessor (&PeriodicSender::m_pktSizeRV),
55 // MakePointerChecker <RandomVariableStream>());
56 return tid;
57}
58
60 : m_interval(Seconds(10)),
61 m_initialDelay(Seconds(1)),
62 m_basePktSize(10),
63 m_pktSizeRV(nullptr)
64
65{
67}
68
70{
72}
73
74void
76{
77 NS_LOG_FUNCTION(this << interval);
78 m_interval = interval;
79}
80
81Time
83{
84 NS_LOG_FUNCTION(this);
85 return m_interval;
86}
87
88void
90{
91 NS_LOG_FUNCTION(this << delay);
92 m_initialDelay = delay;
93}
94
95void
97{
98 m_pktSizeRV = rv;
99}
100
101void
103{
104 m_basePktSize = size;
105}
106
107void
109{
110 NS_LOG_FUNCTION(this);
111
112 // Create and send a new packet
113 Ptr<Packet> packet;
114 if (m_pktSizeRV)
115 {
116 int randomsize = m_pktSizeRV->GetInteger();
117 packet = Create<Packet>(m_basePktSize + randomsize);
118 }
119 else
120 {
121 packet = Create<Packet>(m_basePktSize);
122 }
123 m_mac->Send(packet);
124
125 // Schedule the next SendPacket event
127
128 NS_LOG_DEBUG("Sent a packet of size " << packet->GetSize());
129}
130
131void
133{
134 NS_LOG_FUNCTION(this);
135
136 // Make sure we have a MAC layer
137 if (!m_mac)
138 {
139 // Assumes there's only one device
140 Ptr<LoraNetDevice> loraNetDevice = m_node->GetDevice(0)->GetObject<LoraNetDevice>();
141
142 m_mac = loraNetDevice->GetMac();
144 }
145
146 // Schedule the next SendPacket event
148 NS_LOG_DEBUG("Starting up application with a first event with a " << m_initialDelay.GetSeconds()
149 << " seconds delay");
151 NS_LOG_DEBUG("Event Id: " << m_sendEvent.GetUid());
152}
153
154void
156{
159}
160
161} // namespace lorawan
162} // namespace ns3
The base class for all ns3 applications.
Definition: application.h:62
Ptr< Node > m_node
The node that this application is installed on.
Definition: application.h:158
uint32_t GetUid() const
Definition: event-id.cc:110
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:149
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:285
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Hold together all LoRa related objects.
Ptr< RandomVariableStream > m_pktSizeRV
The random variable that adds bytes to the packet size.
Time m_interval
The interval between to consecutive send events.
~PeriodicSender() override
Destructor.
Time GetInterval() const
Get the sending interval.
Ptr< LorawanMac > m_mac
The MAC layer of this node.
void SetInterval(Time interval)
Set the sending interval.
void SetPacketSize(uint8_t size)
Set packet size.
void StartApplication() override
Start the application by scheduling the first SendPacket event.
void SetInitialDelay(Time delay)
Set the initial delay of this application.
void SetPacketSizeRandomVariable(Ptr< RandomVariableStream > rv)
Set to add randomness to the base packet size.
Time m_initialDelay
The initial delay of this application.
static TypeId GetTypeId()
Register this type.
void SendPacket()
Send a packet using the LoraNetDevice's Send method.
EventId m_sendEvent
The sending event scheduled as next.
PeriodicSender()
Default constructor.
void StopApplication() override
Stop the application.
uint8_t m_basePktSize
The packet size.
#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_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427