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