blob: f23db08149021ba9d6cf4e608081f2671d62b441 [file] [log] [blame]
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080011 */
12
13#ifndef NDN_DETAIL_PENDING_INTEREST_HPP
14#define NDN_DETAIL_PENDING_INTEREST_HPP
15
16#include "../common.hpp"
17#include "../interest.hpp"
18#include "../data.hpp"
19#include "../util/time.hpp"
20
21namespace ndn {
22
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070023class PendingInterest : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070024{
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080025public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026 typedef function<void(const Interest&, Data&)> OnData;
27 typedef function<void(const Interest&)> OnTimeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080028
29 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030 * @brief Create a new PitEntry and set the timeout based on the current time and
31 * the interest lifetime.
32 *
33 * @param interest A shared_ptr for the interest
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080034 * @param onData A function object to call when a matching data packet is received.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035 * @param onTimeout A function object to call if the interest times out.
36 * If onTimeout is an empty OnTimeout(), this does not use it.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080037 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 PendingInterest(const shared_ptr<const Interest>& interest, const OnData& onData,
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080039 const OnTimeout& onTimeout)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070040 : m_interest(interest)
41 , m_onData(onData)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042 , m_onTimeout(onTimeout)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080043 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070044 if (m_interest->getInterestLifetime() >= time::milliseconds::zero())
45 m_timeout = time::steady_clock::now() + m_interest->getInterestLifetime();
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080046 else
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 m_timeout = time::steady_clock::now() + DEFAULT_INTEREST_LIFETIME;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080048 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049
50 const shared_ptr<const Interest>&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080051 getInterest()
52 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070053 return m_interest;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080054 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055
56 const OnData&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080057 getOnData()
58 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070059 return m_onData;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080060 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080062 /**
63 * Check if this interest is timed out.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080064 * @return true if this interest timed out, otherwise false.
65 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 bool
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 isTimedOut(const time::steady_clock::TimePoint& now)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080068 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 return now >= m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080070 }
71
72 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073 * Call m_onTimeout (if defined). This ignores exceptions from the m_onTimeout.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080074 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 void
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080076 callTimeout()
77 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 if (m_onTimeout) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070079 m_onTimeout(*m_interest);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080080 }
81 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080083private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070084 shared_ptr<const Interest> m_interest;
85 const OnData m_onData;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 const OnTimeout m_onTimeout;
87 time::steady_clock::TimePoint m_timeout;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080088};
89
90
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070091class PendingInterestId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080092
93/**
94 * @brief Functor to match pending interests against PendingInterestId
95 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070096class MatchPendingInterestId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080097{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070098public:
99 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 MatchPendingInterestId(const PendingInterestId* pendingInterestId)
101 : m_id(pendingInterestId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800102 {
103 }
104
105 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800107 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800109 }
110private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111 const PendingInterestId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800112};
113
114
115} // namespace ndn
116
117#endif // NDN_DETAIL_PENDING_INTEREST_HPP