blob: bac58367427de1ce3c86a85288692af045559660 [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/**
Alexander Afanasyev9d158f02015-02-17 21:30:19 -08003 * Copyright (c) 2013-2015 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
25#include "../common.hpp"
26#include "../interest.hpp"
27#include "../data.hpp"
28#include "../util/time.hpp"
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080029#include "../util/scheduler.hpp"
30#include "../util/scheduler-scoped-event-id.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070031#include "../lp/nack.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080032
33namespace ndn {
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 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039 * @brief Create a new PitEntry and set the timeout based on the current time and
Eric Newberry83872fd2015-08-06 17:01:24 -070040 * the Interest lifetime.
41 * @param interest shared_ptr for the Interest
42 * @param dataCallback function to call when matching Data packet is received
43 * @param nackCallback function to call when Nack matching Interest is received
44 * @param timeoutCallback function to call if Interest times out
45 * @param scheduler Scheduler instance to use to schedule a timeout event. The scheduled
46 * event will be automatically cancelled when pending Interest is destroyed.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080047 */
Eric Newberry83872fd2015-08-06 17:01:24 -070048 PendingInterest(shared_ptr<const Interest> interest,
49 const DataCallback& dataCallback,
50 const NackCallback& nackCallback,
51 const TimeoutCallback& timeoutCallback,
52 Scheduler& scheduler)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070053 : m_interest(interest)
Eric Newberry83872fd2015-08-06 17:01:24 -070054 , m_dataCallback(dataCallback)
55 , m_nackCallback(nackCallback)
56 , m_timeoutCallback(timeoutCallback)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080057 , m_timeoutEvent(scheduler)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080058 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080059 m_timeoutEvent =
60 scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ?
61 m_interest->getInterestLifetime() :
62 DEFAULT_INTEREST_LIFETIME,
63 bind(&PendingInterest::invokeTimeoutCallback, this));
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080064 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080066 /**
67 * @return the Interest
68 */
Eric Newberry83872fd2015-08-06 17:01:24 -070069 shared_ptr<const Interest>
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -070070 getInterest() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080071 {
Eric Newberry83872fd2015-08-06 17:01:24 -070072 return m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080073 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080075 /**
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080076 * @brief invokes the DataCallback
77 * @note If the DataCallback is an empty function, this method does nothing.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080078 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079 void
Eric Newberry83872fd2015-08-06 17:01:24 -070080 invokeDataCallback(const Data& data)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080081 {
Eric Newberry83872fd2015-08-06 17:01:24 -070082 m_dataCallback(*m_interest, data);
83 }
84
85 /**
86 * @brief invokes the NackCallback
87 * @note If the NackCallback is an empty function, this method does nothing.
88 */
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 /**
96 * @brief Set cleanup function to be called after interest times out
97 */
98 void
99 setDeleter(const std::function<void()>& deleter)
100 {
101 m_deleter = deleter;
102 }
103
104private:
105 /**
106 * @brief invokes the TimeoutCallback
107 * @note If the TimeoutCallback is an empty function, this method does nothing.
108 */
109 void
110 invokeTimeoutCallback()
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800111 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700112 if (m_timeoutCallback) {
113 m_timeoutCallback(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800114 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800115
116 BOOST_ASSERT(m_deleter);
117 m_deleter();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800118 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800120private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700121 shared_ptr<const Interest> m_interest;
Eric Newberry83872fd2015-08-06 17:01:24 -0700122 DataCallback m_dataCallback;
123 NackCallback m_nackCallback;
124 TimeoutCallback m_timeoutCallback;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800125 util::scheduler::ScopedEventId m_timeoutEvent;
126 std::function<void()> m_deleter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800127};
128
129
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700130class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800131
132/**
133 * @brief Functor to match pending interests against PendingInterestId
134 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700135class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800136{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700137public:
138 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
140 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800141 {
142 }
143
144 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800146 {
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700147 return (reinterpret_cast<const PendingInterestId*>(
Eric Newberry83872fd2015-08-06 17:01:24 -0700148 pendingInterest->getInterest().get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800149 }
150private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800152};
153
154
155} // namespace ndn
156
157#endif // NDN_DETAIL_PENDING_INTEREST_HPP