Ilya Moiseenko | 42b4924 | 2011-08-05 20:23:30 -0700 | [diff] [blame] | 1 | // |
| 2 | // ndn_stupidinterestgenerator.cpp |
| 3 | // Abstraction |
| 4 | // |
| 5 | // Created by Ilya Moiseenko on 05.08.11. |
| 6 | // Copyright 2011 UCLA. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #include "ndn_stupidinterestgenerator.h" |
| 10 | #include "ns3/socket.h" |
| 11 | #include "ns3/socket-factory.h" |
| 12 | #include "ns3/simulator.h" |
| 13 | #include "ndn_interestpacket.h" |
| 14 | #include "ndn_namebuilder.h" |
| 15 | |
| 16 | NS_LOG_COMPONENT_DEFINE ("StupidInterestGenerator"); |
| 17 | |
| 18 | namespace ns3 |
| 19 | { |
| 20 | //namespace NDNabstraction |
| 21 | //{ |
| 22 | using namespace NDNabstraction; |
| 23 | |
| 24 | NS_OBJECT_ENSURE_REGISTERED (StupidInterestGenerator); |
| 25 | |
| 26 | TypeId |
| 27 | StupidInterestGenerator::GetTypeId (void) |
| 28 | { |
| 29 | static TypeId tid = TypeId ("ns3::StupidInterestGenerator") |
| 30 | .SetParent<Application> () |
| 31 | .AddConstructor<StupidInterestGenerator> () |
| 32 | .AddAttribute ("Remote", "The address of the destination", |
| 33 | AddressValue (), |
| 34 | MakeAddressAccessor (&StupidInterestGenerator::m_peer), |
| 35 | MakeAddressChecker ()) |
| 36 | .AddAttribute ("OffTime", "Time interval between packets", |
| 37 | TimeValue (Seconds (0.1)), |
| 38 | MakeTimeAccessor (&StupidInterestGenerator::m_offTime), |
| 39 | MakeTimeChecker ()) |
| 40 | .AddAttribute ("Protocol", "The type of protocol to use.", |
| 41 | TypeIdValue (UdpSocketFactory::GetTypeId ()), |
| 42 | MakeTypeIdAccessor (&StupidInterestGenerator::m_tid), |
| 43 | MakeTypeIdChecker ()) |
| 44 | ; |
| 45 | return tid; |
| 46 | } |
| 47 | |
| 48 | StupidInterestGenerator::StupidInterestGenerator () |
| 49 | { |
| 50 | NS_LOG_FUNCTION_NOARGS (); |
| 51 | m_socket = 0; |
| 52 | //m_connected = false; |
| 53 | //m_residualBits = 0; |
| 54 | //m_lastStartTime = Seconds (0); |
| 55 | //m_totBytes = 0; |
| 56 | } |
| 57 | |
| 58 | StupidInterestGenerator::~StupidInterestGenerator() |
| 59 | { |
| 60 | NS_LOG_FUNCTION_NOARGS (); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | StupidInterestGenerator::DoDispose (void) |
| 65 | { |
| 66 | NS_LOG_FUNCTION_NOARGS (); |
| 67 | |
| 68 | m_socket = 0; |
| 69 | // chain up |
| 70 | Application::DoDispose (); |
| 71 | } |
| 72 | |
| 73 | // Application Methods |
| 74 | void StupidInterestGenerator::StartApplication () // Called at time specified by Start |
| 75 | { |
| 76 | NS_LOG_FUNCTION_NOARGS (); |
| 77 | |
| 78 | // Create the socket if not already |
| 79 | if (!m_socket) |
| 80 | { |
| 81 | m_socket = Socket::CreateSocket (GetNode (), m_tid); |
| 82 | m_socket->Bind (); |
| 83 | m_socket->Connect (m_peer); |
| 84 | m_socket->SetAllowBroadcast (true); |
| 85 | m_socket->ShutdownRecv (); |
| 86 | } |
| 87 | // Insure no pending event |
| 88 | CancelEvents (); |
| 89 | // If we are not yet connected, there is nothing to do here |
| 90 | // The ConnectionComplete upcall will start timers at that time |
| 91 | //if (!m_connected) return; |
| 92 | ScheduleStartEvent (); |
| 93 | } |
| 94 | |
| 95 | void StupidInterestGenerator::StopApplication () // Called at time specified by Stop |
| 96 | { |
| 97 | NS_LOG_FUNCTION_NOARGS (); |
| 98 | |
| 99 | CancelEvents (); |
| 100 | if(m_socket != 0) |
| 101 | { |
| 102 | m_socket->Close (); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | NS_LOG_WARN ("OnOffApplication found null socket to close in StopApplication"); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void StupidInterestGenerator::CancelEvents () |
| 111 | { |
| 112 | NS_LOG_FUNCTION_NOARGS (); |
| 113 | |
| 114 | Simulator::Cancel (m_sendEvent); |
| 115 | Simulator::Cancel (m_startStopEvent); |
| 116 | } |
| 117 | |
| 118 | void StupidInterestGenerator::ScheduleStartEvent () |
| 119 | { // Schedules the event to start sending data (switch to the "On" state) |
| 120 | NS_LOG_FUNCTION_NOARGS (); |
| 121 | |
| 122 | Time offInterval = Seconds (m_offTime); |
| 123 | NS_LOG_LOGIC ("start at " << offInterval); |
| 124 | m_startStopEvent = Simulator::Schedule (offInterval, &StupidInterestGenerator::StartSending, this); |
| 125 | } |
| 126 | |
| 127 | // Event handlers |
| 128 | void StupidInterestGenerator::StartSending () |
| 129 | { |
| 130 | NS_LOG_FUNCTION_NOARGS (); |
| 131 | //m_lastStartTime = Simulator::Now (); |
| 132 | ScheduleNextTx (); // Schedule the send packet event |
| 133 | //ScheduleStopEvent (); |
| 134 | } |
| 135 | |
| 136 | void StupidInterestGenerator::StopSending () |
| 137 | { |
| 138 | NS_LOG_FUNCTION_NOARGS (); |
| 139 | CancelEvents (); |
| 140 | |
| 141 | ScheduleStartEvent (); |
| 142 | } |
| 143 | |
| 144 | // Private helpers |
| 145 | void StupidInterestGenerator::ScheduleNextTx () |
| 146 | { |
| 147 | NS_LOG_FUNCTION_NOARGS (); |
| 148 | |
| 149 | |
| 150 | Time nextTime = Seconds(0.); //send now |
| 151 | m_sendEvent = Simulator::Schedule (nextTime, |
| 152 | &StupidInterestGenerator::SendPacket, this); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void StupidInterestGenerator::SendPacket () |
| 157 | { |
| 158 | NS_LOG_FUNCTION_NOARGS (); |
| 159 | NS_LOG_LOGIC ("sending packet at " << Simulator::Now ()); |
| 160 | NS_ASSERT (m_sendEvent.IsExpired ()); |
| 161 | |
| 162 | NameBuilder name; |
| 163 | name.AddComponent("prefix1"); |
| 164 | name.AddComponent("prefix2"); |
| 165 | name.AddComponent("filename"); |
| 166 | ccn_charbuf *output = name.GetName(); |
| 167 | Ptr<InterestPacket> packet = Create<InterestPacket>(output->buf,(uint32_t)output->length); |
| 168 | packet->AddTimeout(4000); |
| 169 | UniformVariable var; |
| 170 | packet->AddNonce(var.GetInteger(1,10000)); |
| 171 | m_socket->Send(packet); |
| 172 | |
| 173 | ScheduleStartEvent(); |
| 174 | } |
| 175 | |
| 176 | //} |
| 177 | } |