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> |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 27 | #include <boost/type_traits.hpp> |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 31 | BOOST_STATIC_ASSERT((boost::is_base_of<tlv::Error, Exclude::Error>::value)); |
| 32 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 33 | Exclude::Exclude() |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 37 | Exclude::Exclude(const Block& wire) |
| 38 | { |
| 39 | wireDecode(wire); |
| 40 | } |
| 41 | |
| 42 | template<bool T> |
| 43 | size_t |
| 44 | Exclude::wireEncode(EncodingImpl<T>& block) const |
| 45 | { |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 46 | if (m_exclude.empty()) { |
| 47 | throw Error("Exclude filter cannot be empty"); |
| 48 | } |
| 49 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 50 | size_t totalLength = 0; |
| 51 | |
| 52 | // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+ |
| 53 | // Any ::= ANY-TYPE TLV-LENGTH(=0) |
| 54 | |
| 55 | for (Exclude::const_iterator i = m_exclude.begin(); i != m_exclude.end(); i++) |
| 56 | { |
| 57 | if (i->second) |
| 58 | { |
| 59 | totalLength += prependBooleanBlock(block, tlv::Any); |
| 60 | } |
| 61 | if (!i->first.empty()) |
| 62 | { |
| 63 | totalLength += i->first.wireEncode(block); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | totalLength += block.prependVarNumber(totalLength); |
| 68 | totalLength += block.prependVarNumber(tlv::Exclude); |
| 69 | return totalLength; |
| 70 | } |
| 71 | |
| 72 | template size_t |
| 73 | Exclude::wireEncode<true>(EncodingImpl<true>& block) const; |
| 74 | |
| 75 | template size_t |
| 76 | Exclude::wireEncode<false>(EncodingImpl<false>& block) const; |
| 77 | |
| 78 | const Block& |
| 79 | Exclude::wireEncode() const |
| 80 | { |
| 81 | if (m_wire.hasWire()) |
| 82 | return m_wire; |
| 83 | |
| 84 | EncodingEstimator estimator; |
| 85 | size_t estimatedSize = wireEncode(estimator); |
| 86 | |
| 87 | EncodingBuffer buffer(estimatedSize, 0); |
| 88 | wireEncode(buffer); |
| 89 | |
| 90 | m_wire = buffer.block(); |
| 91 | return m_wire; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Exclude::wireDecode(const Block& wire) |
| 96 | { |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 97 | clear(); |
| 98 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 99 | if (wire.type() != tlv::Exclude) |
| 100 | throw tlv::Error("Unexpected TLV type when decoding Exclude"); |
| 101 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 102 | m_wire = wire; |
| 103 | m_wire.parse(); |
| 104 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 105 | if (m_wire.elements_size() == 0) { |
| 106 | throw Error("Exclude element cannot be empty"); |
| 107 | } |
| 108 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 109 | // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+ |
| 110 | // Any ::= ANY-TYPE TLV-LENGTH(=0) |
| 111 | |
| 112 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 113 | if (i->type() == tlv::Any) |
| 114 | { |
| 115 | appendExclude(name::Component(), true); |
| 116 | ++i; |
| 117 | } |
| 118 | |
| 119 | while (i != m_wire.elements_end()) |
| 120 | { |
| 121 | if (i->type() != tlv::NameComponent) |
| 122 | throw Error("Incorrect format of Exclude filter"); |
| 123 | |
| 124 | name::Component excludedComponent(i->value(), i->value_size()); |
| 125 | ++i; |
| 126 | |
| 127 | if (i != m_wire.elements_end()) |
| 128 | { |
| 129 | if (i->type() == tlv::Any) |
| 130 | { |
| 131 | appendExclude(excludedComponent, true); |
| 132 | ++i; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | appendExclude(excludedComponent, false); |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | appendExclude(excludedComponent, false); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 146 | // example: ANY /b /d ANY /f |
| 147 | // |
| 148 | // ordered in map as: |
| 149 | // |
| 150 | // /f (false); /d (true); /b (false); / (true) |
| 151 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 152 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 153 | // lower_bound(/a) -> / (true) <-- excluded (any) |
| 154 | // lower_bound(/b) -> /b (false) <--- excluded (equal) |
| 155 | // lower_bound(/c) -> /b (false) <--- not excluded (not equal and no ANY) |
| 156 | // lower_bound(/d) -> /d (true) <- excluded |
| 157 | // lower_bound(/e) -> /d (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 158 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 159 | Exclude::isExcluded(const name::Component& comp) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 161 | const_iterator lowerBound = m_exclude.lower_bound(comp); |
| 162 | if (lowerBound == end()) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 163 | return false; |
| 164 | |
| 165 | if (lowerBound->second) |
| 166 | return true; |
| 167 | else |
| 168 | return lowerBound->first == comp; |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 173 | Exclude& |
| 174 | Exclude::excludeOne(const name::Component& comp) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 175 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 176 | if (!isExcluded(comp)) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 177 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 178 | m_exclude.insert(std::make_pair(comp, false)); |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 179 | m_wire.reset(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 180 | } |
| 181 | return *this; |
| 182 | } |
| 183 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 184 | // example: ANY /b0 /d0 ANY /f0 |
| 185 | // |
| 186 | // ordered in map as: |
| 187 | // |
| 188 | // /f0 (false); /d0 (true); /b0 (false); / (true) |
| 189 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 190 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 191 | // lower_bound(/a0) -> / (true) <-- excluded (any) |
| 192 | // lower_bound(/b0) -> /b0 (false) <--- excluded (equal) |
| 193 | // lower_bound(/c0) -> /b0 (false) <--- not excluded (not equal and no ANY) |
| 194 | // lower_bound(/d0) -> /d0 (true) <- excluded |
| 195 | // lower_bound(/e0) -> /d0 (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 196 | |
| 197 | |
| 198 | // examples with desired outcomes |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 199 | // excludeRange(/, /f0) -> 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(/, /f1) -> ANY /f1 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 202 | // /f1 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 203 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 204 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 205 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 206 | // /f0 (false); / (true) |
| 207 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 208 | // excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 209 | // /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true) |
| 210 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 211 | Exclude& |
| 212 | Exclude::excludeRange(const name::Component& from, const name::Component& to) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 213 | { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 214 | if (from >= to) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 215 | throw Error("Invalid exclude range [" + from.toUri() + ", " + to.toUri() + "] " |
| 216 | "(for single name exclude use Exclude::excludeOne)"); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 217 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 219 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 220 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 221 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 222 | newFrom = fromResult.first; |
| 223 | if (!fromResult.second) { |
| 224 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 225 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 226 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 227 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 228 | // else |
| 229 | // nothing special if start of the range already exists with ANY flag set |
| 230 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 231 | iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end() |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 232 | if (newTo == newFrom || !newTo->second) { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 233 | 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] | 234 | newTo = toResult.first; |
| 235 | ++ newTo; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 236 | } |
| 237 | // else |
| 238 | // nothing to do really |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 239 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 240 | 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] | 241 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 242 | m_wire.reset(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 243 | return *this; |
| 244 | } |
| 245 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 246 | Exclude& |
| 247 | Exclude::excludeAfter(const name::Component& from) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 248 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 249 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 250 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 251 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 252 | newFrom = fromResult.first; |
| 253 | if (!fromResult.second) { |
| 254 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 255 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 256 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 257 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 258 | // else |
| 259 | // nothing special if start of the range already exists with ANY flag set |
| 260 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 261 | if (newFrom != m_exclude.begin()) { |
| 262 | // remove any intermediate node, since all of the are excluded |
| 263 | m_exclude.erase(m_exclude.begin(), newFrom); |
| 264 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 265 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 266 | m_wire.reset(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 267 | return *this; |
| 268 | } |
| 269 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 270 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 271 | operator<<(std::ostream& os, const Exclude& exclude) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 272 | { |
| 273 | bool empty = true; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 274 | for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) { |
| 275 | if (!i->first.empty()) { |
| 276 | if (!empty) os << ","; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 277 | os << i->first.toUri(); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 278 | empty = false; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 279 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 280 | if (i->second) { |
| 281 | if (!empty) os << ","; |
| 282 | os << "*"; |
| 283 | empty = false; |
| 284 | } |
| 285 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 286 | return os; |
| 287 | } |
| 288 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame^] | 289 | std::string |
| 290 | Exclude::toUri() const |
| 291 | { |
| 292 | std::ostringstream os; |
| 293 | os << *this; |
| 294 | return os.str(); |
| 295 | } |
| 296 | |
| 297 | bool |
| 298 | Exclude::operator==(const Exclude& other) const |
| 299 | { |
| 300 | if (empty() && other.empty()) |
| 301 | return true; |
| 302 | if (empty() || other.empty()) |
| 303 | return false; |
| 304 | |
| 305 | return wireEncode() == other.wireEncode(); |
| 306 | } |
| 307 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 308 | } // namespace ndn |