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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 31 | class InterestFilter |
| 32 | { |
| 33 | public: |
| 34 | class Error : public std::runtime_error |
| 35 | { |
| 36 | public: |
| 37 | explicit |
| 38 | Error(const std::string& what) |
| 39 | : std::runtime_error(what) |
| 40 | { |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * @brief Create an Interest filter to match Interests by prefix |
| 46 | * |
| 47 | * This filter will match all Interests, whose name start with the given prefix |
| 48 | * |
| 49 | * Any Name can be implicitly converted to the InterestFilter. |
| 50 | */ |
| 51 | InterestFilter(const Name& prefix); |
| 52 | |
| 53 | /** |
| 54 | * @brief Create an Interest filter to match Interests by prefix URI |
| 55 | * |
| 56 | * This filter will match all Interests, whose name start with the given prefix |
| 57 | * |
| 58 | * Any const char* can be implicitly converted to the InterestFilter. |
| 59 | */ |
| 60 | InterestFilter(const char* prefixUri); |
| 61 | |
| 62 | /** |
| 63 | * @brief Create an Interest filter to match Interests by prefix URI |
| 64 | * |
| 65 | * This filter will match all Interests, whose name start with the given prefix |
| 66 | * |
| 67 | * Any std::string can be implicitly converted to the InterestFilter. |
| 68 | */ |
| 69 | InterestFilter(const std::string& prefixUri); |
| 70 | |
| 71 | /** |
| 72 | * @brief Create an Interest filter to match Interest by prefix and regular expression |
| 73 | * |
| 74 | * This filter will match all Interests, whose name start with the given prefix and |
| 75 | * other components of the Interest name match the given regular expression. |
| 76 | * For example, the following InterestFilter: |
| 77 | * |
| 78 | * InterestFilter("/hello", "<world><>+") |
| 79 | * |
| 80 | * will match all Interests, whose name has prefix `/hello`, which is followed by |
| 81 | * component `world` and has at least one more component after it. Examples: |
| 82 | * |
| 83 | * /hello/world/! |
| 84 | * /hello/world/x/y/z |
| 85 | * |
| 86 | * Note that regular expression will need to match all components (e.g., there is |
| 87 | * an implicit heading `^` and trailing `$` symbols in the regular expression). |
| 88 | */ |
| 89 | InterestFilter(const Name& prefix, const std::string& regexFilter); |
| 90 | |
| 91 | /** |
| 92 | * @brief Implicit conversion to Name (to provide backwards compatibility for onInterest callback) |
| 93 | */ |
| 94 | operator const Name&() const |
| 95 | { |
| 96 | if (static_cast<bool>(m_regexFilter)) { |
| 97 | throw Error("Please update OnInterest callback to accept const InterestFilter& " |
| 98 | "(non-trivial Interest filter is being used)"); |
| 99 | } |
| 100 | return m_prefix; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @brief Check if specified name matches the filter |
| 105 | */ |
| 106 | bool |
| 107 | doesMatch(const Name& name) const; |
| 108 | |
| 109 | const Name& |
| 110 | getPrefix() const |
| 111 | { |
| 112 | return m_prefix; |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | hasRegexFilter() const |
| 117 | { |
| 118 | return static_cast<bool>(m_regexFilter); |
| 119 | } |
| 120 | |
| 121 | const RegexPatternListMatcher& |
| 122 | getRegexFilter() const |
| 123 | { |
| 124 | return *m_regexFilter; |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | Name m_prefix; |
| 129 | shared_ptr<RegexPatternListMatcher> m_regexFilter; |
| 130 | }; |
| 131 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 132 | std::ostream& |
| 133 | operator<<(std::ostream& os, const InterestFilter& filter); |
| 134 | |
| 135 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 136 | inline |
| 137 | InterestFilter::InterestFilter(const Name& prefix) |
| 138 | : m_prefix(prefix) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | inline |
| 143 | InterestFilter::InterestFilter(const char* prefixUri) |
| 144 | : m_prefix(prefixUri) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | inline |
| 149 | InterestFilter::InterestFilter(const std::string& prefixUri) |
| 150 | : m_prefix(prefixUri) |
| 151 | { |
| 152 | } |
| 153 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 154 | } // namespace ndn |
| 155 | |
| 156 | #endif // NDN_INTEREST_FILTER_HPP |