blob: c481acc0a0b106a1034759dccdc049c693239cb4 [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
17class PendingInterest {
18public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080019 typedef function<void(const Interest&, Data&)> OnData;
20 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080021
22 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 * @brief Create a new PitEntry and set the timeout based on the current time and
24 * the interest lifetime.
25 *
26 * @param interest A shared_ptr for the interest
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080027 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028 * @param onTimeout A function object to call if the interest times out.
29 * If onTimeout is an empty OnTimeout(), this does not use it.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080030 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080032 const OnTimeout& onTimeout)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033 : interest_(interest)
34 , onData_(onData)
35 , m_onTimeout(onTimeout)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080036 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070037 if (interest_->getInterestLifetime() >= time::milliseconds::zero())
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 m_timeout = time::steady_clock::now() + interest_->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080039 else
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080041 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042
43 const shared_ptr<const Interest>&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080044 getInterest()
45 {
46 return interest_;
47 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048
49 const OnData&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080050 getOnData()
51 {
52 return onData_;
53 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080055 /**
56 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 * @return true if this interest timed out, otherwise false.
58 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080061 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 return now >= m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080063 }
64
65 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080067 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068 void
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080069 callTimeout()
70 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 if (m_onTimeout) {
72 m_onTimeout(*interest_);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080073 }
74 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080076private:
77 shared_ptr<const Interest> interest_;
78 const OnData onData_;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079 const OnTimeout m_onTimeout;
80 time::steady_clock::TimePoint m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080081};
82
83
84struct PendingInterestId;
85
86/**
87 * @brief Functor to match pending interests against PendingInterestId
88 */
89struct MatchPendingInterestId
90{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
92 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080093 {
94 }
95
96 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080098 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800100 }
101private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800103};
104
105
106} // namespace ndn
107
108#endif // NDN_DETAIL_PENDING_INTEREST_HPP