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