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