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