Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "retx-suppression-exponential.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | namespace fw { |
| 30 | |
| 31 | const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL = |
| 32 | time::milliseconds(1); |
| 33 | const float RetxSuppressionExponential::DEFAULT_MULTIPLIER = 2.0; |
| 34 | const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_MAX_INTERVAL = |
| 35 | time::milliseconds(250); |
| 36 | |
| 37 | class RetxSuppressionExponential::PitInfo : public StrategyInfo |
| 38 | { |
| 39 | public: |
| 40 | static constexpr int |
| 41 | getTypeId() |
| 42 | { |
| 43 | return 1020; |
| 44 | } |
| 45 | |
| 46 | explicit |
| 47 | PitInfo(const Duration& initialInterval) |
| 48 | : suppressionInterval(initialInterval) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | public: |
| 53 | /** \brief if last transmission occurred within suppressionInterval, |
| 54 | * retransmission will be suppressed |
| 55 | */ |
| 56 | Duration suppressionInterval; |
| 57 | }; |
| 58 | |
| 59 | RetxSuppressionExponential::RetxSuppressionExponential(const Duration& initialInterval, |
| 60 | float multiplier, |
| 61 | const Duration& maxInterval) |
| 62 | : m_initialInterval(initialInterval) |
| 63 | , m_multiplier(multiplier) |
| 64 | , m_maxInterval(maxInterval) |
| 65 | { |
| 66 | BOOST_ASSERT(initialInterval > time::milliseconds::zero()); |
| 67 | BOOST_ASSERT(multiplier >= 1.0); |
| 68 | BOOST_ASSERT(maxInterval >= initialInterval); |
| 69 | } |
| 70 | |
| 71 | RetxSuppression::Result |
| 72 | RetxSuppressionExponential::decide(const Face& inFace, const Interest& interest, |
| 73 | pit::Entry& pitEntry) const |
| 74 | { |
| 75 | bool isNewPitEntry = !pitEntry.hasUnexpiredOutRecords(); |
| 76 | if (isNewPitEntry) { |
| 77 | return NEW; |
| 78 | } |
| 79 | |
| 80 | time::steady_clock::TimePoint lastOutgoing = this->getLastOutgoing(pitEntry); |
| 81 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
| 82 | time::steady_clock::Duration sinceLastOutgoing = now - lastOutgoing; |
| 83 | |
| 84 | shared_ptr<PitInfo> pi = pitEntry.getOrCreateStrategyInfo<PitInfo>(m_initialInterval); |
| 85 | bool shouldSuppress = sinceLastOutgoing < pi->suppressionInterval; |
| 86 | |
| 87 | if (shouldSuppress) { |
| 88 | return SUPPRESS; |
| 89 | } |
| 90 | |
| 91 | pi->suppressionInterval = std::min(m_maxInterval, |
| 92 | time::duration_cast<Duration>(pi->suppressionInterval * m_multiplier)); |
| 93 | return FORWARD; |
| 94 | } |
| 95 | |
| 96 | } // namespace fw |
| 97 | } // namespace nfd |