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