26 #include "ns3/address.h"
27 #include "ns3/inet-socket-address.h"
28 #include "ns3/inet6-socket-address.h"
29 #include "ns3/packet-socket-address.h"
31 #include "ns3/nstime.h"
32 #include "ns3/data-rate.h"
33 #include "ns3/random-variable-stream.h"
34 #include "ns3/socket.h"
35 #include "ns3/simulator.h"
36 #include "ns3/socket-factory.h"
37 #include "ns3/packet.h"
38 #include "ns3/uinteger.h"
39 #include "ns3/trace-source-accessor.h"
40 #include "onoff-application.h"
41 #include "ns3/udp-socket-factory.h"
42 #include "ns3/string.h"
43 #include "ns3/pointer.h"
49 NS_OBJECT_ENSURE_REGISTERED (OnOffApplication);
52 OnOffApplication::GetTypeId (
void)
54 static TypeId tid = TypeId (
"ns3::OnOffApplication")
55 .SetParent<Application> ()
56 .AddConstructor<OnOffApplication> ()
57 .AddAttribute (
"DataRate",
"The data rate in on state.",
58 DataRateValue (DataRate (
"500kb/s")),
59 MakeDataRateAccessor (&OnOffApplication::m_cbrRate),
60 MakeDataRateChecker ())
61 .AddAttribute (
"PacketSize",
"The size of packets sent in on state",
63 MakeUintegerAccessor (&OnOffApplication::m_pktSize),
64 MakeUintegerChecker<uint32_t> (1))
65 .AddAttribute (
"Remote",
"The address of the destination",
67 MakeAddressAccessor (&OnOffApplication::m_peer),
68 MakeAddressChecker ())
69 .AddAttribute (
"OnTime",
"A RandomVariableStream used to pick the duration of the 'On' state.",
70 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
71 MakePointerAccessor (&OnOffApplication::m_onTime),
72 MakePointerChecker <RandomVariableStream>())
73 .AddAttribute (
"OffTime",
"A RandomVariableStream used to pick the duration of the 'Off' state.",
74 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
75 MakePointerAccessor (&OnOffApplication::m_offTime),
76 MakePointerChecker <RandomVariableStream>())
77 .AddAttribute (
"MaxBytes",
78 "The total number of bytes to send. Once these bytes are sent, "
79 "no packet is sent again, even in on state. The value zero means "
80 "that there is no limit.",
82 MakeUintegerAccessor (&OnOffApplication::m_maxBytes),
83 MakeUintegerChecker<uint32_t> ())
84 .AddAttribute (
"Protocol",
"The type of protocol to use.",
85 TypeIdValue (UdpSocketFactory::GetTypeId ()),
86 MakeTypeIdAccessor (&OnOffApplication::m_tid),
88 .AddTraceSource (
"Tx",
"A new packet is created and is sent",
95 OnOffApplication::OnOffApplication ()
105 OnOffApplication::~OnOffApplication()
114 m_maxBytes = maxBytes;
166 MakeCallback (&OnOffApplication::ConnectionSucceeded,
this),
167 MakeCallback (&OnOffApplication::ConnectionFailed,
this));
174 ScheduleStartEvent ();
188 NS_LOG_WARN (
"OnOffApplication found null socket to close in StopApplication");
192 void OnOffApplication::CancelEvents ()
200 int64x64_t bits = delta.To (Time::S) * m_cbrRate.
GetBitRate ();
201 m_residualBits += bits.GetHigh ();
208 void OnOffApplication::StartSending ()
213 ScheduleStopEvent ();
216 void OnOffApplication::StopSending ()
221 ScheduleStartEvent ();
225 void OnOffApplication::ScheduleNextTx ()
229 if (m_maxBytes == 0 || m_totBytes < m_maxBytes)
231 uint32_t bits = m_pktSize * 8 - m_residualBits;
234 static_cast<double>(m_cbrRate.
GetBitRate ())));
237 &OnOffApplication::SendPacket,
this);
245 void OnOffApplication::ScheduleStartEvent ()
251 m_startStopEvent =
Simulator::Schedule (offInterval, &OnOffApplication::StartSending,
this);
254 void OnOffApplication::ScheduleStopEvent ()
264 void OnOffApplication::SendPacket ()
269 Ptr<Packet> packet = Create<Packet> (m_pktSize);
271 m_socket->
Send (packet);
272 m_totBytes += m_pktSize;
276 <<
"s on-off application sent "
277 << packet->GetSize () <<
" bytes to "
280 <<
" total Tx " << m_totBytes <<
" bytes");
285 <<
"s on-off application sent "
286 << packet->GetSize () <<
" bytes to "
289 <<
" total Tx " << m_totBytes <<
" bytes");
297 void OnOffApplication::ConnectionSucceeded (Ptr<Socket> socket)
303 void OnOffApplication::ConnectionFailed (Ptr<Socket> socket)
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
virtual void StartApplication(void)
Application specific startup code.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
#define NS_ASSERT(condition)
static bool IsMatchingType(const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual int ShutdownRecv(void)=0
static void Cancel(const EventId &id)
bool IsRunning(void) const
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
virtual void DoDispose(void)
Ptr< Node > GetNode() const
Ptr< Socket > GetSocket(void) const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
int64_t AssignStreams(int64_t stream)
#define NS_LOG_LOGIC(msg)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static InetSocketAddress ConvertFrom(const Address &address)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
uint64_t GetBitRate() const
void SetMaxBytes(uint32_t maxBytes)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
uint16_t GetPort(void) const
Get the port.
uint16_t GetPort(void) const
void SetConnectCallback(Callback< void, Ptr< Socket > > connectionSucceeded, Callback< void, Ptr< Socket > > connectionFailed)
Specify callbacks to allow the caller to determine if the connection succeeds of fails.
virtual void StopApplication(void)
Application specific shutdown code.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
bool IsExpired(void) const
static bool IsMatchingType(const Address &address)