blob: c0a35e3ffb6797e7f9e7025a5fd6148b63b47cd0 [file] [log] [blame]
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08001/* -*- 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
15namespace ndn {
16
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070017class PendingInterest
18{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080019public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080020 typedef function<void(const Interest&, Data&)> OnData;
21 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080022
23 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070024 * @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 Afanasyevc8823bc2014-02-09 19:33:33 -080028 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029 * @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 Afanasyevc8823bc2014-02-09 19:33:33 -080031 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080033 const OnTimeout& onTimeout)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 : interest_(interest)
35 , onData_(onData)
36 , m_onTimeout(onTimeout)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080037 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038 if (interest_->getInterestLifetime() >= time::milliseconds::zero())
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039 m_timeout = time::steady_clock::now() + interest_->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080040 else
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080042 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043
44 const shared_ptr<const Interest>&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080045 getInterest()
46 {
47 return interest_;
48 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049
50 const OnData&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080051 getOnData()
52 {
53 return onData_;
54 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080056 /**
57 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080058 * @return true if this interest timed out, otherwise false.
59 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080062 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 return now >= m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080064 }
65
66 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080068 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 void
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080070 callTimeout()
71 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 if (m_onTimeout) {
73 m_onTimeout(*interest_);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080074 }
75 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080077private:
78 shared_ptr<const Interest> interest_;
79 const OnData onData_;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 const OnTimeout m_onTimeout;
81 time::steady_clock::TimePoint m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080082};
83
84
85struct PendingInterestId;
86
87/**
88 * @brief Functor to match pending interests against PendingInterestId
89 */
90struct MatchPendingInterestId
91{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
93 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080094 {
95 }
96
97 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080099 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800101 }
102private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800104};
105
106
107} // namespace ndn
108
109#endif // NDN_DETAIL_PENDING_INTEREST_HPP