Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011-2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | // dumb-requester.cc |
| 22 | |
| 23 | #include "dumb-requester.h" |
| 24 | #include "ns3/ptr.h" |
| 25 | #include "ns3/log.h" |
| 26 | #include "ns3/simulator.h" |
| 27 | #include "ns3/packet.h" |
| 28 | #include "ns3/random-variable.h" |
| 29 | #include "ns3/string.h" |
| 30 | |
| 31 | #include "ns3/ndn-app-face.h" |
| 32 | #include "ns3/ndn-interest.h" |
| 33 | #include "ns3/ndn-content-object.h" |
| 34 | |
| 35 | NS_LOG_COMPONENT_DEFINE ("DumbRequester"); |
| 36 | |
| 37 | namespace ns3 { |
| 38 | |
| 39 | NS_OBJECT_ENSURE_REGISTERED (DumbRequester); |
| 40 | |
| 41 | // register NS-3 type |
| 42 | TypeId |
| 43 | DumbRequester::GetTypeId () |
| 44 | { |
| 45 | static TypeId tid = TypeId ("DumbRequester") |
| 46 | .SetParent<ndn::App> () |
| 47 | .AddConstructor<DumbRequester> () |
| 48 | |
| 49 | .AddAttribute ("Prefix", "Requested name", |
| 50 | StringValue ("/dumb-interest"), |
| 51 | ndn::MakeNameComponentsAccessor (&DumbRequester::m_name), |
| 52 | ndn::MakeNameComponentsChecker ()) |
| 53 | ; |
| 54 | return tid; |
| 55 | } |
| 56 | |
| 57 | DumbRequester::DumbRequester () |
| 58 | : m_isRunning (false) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | // Processing upon start of the application |
| 63 | void |
| 64 | DumbRequester::StartApplication () |
| 65 | { |
| 66 | // initialize ndn::App |
| 67 | ndn::App::StartApplication (); |
| 68 | |
| 69 | m_isRunning = true; |
| 70 | Simulator::ScheduleNow (&DumbRequester::SendInterest, this); |
| 71 | } |
| 72 | |
| 73 | // Processing when application is stopped |
| 74 | void |
| 75 | DumbRequester::StopApplication () |
| 76 | { |
| 77 | m_isRunning = false; |
| 78 | // cleanup ndn::App |
| 79 | ndn::App::StopApplication (); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | DumbRequester::SendInterest () |
| 84 | { |
| 85 | if (!m_isRunning) return; |
| 86 | |
| 87 | ///////////////////////////////////// |
| 88 | // Sending one Interest packet out // |
| 89 | ///////////////////////////////////// |
| 90 | |
| 91 | Ptr<ndn::NameComponents> prefix = Create<ndn::NameComponents> (m_name); // another way to create name |
| 92 | |
| 93 | // Create and configure ndn::InterestHeader |
| 94 | ndn::InterestHeader interestHeader; |
| 95 | UniformVariable rand (0,std::numeric_limits<uint32_t>::max ()); |
| 96 | interestHeader.SetNonce (rand.GetValue ()); |
| 97 | interestHeader.SetName (prefix); |
| 98 | interestHeader.SetInterestLifetime (Seconds (1.0)); |
| 99 | |
| 100 | // Create packet and add ndn::InterestHeader |
| 101 | Ptr<Packet> packet = Create<Packet> (); |
| 102 | packet->AddHeader (interestHeader); |
| 103 | |
| 104 | NS_LOG_DEBUG ("Sending Interest packet for " << *prefix); |
| 105 | |
| 106 | // Forward packet to lower (network) layer |
| 107 | m_protocolHandler (packet); |
| 108 | |
| 109 | // Call trace (for logging purposes) |
| 110 | m_transmittedInterests (&interestHeader, this, m_face); |
| 111 | Simulator::Schedule (Seconds (1.0), &DumbRequester::SendInterest, this); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | DumbRequester::OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject, |
| 116 | Ptr<Packet> payload) |
| 117 | { |
| 118 | NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ()); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | } // namespace ns3 |