26 #include "ns3/address.h"
27 #include "ns3/inet-socket-address.h"
28 #include "ns3/inet6-socket-address.h"
30 #include "ns3/nstime.h"
31 #include "ns3/data-rate.h"
32 #include "ns3/random-variable-stream.h"
33 #include "ns3/socket.h"
34 #include "ns3/simulator.h"
35 #include "ns3/socket-factory.h"
36 #include "ns3/packet.h"
37 #include "ns3/uinteger.h"
38 #include "ns3/trace-source-accessor.h"
40 #include "ns3/udp-socket-factory.h"
41 #include "ns3/string.h"
42 #include "ns3/pointer.h"
53 OnOffApplication::GetTypeId (
void)
57 .AddConstructor<OnOffApplication> ()
58 .AddAttribute (
"DataRate",
"The data rate in on state.",
60 MakeDataRateAccessor (&OnOffApplication::m_cbrRate),
61 MakeDataRateChecker ())
62 .AddAttribute (
"PacketSize",
"The size of packets sent in on state",
64 MakeUintegerAccessor (&OnOffApplication::m_pktSize),
65 MakeUintegerChecker<uint32_t> (1))
66 .AddAttribute (
"Remote",
"The address of the destination",
68 MakeAddressAccessor (&OnOffApplication::m_peer),
69 MakeAddressChecker ())
70 .AddAttribute (
"OnTime",
"A RandomVariableStream used to pick the duration of the 'On' state.",
71 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
72 MakePointerAccessor (&OnOffApplication::m_onTime),
73 MakePointerChecker <RandomVariableStream>())
74 .AddAttribute (
"OffTime",
"A RandomVariableStream used to pick the duration of the 'Off' state.",
75 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
76 MakePointerAccessor (&OnOffApplication::m_offTime),
77 MakePointerChecker <RandomVariableStream>())
78 .AddAttribute (
"MaxBytes",
79 "The total number of bytes to send. Once these bytes are sent, "
80 "no packet is sent again, even in on state. The value zero means "
81 "that there is no limit.",
83 MakeUintegerAccessor (&OnOffApplication::m_maxBytes),
84 MakeUintegerChecker<uint32_t> ())
85 .AddAttribute (
"Protocol",
"The type of protocol to use.",
87 MakeTypeIdAccessor (&OnOffApplication::m_tid),
89 .AddTraceSource (
"Tx",
"A new packet is created and is sent",
96 OnOffApplication::OnOffApplication ()
106 OnOffApplication::~OnOffApplication()
112 OnOffApplication::SetMaxBytes (uint32_t maxBytes)
115 m_maxBytes = maxBytes;
119 OnOffApplication::GetSocket (
void)
const
126 OnOffApplication::AssignStreams (int64_t stream)
129 m_onTime->SetStream (stream);
130 m_offTime->SetStream (stream + 1);
135 OnOffApplication::DoDispose (
void)
141 Application::DoDispose ();
145 void OnOffApplication::StartApplication ()
152 m_socket = Socket::CreateSocket (GetNode (), m_tid);
154 m_socket->Connect (m_peer);
155 m_socket->SetAllowBroadcast (
true);
156 m_socket->ShutdownRecv ();
163 ScheduleStartEvent ();
166 void OnOffApplication::StopApplication ()
177 NS_LOG_WARN (
"OnOffApplication found null socket to close in StopApplication");
181 void OnOffApplication::CancelEvents ()
185 if (m_sendEvent.IsRunning ())
189 int64x64_t bits = delta.
To (Time::S) * m_cbrRate.GetBitRate ();
190 m_residualBits += bits.GetHigh ();
192 Simulator::Cancel (m_sendEvent);
193 Simulator::Cancel (m_startStopEvent);
197 void OnOffApplication::StartSending ()
202 ScheduleStopEvent ();
205 void OnOffApplication::StopSending ()
210 ScheduleStartEvent ();
214 void OnOffApplication::ScheduleNextTx ()
218 if (m_maxBytes == 0 || m_totBytes < m_maxBytes)
220 uint32_t bits = m_pktSize * 8 - m_residualBits;
223 static_cast<double>(m_cbrRate.GetBitRate ())));
225 m_sendEvent = Simulator::Schedule (nextTime,
234 void OnOffApplication::ScheduleStartEvent ()
238 Time offInterval =
Seconds (m_offTime->GetValue ());
240 m_startStopEvent = Simulator::Schedule (offInterval, &OnOffApplication::StartSending,
this);
243 void OnOffApplication::ScheduleStopEvent ()
249 m_startStopEvent = Simulator::Schedule (onInterval, &OnOffApplication::StopSending,
this);
260 m_socket->Send (packet);
261 m_totBytes += m_pktSize;
262 if (InetSocketAddress::IsMatchingType (m_peer))
265 <<
"s on-off application sent "
266 << packet->GetSize () <<
" bytes to "
267 << InetSocketAddress::ConvertFrom(m_peer).GetIpv4 ()
268 <<
" port " << InetSocketAddress::ConvertFrom (m_peer).GetPort ()
269 <<
" total Tx " << m_totBytes <<
" bytes");
271 else if (Inet6SocketAddress::IsMatchingType (m_peer))
274 <<
"s on-off application sent "
275 << packet->GetSize () <<
" bytes to "
276 << Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6 ()
277 <<
" port " << Inet6SocketAddress::ConvertFrom (m_peer).GetPort ()
278 <<
" total Tx " << m_totBytes <<
" bytes");
290 ScheduleStartEvent ();
296 cout <<
"OnOffApplication, Connection Failed" << endl;