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 | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 40 | : m_interest(interest) |
| 41 | , m_onData(onData) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 42 | , m_onTimeout(onTimeout) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 44 | if (m_interest->getInterestLifetime() >= time::milliseconds::zero()) |
| 45 | m_timeout = time::steady_clock::now() + m_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 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 53 | return m_interest; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 59 | return m_onData; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 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) { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 79 | m_onTimeout(*m_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: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 84 | shared_ptr<const Interest> m_interest; |
| 85 | const OnData m_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 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 91 | class PendingInterestId; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * @brief Functor to match pending interests against PendingInterestId |
| 95 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 96 | class MatchPendingInterestId |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 97 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 98 | public: |
| 99 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 100 | MatchPendingInterestId(const PendingInterestId* pendingInterestId) |
| 101 | : m_id(pendingInterestId) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 102 | { |
| 103 | } |
| 104 | |
| 105 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 106 | operator()(const shared_ptr<const PendingInterest>& pendingInterest) const |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 107 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 108 | return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 109 | } |
| 110 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | const PendingInterestId* m_id; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | |
| 115 | } // namespace ndn |
| 116 | |
| 117 | #endif // NDN_DETAIL_PENDING_INTEREST_HPP |