examples: Clean wifi-example-apps and wifi-example-sim

This commit is contained in:
Eduardo Almeida
2022-11-02 19:35:31 +00:00
parent 095313c9cb
commit fa90164e44
3 changed files with 58 additions and 71 deletions

View File

@@ -37,6 +37,10 @@ using namespace ns3;
NS_LOG_COMPONENT_DEFINE("WiFiDistanceApps");
// ==============================================
// SENDER
// ==============================================
TypeId
Sender::GetTypeId()
{
@@ -46,7 +50,7 @@ Sender::GetTypeId()
.AddAttribute("PacketSize",
"The size of packets transmitted.",
UintegerValue(64),
MakeUintegerAccessor(&Sender::m_pktSize),
MakeUintegerAccessor(&Sender::m_packetSize),
MakeUintegerChecker<uint32_t>(1))
.AddAttribute("Destination",
"Target host address.",
@@ -61,7 +65,7 @@ Sender::GetTypeId()
.AddAttribute("NumPackets",
"Total number of packets to send.",
UintegerValue(30),
MakeUintegerAccessor(&Sender::m_numPkts),
MakeUintegerAccessor(&Sender::m_nPackets),
MakeUintegerChecker<uint32_t>(1))
.AddAttribute("Interval",
"Delay between transmissions.",
@@ -79,7 +83,6 @@ Sender::Sender()
{
NS_LOG_FUNCTION_NOARGS();
m_interval = CreateObject<ConstantRandomVariable>();
m_socket = nullptr;
}
Sender::~Sender()
@@ -114,8 +117,6 @@ Sender::StartApplication()
Simulator::Cancel(m_sendEvent);
m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this);
// end Sender::StartApplication
}
void
@@ -123,7 +124,6 @@ Sender::StopApplication()
{
NS_LOG_FUNCTION_NOARGS();
Simulator::Cancel(m_sendEvent);
// end Sender::StopApplication
}
void
@@ -132,7 +132,7 @@ Sender::SendPacket()
// NS_LOG_FUNCTION_NOARGS();
NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " << m_destAddr);
Ptr<Packet> packet = Create<Packet>(m_pktSize);
Ptr<Packet> packet = Create<Packet>(m_packetSize);
TimestampTag timestamp;
timestamp.SetTimestamp(Simulator::Now());
@@ -145,18 +145,17 @@ Sender::SendPacket()
// Report the event to the trace.
m_txTrace(packet);
if (++m_count < m_numPkts)
if (++m_count < m_nPackets)
{
m_sendEvent =
Simulator::Schedule(Seconds(m_interval->GetValue()), &Sender::SendPacket, this);
}
// end Sender::SendPacket
}
//----------------------------------------------------------------------
//-- Receiver
//------------------------------------------------------
// ==============================================
// RECEIVER
// ==============================================
TypeId
Receiver::GetTypeId()
{
@@ -172,11 +171,8 @@ Receiver::GetTypeId()
}
Receiver::Receiver()
: m_calc(nullptr),
m_delay(nullptr)
{
NS_LOG_FUNCTION_NOARGS();
m_socket = nullptr;
}
Receiver::~Receiver()
@@ -209,8 +205,6 @@ Receiver::StartApplication()
}
m_socket->SetRecvCallback(MakeCallback(&Receiver::Receive, this));
// end Receiver::StartApplication
}
void
@@ -222,22 +216,18 @@ Receiver::StopApplication()
{
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
}
// end Receiver::StopApplication
}
void
Receiver::SetCounter(Ptr<CounterCalculator<>> calc)
{
m_calc = calc;
// end Receiver::SetCounter
}
void
Receiver::SetDelayTracker(Ptr<TimeMinMaxAvgTotalCalculator> delay)
{
m_delay = delay;
// end Receiver::SetDelayTracker
}
void
@@ -272,9 +262,5 @@ Receiver::Receive(Ptr<Socket> socket)
{
m_calc->Update();
}
// end receiving packets
}
// end Receiver::Receive
}

View File

