blob: b53dc1ffd1b1ba7bb0f70a4f41ccfd65b1bbef91 [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 {
33 // Set up timeoutTime_.
34 if (interest_->getInterestLifetime() >= 0)
35 timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetime();
36 else
37 // No timeout.
38 /**
39 * @todo Set more meaningful default timeout. This timeout MUST exist.
40 */
41 timeoutTimeMilliseconds_ = getNowMilliseconds() + 4000;
42 }
43
44 const shared_ptr<const Interest>&
45 getInterest()
46 {
47 return interest_;
48 }
49
50 const OnData&
51 getOnData()
52 {
53 return onData_;
54 }
55
56 /**
57 * Check if this interest is timed out.
58 * @param nowMilliseconds The current time in milliseconds from getNowMilliseconds.
59 * @return true if this interest timed out, otherwise false.
60 */
61 bool
62 isTimedOut(MillisecondsSince1970 nowMilliseconds)
63 {
64 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
65 }
66
67 /**
68 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
69 */
70 void
71 callTimeout()
72 {
73 if (onTimeout_) {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080074 onTimeout_(*interest_);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080075 }
76 }
77
78private:
79 shared_ptr<const Interest> interest_;
80 const OnData onData_;
81 const OnTimeout onTimeout_;
82
83 /** The time when the interest times out in milliseconds according to getNowMilliseconds, or -1 for no timeout. */
84 MillisecondsSince1970 timeoutTimeMilliseconds_;
85};
86
87
88struct PendingInterestId;
89
90/**
91 * @brief Functor to match pending interests against PendingInterestId
92 */
93struct MatchPendingInterestId
94{
95 MatchPendingInterestId(const PendingInterestId *pendingInterestId)
96 : id_(pendingInterestId)
97 {
98 }
99
100 bool
101 operator()(const shared_ptr<const PendingInterest> &pendingInterest) const
102 {
103 return (reinterpret_cast<const PendingInterestId *>(pendingInterest.get()) == id_);
104 }
105private:
106 const PendingInterestId *id_;
107};
108
109
110} // namespace ndn
111
112#endif // NDN_DETAIL_PENDING_INTEREST_HPP