blob: 154112c57e1873b1c2ccd3106dec9cf69a4a3408 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib6828912017-11-20 14:06:32 +00002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -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 Afanasyevee8bb1e2014-05-02 17:39:54 -070020 */
21
22#ifndef NDN_DETAIL_INTEREST_FILTER_RECORD_HPP
23#define NDN_DETAIL_INTEREST_FILTER_RECORD_HPP
24
Junxiao Shib6828912017-11-20 14:06:32 +000025#include "pending-interest.hpp"
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070026
27namespace ndn {
28
Junxiao Shi103d8ed2016-08-07 20:34:10 +000029/**
30 * @brief associates an InterestFilter with Interest callback
31 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070032class InterestFilterRecord : noncopyable
33{
34public:
Junxiao Shi103d8ed2016-08-07 20:34:10 +000035 /**
36 * @brief Construct an Interest filter record
37 *
38 * @param filter an InterestFilter that represents what Interest should invoke the callback
39 * @param interestCallback invoked when matching Interest is received
40 */
41 InterestFilterRecord(const InterestFilter& filter,
42 const InterestCallback& interestCallback)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070043 : m_filter(filter)
Junxiao Shi103d8ed2016-08-07 20:34:10 +000044 , m_interestCallback(interestCallback)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070045 {
46 }
47
48 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000049 * @return the filter
50 */
51 const InterestFilter&
52 getFilter() const
53 {
54 return m_filter;
55 }
56
57 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070058 * @brief Check if Interest name matches the filter
59 * @param name Interest Name
60 */
61 bool
Junxiao Shib6828912017-11-20 14:06:32 +000062 doesMatch(const PendingInterest& entry) const
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070063 {
Junxiao Shib6828912017-11-20 14:06:32 +000064 return (entry.getOrigin() == PendingInterestOrigin::FORWARDER || m_filter.allowsLoopback()) &&
65 m_filter.doesMatch(entry.getInterest()->getName());
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070066 }
67
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080068 /**
69 * @brief invokes the InterestCallback
Junxiao Shi76e0eb22016-08-08 05:54:10 +000070 * @note This method does nothing if the Interest callback is empty
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080071 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070072 void
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080073 invokeInterestCallback(const Interest& interest) const
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070074 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +000075 if (m_interestCallback != nullptr) {
76 m_interestCallback(m_filter, interest);
77 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070078 }
79
80private:
81 InterestFilter m_filter;
Junxiao Shi103d8ed2016-08-07 20:34:10 +000082 InterestCallback m_interestCallback;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070083};
84
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070085/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000086 * @brief Opaque type to identify an InterestFilterRecord
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070087 */
88class InterestFilterId;
89
90/**
91 * @brief Functor to match InterestFilterId
92 */
93class MatchInterestFilterId
94{
95public:
96 explicit
97 MatchInterestFilterId(const InterestFilterId* interestFilterId)
98 : m_id(interestFilterId)
99 {
100 }
101
102 bool
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700103 operator()(const shared_ptr<InterestFilterRecord>& interestFilterId) const
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700104 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000105 return reinterpret_cast<const InterestFilterId*>(interestFilterId.get()) == m_id;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700106 }
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000107
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700108private:
109 const InterestFilterId* m_id;
110};
111
112} // namespace ndn
113
114#endif // NDN_DETAIL_INTEREST_FILTER_RECORD_HPP