Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 20 | // dumb-requester.cpp |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 22 | #include "one-interest-requester.hpp" |
| 23 | |
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 | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 31 | NS_LOG_COMPONENT_DEFINE("OneInterestRequester"); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ns3 { |
| 34 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 35 | NS_OBJECT_ENSURE_REGISTERED(OneInterestRequester); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 36 | |
| 37 | // register NS-3 type |
| 38 | TypeId |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 39 | OneInterestRequester::GetTypeId() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 41 | static TypeId tid = |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 42 | TypeId("OneInterestRequester") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 43 | .SetParent<ndn::App>() |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 44 | .AddConstructor<OneInterestRequester>() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 46 | .AddAttribute("Prefix", "Requested name", StringValue("/dumb-interest"), |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 47 | ndn::MakeNameAccessor(&OneInterestRequester::m_name), ndn::MakeNameChecker()); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 48 | return tid; |
| 49 | } |
| 50 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 51 | OneInterestRequester::OneInterestRequester() |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | : m_isRunning(false) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
| 56 | // Processing upon start of the application |
| 57 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 58 | OneInterestRequester::StartApplication() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 59 | { |
| 60 | // initialize ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | ndn::App::StartApplication(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 62 | |
| 63 | m_isRunning = true; |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 64 | Simulator::ScheduleNow(&OneInterestRequester::SendInterest, this); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Processing when application is stopped |
| 68 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 69 | OneInterestRequester::StopApplication() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 70 | { |
| 71 | m_isRunning = false; |
| 72 | // cleanup ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 73 | ndn::App::StopApplication(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 77 | OneInterestRequester::SendInterest() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 78 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | if (!m_isRunning) |
| 80 | return; |
| 81 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 82 | ///////////////////////////////////// |
| 83 | // Sending one Interest packet out // |
| 84 | ///////////////////////////////////// |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 86 | // Create and configure ndn::Interest |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 87 | auto interest = std::make_shared<ndn::Interest>(m_name); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | UniformVariable rand(0, std::numeric_limits<uint32_t>::max()); |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 89 | interest->setNonce(rand.GetValue()); |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 90 | interest->setInterestLifetime(ndn::time::seconds(1)); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 92 | NS_LOG_DEBUG("Sending Interest packet for " << m_name); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 93 | |
| 94 | // Call trace (for logging purposes) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | m_transmittedInterests(interest, this, m_face); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 97 | NS_LOG_DEBUG(">> I: " << m_name); |
| 98 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 99 | // Forward packet to lower (network) layer |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 100 | m_face->onReceiveInterest(*interest); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 104 | OneInterestRequester::OnData(std::shared_ptr<const ndn::Data> data) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 105 | { |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 106 | NS_LOG_DEBUG("<< D: " << data->getName()); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 109 | } // namespace ns3 |