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