Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 2 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 "../interest.hpp" |
| 26 | #include "../data.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 | /** |
| 33 | * @brief stores a pending Interest and associated callbacks |
| 34 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 35 | class PendingInterest : noncopyable |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 36 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 37 | public: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 38 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 39 | * @brief Construct a pending Interest record |
| 40 | * |
| 41 | * The timeout is set based on the current time and the Interest lifetime. |
| 42 | * This class will invoke the timeout callback unless the record is deleted before timeout. |
| 43 | * |
| 44 | * @param interest the Interest |
| 45 | * @param dataCallback invoked when matching Data packet is received |
| 46 | * @param nackCallback invoked when Nack matching Interest is received |
| 47 | * @param timeoutCallback invoked when Interest times out |
| 48 | * @param scheduler Scheduler for scheduling the timeout event |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 49 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 50 | PendingInterest(shared_ptr<const Interest> interest, |
| 51 | const DataCallback& dataCallback, |
| 52 | const NackCallback& nackCallback, |
| 53 | const TimeoutCallback& timeoutCallback, |
| 54 | Scheduler& scheduler) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 55 | : m_interest(interest) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 56 | , m_dataCallback(dataCallback) |
| 57 | , m_nackCallback(nackCallback) |
| 58 | , m_timeoutCallback(timeoutCallback) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 59 | , m_timeoutEvent(scheduler) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 61 | m_timeoutEvent = |
| 62 | scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ? |
| 63 | m_interest->getInterestLifetime() : |
| 64 | DEFAULT_INTEREST_LIFETIME, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 65 | [=] { this->invokeTimeoutCallback(); }); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 66 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 68 | /** |
| 69 | * @return the Interest |
| 70 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 71 | shared_ptr<const Interest> |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 72 | getInterest() const |
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 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 77 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 78 | * @brief invokes the Data callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 79 | * @note This method does nothing if the Data callback is empty |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 82 | invokeDataCallback(const Data& data) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 83 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 84 | if (m_dataCallback != nullptr) { |
| 85 | m_dataCallback(*m_interest, data); |
| 86 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 90 | * @brief invokes the Nack callback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 91 | * @note This method does nothing if the Nack callback is empty |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 92 | */ |
| 93 | void |
| 94 | invokeNackCallback(const lp::Nack& nack) |
| 95 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 96 | if (m_nackCallback != nullptr) { |
| 97 | m_nackCallback(*m_interest, nack); |
| 98 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 102 | * @brief Set cleanup function to be invoked when Interest times out |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 103 | */ |
| 104 | void |
| 105 | setDeleter(const std::function<void()>& deleter) |
| 106 | { |
| 107 | m_deleter = deleter; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 112 | * @brief invokes the timeout callback (if non-empty) and the deleter |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 113 | */ |
| 114 | void |
| 115 | invokeTimeoutCallback() |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 116 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 117 | if (m_timeoutCallback) { |
| 118 | m_timeoutCallback(*m_interest); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 119 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 120 | |
| 121 | BOOST_ASSERT(m_deleter); |
| 122 | m_deleter(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 123 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 125 | private: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 126 | shared_ptr<const Interest> m_interest; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 127 | DataCallback m_dataCallback; |
| 128 | NackCallback m_nackCallback; |
| 129 | TimeoutCallback m_timeoutCallback; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 130 | util::scheduler::ScopedEventId m_timeoutEvent; |
| 131 | std::function<void()> m_deleter; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 134 | /** |
| 135 | * @brief Opaque type to identify a PendingInterest |
| 136 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 137 | class PendingInterestId; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 140 | * @brief Functor to match PendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 141 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 142 | class MatchPendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 144 | public: |
| 145 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 147 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 148 | { |
| 149 | } |
| 150 | |
| 151 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 153 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 154 | return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 155 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 156 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 157 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 158 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 161 | } // namespace ndn |
| 162 | |
| 163 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |