Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [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 | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_INTEREST_FILTER_HPP |
| 23 | #define NDN_INTEREST_FILTER_HPP |
| 24 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 25 | #include "name.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | class RegexPatternListMatcher; |
| 30 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 31 | /** |
| 32 | * @brief declares the set of Interests a producer can serve, |
| 33 | * which starts with a name prefix, plus an optional regular expression |
| 34 | */ |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 35 | class InterestFilter |
| 36 | { |
| 37 | public: |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 41 | using std::runtime_error::runtime_error; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 45 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 46 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 47 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 48 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 49 | * @note InterestFilter is implicitly convertible from Name. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 50 | */ |
| 51 | InterestFilter(const Name& prefix); |
| 52 | |
| 53 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 54 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 55 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 56 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 57 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 58 | * @param prefixUri name prefix, interpreted as ndn URI |
| 59 | * @note InterestFilter is implicitly convertible from null-terminated byte string. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 60 | */ |
| 61 | InterestFilter(const char* prefixUri); |
| 62 | |
| 63 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 64 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 65 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 66 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 67 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 68 | * @param prefixUri name prefix, interpreted as ndn URI |
| 69 | * @note InterestFilter is implicitly convertible from std::string. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 70 | */ |
| 71 | InterestFilter(const std::string& prefixUri); |
| 72 | |
| 73 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 74 | * @brief Construct an InterestFilter to match Interests by prefix and regular expression |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 75 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 76 | * This filter matches Interests whose name start with the given prefix and |
| 77 | * the remaining components match the given regular expression. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 78 | * For example, the following InterestFilter: |
| 79 | * |
| 80 | * InterestFilter("/hello", "<world><>+") |
| 81 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 82 | * matches Interests whose name has prefix `/hello` followed by component `world` |
| 83 | * and has at least one more component after it, such as: |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 84 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 85 | * /hello/world/%21 |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 86 | * /hello/world/x/y/z |
| 87 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 88 | * Note that regular expression will need to match all components (e.g., there are |
| 89 | * implicit heading `^` and trailing `$` symbols in the regular expression). |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 90 | */ |
| 91 | InterestFilter(const Name& prefix, const std::string& regexFilter); |
| 92 | |
| 93 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 94 | * @brief Implicit conversion to Name |
| 95 | * @note This allows InterestCallback to be declared with `Name` rather than `InterestFilter`, |
| 96 | * but this does not work if InterestFilter has regular expression. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 97 | */ |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 98 | operator const Name&() const; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 99 | |
| 100 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 101 | * @brief Check if specified Interest name matches the filter |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 102 | */ |
| 103 | bool |
| 104 | doesMatch(const Name& name) const; |
| 105 | |
| 106 | const Name& |
| 107 | getPrefix() const |
| 108 | { |
| 109 | return m_prefix; |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | hasRegexFilter() const |
| 114 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 115 | return m_regexFilter != nullptr; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const RegexPatternListMatcher& |
| 119 | getRegexFilter() const |
| 120 | { |
| 121 | return *m_regexFilter; |
| 122 | } |
| 123 | |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 124 | /** \brief Get whether Interest loopback is allowed |
| 125 | */ |
| 126 | bool |
| 127 | allowsLoopback() const |
| 128 | { |
| 129 | return m_allowsLoopback; |
| 130 | } |
| 131 | |
| 132 | /** \brief Set whether Interest loopback is allowed |
| 133 | * \param wantLoopback if true, this InterestFilter may receive Interests that are expressed |
| 134 | * locally on the same \p ndn::Face ; if false, this InterestFilter can only |
| 135 | * receive Interests received from the forwarder. The default is true. |
| 136 | */ |
| 137 | InterestFilter& |
| 138 | allowLoopback(bool wantLoopback) |
| 139 | { |
| 140 | m_allowsLoopback = wantLoopback; |
| 141 | return *this; |
| 142 | } |
| 143 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 144 | private: |
| 145 | Name m_prefix; |
| 146 | shared_ptr<RegexPatternListMatcher> m_regexFilter; |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 147 | bool m_allowsLoopback = true; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 150 | std::ostream& |
| 151 | operator<<(std::ostream& os, const InterestFilter& filter); |
| 152 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 153 | } // namespace ndn |
| 154 | |
| 155 | #endif // NDN_INTEREST_FILTER_HPP |