blob: 202ab91fc68b9011b6de094c8149a0586af36c95 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08006 *
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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "exclude.hpp"
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080025
Junxiao Shi7284a402014-09-12 13:42:16 -070026#include <boost/static_assert.hpp>
27
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -070028namespace ndn {
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080029
Junxiao Shi7284a402014-09-12 13:42:16 -070030BOOST_STATIC_ASSERT((boost::is_base_of<tlv::Error, Exclude::Error>::value));
31
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070032Exclude::Exclude()
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080033{
34}
35
Junxiao Shi75203022014-09-11 10:01:50 -070036Exclude::Exclude(const Block& wire)
37{
38 wireDecode(wire);
39}
40
41template<bool T>
42size_t
43Exclude::wireEncode(EncodingImpl<T>& block) const
44{
Junxiao Shi7284a402014-09-12 13:42:16 -070045 if (m_exclude.empty()) {
46 throw Error("Exclude filter cannot be empty");
47 }
48
Junxiao Shi75203022014-09-11 10:01:50 -070049 size_t totalLength = 0;
50
51 // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
52 // Any ::= ANY-TYPE TLV-LENGTH(=0)
53
54 for (Exclude::const_iterator i = m_exclude.begin(); i != m_exclude.end(); i++)
55 {
56 if (i->second)
57 {
58 totalLength += prependBooleanBlock(block, tlv::Any);
59 }
60 if (!i->first.empty())
61 {
62 totalLength += i->first.wireEncode(block);
63 }
64 }
65
66 totalLength += block.prependVarNumber(totalLength);
67 totalLength += block.prependVarNumber(tlv::Exclude);
68 return totalLength;
69}
70
71template size_t
72Exclude::wireEncode<true>(EncodingImpl<true>& block) const;
73
74template size_t
75Exclude::wireEncode<false>(EncodingImpl<false>& block) const;
76
77const Block&
78Exclude::wireEncode() const
79{
80 if (m_wire.hasWire())
81 return m_wire;
82
83 EncodingEstimator estimator;
84 size_t estimatedSize = wireEncode(estimator);
85
86 EncodingBuffer buffer(estimatedSize, 0);
87 wireEncode(buffer);
88
89 m_wire = buffer.block();
90 return m_wire;
91}
92
93void
94Exclude::wireDecode(const Block& wire)
95{
Junxiao Shi7284a402014-09-12 13:42:16 -070096 if (wire.type() != tlv::Exclude)
97 throw tlv::Error("Unexpected TLV type when decoding Exclude");
98
Junxiao Shi75203022014-09-11 10:01:50 -070099 m_wire = wire;
100 m_wire.parse();
101
Junxiao Shi7284a402014-09-12 13:42:16 -0700102 if (m_wire.elements_size() == 0) {
103 throw Error("Exclude element cannot be empty");
104 }
105
Junxiao Shi75203022014-09-11 10:01:50 -0700106 // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
107 // Any ::= ANY-TYPE TLV-LENGTH(=0)
108
109 Block::element_const_iterator i = m_wire.elements_begin();
110 if (i->type() == tlv::Any)
111 {
112 appendExclude(name::Component(), true);
113 ++i;
114 }
115
116 while (i != m_wire.elements_end())
117 {
118 if (i->type() != tlv::NameComponent)
119 throw Error("Incorrect format of Exclude filter");
120
121 name::Component excludedComponent(i->value(), i->value_size());
122 ++i;
123
124 if (i != m_wire.elements_end())
125 {
126 if (i->type() == tlv::Any)
127 {
128 appendExclude(excludedComponent, true);
129 ++i;
130 }
131 else
132 {
133 appendExclude(excludedComponent, false);
134 }
135 }
136 else
137 {
138 appendExclude(excludedComponent, false);
139 }
140 }
141}
142
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800143// example: ANY /b /d ANY /f
144//
145// ordered in map as:
146//
147// /f (false); /d (true); /b (false); / (true)
148//
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700149// lower_bound(/) -> / (true) <-- excluded (equal)
150// lower_bound(/a) -> / (true) <-- excluded (any)
151// lower_bound(/b) -> /b (false) <--- excluded (equal)
152// lower_bound(/c) -> /b (false) <--- not excluded (not equal and no ANY)
153// lower_bound(/d) -> /d (true) <- excluded
154// lower_bound(/e) -> /d (true) <- excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800155bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700156Exclude::isExcluded(const name::Component& comp) const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800157{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700158 const_iterator lowerBound = m_exclude.lower_bound(comp);
159 if (lowerBound == end())
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800160 return false;
161
162 if (lowerBound->second)
163 return true;
164 else
165 return lowerBound->first == comp;
166
167 return false;
168}
169
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700170Exclude&
171Exclude::excludeOne(const name::Component& comp)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800172{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700173 if (!isExcluded(comp))
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800174 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700175 m_exclude.insert(std::make_pair(comp, false));
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800176 }
177 return *this;
178}
179
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800180// example: ANY /b0 /d0 ANY /f0
181//
182// ordered in map as:
183//
184// /f0 (false); /d0 (true); /b0 (false); / (true)
185//
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700186// lower_bound(/) -> / (true) <-- excluded (equal)
187// lower_bound(/a0) -> / (true) <-- excluded (any)
188// lower_bound(/b0) -> /b0 (false) <--- excluded (equal)
189// lower_bound(/c0) -> /b0 (false) <--- not excluded (not equal and no ANY)
190// lower_bound(/d0) -> /d0 (true) <- excluded
191// lower_bound(/e0) -> /d0 (true) <- excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800192
193
194// examples with desired outcomes
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700195// excludeRange(/, /f0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800196// /f0 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700197// excludeRange(/, /f1) -> ANY /f1
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800198// /f1 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700199// excludeRange(/a0, /e0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800200// /f0 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700201// excludeRange(/a0, /e0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800202// /f0 (false); / (true)
203
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700204// excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800205// /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true)
206
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700207Exclude&
208Exclude::excludeRange(const name::Component& from, const name::Component& to)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800209{
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700210 if (from >= to) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700211 throw Error("Invalid exclude range [" + from.toUri() + ", " + to.toUri() + "] "
212 "(for single name exclude use Exclude::excludeOne)");
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700213 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800214
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700215 iterator newFrom = m_exclude.lower_bound(from);
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700216 if (newFrom == end() || !newFrom->second /*without ANY*/) {
217 std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true));
218 newFrom = fromResult.first;
219 if (!fromResult.second) {
220 // this means that the lower bound is equal to the item itself. So, just update ANY flag
221 newFrom->second = true;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800222 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700223 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800224 // else
225 // nothing special if start of the range already exists with ANY flag set
226
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700227 iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end()
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700228 if (newTo == newFrom || !newTo->second) {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700229 std::pair<iterator, bool> toResult = m_exclude.insert(std::make_pair(to, false));
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800230 newTo = toResult.first;
231 ++ newTo;
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700232 }
233 // else
234 // nothing to do really
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800235
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700236 m_exclude.erase(newTo, newFrom); // remove any intermediate node, since all of the are excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800237
238 return *this;
239}
240
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700241Exclude&
242Exclude::excludeAfter(const name::Component& from)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800243{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700244 iterator newFrom = m_exclude.lower_bound(from);
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700245 if (newFrom == end() || !newFrom->second /*without ANY*/) {
246 std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true));
247 newFrom = fromResult.first;
248 if (!fromResult.second) {
249 // this means that the lower bound is equal to the item itself. So, just update ANY flag
250 newFrom->second = true;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800251 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700252 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800253 // else
254 // nothing special if start of the range already exists with ANY flag set
255
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700256 if (newFrom != m_exclude.begin()) {
257 // remove any intermediate node, since all of the are excluded
258 m_exclude.erase(m_exclude.begin(), newFrom);
259 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800260
261 return *this;
262}
263
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800264std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700265operator<<(std::ostream& os, const Exclude& exclude)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800266{
267 bool empty = true;
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700268 for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) {
269 if (!i->first.empty()) {
270 if (!empty) os << ",";
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700271 os << i->first.toUri();
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700272 empty = false;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800273 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700274 if (i->second) {
275 if (!empty) os << ",";
276 os << "*";
277 empty = false;
278 }
279 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800280 return os;
281}
282
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700283} // namespace ndn