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) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 83 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 84 | scheduleTimeoutEvent(scheduler); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 85 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 87 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 88 | * @brief Construct a pending Interest record for an Interest from NFD |
| 89 | * |
| 90 | * @param interest the Interest |
| 91 | * @param scheduler Scheduler for scheduling the timeout event |
| 92 | */ |
| 93 | PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler) |
| 94 | : m_interest(std::move(interest)) |
| 95 | , m_origin(PendingInterestOrigin::FORWARDER) |
| 96 | , m_timeoutEvent(scheduler) |
| 97 | { |
| 98 | scheduleTimeoutEvent(scheduler); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @brief Get the Interest |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 103 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 104 | shared_ptr<const Interest> |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 105 | getInterest() const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 106 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 107 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 108 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 110 | PendingInterestOrigin |
| 111 | getOrigin() const |
| 112 | { |
| 113 | return m_origin; |
| 114 | } |
| 115 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 116 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 117 | * @brief Invoke the Data callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 118 | * @note This method does nothing if the Data callback is empty |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 119 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 120 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 121 | invokeDataCallback(const Data& data) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 122 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 123 | if (m_dataCallback != nullptr) { |
| 124 | m_dataCallback(*m_interest, data); |
| 125 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 129 | * @brief Invoke the Nack callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 130 | * @note This method does nothing if the Nack callback is empty |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 131 | */ |
| 132 | void |
| 133 | invokeNackCallback(const lp::Nack& nack) |
| 134 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 135 | if (m_nackCallback != nullptr) { |
| 136 | m_nackCallback(*m_interest, nack); |
| 137 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 141 | * @brief Set cleanup function to be invoked when Interest times out |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 142 | */ |
| 143 | void |
| 144 | setDeleter(const std::function<void()>& deleter) |
| 145 | { |
| 146 | m_deleter = deleter; |
| 147 | } |
| 148 | |
| 149 | private: |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 150 | void |
| 151 | scheduleTimeoutEvent(Scheduler& scheduler) |
| 152 | { |
| 153 | m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(), |
| 154 | [=] { this->invokeTimeoutCallback(); }); |
| 155 | } |
| 156 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 157 | /** |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 158 | * @brief Invoke the timeout callback (if non-empty) and the deleter |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 159 | */ |
| 160 | void |
| 161 | invokeTimeoutCallback() |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 162 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 163 | if (m_timeoutCallback) { |
| 164 | m_timeoutCallback(*m_interest); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 165 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 166 | |
| 167 | BOOST_ASSERT(m_deleter); |
| 168 | m_deleter(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 169 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 171 | private: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 172 | shared_ptr<const Interest> m_interest; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 173 | PendingInterestOrigin m_origin; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 174 | DataCallback m_dataCallback; |
| 175 | NackCallback m_nackCallback; |
| 176 | TimeoutCallback m_timeoutCallback; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 177 | util::scheduler::ScopedEventId m_timeoutEvent; |
| 178 | std::function<void()> m_deleter; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 181 | /** |
| 182 | * @brief Opaque type to identify a PendingInterest |
| 183 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 184 | class PendingInterestId; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 187 | * @brief Functor to match PendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 188 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 189 | class MatchPendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 190 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 191 | public: |
| 192 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 193 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 194 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 195 | { |
| 196 | } |
| 197 | |
| 198 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 199 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 200 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 201 | return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 202 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 203 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 204 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 205 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 208 | } // namespace ndn |
| 209 | |
| 210 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |