blob: 791f59b267aa0ec1133339cdb0ce541880d33c48 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
29
30namespace ndn {
31
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070032class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080034public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080035 typedef function<void(const Interest&, Data&)> OnData;
36 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080037
38 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039 * @brief Create a new PitEntry and set the timeout based on the current time and
40 * the interest lifetime.
41 *
42 * @param interest A shared_ptr for the interest
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080043 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 * @param onTimeout A function object to call if the interest times out.
45 * If onTimeout is an empty OnTimeout(), this does not use it.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080046 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080048 const OnTimeout& onTimeout)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070049 : m_interest(interest)
50 , m_onData(onData)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051 , m_onTimeout(onTimeout)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080052 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070053 if (m_interest->getInterestLifetime() >= time::milliseconds::zero())
54 m_timeout = time::steady_clock::now() + m_interest->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080055 else
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058
59 const shared_ptr<const Interest>&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080060 getInterest()
61 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070062 return m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080063 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064
65 const OnData&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080066 getOnData()
67 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070068 return m_onData;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080069 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080071 /**
72 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080073 * @return true if this interest timed out, otherwise false.
74 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080077 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 return now >= m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080079 }
80
81 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082 * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080083 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084 void
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080085 callTimeout()
86 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 if (m_onTimeout) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070088 m_onTimeout(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080089 }
90 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080092private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070093 shared_ptr<const Interest> m_interest;
94 const OnData m_onData;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 const OnTimeout m_onTimeout;
96 time::steady_clock::TimePoint m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080097};
98
99
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700100class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800101
102/**
103 * @brief Functor to match pending interests against PendingInterestId
104 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700105class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800106{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700107public:
108 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
110 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800111 {
112 }
113
114 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800116 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800118 }
119private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800121};
122
123
124} // namespace ndn
125
126#endif // NDN_DETAIL_PENDING_INTEREST_HPP