Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2019 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 | * @author Chavoosh Ghasemi <chghasemi@cs.arizona.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "ndn-cxx/metadata-object.hpp" |
| 23 | |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 24 | namespace ndn { |
| 25 | |
| 26 | static_assert(std::is_base_of<tlv::Error, MetadataObject::Error>::value, |
| 27 | "MetadataObject::Error must inherit from tlv::Error"); |
| 28 | |
| 29 | const name::Component KEYWORD_METADATA_COMP = "20 08 6D65746164617461"_block; // 32=metadata |
| 30 | |
| 31 | MetadataObject::MetadataObject() = default; |
| 32 | |
| 33 | MetadataObject::MetadataObject(const Data& data) |
| 34 | { |
| 35 | if (data.getContentType() != tlv::ContentType_Blob) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 36 | NDN_THROW(Error("Expecting ContentType Blob, got " + to_string(data.getContentType()))); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | if (!isValidName(data.getName())) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 40 | NDN_THROW(Error("Name " + data.getName().toUri() + " is not a valid MetadataObject name")); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | data.getContent().parse(); |
| 44 | // ignore non-Name elements before the first one |
| 45 | m_versionedName.wireDecode(data.getContent().get(tlv::Name)); |
| 46 | } |
| 47 | |
| 48 | Data |
| 49 | MetadataObject::makeData(Name discoveryInterestName, |
| 50 | KeyChain& keyChain, |
| 51 | const ndn::security::SigningInfo& si, |
| 52 | optional<uint64_t> version, |
| 53 | time::milliseconds freshnessPeriod) const |
| 54 | { |
| 55 | if (discoveryInterestName.empty() || discoveryInterestName[-1] != KEYWORD_METADATA_COMP) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 56 | NDN_THROW(Error("Name " + discoveryInterestName.toUri() + |
| 57 | " is not a valid discovery Interest name")); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 58 | } |
| 59 | discoveryInterestName.appendVersion(version); |
| 60 | discoveryInterestName.appendSegment(0); |
| 61 | |
| 62 | Data data(discoveryInterestName); |
| 63 | data.setContent(m_versionedName.wireEncode()); |
| 64 | data.setFreshnessPeriod(freshnessPeriod); |
| 65 | keyChain.sign(data, si); |
| 66 | |
| 67 | return data; |
| 68 | } |
| 69 | |
| 70 | MetadataObject& |
| 71 | MetadataObject::setVersionedName(const Name& name) |
| 72 | { |
| 73 | m_versionedName = name; |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | MetadataObject::isValidName(const Name& name) |
| 79 | { |
| 80 | return name.size() >= 3 && name[-3] == KEYWORD_METADATA_COMP && |
| 81 | name[-2].isVersion() && name[-1].isSegment(); |
| 82 | } |
| 83 | |
| 84 | Interest |
| 85 | MetadataObject::makeDiscoveryInterest(Name name) |
| 86 | { |
| 87 | return Interest(name.append(KEYWORD_METADATA_COMP)) |
| 88 | .setCanBePrefix(true) |
| 89 | .setMustBeFresh(true); |
| 90 | } |
| 91 | |
| 92 | } // namespace ndn |