blob: d6241a147f91327eaa9e836708371425c1287627 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev90164962014-03-06 08:29:59 +00002/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyev90164962014-03-06 08:29:59 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev90164962014-03-06 08:29:59 +00006 *
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 Afanasyev90164962014-03-06 08:29:59 +000020 */
21
22#ifndef NDN_INTEREST_FILTER_HPP
23#define NDN_INTEREST_FILTER_HPP
24
Alexander Afanasyev90164962014-03-06 08:29:59 +000025#include "name.hpp"
26
27namespace ndn {
28
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029class RegexPatternListMatcher;
30
Junxiao Shi103d8ed2016-08-07 20:34:10 +000031/**
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 Afanasyev90164962014-03-06 08:29:59 +000035class InterestFilter
36{
37public:
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 Shi103d8ed2016-08-07 20:34:10 +000049 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000050 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000051 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000052 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000053 * @note InterestFilter is implicitly convertible from Name.
Alexander Afanasyev90164962014-03-06 08:29:59 +000054 */
55 InterestFilter(const Name& prefix);
56
57 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000058 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000059 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000060 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000061 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000062 * @param prefixUri name prefix, interpreted as ndn URI
63 * @note InterestFilter is implicitly convertible from null-terminated byte string.
Alexander Afanasyev90164962014-03-06 08:29:59 +000064 */
65 InterestFilter(const char* prefixUri);
66
67 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000068 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000069 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000070 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000071 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000072 * @param prefixUri name prefix, interpreted as ndn URI
73 * @note InterestFilter is implicitly convertible from std::string.
Alexander Afanasyev90164962014-03-06 08:29:59 +000074 */
75 InterestFilter(const std::string& prefixUri);
76
77 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000078 * @brief Construct an InterestFilter to match Interests by prefix and regular expression
Alexander Afanasyev90164962014-03-06 08:29:59 +000079 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000080 * This filter matches Interests whose name start with the given prefix and
81 * the remaining components match the given regular expression.
Alexander Afanasyev90164962014-03-06 08:29:59 +000082 * For example, the following InterestFilter:
83 *
84 * InterestFilter("/hello", "<world><>+")
85 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000086 * matches Interests whose name has prefix `/hello` followed by component `world`
87 * and has at least one more component after it, such as:
Alexander Afanasyev90164962014-03-06 08:29:59 +000088 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000089 * /hello/world/%21
Alexander Afanasyev90164962014-03-06 08:29:59 +000090 * /hello/world/x/y/z
91 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092 * 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 Afanasyev90164962014-03-06 08:29:59 +000094 */
95 InterestFilter(const Name& prefix, const std::string& regexFilter);
96
97 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000098 * @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 Afanasyev90164962014-03-06 08:29:59 +0000101 */
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000102 operator const Name&() const;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000103
104 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000105 * @brief Check if specified Interest name matches the filter
Alexander Afanasyev90164962014-03-06 08:29:59 +0000106 */
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 Shi103d8ed2016-08-07 20:34:10 +0000119 return m_regexFilter != nullptr;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000120 }
121
122 const RegexPatternListMatcher&
123 getRegexFilter() const
124 {
125 return *m_regexFilter;
126 }
127
128private:
129 Name m_prefix;
130 shared_ptr<RegexPatternListMatcher> m_regexFilter;
131};
132
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700133std::ostream&
134operator<<(std::ostream& os, const InterestFilter& filter);
135
Alexander Afanasyev90164962014-03-06 08:29:59 +0000136} // namespace ndn
137
138#endif // NDN_INTEREST_FILTER_HPP