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 | /** |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | |
| 25 | #include "../common.hpp" |
| 26 | #include "../interest.hpp" |
| 27 | #include "../data.hpp" |
| 28 | #include "../util/time.hpp" |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 29 | #include "../util/scheduler.hpp" |
| 30 | #include "../util/scheduler-scoped-event-id.hpp" |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 31 | #include "../lp/nack.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 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 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 39 | * @brief Create a new PitEntry and set the timeout based on the current time and |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 40 | * the Interest lifetime. |
| 41 | * @param interest shared_ptr for the Interest |
| 42 | * @param dataCallback function to call when matching Data packet is received |
| 43 | * @param nackCallback function to call when Nack matching Interest is received |
| 44 | * @param timeoutCallback function to call if Interest times out |
| 45 | * @param scheduler Scheduler instance to use to schedule a timeout event. The scheduled |
| 46 | * event will be automatically cancelled when pending Interest is destroyed. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 47 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 48 | PendingInterest(shared_ptr<const Interest> interest, |
| 49 | const DataCallback& dataCallback, |
| 50 | const NackCallback& nackCallback, |
| 51 | const TimeoutCallback& timeoutCallback, |
| 52 | Scheduler& scheduler) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 53 | : m_interest(interest) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 54 | , m_dataCallback(dataCallback) |
| 55 | , m_nackCallback(nackCallback) |
| 56 | , m_timeoutCallback(timeoutCallback) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 57 | , m_timeoutEvent(scheduler) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 59 | m_timeoutEvent = |
| 60 | scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ? |
| 61 | m_interest->getInterestLifetime() : |
| 62 | DEFAULT_INTEREST_LIFETIME, |
| 63 | bind(&PendingInterest::invokeTimeoutCallback, this)); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 64 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 66 | /** |
| 67 | * @return the Interest |
| 68 | */ |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 69 | shared_ptr<const Interest> |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 70 | getInterest() const |
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 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 73 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 75 | /** |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 76 | * @brief invokes the DataCallback |
| 77 | * @note If the DataCallback is an empty function, this method does nothing. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 80 | invokeDataCallback(const Data& data) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 81 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 82 | m_dataCallback(*m_interest, data); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @brief invokes the NackCallback |
| 87 | * @note If the NackCallback is an empty function, this method does nothing. |
| 88 | */ |
| 89 | void |
| 90 | invokeNackCallback(const lp::Nack& nack) |
| 91 | { |
| 92 | m_nackCallback(*m_interest, nack); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @brief Set cleanup function to be called after interest times out |
| 97 | */ |
| 98 | void |
| 99 | setDeleter(const std::function<void()>& deleter) |
| 100 | { |
| 101 | m_deleter = deleter; |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | /** |
| 106 | * @brief invokes the TimeoutCallback |
| 107 | * @note If the TimeoutCallback is an empty function, this method does nothing. |
| 108 | */ |
| 109 | void |
| 110 | invokeTimeoutCallback() |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 111 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 112 | if (m_timeoutCallback) { |
| 113 | m_timeoutCallback(*m_interest); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 115 | |
| 116 | BOOST_ASSERT(m_deleter); |
| 117 | m_deleter(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 118 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 120 | private: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 121 | shared_ptr<const Interest> m_interest; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 122 | DataCallback m_dataCallback; |
| 123 | NackCallback m_nackCallback; |
| 124 | TimeoutCallback m_timeoutCallback; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 125 | util::scheduler::ScopedEventId m_timeoutEvent; |
| 126 | std::function<void()> m_deleter; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 130 | class PendingInterestId; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Functor to match pending interests against PendingInterestId |
| 134 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 135 | class MatchPendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 136 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 137 | public: |
| 138 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 139 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 140 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 141 | { |
| 142 | } |
| 143 | |
| 144 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 145 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 146 | { |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 147 | return (reinterpret_cast<const PendingInterestId*>( |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 148 | pendingInterest->getInterest().get()) == m_id); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 149 | } |
| 150 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 151 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | |
| 155 | } // namespace ndn |
| 156 | |
| 157 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |