blob: 5eb088aded386b5bbdf0878ac25188b42de94efb [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib6828912017-11-20 14:06:32 +00002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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:
Junxiao Shi68b53852018-07-25 13:56:38 -060041 using std::runtime_error::runtime_error;
Alexander Afanasyev90164962014-03-06 08:29:59 +000042 };
43
44 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000045 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000046 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000047 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000048 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000049 * @note InterestFilter is implicitly convertible from Name.
Alexander Afanasyev90164962014-03-06 08:29:59 +000050 */
51 InterestFilter(const Name& prefix);
52
53 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000054 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000055 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000056 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000057 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000058 * @param prefixUri name prefix, interpreted as ndn URI
59 * @note InterestFilter is implicitly convertible from null-terminated byte string.
Alexander Afanasyev90164962014-03-06 08:29:59 +000060 */
61 InterestFilter(const char* prefixUri);
62
63 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000064 * @brief Construct an InterestFilter to match Interests by prefix
Alexander Afanasyev90164962014-03-06 08:29:59 +000065 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000066 * This filter matches Interests whose name start with the given prefix.
Alexander Afanasyev90164962014-03-06 08:29:59 +000067 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000068 * @param prefixUri name prefix, interpreted as ndn URI
69 * @note InterestFilter is implicitly convertible from std::string.
Alexander Afanasyev90164962014-03-06 08:29:59 +000070 */
71 InterestFilter(const std::string& prefixUri);
72
73 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000074 * @brief Construct an InterestFilter to match Interests by prefix and regular expression
Alexander Afanasyev90164962014-03-06 08:29:59 +000075 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000076 * This filter matches Interests whose name start with the given prefix and
77 * the remaining components match the given regular expression.
Alexander Afanasyev90164962014-03-06 08:29:59 +000078 * For example, the following InterestFilter:
79 *
80 * InterestFilter("/hello", "<world><>+")
81 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000082 * matches Interests whose name has prefix `/hello` followed by component `world`
83 * and has at least one more component after it, such as:
Alexander Afanasyev90164962014-03-06 08:29:59 +000084 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000085 * /hello/world/%21
Alexander Afanasyev90164962014-03-06 08:29:59 +000086 * /hello/world/x/y/z
87 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +000088 * 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 Afanasyev90164962014-03-06 08:29:59 +000090 */
91 InterestFilter(const Name& prefix, const std::string& regexFilter);
92
93 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000094 * @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 Afanasyev90164962014-03-06 08:29:59 +000097 */
Junxiao Shi103d8ed2016-08-07 20:34:10 +000098 operator const Name&() const;
Alexander Afanasyev90164962014-03-06 08:29:59 +000099
100 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000101 * @brief Check if specified Interest name matches the filter
Alexander Afanasyev90164962014-03-06 08:29:59 +0000102 */
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 Shi103d8ed2016-08-07 20:34:10 +0000115 return m_regexFilter != nullptr;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000116 }
117
118 const RegexPatternListMatcher&
119 getRegexFilter() const
120 {
121 return *m_regexFilter;
122 }
123
Junxiao Shib6828912017-11-20 14:06:32 +0000124 /** \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 Afanasyev90164962014-03-06 08:29:59 +0000144private:
145 Name m_prefix;
146 shared_ptr<RegexPatternListMatcher> m_regexFilter;
Junxiao Shib6828912017-11-20 14:06:32 +0000147 bool m_allowsLoopback = true;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000148};
149
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700150std::ostream&
151operator<<(std::ostream& os, const InterestFilter& filter);
152
Alexander Afanasyev90164962014-03-06 08:29:59 +0000153} // namespace ndn
154
155#endif // NDN_INTEREST_FILTER_HPP