Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_DETAIL_PENDING_INTEREST_HPP |
| 23 | #define NDN_DETAIL_PENDING_INTEREST_HPP |
| 24 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 25 | #include "../face.hpp" |
| 26 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 27 | #include "../data.hpp" |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 28 | #include "../interest.hpp" |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 29 | #include "../lp/nack.hpp" |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 30 | #include "../util/scheduler-scoped-event-id.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 34 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 35 | * @brief Indicates where a pending Interest came from |
| 36 | */ |
| 37 | enum class PendingInterestOrigin |
| 38 | { |
| 39 | APP, ///< Interest was received from this app via Face::expressInterest API |
| 40 | FORWARDER ///< Interest was received from the forwarder via Transport |
| 41 | }; |
| 42 | |
| 43 | std::ostream& |
| 44 | operator<<(std::ostream& os, PendingInterestOrigin origin) |
| 45 | { |
| 46 | switch (origin) { |
| 47 | case PendingInterestOrigin::APP: |
| 48 | return os << "app"; |
| 49 | case PendingInterestOrigin::FORWARDER: |
| 50 | return os << "forwarder"; |
| 51 | } |
| 52 | BOOST_ASSERT(false); |
| 53 | return os; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @brief Stores a pending Interest and associated callbacks |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 59 | class PendingInterest : noncopyable |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 60 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 61 | public: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 62 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 63 | * @brief Construct a pending Interest record for an Interest from Face::expressInterest |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 64 | * |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 65 | * The timeout is set based on the current time and InterestLifetime. |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 66 | * This class will invoke the timeout callback unless the record is deleted before timeout. |
| 67 | * |
| 68 | * @param interest the Interest |
| 69 | * @param dataCallback invoked when matching Data packet is received |
| 70 | * @param nackCallback invoked when Nack matching Interest is received |
| 71 | * @param timeoutCallback invoked when Interest times out |
| 72 | * @param scheduler Scheduler for scheduling the timeout event |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 73 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 74 | PendingInterest(shared_ptr<const Interest> interest, |
| 75 | const DataCallback& dataCallback, |
| 76 | const NackCallback& nackCallback, |
| 77 | const TimeoutCallback& timeoutCallback, |
| 78 | Scheduler& scheduler) |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 79 | : m_interest(std::move(interest)) |
| 80 | , m_origin(PendingInterestOrigin::APP) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 81 | , m_dataCallback(dataCallback) |
| 82 | , m_nackCallback(nackCallback) |
| 83 | , m_timeoutCallback(timeoutCallback) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 84 | , m_timeoutEvent(scheduler) |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 85 | , m_nNotNacked(0) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 86 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 87 | scheduleTimeoutEvent(scheduler); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 88 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 90 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 91 | * @brief Construct a pending Interest record for an Interest from NFD |
| 92 | * |
| 93 | * @param interest the Interest |
| 94 | * @param scheduler Scheduler for scheduling the timeout event |
| 95 | */ |
| 96 | PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler) |
| 97 | : m_interest(std::move(interest)) |
| 98 | , m_origin(PendingInterestOrigin::FORWARDER) |
| 99 | , m_timeoutEvent(scheduler) |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 100 | , m_nNotNacked(0) |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 101 | { |
| 102 | scheduleTimeoutEvent(scheduler); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @brief Get the Interest |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 107 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 108 | shared_ptr<const Interest> |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 109 | getInterest() const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 110 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 111 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 112 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 114 | PendingInterestOrigin |
| 115 | getOrigin() const |
| 116 | { |
| 117 | return m_origin; |
| 118 | } |
| 119 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 120 | /** |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 121 | * @brief Record that the Interest has been forwarded to one destination |
| 122 | * |
| 123 | * A "destination" could be either a local InterestFilter or the forwarder. |
| 124 | */ |
| 125 | void |
| 126 | recordForwarding() |
| 127 | { |
| 128 | ++m_nNotNacked; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @brief Record an incoming Nack against a forwarded Interest |
| 133 | * @return least severe Nack if all destinations where the Interest was forwarded have Nacked; |
| 134 | * otherwise, nullopt |
| 135 | */ |
| 136 | optional<lp::Nack> |
| 137 | recordNack(const lp::Nack& nack) |
| 138 | { |
| 139 | --m_nNotNacked; |
| 140 | BOOST_ASSERT(m_nNotNacked >= 0); |
| 141 | |
| 142 | if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) { |
| 143 | m_leastSevereNack = nack; |
| 144 | } |
| 145 | |
| 146 | return m_nNotNacked > 0 ? nullopt : m_leastSevereNack; |
| 147 | } |
| 148 | |
| 149 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 150 | * @brief Invoke the Data callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 151 | * @note This method does nothing if the Data callback is empty |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 152 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 153 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 154 | invokeDataCallback(const Data& data) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 155 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 156 | if (m_dataCallback != nullptr) { |
| 157 | m_dataCallback(*m_interest, data); |
| 158 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 162 | * @brief Invoke the Nack callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 163 | * @note This method does nothing if the Nack callback is empty |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 164 | */ |
| 165 | void |
| 166 | invokeNackCallback(const lp::Nack& nack) |
| 167 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 168 | if (m_nackCallback != nullptr) { |
| 169 | m_nackCallback(*m_interest, nack); |
| 170 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 174 | * @brief Set cleanup function to be invoked when Interest times out |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 175 | */ |
| 176 | void |
| 177 | setDeleter(const std::function<void()>& deleter) |
| 178 | { |
| 179 | m_deleter = deleter; |
| 180 | } |
| 181 | |
| 182 | private: |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 183 | void |
| 184 | scheduleTimeoutEvent(Scheduler& scheduler) |
| 185 | { |
| 186 | m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(), |
| 187 | [=] { this->invokeTimeoutCallback(); }); |
| 188 | } |
| 189 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 190 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 191 | * @brief Invoke the timeout callback (if non-empty) and the deleter |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 192 | */ |
| 193 | void |
| 194 | invokeTimeoutCallback() |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 195 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 196 | if (m_timeoutCallback) { |
| 197 | m_timeoutCallback(*m_interest); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 198 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 199 | |
| 200 | BOOST_ASSERT(m_deleter); |
| 201 | m_deleter(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 202 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 204 | private: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 205 | shared_ptr<const Interest> m_interest; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 206 | PendingInterestOrigin m_origin; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 207 | DataCallback m_dataCallback; |
| 208 | NackCallback m_nackCallback; |
| 209 | TimeoutCallback m_timeoutCallback; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 210 | util::scheduler::ScopedEventId m_timeoutEvent; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 211 | int m_nNotNacked; ///< number of Interest destinations that have not Nacked |
| 212 | optional<lp::Nack> m_leastSevereNack; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 213 | std::function<void()> m_deleter; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 216 | /** |
| 217 | * @brief Opaque type to identify a PendingInterest |
| 218 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 219 | class PendingInterestId; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 220 | |
| 221 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 222 | * @brief Functor to match PendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 223 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 224 | class MatchPendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 226 | public: |
| 227 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 228 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 229 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 230 | { |
| 231 | } |
| 232 | |
| 233 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 234 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 235 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 236 | return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 237 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 238 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 239 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 240 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 243 | } // namespace ndn |
| 244 | |
| 245 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |