blob: ad037467e0e329f3583887b7e05008f8212ce154 [file] [log] [blame]
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyevc8823bc2014-02-09 19:33:33 -080011 */
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
21namespace ndn {
22
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070023class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070024{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080025public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026 typedef function<void(const Interest&, Data&)> OnData;
27 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080028
29 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030 * @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 Afanasyevc8823bc2014-02-09 19:33:33 -080034 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035 * @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 Afanasyevc8823bc2014-02-09 19:33:33 -080037 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080039 const OnTimeout& onTimeout)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 : interest_(interest)
41 , onData_(onData)
42 , m_onTimeout(onTimeout)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080043 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 if (interest_->getInterestLifetime() >= time::milliseconds::zero())
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 m_timeout = time::steady_clock::now() + interest_->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080046 else
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080048 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049
50 const shared_ptr<const Interest>&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080051 getInterest()
52 {
53 return interest_;
54 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055
56 const OnData&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 getOnData()
58 {
59 return onData_;
60 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080062 /**
63 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080064 * @return true if this interest timed out, otherwise false.
65 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080068 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 return now >= m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080070 }
71
72 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073 * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080074 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 void
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080076 callTimeout()
77 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 if (m_onTimeout) {
79 m_onTimeout(*interest_);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080080 }
81 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080083private:
84 shared_ptr<const Interest> interest_;
85 const OnData onData_;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 const OnTimeout m_onTimeout;
87 time::steady_clock::TimePoint m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080088};
89
90
91struct PendingInterestId;
92
93/**
94 * @brief Functor to match pending interests against PendingInterestId
95 */
96struct MatchPendingInterestId
97{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
99 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800100 {
101 }
102
103 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800105 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800107 }
108private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800110};
111
112
113} // namespace ndn
114
115#endif // NDN_DETAIL_PENDING_INTEREST_HPP