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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame^] | 23 | #include "dumb-requester.hpp" |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame^] | 31 | #include "ns3/ndn-app-face.hpp" |
| 32 | #include "ns3/ndn-interest.hpp" |
| 33 | #include "ns3/ndn-data.hpp" |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 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"), |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 51 | ndn::MakeNameAccessor (&DumbRequester::m_name), |
| 52 | ndn::MakeNameChecker ()) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 91 | Ptr<ndn::Name> prefix = Create<ndn::Name> (m_name); // another way to create name |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 93 | // Create and configure ndn::Interest |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 94 | Ptr<ndn::Interest> interest = Create<ndn::Interest> (); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 95 | UniformVariable rand (0,std::numeric_limits<uint32_t>::max ()); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 96 | interest->SetNonce (rand.GetValue ()); |
| 97 | interest->SetName (prefix); |
| 98 | interest->SetInterestLifetime (Seconds (1.0)); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 99 | |
| 100 | NS_LOG_DEBUG ("Sending Interest packet for " << *prefix); |
| 101 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 102 | |
| 103 | // Call trace (for logging purposes) |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 104 | m_transmittedInterests (interest, this, m_face); |
| 105 | |
| 106 | // Forward packet to lower (network) layer |
| 107 | m_face->ReceiveInterest (interest); |
| 108 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 109 | Simulator::Schedule (Seconds (1.0), &DumbRequester::SendInterest, this); |
| 110 | } |
| 111 | |
| 112 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 113 | DumbRequester::OnData (Ptr<const ndn::Data> contentObject) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 115 | NS_LOG_DEBUG ("Receiving Data packet for " << contentObject->GetName ()); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
| 119 | } // namespace ns3 |