blob: f21fc2ed6b8bd01ad588fc578b7e56ca2531b699 [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
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080079 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 void
Eric Newberry83872fd2015-08-06 17:01:24 -070081 invokeDataCallback(const Data& data)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080082 {
Eric Newberry83872fd2015-08-06 17:01:24 -070083 m_dataCallback(*m_interest, data);
84 }
85
86 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000087 * @brief invokes the Nack callback
Eric Newberry83872fd2015-08-06 17:01:24 -070088 */
89 void
90 invokeNackCallback(const lp::Nack& nack)
91 {
92 m_nackCallback(*m_interest, nack);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080093 }
94
95 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000096 * @brief Set cleanup function to be invoked when Interest times out
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080097 */
98 void
99 setDeleter(const std::function<void()>& deleter)
100 {
101 m_deleter = deleter;
102 }
103
104private:
105 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000106 * @brief invokes the timeout callback (if non-empty) and the deleter
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800107 */
108 void
109 invokeTimeoutCallback()
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800110 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700111 if (m_timeoutCallback) {
112 m_timeoutCallback(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800113 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800114
115 BOOST_ASSERT(m_deleter);
116 m_deleter();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800117 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800119private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700120 shared_ptr<const Interest> m_interest;
Eric Newberry83872fd2015-08-06 17:01:24 -0700121 DataCallback m_dataCallback;
122 NackCallback m_nackCallback;
123 TimeoutCallback m_timeoutCallback;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800124 util::scheduler::ScopedEventId m_timeoutEvent;
125 std::function<void()> m_deleter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800126};
127
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000128/**
129 * @brief Opaque type to identify a PendingInterest
130 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700131class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800132
133/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000134 * @brief Functor to match PendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800135 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700136class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800137{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700138public:
139 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
141 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800142 {
143 }
144
145 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800147 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000148 return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800149 }
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000150
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800151private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800153};
154
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800155} // namespace ndn
156
157#endif // NDN_DETAIL_PENDING_INTEREST_HPP