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