Index: src/applications/wimax/wimax1ipp-application.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/applications/wimax/wimax1ipp-application.cc |
@@ -0,0 +1,241 @@ |
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
+// |
+// Copyright (c) 2009 Universita' di Firenze |
+// |
+// 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 |
+// |
+// Author: Tommaso Pecorella <tommaso.pecorella@unifi.it> |
+// |
+ |
+#include "ns3/log.h" |
+#include "ns3/address.h" |
+#include "ns3/node.h" |
+#include "ns3/nstime.h" |
+#include "ns3/data-rate.h" |
+#include "ns3/random-variable.h" |
+#include "ns3/socket.h" |
+#include "ns3/simulator.h" |
+#include "ns3/socket-factory.h" |
+#include "ns3/packet.h" |
+#include "ns3/uinteger.h" |
+#include "ns3/trace-source-accessor.h" |
+#include "wimax1ipp-application.h" |
+#include "ns3/udp-socket-factory.h" |
+ |
+NS_LOG_COMPONENT_DEFINE ("Wimax1ippApplication"); |
+ |
+using namespace std; |
+ |
+namespace ns3 { |
+ |
+NS_OBJECT_ENSURE_REGISTERED (Wimax1ippApplication); |
+ |
+TypeId |
+Wimax1ippApplication::GetTypeId (void) |
+{ |
+ static TypeId tid = TypeId ("ns3::Wimax1ippApplication") |
+ .SetParent<Application> () |
+ .AddConstructor<Wimax1ippApplication> () |
+ .AddAttribute ("DataRate", "The data rate in on state.", |
+ DataRateValue (DataRate ("15kb/s")), |
+ MakeDataRateAccessor (&Wimax1ippApplication::m_rate), |
+ MakeDataRateChecker ()) |
+ .AddAttribute ("Remote", "The address of the destination", |
+ AddressValue (), |
+ MakeAddressAccessor (&Wimax1ippApplication::m_peer), |
+ MakeAddressChecker ()) |
+ .AddAttribute ("MaxBytes", |
+ "The total number of bytes to send. Once these bytes are sent, " |
+ "no packet is sent again, even in on state. The value zero means " |
+ "that there is no limit.", |
+ UintegerValue (0), |
+ MakeUintegerAccessor (&Wimax1ippApplication::m_maxBytes), |
+ MakeUintegerChecker<uint32_t> ()) |
+ .AddAttribute ("Protocol", "The type of protocol to use.", |
+ TypeIdValue (UdpSocketFactory::GetTypeId ()), |
+ MakeTypeIdAccessor (&Wimax1ippApplication::m_tid), |
+ MakeTypeIdChecker ()) |
+ .AddTraceSource ("Tx", "A new packet is created and is sent", |
+ MakeTraceSourceAccessor (&Wimax1ippApplication::m_txTrace)) |
+ ; |
+ return tid; |
+} |
+ |
+ |
+Wimax1ippApplication::Wimax1ippApplication () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ m_socket = 0; |
+ m_connected = false; |
+ m_totBytes = 0; |
+ m_pktSize = 192; |
+ m_basicOnRate = 1.445E-02; |
+ m_basicOffRate = 1.084E-02; |
+ m_basicPktRate = 1.698; |
+} |
+ |
+Wimax1ippApplication::~Wimax1ippApplication () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+} |
+ |
+void |
+Wimax1ippApplication::SetMaxBytes (uint32_t maxBytes) |
+{ |
+ NS_LOG_FUNCTION (this << maxBytes); |
+ m_maxBytes = maxBytes; |
+} |
+ |
+ |
+void |
+Wimax1ippApplication::DoDispose (void) |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ m_socket = 0; |
+ // chain up |
+ Application::DoDispose (); |
+} |
+ |
+// Application Methods |
+void Wimax1ippApplication::StartApplication () // Called at time specified by Start |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ // Create the socket if not already |
+ if (!m_socket) |
+ { |
+ m_socket = Socket::CreateSocket (GetNode (), m_tid); |
+ m_socket->Bind (); |
+ m_socket->Connect (m_peer); |
+ } |
+ // Insure no pending event and start them |
+ CancelEvents (); |
+ double scaleFactor = (0.7278 * m_pktSize * 8.0 ) / static_cast<double> (m_rate.GetBitRate ()); |
+ m_onTime = ExponentialVariable (scaleFactor / m_basicOnRate); |
+ m_offTime = ExponentialVariable (scaleFactor / m_basicOffRate); |
+ m_pktTime = ExponentialVariable (scaleFactor / m_basicPktRate); |
+ ScheduleStartEvent (); |
+ // std::cout << "m_onTime: " << scaleFactor/m_basicOnRate << " or " << m_basicOnRate/scaleFactor << std::endl; |
+ // std::cout << "m_offTime: " << scaleFactor/m_basicOffRate << " or " << m_basicOffRate/scaleFactor << std::endl; |
+ // std::cout << "m_pktTime: " << scaleFactor/m_basicPktRate << " or " << m_basicPktRate/scaleFactor << std::endl; |
+ // std::cout << "m_basicPktRate: " << m_basicPktRate << std::endl; |
+} |
+ |
+void Wimax1ippApplication::StopApplication () // Called at time specified by Stop |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ CancelEvents (); |
+ if (m_socket != 0) |
+ { |
+ m_socket->Close (); |
+ } |
+ else |
+ { |
+ NS_LOG_WARN ("Wimax1ippApplication found null socket to close in StopApplication"); |
+ } |
+} |
+ |
+void Wimax1ippApplication::CancelEvents () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ // Cancel the pending send packet event |
+ Simulator::Cancel (m_sendEvent); |
+ Simulator::Cancel (m_startStopEvent); |
+} |
+ |
+// Event handlers |
+void Wimax1ippApplication::StartSending () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ SendPacket (); // Send a packet and schedule the send packet event |
+ ScheduleStopEvent (); |
+} |
+ |
+void Wimax1ippApplication::StopSending () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ CancelEvents (); |
+ |
+ ScheduleStartEvent (); |
+} |
+ |
+// Private helpers |
+void Wimax1ippApplication::ScheduleNextTx () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ if (m_maxBytes == 0 || m_totBytes < m_maxBytes) |
+ { |
+ uint32_t bits = m_pktSize * 8; |
+ NS_LOG_LOGIC ("bits = " << bits); |
+ Time nextTime = Seconds (m_pktTime.GetValue ()); // Time till next packet |
+ NS_LOG_LOGIC ("nextTime = " << nextTime); |
+ m_sendEvent = Simulator::Schedule (nextTime, |
+ &Wimax1ippApplication::SendPacket, this); |
+ } |
+ else |
+ { // All done, cancel any pending events |
+ StopApplication (); |
+ } |
+} |
+ |
+void Wimax1ippApplication::ScheduleStartEvent () |
+{ // Schedules the event to start sending data (switch to the "On" state) |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ Time offInterval = Seconds (m_offTime.GetValue ()); |
+ NS_LOG_LOGIC ("start at " << offInterval); |
+ m_startStopEvent = Simulator::Schedule (offInterval, &Wimax1ippApplication::StartSending, this); |
+} |
+ |
+void Wimax1ippApplication::ScheduleStopEvent () |
+{ // Schedules the event to stop sending data (switch to "Off" state) |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ Time onInterval = Seconds (m_onTime.GetValue ()); |
+ NS_LOG_LOGIC ("stop at " << onInterval); |
+ m_startStopEvent = Simulator::Schedule (onInterval, &Wimax1ippApplication::StopSending, this); |
+} |
+ |
+ |
+void Wimax1ippApplication::SendPacket () |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ NS_LOG_LOGIC ("sending packet at " << Simulator::Now ()); |
+ NS_ASSERT (m_sendEvent.IsExpired ()); |
+ Ptr<Packet> packet = Create<Packet> (m_pktSize); |
+ m_txTrace (packet); |
+ m_socket->Send (packet); |
+ m_totBytes += m_pktSize; |
+ ScheduleNextTx (); |
+} |
+ |
+void Wimax1ippApplication::ConnectionSucceeded (Ptr<Socket>) |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ |
+ m_connected = true; |
+ ScheduleStartEvent (); |
+} |
+ |
+void Wimax1ippApplication::ConnectionFailed (Ptr<Socket>) |
+{ |
+ NS_LOG_FUNCTION_NOARGS (); |
+ cout << "Wimax1ippApplication, Connection Failed" << endl; |
+} |
+ |
+} // Namespace ns3 |