blob: 41f6b37f7a2d057cdfe0c1366f46bb705ef448df [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"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080031
32namespace ndn {
33
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070034class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070035{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080036public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080037 typedef function<void(const Interest&, Data&)> OnData;
38 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080039
40 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 * @brief Create a new PitEntry and set the timeout based on the current time and
42 * the interest lifetime.
43 *
44 * @param interest A shared_ptr for the interest
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080045 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 * @param onTimeout A function object to call if the interest times out.
47 * If onTimeout is an empty OnTimeout(), this does not use it.
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080048 * @param scheduler Scheduler instance to use to schedule a timeout event. The scheduled
49 * event will be automatically cancelled when pending interest is destroyed.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080050 */
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080051 PendingInterest(shared_ptr<const Interest> interest, const OnData& onData,
52 const OnTimeout& onTimeout, Scheduler& scheduler)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070053 : m_interest(interest)
54 , m_onData(onData)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055 , m_onTimeout(onTimeout)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080056 , m_timeoutEvent(scheduler)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080058 m_timeoutEvent =
59 scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ?
60 m_interest->getInterestLifetime() :
61 DEFAULT_INTEREST_LIFETIME,
62 bind(&PendingInterest::invokeTimeoutCallback, this));
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080063 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080065 /**
66 * @return the Interest
67 */
68 const Interest&
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -070069 getInterest() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080070 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080071 return *m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080072 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080074 /**
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080075 * @brief invokes the DataCallback
76 * @note If the DataCallback is an empty function, this method does nothing.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080077 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 void
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080079 invokeDataCallback(Data& data)
80 {
81 m_onData(*m_interest, data);
82 }
83
84 /**
85 * @brief Set cleanup function to be called after interest times out
86 */
87 void
88 setDeleter(const std::function<void()>& deleter)
89 {
90 m_deleter = deleter;
91 }
92
93private:
94 /**
95 * @brief invokes the TimeoutCallback
96 * @note If the TimeoutCallback is an empty function, this method does nothing.
97 */
98 void
99 invokeTimeoutCallback()
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800100 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700101 if (m_onTimeout) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700102 m_onTimeout(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800103 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800104
105 BOOST_ASSERT(m_deleter);
106 m_deleter();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800107 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800109private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700110 shared_ptr<const Interest> m_interest;
111 const OnData m_onData;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 const OnTimeout m_onTimeout;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 util::scheduler::ScopedEventId m_timeoutEvent;
114 std::function<void()> m_deleter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800115};
116
117
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700118class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800119
120/**
121 * @brief Functor to match pending interests against PendingInterestId
122 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700123class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800124{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700125public:
126 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700127 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
128 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800129 {
130 }
131
132 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800134 {
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700135 return (reinterpret_cast<const PendingInterestId*>(
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800136 &pendingInterest->getInterest()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800137 }
138private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800140};
141
142
143} // namespace ndn
144
145#endif // NDN_DETAIL_PENDING_INTEREST_HPP