blob: 2a4da87adb81b54ec52326aedee92d1021ba3682 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +00002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080020 */
21
22#ifndef NDN_DETAIL_PENDING_INTEREST_HPP
23#define NDN_DETAIL_PENDING_INTEREST_HPP
24
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080025#include "../data.hpp"
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000026#include "../interest.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070027#include "../lp/nack.hpp"
Junxiao Shi103d8ed2016-08-07 20:34:10 +000028#include "../util/scheduler-scoped-event-id.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080029
30namespace ndn {
31
Junxiao Shi103d8ed2016-08-07 20:34:10 +000032/**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000033 * @brief Indicates where a pending Interest came from
34 */
35enum class PendingInterestOrigin
36{
37 APP, ///< Interest was received from this app via Face::expressInterest API
38 FORWARDER ///< Interest was received from the forwarder via Transport
39};
40
41std::ostream&
42operator<<(std::ostream& os, PendingInterestOrigin origin)
43{
44 switch (origin) {
45 case PendingInterestOrigin::APP:
46 return os << "app";
47 case PendingInterestOrigin::FORWARDER:
48 return os << "forwarder";
49 }
50 BOOST_ASSERT(false);
51 return os;
52}
53
54/**
55 * @brief Stores a pending Interest and associated callbacks
Junxiao Shi103d8ed2016-08-07 20:34:10 +000056 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070057class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070058{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080059public:
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080060 /**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000061 * @brief Construct a pending Interest record for an Interest from Face::expressInterest
Junxiao Shi103d8ed2016-08-07 20:34:10 +000062 *
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000063 * The timeout is set based on the current time and InterestLifetime.
Junxiao Shi103d8ed2016-08-07 20:34:10 +000064 * This class will invoke the timeout callback unless the record is deleted before timeout.
65 *
66 * @param interest the Interest
67 * @param dataCallback invoked when matching Data packet is received
68 * @param nackCallback invoked when Nack matching Interest is received
69 * @param timeoutCallback invoked when Interest times out
70 * @param scheduler Scheduler for scheduling the timeout event
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080071 */
Eric Newberry83872fd2015-08-06 17:01:24 -070072 PendingInterest(shared_ptr<const Interest> interest,
73 const DataCallback& dataCallback,
74 const NackCallback& nackCallback,
75 const TimeoutCallback& timeoutCallback,
76 Scheduler& scheduler)
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000077 : m_interest(std::move(interest))
78 , m_origin(PendingInterestOrigin::APP)
Eric Newberry83872fd2015-08-06 17:01:24 -070079 , m_dataCallback(dataCallback)
80 , m_nackCallback(nackCallback)
81 , m_timeoutCallback(timeoutCallback)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080082 , m_timeoutEvent(scheduler)
Junxiao Shi79a7a162017-09-09 08:33:57 +000083 , m_nNotNacked(0)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080084 {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000085 scheduleTimeoutEvent(scheduler);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080086 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080088 /**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000089 * @brief Construct a pending Interest record for an Interest from NFD
90 *
91 * @param interest the Interest
92 * @param scheduler Scheduler for scheduling the timeout event
93 */
94 PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler)
95 : m_interest(std::move(interest))
96 , m_origin(PendingInterestOrigin::FORWARDER)
97 , m_timeoutEvent(scheduler)
Junxiao Shi79a7a162017-09-09 08:33:57 +000098 , m_nNotNacked(0)
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000099 {
100 scheduleTimeoutEvent(scheduler);
101 }
102
103 /**
104 * @brief Get the Interest
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800105 */
Eric Newberry83872fd2015-08-06 17:01:24 -0700106 shared_ptr<const Interest>
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700107 getInterest() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800108 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700109 return m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800110 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000112 PendingInterestOrigin
113 getOrigin() const
114 {
115 return m_origin;
116 }
117
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800118 /**
Junxiao Shi79a7a162017-09-09 08:33:57 +0000119 * @brief Record that the Interest has been forwarded to one destination
120 *
121 * A "destination" could be either a local InterestFilter or the forwarder.
122 */
123 void
124 recordForwarding()
125 {
126 ++m_nNotNacked;
127 }
128
129 /**
130 * @brief Record an incoming Nack against a forwarded Interest
131 * @return least severe Nack if all destinations where the Interest was forwarded have Nacked;
132 * otherwise, nullopt
133 */
134 optional<lp::Nack>
135 recordNack(const lp::Nack& nack)
136 {
137 --m_nNotNacked;
138 BOOST_ASSERT(m_nNotNacked >= 0);
139
140 if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) {
141 m_leastSevereNack = nack;
142 }
143
144 return m_nNotNacked > 0 ? nullopt : m_leastSevereNack;
145 }
146
147 /**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000148 * @brief Invoke the Data callback
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000149 * @note This method does nothing if the Data callback is empty
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800150 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700152 invokeDataCallback(const Data& data)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800153 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000154 if (m_dataCallback != nullptr) {
155 m_dataCallback(*m_interest, data);
156 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700157 }
158
159 /**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000160 * @brief Invoke the Nack callback
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000161 * @note This method does nothing if the Nack callback is empty
Eric Newberry83872fd2015-08-06 17:01:24 -0700162 */
163 void
164 invokeNackCallback(const lp::Nack& nack)
165 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000166 if (m_nackCallback != nullptr) {
167 m_nackCallback(*m_interest, nack);
168 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800169 }
170
171 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000172 * @brief Set cleanup function to be invoked when Interest times out
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800173 */
174 void
175 setDeleter(const std::function<void()>& deleter)
176 {
177 m_deleter = deleter;
178 }
179
180private:
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000181 void
182 scheduleTimeoutEvent(Scheduler& scheduler)
183 {
184 m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(),
185 [=] { this->invokeTimeoutCallback(); });
186 }
187
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800188 /**
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000189 * @brief Invoke the timeout callback (if non-empty) and the deleter
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800190 */
191 void
192 invokeTimeoutCallback()
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800193 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700194 if (m_timeoutCallback) {
195 m_timeoutCallback(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800196 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800197
198 BOOST_ASSERT(m_deleter);
199 m_deleter();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800200 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800202private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700203 shared_ptr<const Interest> m_interest;
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000204 PendingInterestOrigin m_origin;
Eric Newberry83872fd2015-08-06 17:01:24 -0700205 DataCallback m_dataCallback;
206 NackCallback m_nackCallback;
207 TimeoutCallback m_timeoutCallback;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800208 util::scheduler::ScopedEventId m_timeoutEvent;
Junxiao Shi79a7a162017-09-09 08:33:57 +0000209 int m_nNotNacked; ///< number of Interest destinations that have not Nacked
210 optional<lp::Nack> m_leastSevereNack;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800211 std::function<void()> m_deleter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800212};
213
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000214/**
215 * @brief Opaque type to identify a PendingInterest
216 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700217class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800218
219/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000220 * @brief Functor to match PendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800221 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700222class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800223{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700224public:
225 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
227 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800228 {
229 }
230
231 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800233 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000234 return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800235 }
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000236
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800237private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700238 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800239};
240
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800241} // namespace ndn
242
243#endif // NDN_DETAIL_PENDING_INTEREST_HPP