2008-08-29 13:22:09 -04: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
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
|
|
#include "ns3/core-module.h"
|
2011-02-21 09:11:37 -08:00
|
|
|
#include "ns3/network-module.h"
|
2011-02-25 10:32:35 -08:00
|
|
|
#include "ns3/internet-module.h"
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
#include "ns3/stats-module.h"
|
|
|
|
|
|
|
|
|
|
#include "wifi-example-apps.h"
|
|
|
|
|
|
|
|
|
|
using namespace ns3;
|
|
|
|
|
|
2009-06-02 19:45:03 +02:00
|
|
|
NS_LOG_COMPONENT_DEFINE ("WiFiDistanceApps");
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
TypeId
|
2011-05-22 23:18:47 -07:00
|
|
|
Sender::GetTypeId (void)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
static TypeId tid = TypeId ("Sender")
|
|
|
|
|
.SetParent<Application> ()
|
|
|
|
|
.AddConstructor<Sender> ()
|
2009-06-02 19:45:03 +02:00
|
|
|
.AddAttribute ("PacketSize", "The size of packets transmitted.",
|
2011-05-22 23:18:47 -07:00
|
|
|
UintegerValue (64),
|
|
|
|
|
MakeUintegerAccessor (&Sender::m_pktSize),
|
|
|
|
|
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_numPkts),
|
2008-08-29 13:22:09 -04:00
|
|
|
MakeUintegerChecker<uint32_t>(1))
|
2009-06-02 19:45:03 +02:00
|
|
|
.AddAttribute ("Interval", "Delay between transmissions.",
|
2011-05-22 23:18:47 -07:00
|
|
|
RandomVariableValue (ConstantVariable (0.5)),
|
|
|
|
|
MakeRandomVariableAccessor (&Sender::m_interval),
|
|
|
|
|
MakeRandomVariableChecker ())
|
2008-08-29 13:22:09 -04:00
|
|
|
.AddTraceSource ("Tx", "A new packet is created and is sent",
|
|
|
|
|
MakeTraceSourceAccessor (&Sender::m_txTrace))
|
2011-05-13 15:35:31 -04:00
|
|
|
;
|
2008-08-29 13:22:09 -04:00
|
|
|
return tid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sender::Sender()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
m_socket = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sender::~Sender()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Sender::DoDispose (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
|
|
|
|
|
m_socket = 0;
|
|
|
|
|
// chain up
|
|
|
|
|
Application::DoDispose ();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
void Sender::StartApplication ()
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
|
|
|
|
|
if (m_socket == 0) {
|
2011-05-22 23:18:47 -07:00
|
|
|
Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory>
|
|
|
|
|
(UdpSocketFactory::GetTypeId ());
|
2008-08-29 13:22:09 -04:00
|
|
|
m_socket = socketFactory->CreateSocket ();
|
|
|
|
|
m_socket->Bind ();
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
2009-06-02 19:45:03 +02:00
|
|
|
|
2008-08-29 13:22:09 -04:00
|
|
|
m_count = 0;
|
|
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
Simulator::Cancel (m_sendEvent);
|
|
|
|
|
m_sendEvent = Simulator::ScheduleNow (&Sender::SendPacket, this);
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
// end Sender::StartApplication
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
void Sender::StopApplication ()
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2011-05-22 23:18:47 -07:00
|
|
|
Simulator::Cancel (m_sendEvent);
|
2008-08-29 13:22:09 -04:00
|
|
|
// end Sender::StopApplication
|
|
|
|
|
}
|
2011-05-13 15:35:31 -04:00
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
void Sender::SendPacket ()
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
// NS_LOG_FUNCTION_NOARGS ();
|
2011-05-22 23:18:47 -07: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_pktSize);
|
|
|
|
|
|
|
|
|
|
TimestampTag timestamp;
|
2011-05-22 23:18:47 -07:00
|
|
|
timestamp.SetTimestamp (Simulator::Now ());
|
2009-06-03 08:49:40 +02:00
|
|
|
packet->AddByteTag (timestamp);
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
// Could connect the socket since the address never changes; using SendTo
|
|
|
|
|
// here simply because all of the standard apps do not.
|
2011-05-22 23:18:47 -07:00
|
|
|
m_socket->SendTo (packet, 0, InetSocketAddress (m_destAddr, m_destPort));
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
// Report the event to the trace.
|
2011-05-22 23:18:47 -07:00
|
|
|
m_txTrace (packet);
|
2008-08-29 13:22:09 -04:00
|
|
|
|
2009-06-02 19:45:03 +02:00
|
|
|
if (++m_count < m_numPkts) {
|
2011-05-22 23:18:47 -07:00
|
|
|
m_sendEvent = Simulator::Schedule (Seconds (m_interval.GetValue ()),
|
|
|
|
|
&Sender::SendPacket, this);
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
2009-06-02 19:45:03 +02:00
|
|
|
|
2008-08-29 13:22:09 -04:00
|
|
|
// end Sender::SendPacket
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
//-- Receiver
|
|
|
|
|
//------------------------------------------------------
|
|
|
|
|
TypeId
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::GetTypeId (void)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
static TypeId tid = TypeId ("Receiver")
|
|
|
|
|
.SetParent<Application> ()
|
|
|
|
|
.AddConstructor<Receiver> ()
|
2011-05-22 23:18:47 -07:00
|
|
|
.AddAttribute ("Port", "Listening port.",
|
|
|
|
|
UintegerValue (1603),
|
|
|
|
|
MakeUintegerAccessor (&Receiver::m_port),
|
|
|
|
|
MakeUintegerChecker<uint32_t>())
|
2011-05-13 15:35:31 -04:00
|
|
|
;
|
2008-08-29 13:22:09 -04:00
|
|
|
return tid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Receiver::Receiver() :
|
2011-05-22 23:18:47 -07:00
|
|
|
m_calc (0),
|
|
|
|
|
m_delay (0)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
m_socket = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Receiver::~Receiver()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Receiver::DoDispose (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
|
|
|
|
|
m_socket = 0;
|
|
|
|
|
// chain up
|
|
|
|
|
Application::DoDispose ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::StartApplication ()
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
|
|
|
|
|
if (m_socket == 0) {
|
2011-05-22 23:18:47 -07:00
|
|
|
Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory>
|
|
|
|
|
(UdpSocketFactory::GetTypeId ());
|
|
|
|
|
m_socket = socketFactory->CreateSocket ();
|
2008-08-29 13:22:09 -04:00
|
|
|
InetSocketAddress local =
|
2011-05-22 23:18:47 -07:00
|
|
|
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
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
m_socket->SetRecvCallback (MakeCallback (&Receiver::Receive, this));
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
// end Receiver::StartApplication
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::StopApplication ()
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
|
|
|
|
|
if (m_socket != 0) {
|
2011-05-22 23:18:47 -07:00
|
|
|
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
2008-08-29 13:22:09 -04:00
|
|
|
|
|
|
|
|
// end Receiver::StopApplication
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::SetCounter (Ptr<CounterCalculator<> > calc)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
m_calc = calc;
|
|
|
|
|
// end Receiver::SetCounter
|
|
|
|
|
}
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::SetDelayTracker (Ptr<TimeMinMaxAvgTotalCalculator> delay)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
m_delay = delay;
|
|
|
|
|
// end Receiver::SetDelayTracker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
Receiver::Receive (Ptr<Socket> socket)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
// NS_LOG_FUNCTION (this << socket << packet << from);
|
|
|
|
|
|
|
|
|
|
Ptr<Packet> packet;
|
|
|
|
|
Address from;
|
2012-02-22 13:50:26 +00:00
|
|
|
while ((packet = socket->RecvFrom (from))) {
|
2011-05-13 15:35:31 -04:00
|
|
|
if (InetSocketAddress::IsMatchingType (from)) {
|
2011-05-22 23:18:47 -07:00
|
|
|
NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " <<
|
|
|
|
|
InetSocketAddress::ConvertFrom (from).GetIpv4 ());
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimestampTag timestamp;
|
|
|
|
|
// Should never not be found since the sender is adding it, but
|
|
|
|
|
// you never know.
|
2011-05-22 23:18:47 -07:00
|
|
|
if (packet->FindFirstMatchingByteTag (timestamp)) {
|
|
|
|
|
Time tx = timestamp.GetTimestamp ();
|
2011-05-13 15:35:31 -04:00
|
|
|
|
|
|
|
|
if (m_delay != 0) {
|
2011-05-22 23:18:47 -07:00
|
|
|
m_delay->Update (Simulator::Now () - tx);
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_calc != 0) {
|
2011-05-22 23:18:47 -07:00
|
|
|
m_calc->Update ();
|
2011-05-13 15:35:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// end receiving packets
|
2008-08-29 13:22:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// end Receiver::Receive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
//-- TimestampTag
|
|
|
|
|
//------------------------------------------------------
|
|
|
|
|
TypeId
|
2011-05-22 23:18:47 -07:00
|
|
|
TimestampTag::GetTypeId (void)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
static TypeId tid = TypeId ("TimestampTag")
|
|
|
|
|
.SetParent<Tag> ()
|
|
|
|
|
.AddConstructor<TimestampTag> ()
|
|
|
|
|
.AddAttribute ("Timestamp",
|
|
|
|
|
"Some momentous point in time!",
|
2011-05-22 23:18:47 -07:00
|
|
|
EmptyAttributeValue (),
|
|
|
|
|
MakeTimeAccessor (&TimestampTag::GetTimestamp),
|
|
|
|
|
MakeTimeChecker ())
|
2011-05-13 15:35:31 -04:00
|
|
|
;
|
2008-08-29 13:22:09 -04:00
|
|
|
return tid;
|
|
|
|
|
}
|
|
|
|
|
TypeId
|
2011-05-22 23:18:47 -07:00
|
|
|
TimestampTag::GetInstanceTypeId (void) const
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
return GetTypeId ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
TimestampTag::GetSerializedSize (void) const
|
|
|
|
|
{
|
|
|
|
|
return 8;
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
TimestampTag::Serialize (TagBuffer i) const
|
|
|
|
|
{
|
2011-05-22 23:18:47 -07:00
|
|
|
int64_t t = m_timestamp.GetNanoSeconds ();
|
|
|
|
|
i.Write ((const uint8_t *)&t, 8);
|
2008-08-29 13:22:09 -04:00
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
TimestampTag::Deserialize (TagBuffer i)
|
|
|
|
|
{
|
|
|
|
|
int64_t t;
|
2011-05-22 23:18:47 -07:00
|
|
|
i.Read ((uint8_t *)&t, 8);
|
|
|
|
|
m_timestamp = NanoSeconds (t);
|
2008-08-29 13:22:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
TimestampTag::SetTimestamp (Time time)
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
m_timestamp = time;
|
|
|
|
|
}
|
|
|
|
|
Time
|
2011-05-22 23:18:47 -07:00
|
|
|
TimestampTag::GetTimestamp (void) const
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
return m_timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-22 23:18:47 -07:00
|
|
|
TimestampTag::Print (std::ostream &os) const
|
2008-08-29 13:22:09 -04:00
|
|
|
{
|
|
|
|
|
os << "t=" << m_timestamp;
|
|
|
|
|
}
|