Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018 Regents of the University of California, |
Davide Pesavento | b5f8bcc | 2017-02-05 17:58:05 -0500 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 28 | #include "notification-subscriber.hpp" |
| 29 | #include "random.hpp" |
| 30 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 31 | #include <cmath> |
| 32 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace util { |
| 35 | |
| 36 | NotificationSubscriberBase::NotificationSubscriberBase(Face& face, const Name& prefix, |
| 37 | time::milliseconds interestLifetime) |
| 38 | : m_face(face) |
| 39 | , m_prefix(prefix) |
| 40 | , m_isRunning(false) |
| 41 | , m_lastSequenceNo(std::numeric_limits<uint64_t>::max()) |
| 42 | , m_lastNackSequenceNo(std::numeric_limits<uint64_t>::max()) |
| 43 | , m_attempts(1) |
| 44 | , m_scheduler(face.getIoService()) |
| 45 | , m_nackEvent(m_scheduler) |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 46 | , m_lastInterestId(nullptr) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 47 | , m_interestLifetime(interestLifetime) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | NotificationSubscriberBase::~NotificationSubscriberBase() = default; |
| 52 | |
| 53 | void |
| 54 | NotificationSubscriberBase::start() |
| 55 | { |
| 56 | if (m_isRunning) // already running |
| 57 | return; |
| 58 | m_isRunning = true; |
| 59 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 60 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
| 64 | NotificationSubscriberBase::stop() |
| 65 | { |
| 66 | if (!m_isRunning) // not running |
| 67 | return; |
| 68 | m_isRunning = false; |
| 69 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 70 | if (m_lastInterestId != nullptr) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 71 | m_face.removePendingInterest(m_lastInterestId); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 72 | m_lastInterestId = nullptr; |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | NotificationSubscriberBase::sendInitialInterest() |
| 77 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 78 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 79 | return; |
| 80 | |
| 81 | auto interest = make_shared<Interest>(m_prefix); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 82 | interest->setCanBePrefix(true); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 83 | interest->setMustBeFresh(true); |
| 84 | interest->setChildSelector(1); |
| 85 | interest->setInterestLifetime(getInterestLifetime()); |
| 86 | |
| 87 | m_lastInterestId = m_face.expressInterest(*interest, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 88 | [this] (const auto&, const auto& d) { this->afterReceiveData(d); }, |
| 89 | [this] (const auto&, const auto& n) { this->afterReceiveNack(n); }, |
| 90 | [this] (const auto&) { this->afterTimeout(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
| 94 | NotificationSubscriberBase::sendNextInterest() |
| 95 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 96 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 97 | return; |
| 98 | |
| 99 | BOOST_ASSERT(m_lastSequenceNo != std::numeric_limits<uint64_t>::max()); // overflow or missing initial reply |
| 100 | |
| 101 | Name nextName = m_prefix; |
| 102 | nextName.appendSequenceNumber(m_lastSequenceNo + 1); |
| 103 | |
| 104 | auto interest = make_shared<Interest>(nextName); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 105 | interest->setCanBePrefix(false); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 106 | interest->setInterestLifetime(getInterestLifetime()); |
| 107 | |
| 108 | m_lastInterestId = m_face.expressInterest(*interest, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 109 | [this] (const auto&, const auto& d) { this->afterReceiveData(d); }, |
| 110 | [this] (const auto&, const auto& n) { this->afterReceiveNack(n); }, |
| 111 | [this] (const auto&) { this->afterTimeout(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool |
| 115 | NotificationSubscriberBase::shouldStop() |
| 116 | { |
| 117 | if (!m_isRunning) |
| 118 | return true; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 119 | |
| 120 | if (!hasSubscriber() && onNack.isEmpty()) { |
| 121 | stop(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | NotificationSubscriberBase::afterReceiveData(const Data& data) |
| 129 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 130 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 131 | return; |
| 132 | |
| 133 | try { |
| 134 | m_lastSequenceNo = data.getName().get(-1).toSequenceNumber(); |
| 135 | } |
| 136 | catch (const tlv::Error&) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 137 | onDecodeError(data); |
| 138 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 142 | if (!decodeAndDeliver(data)) { |
| 143 | onDecodeError(data); |
| 144 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 148 | sendNextInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
| 152 | NotificationSubscriberBase::afterReceiveNack(const lp::Nack& nack) |
| 153 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 154 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 157 | onNack(nack); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 158 | |
| 159 | time::milliseconds delay = exponentialBackoff(nack); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 160 | m_nackEvent = m_scheduler.scheduleEvent(delay, [this] { sendInitialInterest(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
| 164 | NotificationSubscriberBase::afterTimeout() |
| 165 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 166 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 167 | return; |
| 168 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 169 | onTimeout(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 170 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 171 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | time::milliseconds |
| 175 | NotificationSubscriberBase::exponentialBackoff(lp::Nack nack) |
| 176 | { |
| 177 | uint64_t nackSequenceNo; |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 178 | try { |
| 179 | nackSequenceNo = nack.getInterest().getName().get(-1).toSequenceNumber(); |
| 180 | } |
| 181 | catch (const tlv::Error&) { |
| 182 | nackSequenceNo = 0; |
| 183 | } |
| 184 | |
| 185 | if (m_lastNackSequenceNo == nackSequenceNo) { |
| 186 | ++m_attempts; |
| 187 | } |
| 188 | else { |
| 189 | m_attempts = 1; |
| 190 | } |
| 191 | |
| 192 | m_lastNackSequenceNo = nackSequenceNo; |
| 193 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 194 | return time::milliseconds(static_cast<time::milliseconds::rep>(std::pow(2, m_attempts) * 100 + |
| 195 | random::generateWord32() % 100)); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // namespace util |
| 199 | } // namespace ndn |