@@ -31,6 +31,10 @@
using namespace ns3;
// ==============================================
// SENDER
// ==============================================
/**
* Sender application.
*/
@@ -42,6 +46,7 @@ class Sender : public Application
* \return The object TypeId.
*/
static TypeId GetTypeId();
Sender();
~Sender() override;
@@ -57,23 +62,24 @@ class Sender : public Application
*/
void SendPacket();
uint32_t m_pktSize; //!< The packet size.
Ipv4Address m_destAddr; //!< Destination address.
uint32_t m_destPort; //!< Destination port.
Ptr<ConstantRandomVariable> m_interval; //!< Rng for sending packets.
uint32_t m_numPkts; //!< Number of packets to send.
Ipv4Address m_destAddr; //!< Destination address
uint32_t m_destPort{0}; //!< Destination port
uint32_t m_packetSize{0}; //!< The packet size
Ptr<ConstantRandomVariable> m_interval; //!< Rng for sending packets
uint32_t m_nPackets{0}; //!< Number of packets to send
uint32_t m_count{0}; //!< Number of packets sent
Ptr<Socket> m_socket; //!< Sending socket.
EventId m_sendEvent; //!< Send packet event.
Ptr<Socket> m_socket; //!< Sending socket
EventId m_sendEvent; //!< Send packet event
/// Tx TracedCallback.
/// Tx TracedCallback
TracedCallback<Ptr<const Packet>> m_txTrace;
uint32_t m_count; //!< Number of packets sent.
// end class Sender
};
// ==============================================
// RECEIVER
// ==============================================
/**
* Receiver application.
*/
@@ -85,6 +91,7 @@ class Receiver : public Application
* \return The object TypeId.
*/
static TypeId GetTypeId();
Receiver();
~Receiver() override;
@@ -113,11 +120,9 @@ class Receiver : public Application
*/
void Receive(Ptr<Socket> socket);
Ptr<Socket> m_socket; //!< Receiving socket.
uint32_t m_port; //!< Listening port.
Ptr<Socket> m_socket; //!< Receiving socket
uint32_t m_port{0}; //!< Listening port
Ptr<CounterCalculator<>> m_calc; //!< Counter of the number of received packets.
Ptr<TimeMinMaxAvgTotalCalculator> m_delay; //!< Delay calculator.
// end class Receiver
Ptr<CounterCalculator<>> m_calc; //!< Counter of the number of received packets
Ptr<TimeMinMaxAvgTotalCalculator> m_delay; //!< Delay calculator
};

View File

@@ -36,7 +36,7 @@
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/stats-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/wifi-module.h"
#include <ctime>
#include <sstream>
@@ -49,20 +49,16 @@ NS_LOG_COMPONENT_DEFINE("WiFiDistanceExperiment");
* Function called when a packet is transmitted.
*
* \param datac The counter of the number of transmitted packets.
* \param path The callback context
* \param packet The transmsiotted packet.
* \param path The callback context.
* \param packet The transmitted packet.
*/
void
TxCallback(Ptr<CounterCalculator<uint32_t>> datac, std::string path, Ptr<const Packet> packet)
{
NS_LOG_INFO("Sent frame counted in " << datac->GetKey());
datac->Update();
// end TxCallback
}
//----------------------------------------------------------------------
//-- main
//----------------------------------------------
int
main(int argc, char* argv[])
{
@@ -109,7 +105,7 @@ main(int argc, char* argv[])
input = sstr.str();
}
//------------------------------------------------------------
//--------------------------------------------
//-- Create nodes and network stacks
//--------------------------------------------
NS_LOG_INFO("Creating nodes.");
@@ -131,7 +127,7 @@ main(int argc, char* argv[])
ipAddrs.SetBase("192.168.0.0", "255.255.255.0");
ipAddrs.Assign(nodeDevices);
//------------------------------------------------------------
//--------------------------------------------
//-- Setup physical layout
//--------------------------------------------
NS_LOG_INFO("Installing static mobility; distance " << distance << " .");
@@ -142,7 +138,7 @@ main(int argc, char* argv[])
mobility.SetPositionAllocator(positionAlloc);
mobility.Install(nodes);
//------------------------------------------------------------
//--------------------------------------------
//-- Create a custom traffic source and sink
//--------------------------------------------
NS_LOG_INFO("Create traffic source & sink.");
@@ -159,7 +155,7 @@ main(int argc, char* argv[])
Config::Set("/NodeList/*/ApplicationList/*/$Sender/Destination",
Ipv4AddressValue("192.168.0.2"));
//------------------------------------------------------------
//--------------------------------------------
//-- Setup stats and data collection
//--------------------------------------------
@@ -212,8 +208,9 @@ main(int argc, char* argv[])
receiver->SetCounter(appRx);
data.AddDataCalculator(appRx);
/**
* Just to show this is here...
// Just to show this is here...
/*
Ptr<MinMaxAvgTotalCalculator<uint32_t>> test =
CreateObject<MinMaxAvgTotalCalculator<uint32_t>>();
test->SetKey("test-dc");
@@ -223,12 +220,12 @@ main(int argc, char* argv[])
test->Update(8);
test->Update(24);
test->Update(12);
**/
*/
// This DataCalculator connects directly to the transmit trace
// provided by our Sender Application. It records some basic
// statistics about the sizes of the packets received (min, max,
// avg, total # bytes), although in this scenaro they're fixed.
// avg, total # bytes), although in this scenario they're fixed.
Ptr<PacketSizeMinMaxAvgTotalCalculator> appTxPkts =
CreateObject<PacketSizeMinMaxAvgTotalCalculator>();
appTxPkts->SetKey("tx-pkt-size");
@@ -247,13 +244,13 @@ main(int argc, char* argv[])
receiver->SetDelayTracker(delayStat);
data.AddDataCalculator(delayStat);
//------------------------------------------------------------
//--------------------------------------------
//-- Run the simulation
//--------------------------------------------
NS_LOG_INFO("Run Simulation.");
Simulator::Run();
//------------------------------------------------------------
//--------------------------------------------
//-- Generate statistics output.
//--------------------------------------------
@@ -286,6 +283,5 @@ main(int argc, char* argv[])
// Free any memory here at the end of this example.
Simulator::Destroy();
// end main
return 0;
}