A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
tutorial-app.cc
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: GPL-2.0-only
3
*/
4
5
#include "
tutorial-app.h
"
6
7
#include "ns3/applications-module.h"
8
9
using namespace
ns3
;
10
11
TutorialApp::TutorialApp
()
12
:
m_socket
(nullptr),
13
m_peer
(),
14
m_packetSize
(0),
15
m_nPackets
(0),
16
m_dataRate
(0),
17
m_sendEvent
(),
18
m_running
(false),
19
m_packetsSent
(0)
20
{
21
}
22
23
TutorialApp::~TutorialApp
()
24
{
25
m_socket
=
nullptr
;
26
}
27
28
/* static */
29
TypeId
30
TutorialApp::GetTypeId
()
31
{
32
static
TypeId
tid =
TypeId
(
"TutorialApp"
)
33
.
SetParent
<
Application
>()
34
.SetGroupName(
"Tutorial"
)
35
.AddConstructor<
TutorialApp
>();
36
return
tid;
37
}
38
39
void
40
TutorialApp::Setup
(
Ptr<Socket>
socket,
41
Address
address,
42
uint32_t
packetSize
,
43
uint32_t
nPackets,
44
DataRate
dataRate)
45
{
46
m_socket
= socket;
47
m_peer
= address;
48
m_packetSize
=
packetSize
;
49
m_nPackets
= nPackets;
50
m_dataRate
= dataRate;
51
}
52
53
void
54
TutorialApp::StartApplication
()
55
{
56
m_running
=
true
;
57
m_packetsSent
= 0;
58
m_socket
->Bind();
59
m_socket
->Connect(
m_peer
);
60
SendPacket
();
61
}
62
63
void
64
TutorialApp::StopApplication
()
65
{
66
m_running
=
false
;
67
68
if
(
m_sendEvent
.IsPending())
69
{
70
Simulator::Cancel
(
m_sendEvent
);
71
}
72
73
if
(
m_socket
)
74
{
75
m_socket
->Close();
76
}
77
}
78
79
void
80
TutorialApp::SendPacket
()
81
{
82
Ptr<Packet>
packet =
Create<Packet>
(
m_packetSize
);
83
m_socket
->Send(packet);
84
85
if
(++
m_packetsSent
<
m_nPackets
)
86
{
87
ScheduleTx
();
88
}
89
}
90
91
void
92
TutorialApp::ScheduleTx
()
93
{
94
if
(
m_running
)
95
{
96
Time
tNext(
Seconds
(
m_packetSize
* 8 /
static_cast<
double
>
(
m_dataRate
.GetBitRate())));
97
m_sendEvent
=
Simulator::Schedule
(tNext, &
TutorialApp::SendPacket
,
this
);
98
}
99
}
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::Application::Application
Application()
Definition
application.cc:49
ns3::DataRate
Class for representing data rates.
Definition
data-rate.h:78
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:561
ns3::Simulator::Cancel
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:274
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition
nstime.h:94
ns3::TutorialApp::StopApplication
void StopApplication() override
Application specific shutdown code.
Definition
tutorial-app.cc:64
ns3::TutorialApp::ScheduleTx
void ScheduleTx()
Schedule a new transmission.
Definition
tutorial-app.cc:92
ns3::TutorialApp::GetTypeId
static TypeId GetTypeId()
Register this type.
Definition
tutorial-app.cc:30
ns3::TutorialApp::TutorialApp
TutorialApp()
Definition
tutorial-app.cc:11
ns3::TutorialApp::~TutorialApp
~TutorialApp() override
Definition
tutorial-app.cc:23
ns3::TutorialApp::m_sendEvent
EventId m_sendEvent
Send event.
Definition
tutorial-app.h:60
ns3::TutorialApp::m_socket
Ptr< Socket > m_socket
The transmission socket.
Definition
tutorial-app.h:55
ns3::TutorialApp::m_packetsSent
uint32_t m_packetsSent
The number of packets sent.
Definition
tutorial-app.h:62
ns3::TutorialApp::SendPacket
void SendPacket()
Send a packet.
Definition
tutorial-app.cc:80
ns3::TutorialApp::m_peer
Address m_peer
The destination address.
Definition
tutorial-app.h:56
ns3::TutorialApp::m_packetSize
uint32_t m_packetSize
The packet size.
Definition
tutorial-app.h:57
ns3::TutorialApp::Setup
void Setup(Ptr< Socket > socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
Setup the socket.
Definition
tutorial-app.cc:40
ns3::TutorialApp::StartApplication
void StartApplication() override
Application specific startup code.
Definition
tutorial-app.cc:54
ns3::TutorialApp::m_dataRate
DataRate m_dataRate
The data rate to use.
Definition
tutorial-app.h:59
ns3::TutorialApp::m_nPackets
uint32_t m_nPackets
The number of packets to send.
Definition
tutorial-app.h:58
ns3::TutorialApp::m_running
bool m_running
True if the application is running.
Definition
tutorial-app.h:61
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
uint32_t
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1345
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
tutorial-app.h
packetSize
static const uint32_t packetSize
Packet size generated at the AP.
Definition
wifi-power-adaptation-distance.cc:96
examples
tutorial
tutorial-app.cc
Generated on Wed Jun 11 2025 13:15:26 for ns-3 by
1.13.2