blob: 4cfc48a70819cd5a53bd2081d24d8e5b7d2dfccc [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 /**
23 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
24 * @param interest A shared_ptr for the interest.
25 * @param onData A function object to call when a matching data packet is received.
26 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
27 */
28 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
29 const OnTimeout& onTimeout)
30 : interest_(interest),
31 onData_(onData), onTimeout_(onTimeout)
32 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033 if (interest_->getInterestLifetime() >= time::milliseconds::zero())
34 timeout_ = time::steady_clock::now() + interest_->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080035 else
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036 timeout_ = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080037 }
38
39 const shared_ptr<const Interest>&
40 getInterest()
41 {
42 return interest_;
43 }
44
45 const OnData&
46 getOnData()
47 {
48 return onData_;
49 }
50
51 /**
52 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080053 * @return true if this interest timed out, otherwise false.
54 */
55 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 return now >= timeout_;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080059 }
60
61 /**
62 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
63 */
64 void
65 callTimeout()
66 {
67 if (onTimeout_) {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080068 onTimeout_(*interest_);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080069 }
70 }
71
72private:
73 shared_ptr<const Interest> interest_;
74 const OnData onData_;
75 const OnTimeout onTimeout_;
76
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 /** The time when the interest times out in time::milliseconds according to getNowMilliseconds, or -1 for no timeout. */
78 time::steady_clock::TimePoint timeout_;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080079};
80
81
82struct PendingInterestId;
83
84/**
85 * @brief Functor to match pending interests against PendingInterestId
86 */
87struct MatchPendingInterestId
88{
89 MatchPendingInterestId(const PendingInterestId *pendingInterestId)
90 : id_(pendingInterestId)
91 {
92 }
93
94 bool
95 operator()(const shared_ptr<const PendingInterest> &pendingInterest) const
96 {
97 return (reinterpret_cast<const PendingInterestId *>(pendingInterest.get()) == id_);
98 }
99private:
100 const PendingInterestId *id_;
101};
102
103
104} // namespace ndn
105
106#endif // NDN_DETAIL_PENDING_INTEREST_HPP