112 lines
2.4 KiB
C++
112 lines
2.4 KiB
C++
/* -*- 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"
|
|
|
|
#include "ns3/applications-module.h"
|
|
|
|
using namespace ns3;
|
|
|
|
TutorialApp::TutorialApp()
|
|
: m_socket(nullptr),
|
|
m_peer(),
|
|
m_packetSize(0),
|
|
m_nPackets(0),
|
|
m_dataRate(0),
|
|
m_sendEvent(),
|
|
m_running(false),
|
|
m_packetsSent(0)
|
|
{
|
|
}
|
|
|
|
TutorialApp::~TutorialApp()
|
|
{
|
|
m_socket = nullptr;
|
|
}
|
|
|
|
/* static */
|
|
TypeId
|
|
TutorialApp::GetTypeId()
|
|
{
|
|
static TypeId tid = TypeId("TutorialApp")
|
|
.SetParent<Application>()
|
|
.SetGroupName("Tutorial")
|
|
.AddConstructor<TutorialApp>();
|
|
return tid;
|
|
}
|
|
|
|
void
|
|
TutorialApp::Setup(Ptr<Socket> socket,
|
|
Address address,
|
|
uint32_t packetSize,
|
|
uint32_t nPackets,
|
|
DataRate dataRate)
|
|
{
|
|
m_socket = socket;
|
|
m_peer = address;
|
|
m_packetSize = packetSize;
|
|
m_nPackets = nPackets;
|
|
m_dataRate = dataRate;
|
|
}
|
|
|
|
void
|
|
TutorialApp::StartApplication()
|
|
{
|
|
m_running = true;
|
|
m_packetsSent = 0;
|
|
m_socket->Bind();
|
|
m_socket->Connect(m_peer);
|
|
SendPacket();
|
|
}
|
|
|
|
void
|
|
TutorialApp::StopApplication()
|
|
{
|
|
m_running = false;
|
|
|
|
if (m_sendEvent.IsRunning())
|
|
{
|
|
Simulator::Cancel(m_sendEvent);
|
|
}
|
|
|
|
if (m_socket)
|
|
{
|
|
m_socket->Close();
|
|
}
|
|
}
|
|
|
|
void
|
|
TutorialApp::SendPacket()
|
|
{
|
|
Ptr<Packet> packet = Create<Packet>(m_packetSize);
|
|
m_socket->Send(packet);
|
|
|
|
if (++m_packetsSent < m_nPackets)
|
|
{
|
|
ScheduleTx();
|
|
}
|
|
}
|
|
|
|
void
|
|
TutorialApp::ScheduleTx()
|
|
{
|
|
if (m_running)
|
|
{
|
|
Time tNext(Seconds(m_packetSize * 8 / static_cast<double>(m_dataRate.GetBitRate())));
|
|
m_sendEvent = Simulator::Schedule(tNext, &TutorialApp::SendPacket, this);
|
|
}
|
|
}
|