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 | /* |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 22 | #ifndef NDN_IMPL_PENDING_INTEREST_HPP |
| 23 | #define NDN_IMPL_PENDING_INTEREST_HPP |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/data.hpp" |
| 26 | #include "ndn-cxx/face.hpp" |
| 27 | #include "ndn-cxx/interest.hpp" |
| 28 | #include "ndn-cxx/lp/nack.hpp" |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 29 | #include "ndn-cxx/util/scheduler.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 33 | /** |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 34 | * @brief Opaque type to identify a PendingInterest |
| 35 | */ |
| 36 | class PendingInterestId; |
| 37 | |
| 38 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 39 | * @brief Indicates where a pending Interest came from |
| 40 | */ |
| 41 | enum class PendingInterestOrigin |
| 42 | { |
| 43 | APP, ///< Interest was received from this app via Face::expressInterest API |
| 44 | FORWARDER ///< Interest was received from the forwarder via Transport |
| 45 | }; |
| 46 | |
| 47 | std::ostream& |
| 48 | operator<<(std::ostream& os, PendingInterestOrigin origin) |
| 49 | { |
| 50 | switch (origin) { |
| 51 | case PendingInterestOrigin::APP: |
| 52 | return os << "app"; |
| 53 | case PendingInterestOrigin::FORWARDER: |
| 54 | return os << "forwarder"; |
| 55 | } |
| 56 | BOOST_ASSERT(false); |
| 57 | return os; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @brief Stores a pending Interest and associated callbacks |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 62 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 63 | class PendingInterest : noncopyable |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 64 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 65 | public: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 66 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 67 | * @brief Construct a pending Interest record for an Interest from Face::expressInterest |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 68 | * |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 69 | * The timeout is set based on the current time and InterestLifetime. |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 70 | * This class will invoke the timeout callback unless the record is deleted before timeout. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 71 | */ |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 72 | PendingInterest(const PendingInterestId* id, |
| 73 | shared_ptr<const Interest> interest, |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 74 | const DataCallback& dataCallback, |
| 75 | const NackCallback& nackCallback, |
| 76 | const TimeoutCallback& timeoutCallback, |
| 77 | Scheduler& scheduler) |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 78 | : m_id(id) |
| 79 | , m_interest(std::move(interest)) |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 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) |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 84 | , m_nNotNacked(0) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 85 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 86 | scheduleTimeoutEvent(scheduler); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 87 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 89 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 90 | * @brief Construct a pending Interest record for an Interest from NFD |
| 91 | * |
| 92 | * @param interest the Interest |
| 93 | * @param scheduler Scheduler for scheduling the timeout event |
| 94 | */ |
| 95 | PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler) |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 96 | : m_id(nullptr) |
| 97 | , m_interest(std::move(interest)) |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 98 | , m_origin(PendingInterestOrigin::FORWARDER) |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 99 | , m_nNotNacked(0) |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 100 | { |
| 101 | scheduleTimeoutEvent(scheduler); |
| 102 | } |
| 103 | |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 104 | const PendingInterestId* |
| 105 | getId() const |
| 106 | { |
| 107 | return m_id; |
| 108 | } |
| 109 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 110 | shared_ptr<const Interest> |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 111 | getInterest() const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 112 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 113 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 116 | PendingInterestOrigin |
| 117 | getOrigin() const |
| 118 | { |
| 119 | return m_origin; |
| 120 | } |
| 121 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 122 | /** |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 123 | * @brief Record that the Interest has been forwarded to one destination |
| 124 | * |
| 125 | * A "destination" could be either a local InterestFilter or the forwarder. |
| 126 | */ |
| 127 | void |
| 128 | recordForwarding() |
| 129 | { |
| 130 | ++m_nNotNacked; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @brief Record an incoming Nack against a forwarded Interest |
| 135 | * @return least severe Nack if all destinations where the Interest was forwarded have Nacked; |
| 136 | * otherwise, nullopt |
| 137 | */ |
| 138 | optional<lp::Nack> |
| 139 | recordNack(const lp::Nack& nack) |
| 140 | { |
| 141 | --m_nNotNacked; |
| 142 | BOOST_ASSERT(m_nNotNacked >= 0); |
| 143 | |
| 144 | if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) { |
| 145 | m_leastSevereNack = nack; |
| 146 | } |
| 147 | |
| 148 | return m_nNotNacked > 0 ? nullopt : m_leastSevereNack; |
| 149 | } |
| 150 | |
| 151 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 152 | * @brief Invoke the Data callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 153 | * @note This method does nothing if the Data callback is empty |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 154 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 155 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 156 | invokeDataCallback(const Data& data) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 157 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 158 | if (m_dataCallback != nullptr) { |
| 159 | m_dataCallback(*m_interest, data); |
| 160 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 164 | * @brief Invoke the Nack callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 165 | * @note This method does nothing if the Nack callback is empty |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 166 | */ |
| 167 | void |
| 168 | invokeNackCallback(const lp::Nack& nack) |
| 169 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 170 | if (m_nackCallback != nullptr) { |
| 171 | m_nackCallback(*m_interest, nack); |
| 172 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 176 | * @brief Set cleanup function to be invoked when Interest times out |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 177 | */ |
| 178 | void |
| 179 | setDeleter(const std::function<void()>& deleter) |
| 180 | { |
| 181 | m_deleter = deleter; |
| 182 | } |
| 183 | |
| 184 | private: |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 185 | void |
| 186 | scheduleTimeoutEvent(Scheduler& scheduler) |
| 187 | { |
| 188 | m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(), |
| 189 | [=] { this->invokeTimeoutCallback(); }); |
| 190 | } |
| 191 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 192 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 193 | * @brief Invoke the timeout callback (if non-empty) and the deleter |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 194 | */ |
| 195 | void |
| 196 | invokeTimeoutCallback() |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 197 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 198 | if (m_timeoutCallback) { |
| 199 | m_timeoutCallback(*m_interest); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 200 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 201 | |
| 202 | BOOST_ASSERT(m_deleter); |
| 203 | m_deleter(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 204 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 205 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 206 | private: |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 207 | const PendingInterestId* m_id; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 208 | shared_ptr<const Interest> m_interest; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 209 | PendingInterestOrigin m_origin; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 210 | DataCallback m_dataCallback; |
| 211 | NackCallback m_nackCallback; |
| 212 | TimeoutCallback m_timeoutCallback; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 213 | util::scheduler::ScopedEventId m_timeoutEvent; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 214 | int m_nNotNacked; ///< number of Interest destinations that have not Nacked |
| 215 | optional<lp::Nack> m_leastSevereNack; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 216 | std::function<void()> m_deleter; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 217 | }; |
| 218 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 219 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 220 | * @brief Functor to match PendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 221 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 222 | class MatchPendingInterestId |
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 | public: |
| 225 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 226 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 227 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 228 | { |
| 229 | } |
| 230 | |
| 231 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 232 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 233 | { |
Junxiao Shi | da9761f | 2019-02-11 15:05:33 -0700 | [diff] [blame^] | 234 | return pendingInterest->getId() == m_id; |
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 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 237 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 238 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 241 | } // namespace ndn |
| 242 | |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 243 | #endif // NDN_IMPL_PENDING_INTEREST_HPP |