120 lines
2.7 KiB
C++
120 lines
2.7 KiB
C++
|
|
#include "myCustomApp.h"
|
||
|
|
|
||
|
|
#include "ns3/log.h"
|
||
|
|
#include "ns3/simulator.h"
|
||
|
|
#include "ns3/socket-factory.h"
|
||
|
|
#include "ns3/socket.h"
|
||
|
|
#include "ns3/uinteger.h"
|
||
|
|
#include "ns3/names.h"
|
||
|
|
#include "ns3/point-to-point-net-device.h"
|
||
|
|
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstdlib>
|
||
|
|
|
||
|
|
namespace ns3
|
||
|
|
{
|
||
|
|
|
||
|
|
NS_LOG_COMPONENT_DEFINE("myCustomApp");
|
||
|
|
|
||
|
|
NS_OBJECT_ENSURE_REGISTERED(myCustomApp);
|
||
|
|
|
||
|
|
TypeId
|
||
|
|
myCustomApp::GetTypeId()
|
||
|
|
{
|
||
|
|
static TypeId tid =
|
||
|
|
TypeId("ns3::myCustomApp")
|
||
|
|
.SetParent<Application>()
|
||
|
|
.SetGroupName("Applications")
|
||
|
|
.AddConstructor<myCustomApp>();
|
||
|
|
return tid;
|
||
|
|
}
|
||
|
|
|
||
|
|
myCustomApp::myCustomApp()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
m_sent = 0;
|
||
|
|
m_totalTx = 0;
|
||
|
|
m_totalRx = 0;
|
||
|
|
m_sendEvent = EventId();
|
||
|
|
}
|
||
|
|
|
||
|
|
myCustomApp::~myCustomApp()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
myCustomApp::Setup(Ptr<PointToPointNetDevice> net, int t)
|
||
|
|
{
|
||
|
|
m_net = net;
|
||
|
|
Packet::EnablePrinting();
|
||
|
|
m_sendEvent = Simulator::Schedule(Seconds(t), &myCustomApp::Send, this);
|
||
|
|
m_net->SetReceiveCallback(MakeCallback(&myCustomApp::RxPacket, this));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool
|
||
|
|
myCustomApp::RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender)
|
||
|
|
{
|
||
|
|
m_totalRx = m_totalRx + pkt->GetSize();
|
||
|
|
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << Names::FindName(m_net->GetNode()) << "/" << Names::FindName(m_net) <<" : Pkt rcvd ! " << pkt->ToString());
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
myCustomApp::DoDispose()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
Application::DoDispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
myCustomApp::StartApplication()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
myCustomApp::StopApplication()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
Simulator::Cancel(m_sendEvent);
|
||
|
|
NS_LOG_INFO("\tNb bytes rcvd :" << GetTotalRx());
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
myCustomApp::Send()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
NS_ASSERT(m_sendEvent.IsExpired());
|
||
|
|
|
||
|
|
|
||
|
|
uint8_t txBuffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
|
||
|
|
"true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - "
|
||
|
|
"for her merchandise, he traded in his prize.";
|
||
|
|
size_t txBufferSize = sizeof(txBuffer);
|
||
|
|
Ptr<Packet> p = Create<Packet>(txBuffer, txBufferSize);
|
||
|
|
|
||
|
|
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << Names::FindName(m_net->GetNode()) << "/" << Names::FindName(m_net) <<" : Pkt sent ! " << p->ToString());
|
||
|
|
|
||
|
|
m_net->Send(p, m_net->GetBroadcast(), 0x800);
|
||
|
|
|
||
|
|
m_sendEvent = Simulator::Schedule(Seconds(2), &myCustomApp::Send, this);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
uint64_t
|
||
|
|
myCustomApp::GetTotalTx() const
|
||
|
|
{
|
||
|
|
return m_totalTx;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint64_t
|
||
|
|
myCustomApp::GetTotalRx() const
|
||
|
|
{
|
||
|
|
return m_totalRx;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // Namespace ns3
|