Files
unison/examples/stats/wifi-example-apps.cc

256 lines
7.2 KiB
C++
Raw Permalink Normal View History

2008-08-29 13:22:09 -04:00
/*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
2008-08-29 13:22:09 -04:00
*
* Authors: Joe Kopena <tjkopena@cs.drexel.edu>
*
* These applications are used in the WiFi Distance Test experiment,
* described and implemented in test02.cc. That file should be in the
* same place as this file. The applications have two very simple
* jobs, they just generate and receive packets. We could use the
* standard Application classes included in the NS-3 distribution.
* These have been written just to change the behavior a little, and
* provide more examples.
*
*/
2022-10-07 20:08:35 +00:00
#include "wifi-example-apps.h"
2008-08-29 13:22:09 -04:00
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
2022-10-07 20:08:35 +00:00
#include "ns3/network-module.h"
2008-08-29 13:22:09 -04:00
#include "ns3/stats-module.h"
2022-10-07 20:08:35 +00:00
#include <ostream>
2008-08-29 13:22:09 -04:00
using namespace ns3;
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("WiFiDistanceApps");
2008-08-29 13:22:09 -04:00
// ==============================================
// SENDER
// ==============================================
2008-08-29 13:22:09 -04:00
TypeId
2022-10-07 20:08:35 +00:00
Sender::GetTypeId()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
static TypeId tid = TypeId("Sender")
.SetParent<Application>()
.AddConstructor<Sender>()
.AddAttribute("PacketSize",
"The size of packets transmitted.",
UintegerValue(64),
MakeUintegerAccessor(&Sender::m_packetSize),
2022-10-07 20:08:35 +00:00
MakeUintegerChecker<uint32_t>(1))
.AddAttribute("Destination",
"Target host address.",
Ipv4AddressValue("255.255.255.255"),
MakeIpv4AddressAccessor(&Sender::m_destAddr),
MakeIpv4AddressChecker())
.AddAttribute("Port",
"Destination app port.",
UintegerValue(1603),
MakeUintegerAccessor(&Sender::m_destPort),
MakeUintegerChecker<uint32_t>())
.AddAttribute("NumPackets",
"Total number of packets to send.",
UintegerValue(30),
MakeUintegerAccessor(&Sender::m_nPackets),
2022-10-07 20:08:35 +00:00
MakeUintegerChecker<uint32_t>(1))
.AddAttribute("Interval",
"Delay between transmissions.",
StringValue("ns3::ConstantRandomVariable[Constant=0.5]"),
MakePointerAccessor(&Sender::m_interval),
MakePointerChecker<RandomVariableStream>())
.AddTraceSource("Tx",
"A new packet is created and is sent",
MakeTraceSourceAccessor(&Sender::m_txTrace),
"ns3::Packet::TracedCallback");
return tid;
2008-08-29 13:22:09 -04:00
}
Sender::Sender()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
m_interval = CreateObject<ConstantRandomVariable>();
2008-08-29 13:22:09 -04:00
}
Sender::~Sender()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Sender::DoDispose()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
m_socket = nullptr;
// chain up
Application::DoDispose();
2008-08-29 13:22:09 -04:00
}
2022-10-07 20:08:35 +00:00
void
Sender::StartApplication()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
if (!m_socket)
{
Ptr<SocketFactory> socketFactory =
GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
m_socket = socketFactory->CreateSocket();
m_socket->Bind();
2011-05-13 15:35:31 -04:00
}
2009-06-02 19:45:03 +02:00
2022-10-07 20:08:35 +00:00
m_count = 0;
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
Simulator::Cancel(m_sendEvent);
m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this);
2008-08-29 13:22:09 -04:00
}
2022-10-07 20:08:35 +00:00
void
Sender::StopApplication()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
Simulator::Cancel(m_sendEvent);
2008-08-29 13:22:09 -04:00
}
2011-05-13 15:35:31 -04:00
2022-10-07 20:08:35 +00:00
void
Sender::SendPacket()
2008-08-29 13:22:09 -04:00
{
// NS_LOG_FUNCTION_NOARGS();
2022-10-07 20:08:35 +00:00
NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " << m_destAddr);
2008-08-29 13:22:09 -04:00
Ptr<Packet> packet = Create<Packet>(m_packetSize);
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
TimestampTag timestamp;
timestamp.SetTimestamp(Simulator::Now());
packet->AddByteTag(timestamp);
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
// Could connect the socket since the address never changes; using SendTo
// here simply because all of the standard apps do not.
m_socket->SendTo(packet, 0, InetSocketAddress(m_destAddr, m_destPort));
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
// Report the event to the trace.
m_txTrace(packet);
2008-08-29 13:22:09 -04:00
if (++m_count < m_nPackets)
2022-10-07 20:08:35 +00:00
{
m_sendEvent =
Simulator::Schedule(Seconds(m_interval->GetValue()), &Sender::SendPacket, this);
2011-05-13 15:35:31 -04:00
}
2008-08-29 13:22:09 -04:00
}
// ==============================================
// RECEIVER
// ==============================================
2008-08-29 13:22:09 -04:00
TypeId
2022-10-07 20:08:35 +00:00
Receiver::GetTypeId()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
static TypeId tid = TypeId("Receiver")
.SetParent<Application>()
.AddConstructor<Receiver>()
.AddAttribute("Port",
"Listening port.",
UintegerValue(1603),
MakeUintegerAccessor(&Receiver::m_port),
MakeUintegerChecker<uint32_t>());
return tid;
2008-08-29 13:22:09 -04:00
}
2022-10-07 20:08:35 +00:00
Receiver::Receiver()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
}
Receiver::~Receiver()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Receiver::DoDispose()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
m_socket = nullptr;
// chain up
Application::DoDispose();
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Receiver::StartApplication()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
if (!m_socket)
{
Ptr<SocketFactory> socketFactory =
GetNode()->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
m_socket = socketFactory->CreateSocket();
InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), m_port);
m_socket->Bind(local);
2011-05-13 15:35:31 -04:00
}
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
m_socket->SetRecvCallback(MakeCallback(&Receiver::Receive, this));
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Receiver::StopApplication()
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
2008-08-29 13:22:09 -04:00
2022-10-07 20:08:35 +00:00
if (m_socket)
{
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
2011-05-13 15:35:31 -04:00
}
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Receiver::SetCounter(Ptr<CounterCalculator<>> calc)
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
m_calc = calc;
2008-08-29 13:22:09 -04:00
}
2022-10-07 20:08:35 +00:00
2008-08-29 13:22:09 -04:00
void
2022-10-07 20:08:35 +00:00
Receiver::SetDelayTracker(Ptr<TimeMinMaxAvgTotalCalculator> delay)
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
m_delay = delay;
2008-08-29 13:22:09 -04:00
}
void
2022-10-07 20:08:35 +00:00
Receiver::Receive(Ptr<Socket> socket)
2008-08-29 13:22:09 -04:00
{
2022-10-07 20:08:35 +00:00
// NS_LOG_FUNCTION (this << socket << packet << from);
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom(from)))
{
if (InetSocketAddress::IsMatchingType(from))
{
NS_LOG_INFO("Received " << packet->GetSize() << " bytes from "
<< InetSocketAddress::ConvertFrom(from).GetIpv4());
2011-05-13 15:35:31 -04:00
}
2022-10-07 20:08:35 +00:00
TimestampTag timestamp;
// Should never not be found since the sender is adding it, but
// you never know.
if (packet->FindFirstMatchingByteTag(timestamp))
{
Time tx = timestamp.GetTimestamp();
2011-05-13 15:35:31 -04:00
2022-10-07 20:08:35 +00:00
if (m_delay)
{
m_delay->Update(Simulator::Now() - tx);
2011-05-13 15:35:31 -04:00
}
}
2022-10-07 20:08:35 +00:00
if (m_calc)
{
m_calc->Update();
2011-05-13 15:35:31 -04:00
}
2008-08-29 13:22:09 -04:00
}
}