Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_DETAIL_INTEREST_FILTER_RECORD_HPP |
| 23 | #define NDN_DETAIL_INTEREST_FILTER_RECORD_HPP |
| 24 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 25 | #include "../name.hpp" |
| 26 | #include "../interest.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 30 | /** |
| 31 | * @brief associates an InterestFilter with Interest callback |
| 32 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 33 | class InterestFilterRecord : noncopyable |
| 34 | { |
| 35 | public: |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 36 | /** |
| 37 | * @brief Construct an Interest filter record |
| 38 | * |
| 39 | * @param filter an InterestFilter that represents what Interest should invoke the callback |
| 40 | * @param interestCallback invoked when matching Interest is received |
| 41 | */ |
| 42 | InterestFilterRecord(const InterestFilter& filter, |
| 43 | const InterestCallback& interestCallback) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 44 | : m_filter(filter) |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 45 | , m_interestCallback(interestCallback) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 50 | * @return the filter |
| 51 | */ |
| 52 | const InterestFilter& |
| 53 | getFilter() const |
| 54 | { |
| 55 | return m_filter; |
| 56 | } |
| 57 | |
| 58 | /** |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 59 | * @brief Check if Interest name matches the filter |
| 60 | * @param name Interest Name |
| 61 | */ |
| 62 | bool |
| 63 | doesMatch(const Name& name) const |
| 64 | { |
| 65 | return m_filter.doesMatch(name); |
| 66 | } |
| 67 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 68 | /** |
| 69 | * @brief invokes the InterestCallback |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 70 | * @note This method does nothing if the Interest callback is empty |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 71 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 72 | void |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 73 | invokeInterestCallback(const Interest& interest) const |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 74 | { |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 75 | if (m_interestCallback != nullptr) { |
| 76 | m_interestCallback(m_filter, interest); |
| 77 | } |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
| 81 | InterestFilter m_filter; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 82 | InterestCallback m_interestCallback; |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 85 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 86 | * @brief Opaque type to identify an InterestFilterRecord |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 87 | */ |
| 88 | class InterestFilterId; |
| 89 | |
| 90 | /** |
| 91 | * @brief Functor to match InterestFilterId |
| 92 | */ |
| 93 | class MatchInterestFilterId |
| 94 | { |
| 95 | public: |
| 96 | explicit |
| 97 | MatchInterestFilterId(const InterestFilterId* interestFilterId) |
| 98 | : m_id(interestFilterId) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | bool |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 103 | operator()(const shared_ptr<InterestFilterRecord>& interestFilterId) const |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 104 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 105 | return reinterpret_cast<const InterestFilterId*>(interestFilterId.get()) == m_id; |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 106 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 107 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 108 | private: |
| 109 | const InterestFilterId* m_id; |
| 110 | }; |
| 111 | |
| 112 | } // namespace ndn |
| 113 | |
| 114 | #endif // NDN_DETAIL_INTEREST_FILTER_RECORD_HPP |