A Discrete-Event Network Simulator
API
tutorial-app.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 #include "tutorial-app.h"
17 
18 #include "ns3/applications-module.h"
19 
20 using namespace ns3;
21 
23  : m_socket(nullptr),
24  m_peer(),
25  m_packetSize(0),
26  m_nPackets(0),
27  m_dataRate(0),
28  m_sendEvent(),
29  m_running(false),
30  m_packetsSent(0)
31 {
32 }
33 
35 {
36  m_socket = nullptr;
37 }
38 
39 /* static */
40 TypeId
42 {
43  static TypeId tid = TypeId("TutorialApp")
45  .SetGroupName("Tutorial")
46  .AddConstructor<TutorialApp>();
47  return tid;
48 }
49 
50 void
53  uint32_t packetSize,
54  uint32_t nPackets,
55  DataRate dataRate)
56 {
57  m_socket = socket;
58  m_peer = address;
60  m_nPackets = nPackets;
61  m_dataRate = dataRate;
62 }
63 
64 void
66 {
67  m_running = true;
68  m_packetsSent = 0;
69  m_socket->Bind();
71  SendPacket();
72 }
73 
74 void
76 {
77  m_running = false;
78 
79  if (m_sendEvent.IsRunning())
80  {
82  }
83 
84  if (m_socket)
85  {
86  m_socket->Close();
87  }
88 }
89 
90 void
92 {
93  Ptr<Packet> packet = Create<Packet>(m_packetSize);
94  m_socket->Send(packet);
95 
96  if (++m_packetsSent < m_nPackets)
97  {
98  ScheduleTx();
99  }
100 }
101 
102 void
104 {
105  if (m_running)
106  {
107  Time tNext(Seconds(m_packetSize * 8 / static_cast<double>(m_dataRate.GetBitRate())));
109  }
110 }
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
Class for representing data rates.
Definition: data-rate.h:89
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:305
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
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
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Tutorial - a simple Application sending packets.
Definition: tutorial-app.h:32
void StopApplication() override
Application specific shutdown code.
Definition: tutorial-app.cc:75
void ScheduleTx()
Schedule a new transmission.
static TypeId GetTypeId()
Register this type.
Definition: tutorial-app.cc:41
~TutorialApp() override
Definition: tutorial-app.cc:34
EventId m_sendEvent
Send event.
Definition: tutorial-app.h:71
Ptr< Socket > m_socket
The transmission socket.
Definition: tutorial-app.h:66
uint32_t m_packetsSent
The number of packets sent.
Definition: tutorial-app.h:73
void SendPacket()
Send a packet.
Definition: tutorial-app.cc:91
Address m_peer
The destination address.
Definition: tutorial-app.h:67
uint32_t m_packetSize
The packet size.
Definition: tutorial-app.h:68
void Setup(Ptr< Socket > socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
Setup the socket.
Definition: tutorial-app.cc:51
void StartApplication() override
Application specific startup code.
Definition: tutorial-app.cc:65
DataRate m_dataRate
The data rate to use.
Definition: tutorial-app.h:70
uint32_t m_nPackets
The number of packets to send.
Definition: tutorial-app.h:69
bool m_running
True if the application is running.
Definition: tutorial-app.h:72
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.