Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_DETAIL_PENDING_INTEREST_HPP |
| 8 | #define NDN_DETAIL_PENDING_INTEREST_HPP |
| 9 | |
| 10 | #include "../common.hpp" |
| 11 | #include "../interest.hpp" |
| 12 | #include "../data.hpp" |
| 13 | #include "../util/time.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | class PendingInterest { |
| 18 | public: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 19 | typedef function<void(const Interest&, Data&)> OnData; |
| 20 | typedef function<void(const Interest&)> OnTimeout; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 23 | * @brief Create a new PitEntry and set the timeout based on the current time and |
| 24 | * the interest lifetime. |
| 25 | * |
| 26 | * @param interest A shared_ptr for the interest |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 27 | * @param onData A function object to call when a matching data packet is received. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 28 | * @param onTimeout A function object to call if the interest times out. |
| 29 | * If onTimeout is an empty OnTimeout(), this does not use it. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 30 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 31 | PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData, |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 32 | const OnTimeout& onTimeout) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | : interest_(interest) |
| 34 | , onData_(onData) |
| 35 | , m_onTimeout(onTimeout) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 37 | if (interest_->getInterestLifetime() >= time::milliseconds::zero()) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | m_timeout = time::steady_clock::now() + interest_->getInterestLifetime(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 39 | else |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 41 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 42 | |
| 43 | const shared_ptr<const Interest>& |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 44 | getInterest() |
| 45 | { |
| 46 | return interest_; |
| 47 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | |
| 49 | const OnData& |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 50 | getOnData() |
| 51 | { |
| 52 | return onData_; |
| 53 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Check if this interest is timed out. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 57 | * @return true if this interest timed out, otherwise false. |
| 58 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | bool |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | isTimedOut(const time::steady_clock::TimePoint& now) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 61 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | return now >= m_timeout; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 67 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | void |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 69 | callTimeout() |
| 70 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | if (m_onTimeout) { |
| 72 | m_onTimeout(*interest_); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 76 | private: |
| 77 | shared_ptr<const Interest> interest_; |
| 78 | const OnData onData_; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | const OnTimeout m_onTimeout; |
| 80 | time::steady_clock::TimePoint m_timeout; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | |
| 84 | struct PendingInterestId; |
| 85 | |
| 86 | /** |
| 87 | * @brief Functor to match pending interests against PendingInterestId |
| 88 | */ |
| 89 | struct MatchPendingInterestId |
| 90 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 92 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 93 | { |
| 94 | } |
| 95 | |
| 96 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 99 | return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 100 | } |
| 101 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
| 106 | } // namespace ndn |
| 107 | |
| 108 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |