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 | 9016496 | 2014-03-06 08:29:59 +0000 | [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 | 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: |
| 41 | explicit |
| 42 | Error(const std::string& what) |
| 43 | : std::runtime_error(what) |
| 44 | { |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 49 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 50 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 51 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 52 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 53 | * @note InterestFilter is implicitly convertible from Name. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 54 | */ |
| 55 | InterestFilter(const Name& prefix); |
| 56 | |
| 57 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 58 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 59 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 60 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 61 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 62 | * @param prefixUri name prefix, interpreted as ndn URI |
| 63 | * @note InterestFilter is implicitly convertible from null-terminated byte string. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 64 | */ |
| 65 | InterestFilter(const char* prefixUri); |
| 66 | |
| 67 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 68 | * @brief Construct an InterestFilter to match Interests by prefix |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 69 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 70 | * This filter matches Interests whose name start with the given prefix. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 71 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 72 | * @param prefixUri name prefix, interpreted as ndn URI |
| 73 | * @note InterestFilter is implicitly convertible from std::string. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 74 | */ |
| 75 | InterestFilter(const std::string& prefixUri); |
| 76 | |
| 77 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 78 | * @brief Construct an InterestFilter to match Interests by prefix and regular expression |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 79 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 80 | * This filter matches Interests whose name start with the given prefix and |
| 81 | * the remaining components match the given regular expression. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 82 | * For example, the following InterestFilter: |
| 83 | * |
| 84 | * InterestFilter("/hello", "<world><>+") |
| 85 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 86 | * matches Interests whose name has prefix `/hello` followed by component `world` |
| 87 | * and has at least one more component after it, such as: |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 88 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 89 | * /hello/world/%21 |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 90 | * /hello/world/x/y/z |
| 91 | * |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 92 | * Note that regular expression will need to match all components (e.g., there are |
| 93 | * implicit heading `^` and trailing `$` symbols in the regular expression). |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 94 | */ |
| 95 | InterestFilter(const Name& prefix, const std::string& regexFilter); |
| 96 | |
| 97 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 98 | * @brief Implicit conversion to Name |
| 99 | * @note This allows InterestCallback to be declared with `Name` rather than `InterestFilter`, |
| 100 | * but this does not work if InterestFilter has regular expression. |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 101 | */ |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 102 | operator const Name&() const; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 105 | * @brief Check if specified Interest name matches the filter |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 106 | */ |
| 107 | bool |
| 108 | doesMatch(const Name& name) const; |
| 109 | |
| 110 | const Name& |
| 111 | getPrefix() const |
| 112 | { |
| 113 | return m_prefix; |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | hasRegexFilter() const |
| 118 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame^] | 119 | return m_regexFilter != nullptr; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | const RegexPatternListMatcher& |
| 123 | getRegexFilter() const |
| 124 | { |
| 125 | return *m_regexFilter; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | Name m_prefix; |
| 130 | shared_ptr<RegexPatternListMatcher> m_regexFilter; |
| 131 | }; |
| 132 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 133 | std::ostream& |
| 134 | operator<<(std::ostream& os, const InterestFilter& filter); |
| 135 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 136 | } // namespace ndn |
| 137 | |
| 138 | #endif // NDN_INTEREST_FILTER_HPP |