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); |
| 82 | interest->setMustBeFresh(true); |
| 83 | interest->setChildSelector(1); |
| 84 | interest->setInterestLifetime(getInterestLifetime()); |
| 85 | |
| 86 | m_lastInterestId = m_face.expressInterest(*interest, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 87 | [this] (const auto&, const auto& d) { this->afterReceiveData(d); }, |
| 88 | [this] (const auto&, const auto& n) { this->afterReceiveNack(n); }, |
| 89 | [this] (const auto&) { this->afterTimeout(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
| 93 | NotificationSubscriberBase::sendNextInterest() |
| 94 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 95 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 96 | return; |
| 97 | |
| 98 | BOOST_ASSERT(m_lastSequenceNo != std::numeric_limits<uint64_t>::max()); // overflow or missing initial reply |
| 99 | |
| 100 | Name nextName = m_prefix; |
| 101 | nextName.appendSequenceNumber(m_lastSequenceNo + 1); |
| 102 | |
| 103 | auto interest = make_shared<Interest>(nextName); |
| 104 | interest->setInterestLifetime(getInterestLifetime()); |
| 105 | |
| 106 | m_lastInterestId = m_face.expressInterest(*interest, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 107 | [this] (const auto&, const auto& d) { this->afterReceiveData(d); }, |
| 108 | [this] (const auto&, const auto& n) { this->afterReceiveNack(n); }, |
| 109 | [this] (const auto&) { this->afterTimeout(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool |
| 113 | NotificationSubscriberBase::shouldStop() |
| 114 | { |
| 115 | if (!m_isRunning) |
| 116 | return true; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 117 | |
| 118 | if (!hasSubscriber() && onNack.isEmpty()) { |
| 119 | stop(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | NotificationSubscriberBase::afterReceiveData(const Data& data) |
| 127 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 128 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 129 | return; |
| 130 | |
| 131 | try { |
| 132 | m_lastSequenceNo = data.getName().get(-1).toSequenceNumber(); |
| 133 | } |
| 134 | catch (const tlv::Error&) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 135 | onDecodeError(data); |
| 136 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 140 | if (!decodeAndDeliver(data)) { |
| 141 | onDecodeError(data); |
| 142 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 146 | sendNextInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | NotificationSubscriberBase::afterReceiveNack(const lp::Nack& nack) |
| 151 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 152 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 155 | onNack(nack); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 156 | |
| 157 | time::milliseconds delay = exponentialBackoff(nack); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 158 | m_nackEvent = m_scheduler.scheduleEvent(delay, [this] { sendInitialInterest(); }); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
| 162 | NotificationSubscriberBase::afterTimeout() |
| 163 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 164 | if (shouldStop()) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 165 | return; |
| 166 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 167 | onTimeout(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 168 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 169 | sendInitialInterest(); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | time::milliseconds |
| 173 | NotificationSubscriberBase::exponentialBackoff(lp::Nack nack) |
| 174 | { |
| 175 | uint64_t nackSequenceNo; |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 176 | try { |
| 177 | nackSequenceNo = nack.getInterest().getName().get(-1).toSequenceNumber(); |
| 178 | } |
| 179 | catch (const tlv::Error&) { |
| 180 | nackSequenceNo = 0; |
| 181 | } |
| 182 | |
| 183 | if (m_lastNackSequenceNo == nackSequenceNo) { |
| 184 | ++m_attempts; |
| 185 | } |
| 186 | else { |
| 187 | m_attempts = 1; |
| 188 | } |
| 189 | |
| 190 | m_lastNackSequenceNo = nackSequenceNo; |
| 191 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 192 | return time::milliseconds(static_cast<time::milliseconds::rep>(std::pow(2, m_attempts) * 100 + |
| 193 | random::generateWord32() % 100)); |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | } // namespace util |
| 197 | } // namespace ndn |