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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "exclude.hpp" |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 25 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame^] | 26 | #include <boost/static_assert.hpp> |
| 27 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame^] | 30 | BOOST_STATIC_ASSERT((boost::is_base_of<tlv::Error, Exclude::Error>::value)); |
| 31 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 32 | Exclude::Exclude() |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 36 | Exclude::Exclude(const Block& wire) |
| 37 | { |
| 38 | wireDecode(wire); |
| 39 | } |
| 40 | |
| 41 | template<bool T> |
| 42 | size_t |
| 43 | Exclude::wireEncode(EncodingImpl<T>& block) const |
| 44 | { |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame^] | 45 | if (m_exclude.empty()) { |
| 46 | throw Error("Exclude filter cannot be empty"); |
| 47 | } |
| 48 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 49 | 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 | |
| 71 | template size_t |
| 72 | Exclude::wireEncode<true>(EncodingImpl<true>& block) const; |
| 73 | |
| 74 | template size_t |
| 75 | Exclude::wireEncode<false>(EncodingImpl<false>& block) const; |
| 76 | |
| 77 | const Block& |
| 78 | Exclude::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 | |
| 93 | void |
| 94 | Exclude::wireDecode(const Block& wire) |
| 95 | { |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame^] | 96 | if (wire.type() != tlv::Exclude) |
| 97 | throw tlv::Error("Unexpected TLV type when decoding Exclude"); |
| 98 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 99 | m_wire = wire; |
| 100 | m_wire.parse(); |
| 101 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame^] | 102 | if (m_wire.elements_size() == 0) { |
| 103 | throw Error("Exclude element cannot be empty"); |
| 104 | } |
| 105 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 106 | // 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 143 | // example: ANY /b /d ANY /f |
| 144 | // |
| 145 | // ordered in map as: |
| 146 | // |
| 147 | // /f (false); /d (true); /b (false); / (true) |
| 148 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 149 | // 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 155 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 156 | Exclude::isExcluded(const name::Component& comp) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 157 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 158 | const_iterator lowerBound = m_exclude.lower_bound(comp); |
| 159 | if (lowerBound == end()) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 160 | return false; |
| 161 | |
| 162 | if (lowerBound->second) |
| 163 | return true; |
| 164 | else |
| 165 | return lowerBound->first == comp; |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 170 | Exclude& |
| 171 | Exclude::excludeOne(const name::Component& comp) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 172 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 173 | if (!isExcluded(comp)) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 174 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 175 | m_exclude.insert(std::make_pair(comp, false)); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 176 | } |
| 177 | return *this; |
| 178 | } |
| 179 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 180 | // example: ANY /b0 /d0 ANY /f0 |
| 181 | // |
| 182 | // ordered in map as: |
| 183 | // |
| 184 | // /f0 (false); /d0 (true); /b0 (false); / (true) |
| 185 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 186 | // 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 192 | |
| 193 | |
| 194 | // examples with desired outcomes |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 195 | // excludeRange(/, /f0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 196 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 197 | // excludeRange(/, /f1) -> ANY /f1 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 198 | // /f1 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 199 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 200 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 201 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 202 | // /f0 (false); / (true) |
| 203 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 204 | // excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 205 | // /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true) |
| 206 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 207 | Exclude& |
| 208 | Exclude::excludeRange(const name::Component& from, const name::Component& to) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 209 | { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 210 | if (from >= to) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 211 | throw Error("Invalid exclude range [" + from.toUri() + ", " + to.toUri() + "] " |
| 212 | "(for single name exclude use Exclude::excludeOne)"); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 213 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 214 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 215 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 216 | 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 222 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 223 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 224 | // else |
| 225 | // nothing special if start of the range already exists with ANY flag set |
| 226 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 227 | iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end() |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 228 | if (newTo == newFrom || !newTo->second) { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 229 | std::pair<iterator, bool> toResult = m_exclude.insert(std::make_pair(to, false)); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 230 | newTo = toResult.first; |
| 231 | ++ newTo; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 232 | } |
| 233 | // else |
| 234 | // nothing to do really |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 236 | m_exclude.erase(newTo, newFrom); // remove any intermediate node, since all of the are excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 237 | |
| 238 | return *this; |
| 239 | } |
| 240 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 241 | Exclude& |
| 242 | Exclude::excludeAfter(const name::Component& from) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 243 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 244 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 245 | 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 251 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 252 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 253 | // else |
| 254 | // nothing special if start of the range already exists with ANY flag set |
| 255 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 256 | 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 260 | |
| 261 | return *this; |
| 262 | } |
| 263 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 264 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 265 | operator<<(std::ostream& os, const Exclude& exclude) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 266 | { |
| 267 | bool empty = true; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 268 | for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) { |
| 269 | if (!i->first.empty()) { |
| 270 | if (!empty) os << ","; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 271 | os << i->first.toUri(); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 272 | empty = false; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 273 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 274 | if (i->second) { |
| 275 | if (!empty) os << ","; |
| 276 | os << "*"; |
| 277 | empty = false; |
| 278 | } |
| 279 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 280 | return os; |
| 281 | } |
| 282 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 283 | } // namespace ndn |