Files
unison/examples/tutorial/tutorial-app.cc

112 lines
2.4 KiB
C++
Raw Normal View History

2022-01-30 11:14:47 -06:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tutorial-app.h"
2022-10-07 20:08:35 +00:00
2022-01-30 11:14:47 -06:00
#include "ns3/applications-module.h"
using namespace ns3;
2022-10-07 20:08:35 +00:00
TutorialApp::TutorialApp()
: m_socket(nullptr),
m_peer(),
m_packetSize(0),
m_nPackets(0),
m_dataRate(0),
m_sendEvent(),
m_running(false),
m_packetsSent(0)
2022-01-30 11:14:47 -06:00
{
}
2022-10-07 20:08:35 +00:00
TutorialApp::~TutorialApp()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
m_socket = nullptr;
2022-01-30 11:14:47 -06:00
}
/* static */
2022-10-07 20:08:35 +00:00
TypeId
TutorialApp::GetTypeId()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
static TypeId tid = TypeId("TutorialApp")
.SetParent<Application>()
.SetGroupName("Tutorial")
.AddConstructor<TutorialApp>();
return tid;
2022-01-30 11:14:47 -06:00
}
void
2022-10-07 20:08:35 +00:00
TutorialApp::Setup(Ptr<Socket> socket,
Address address,
uint32_t packetSize,
uint32_t nPackets,
DataRate dataRate)
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
2022-01-30 11:14:47 -06:00
}
void
2022-10-07 20:08:35 +00:00
TutorialApp::StartApplication()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
m_running = true;
m_packetsSent = 0;
m_socket->Bind();
m_socket->Connect(m_peer);
SendPacket();
2022-01-30 11:14:47 -06:00
}
void
2022-10-07 20:08:35 +00:00
TutorialApp::StopApplication()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
m_running = false;
2022-01-30 11:14:47 -06:00
2022-10-07 20:08:35 +00:00
if (m_sendEvent.IsRunning())
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
Simulator::Cancel(m_sendEvent);
2022-01-30 11:14:47 -06:00
}
2022-10-07 20:08:35 +00:00
if (m_socket)
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
m_socket->Close();
2022-01-30 11:14:47 -06:00
}
}
void
2022-10-07 20:08:35 +00:00
TutorialApp::SendPacket()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
Ptr<Packet> packet = Create<Packet>(m_packetSize);
m_socket->Send(packet);
2022-01-30 11:14:47 -06:00
2022-10-07 20:08:35 +00:00
if (++m_packetsSent < m_nPackets)
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
ScheduleTx();
2022-01-30 11:14:47 -06:00
}
}
void
2022-10-07 20:08:35 +00:00
TutorialApp::ScheduleTx()
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
if (m_running)
2022-01-30 11:14:47 -06:00
{
2022-10-07 20:08:35 +00:00
Time tNext(Seconds(m_packetSize * 8 / static_cast<double>(m_dataRate.GetBitRate())));
m_sendEvent = Simulator::Schedule(tNext, &TutorialApp::SendPacket, this);
2022-01-30 11:14:47 -06:00
}
}