blob: 735db8051353b6fe34e4b39cdacbd13f509c00f5 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08002/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +00003 * Copyright (c) 2013-2016 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 "../interest.hpp"
26#include "../data.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/**
33 * @brief stores a pending Interest and associated callbacks
34 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070035class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070036{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080037public:
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080038 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000039 * @brief Construct a pending Interest record
40 *
41 * The timeout is set based on the current time and the Interest lifetime.
42 * This class will invoke the timeout callback unless the record is deleted before timeout.
43 *
44 * @param interest the Interest
45 * @param dataCallback invoked when matching Data packet is received
46 * @param nackCallback invoked when Nack matching Interest is received
47 * @param timeoutCallback invoked when Interest times out
48 * @param scheduler Scheduler for scheduling the timeout event
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080049 */
Eric Newberry83872fd2015-08-06 17:01:24 -070050 PendingInterest(shared_ptr<const Interest> interest,
51 const DataCallback& dataCallback,
52 const NackCallback& nackCallback,
53 const TimeoutCallback& timeoutCallback,
54 Scheduler& scheduler)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070055 : m_interest(interest)
Eric Newberry83872fd2015-08-06 17:01:24 -070056 , m_dataCallback(dataCallback)
57 , m_nackCallback(nackCallback)
58 , m_timeoutCallback(timeoutCallback)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080059 , m_timeoutEvent(scheduler)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080060 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080061 m_timeoutEvent =
62 scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ?
63 m_interest->getInterestLifetime() :
64 DEFAULT_INTEREST_LIFETIME,
Junxiao Shi103d8ed2016-08-07 20:34:10 +000065 [=] { this->invokeTimeoutCallback(); });
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080066 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080068 /**
69 * @return the Interest
70 */
Eric Newberry83872fd2015-08-06 17:01:24 -070071 shared_ptr<const Interest>
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -070072 getInterest() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080073 {
Eric Newberry83872fd2015-08-06 17:01:24 -070074 return m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080075 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080077 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000078 * @brief invokes the Data callback
Junxiao Shi76e0eb22016-08-08 05:54:10 +000079 * @note This method does nothing if the Data callback is empty
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080080 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081 void
Eric Newberry83872fd2015-08-06 17:01:24 -070082 invokeDataCallback(const Data& data)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080083 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +000084 if (m_dataCallback != nullptr) {
85 m_dataCallback(*m_interest, data);
86 }
Eric Newberry83872fd2015-08-06 17:01:24 -070087 }
88
89 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000090 * @brief invokes the Nack callback
Junxiao Shi76e0eb22016-08-08 05:54:10 +000091 * @note This method does nothing if the Nack callback is empty
Eric Newberry83872fd2015-08-06 17:01:24 -070092 */
93 void
94 invokeNackCallback(const lp::Nack& nack)
95 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +000096 if (m_nackCallback != nullptr) {
97 m_nackCallback(*m_interest, nack);
98 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080099 }
100
101 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000102 * @brief Set cleanup function to be invoked when Interest times out
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800103 */
104 void
105 setDeleter(const std::function<void()>& deleter)
106 {
107 m_deleter = deleter;
108 }
109
110private:
111 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000112 * @brief invokes the timeout callback (if non-empty) and the deleter
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 */
114 void
115 invokeTimeoutCallback()
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800116 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700117 if (m_timeoutCallback) {
118 m_timeoutCallback(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800119 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800120
121 BOOST_ASSERT(m_deleter);
122 m_deleter();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800123 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800125private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700126 shared_ptr<const Interest> m_interest;
Eric Newberry83872fd2015-08-06 17:01:24 -0700127 DataCallback m_dataCallback;
128 NackCallback m_nackCallback;
129 TimeoutCallback m_timeoutCallback;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800130 util::scheduler::ScopedEventId m_timeoutEvent;
131 std::function<void()> m_deleter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800132};
133
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000134/**
135 * @brief Opaque type to identify a PendingInterest
136 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700137class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800138
139/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000140 * @brief Functor to match PendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800141 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700142class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800143{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700144public:
145 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
147 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800148 {
149 }
150
151 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800153 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000154 return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800155 }
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000156
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800157private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800159};
160
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800161} // namespace ndn
162
163#endif // NDN_DETAIL_PENDING_INTEREST_HPP