Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 24 | */ |
| 25 | |
| 26 | #include "name.hpp" |
| 27 | |
| 28 | #include "util/time.hpp" |
| 29 | #include "util/string-helper.hpp" |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 30 | #include "util/concepts.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 31 | #include "encoding/block.hpp" |
| 32 | #include "encoding/encoding-buffer.hpp" |
| 33 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame^] | 34 | #include <boost/functional/hash.hpp> |
| 35 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 36 | namespace ndn { |
| 37 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 38 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
| 39 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
| 40 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
| 41 | static_assert(std::is_base_of<tlv::Error, Name::Error>::value, |
| 42 | "Name::Error must inherit from tlv::Error"); |
| 43 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 44 | template<bool T> |
| 45 | size_t |
| 46 | Name::wireEncode(EncodingImpl<T>& encoder) const |
| 47 | { |
| 48 | size_t totalLength = 0; |
| 49 | |
| 50 | for (const_reverse_iterator i = rbegin(); i != rend(); ++i) |
| 51 | { |
| 52 | totalLength += i->wireEncode(encoder); |
| 53 | } |
| 54 | |
| 55 | totalLength += encoder.prependVarNumber(totalLength); |
| 56 | totalLength += encoder.prependVarNumber(tlv::Name); |
| 57 | return totalLength; |
| 58 | } |
| 59 | |
| 60 | template size_t |
| 61 | Name::wireEncode<true>(EncodingImpl<true>& estimator) const; |
| 62 | |
| 63 | template size_t |
| 64 | Name::wireEncode<false>(EncodingImpl<false>& encoder) const; |
| 65 | |
| 66 | const Block& |
| 67 | Name::wireEncode() const |
| 68 | { |
| 69 | if (m_nameBlock.hasWire()) |
| 70 | return m_nameBlock; |
| 71 | |
| 72 | EncodingEstimator estimator; |
| 73 | size_t estimatedSize = wireEncode(estimator); |
| 74 | |
| 75 | EncodingBuffer buffer(estimatedSize, 0); |
| 76 | wireEncode(buffer); |
| 77 | |
| 78 | m_nameBlock = buffer.block(); |
| 79 | m_nameBlock.parse(); |
| 80 | |
| 81 | return m_nameBlock; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | Name::wireDecode(const Block& wire) |
| 86 | { |
| 87 | if (wire.type() != tlv::Name) |
| 88 | throw tlv::Error("Unexpected TLV type when decoding Name"); |
| 89 | |
| 90 | m_nameBlock = wire; |
| 91 | m_nameBlock.parse(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Name::set(const char* uriOrig) |
| 96 | { |
| 97 | clear(); |
| 98 | |
| 99 | std::string uri = uriOrig; |
| 100 | trim(uri); |
| 101 | if (uri.size() == 0) |
| 102 | return; |
| 103 | |
| 104 | size_t iColon = uri.find(':'); |
| 105 | if (iColon != std::string::npos) { |
| 106 | // Make sure the colon came before a '/'. |
| 107 | size_t iFirstSlash = uri.find('/'); |
| 108 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 109 | // Omit the leading protocol such as ndn: |
| 110 | uri.erase(0, iColon + 1); |
| 111 | trim(uri); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Trim the leading slash and possibly the authority. |
| 116 | if (uri[0] == '/') { |
| 117 | if (uri.size() >= 2 && uri[1] == '/') { |
| 118 | // Strip the authority following "//". |
| 119 | size_t iAfterAuthority = uri.find('/', 2); |
| 120 | if (iAfterAuthority == std::string::npos) |
| 121 | // Unusual case: there was only an authority. |
| 122 | return; |
| 123 | else { |
| 124 | uri.erase(0, iAfterAuthority + 1); |
| 125 | trim(uri); |
| 126 | } |
| 127 | } |
| 128 | else { |
| 129 | uri.erase(0, 1); |
| 130 | trim(uri); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | size_t iComponentStart = 0; |
| 135 | |
| 136 | // Unescape the components. |
| 137 | while (iComponentStart < uri.size()) { |
| 138 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 139 | if (iComponentEnd == std::string::npos) |
| 140 | iComponentEnd = uri.size(); |
| 141 | |
| 142 | Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd); |
| 143 | // Ignore illegal components. This also gets rid of a trailing '/'. |
| 144 | if (!component.empty()) |
| 145 | append(Component(component)); |
| 146 | |
| 147 | iComponentStart = iComponentEnd + 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | std::string |
| 152 | Name::toUri() const |
| 153 | { |
| 154 | std::ostringstream os; |
| 155 | os << *this; |
| 156 | return os.str(); |
| 157 | } |
| 158 | |
| 159 | Name& |
| 160 | Name::append(const Name& name) |
| 161 | { |
| 162 | if (&name == this) |
| 163 | // Copying from this name, so need to make a copy first. |
| 164 | return append(Name(name)); |
| 165 | |
| 166 | for (size_t i = 0; i < name.size(); ++i) |
| 167 | append(name.at(i)); |
| 168 | |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | Name& |
| 173 | Name::appendNumber(uint64_t number) |
| 174 | { |
| 175 | m_nameBlock.push_back(Component::fromNumber(number)); |
| 176 | return *this; |
| 177 | } |
| 178 | |
| 179 | Name& |
| 180 | Name::appendNumberWithMarker(uint8_t marker, uint64_t number) |
| 181 | { |
| 182 | m_nameBlock.push_back(Component::fromNumberWithMarker(marker, number)); |
| 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | Name& |
| 187 | Name::appendVersion(uint64_t version) |
| 188 | { |
| 189 | m_nameBlock.push_back(Component::fromVersion(version)); |
| 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | Name& |
| 194 | Name::appendVersion() |
| 195 | { |
Junxiao Shi | 937e461 | 2014-10-22 15:39:07 -0700 | [diff] [blame] | 196 | appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 197 | return *this; |
| 198 | } |
| 199 | |
| 200 | Name& |
| 201 | Name::appendSegment(uint64_t segmentNo) |
| 202 | { |
| 203 | m_nameBlock.push_back(Component::fromSegment(segmentNo)); |
| 204 | return *this; |
| 205 | } |
| 206 | |
| 207 | Name& |
| 208 | Name::appendSegmentOffset(uint64_t offset) |
| 209 | { |
| 210 | m_nameBlock.push_back(Component::fromSegmentOffset(offset)); |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | Name& |
| 215 | Name::appendTimestamp(const time::system_clock::TimePoint& timePoint) |
| 216 | { |
| 217 | m_nameBlock.push_back(Component::fromTimestamp(timePoint)); |
| 218 | return *this; |
| 219 | } |
| 220 | |
| 221 | Name& |
| 222 | Name::appendSequenceNumber(uint64_t seqNo) |
| 223 | { |
| 224 | m_nameBlock.push_back(Component::fromSequenceNumber(seqNo)); |
| 225 | return *this; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | Name |
| 230 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 231 | { |
| 232 | Name result; |
| 233 | |
| 234 | size_t iEnd = iStartComponent + nComponents; |
| 235 | for (size_t i = iStartComponent; i < iEnd && i < size(); ++i) |
| 236 | result.append(at(i)); |
| 237 | |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | Name |
| 242 | Name::getSubName(size_t iStartComponent) const |
| 243 | { |
| 244 | Name result; |
| 245 | |
| 246 | for (size_t i = iStartComponent; i < size(); ++i) |
| 247 | result.append(at(i)); |
| 248 | |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | Name |
| 253 | Name::getSuccessor() const |
| 254 | { |
| 255 | if (empty()) { |
| 256 | static uint8_t firstValue[] = { 0 }; |
| 257 | Name firstName; |
| 258 | firstName.append(firstValue, 1); |
| 259 | return firstName; |
| 260 | } |
| 261 | |
| 262 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 263 | } |
| 264 | |
| 265 | bool |
| 266 | Name::equals(const Name& name) const |
| 267 | { |
| 268 | if (size() != name.size()) |
| 269 | return false; |
| 270 | |
| 271 | for (size_t i = 0; i < size(); ++i) { |
| 272 | if (at(i) != name.at(i)) |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool |
| 280 | Name::isPrefixOf(const Name& name) const |
| 281 | { |
| 282 | // This name is longer than the name we are checking against. |
| 283 | if (size() > name.size()) |
| 284 | return false; |
| 285 | |
| 286 | // Check if at least one of given components doesn't match. |
| 287 | for (size_t i = 0; i < size(); ++i) { |
| 288 | if (at(i) != name.at(i)) |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | int |
| 297 | Name::compare(const Name& other) const |
| 298 | { |
| 299 | for (size_t i = 0; i < size() && i < other.size(); ++i) { |
| 300 | int comparison = at(i).compare(other.at(i)); |
| 301 | if (comparison == 0) |
| 302 | // The components at this index are equal, so check the next components. |
| 303 | continue; |
| 304 | |
| 305 | // Otherwise, the result is based on the components at this index. |
| 306 | return comparison; |
| 307 | } |
| 308 | |
| 309 | // The components up to min(this.size(), other.size()) are equal, so the shorter name is less. |
| 310 | if (size() < other.size()) |
| 311 | return -1; |
| 312 | else if (size() > other.size()) |
| 313 | return 1; |
| 314 | else |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | std::ostream& |
| 319 | operator<<(std::ostream& os, const Name& name) |
| 320 | { |
| 321 | if (name.empty()) |
| 322 | { |
| 323 | os << "/"; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 328 | os << "/"; |
| 329 | i->toUri(os); |
| 330 | } |
| 331 | } |
| 332 | return os; |
| 333 | } |
| 334 | |
| 335 | std::istream& |
| 336 | operator>>(std::istream& is, Name& name) |
| 337 | { |
| 338 | std::string inputString; |
| 339 | is >> inputString; |
| 340 | name.set(inputString); |
| 341 | |
| 342 | return is; |
| 343 | } |
| 344 | |
| 345 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame^] | 346 | |
| 347 | namespace std { |
| 348 | size_t |
| 349 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 350 | { |
| 351 | return boost::hash_range(name.wireEncode().wire(), |
| 352 | name.wireEncode().wire() + name.wireEncode().size()); |
| 353 | } |
| 354 | |
| 355 | } // namespace std |