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