metadata-object.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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 Chavoosh Ghasemi <chghasemi@cs.arizona.edu>
22  */
23 
25 
26 namespace ndn {
27 
29 
31 {
32  if (!isValidName(data.getName())) {
33  NDN_THROW(Error("Name " + data.getName().toUri() + " is not a valid MetadataObject name"));
34  }
35 
36  if (data.getContentType() != tlv::ContentType_Blob) {
37  NDN_THROW(Error("MetadataObject has invalid ContentType " + to_string(data.getContentType())));
38  }
39 
40  if (data.getContent().value_size() == 0) {
41  NDN_THROW(Error("MetadataObject is empty"));
42  }
43 
44  data.getContent().parse();
45  // ignore non-Name elements before the first one
46  m_versionedName.wireDecode(data.getContent().get(tlv::Name));
47 }
48 
49 Data
50 MetadataObject::makeData(Name discoveryInterestName,
51  KeyChain& keyChain,
53  std::optional<uint64_t> version,
54  time::milliseconds freshnessPeriod) const
55 {
56  if (discoveryInterestName.empty() || discoveryInterestName[-1] != getKeywordComponent()) {
57  NDN_THROW(Error("Name " + discoveryInterestName.toUri() +
58  " is not a valid discovery Interest name"));
59  }
60  discoveryInterestName.appendVersion(version);
61  discoveryInterestName.appendSegment(0);
62 
63  Data data(discoveryInterestName);
64  data.setContent(m_versionedName.wireEncode());
65  data.setFreshnessPeriod(freshnessPeriod);
66  keyChain.sign(data, si);
67 
68  return data;
69 }
70 
73 {
74  m_versionedName = name;
75  return *this;
76 }
77 
78 const name::Component&
80 {
81  static const name::Component nc(tlv::KeywordNameComponent, {'m', 'e', 't', 'a', 'd', 'a', 't', 'a'});
82  return nc;
83 }
84 
85 bool
87 {
88  return name.size() >= 3 && name[-3] == getKeywordComponent() &&
89  name[-2].isVersion() && name[-1].isSegment();
90 }
91 
94 {
95  return Interest(name.append(getKeywordComponent()))
96  .setCanBePrefix(true)
97  .setMustBeFresh(true);
98 }
99 
100 } // namespace ndn
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:326
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:414
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Definition: block.hpp:299
Represents a Data packet.
Definition: data.hpp:39
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:239
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Set the FreshnessPeriod.
Definition: data.cpp:347
uint32_t getContentType() const noexcept
Return the value of ContentType.
Definition: data.hpp:312
const Name & getName() const noexcept
Get the Data name.
Definition: data.hpp:137
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:188
Represents an Interest packet.
Definition: interest.hpp:50
Class for RDR-style metadata encoding/decoding.
static const name::Component & getKeywordComponent()
Returns the well-known keyword name component used for metadata objects (32=metadata)
Data makeData(Name discoveryInterestName, KeyChain &keyChain, const ndn::security::SigningInfo &si=security::SigningInfo(), std::optional< uint64_t > version=std::nullopt, time::milliseconds freshnessPeriod=10_ms) const
Create a Data packet representing this metadata object.
MetadataObject()
Create an empty metadata object.
MetadataObject & setVersionedName(const Name &name)
Set the versioned name.
static Interest makeDiscoveryInterest(Name name)
Generate a discovery interest packet based on name.
static bool isValidName(const Name &name)
Check whether name can be a valid metadata name.
Represents an absolute name.
Definition: name.hpp:45
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:431
Name & appendVersion(const std::optional< uint64_t > &version=std::nullopt)
Append a version component.
Definition: name.cpp:205
size_t size() const noexcept
Returns the number of components.
Definition: name.hpp:180
bool empty() const noexcept
Checks if the name is empty, i.e., has no components.
Definition: name.hpp:171
Name & append(const Component &component)
Append a name component.
Definition: name.hpp:308
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: name.cpp:92
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:324
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:125
Represents a name component.
The main interface for signing key management.
Definition: key-chain.hpp:87
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:400
Signing parameters passed to KeyChain.
#define NDN_THROW(e)
Definition: exception.hpp:56
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ Name
Definition: tlv.hpp:71
@ KeywordNameComponent
Definition: tlv.hpp:75
@ Interest
Definition: tlv.hpp:68
@ ContentType_Blob
payload
Definition: tlv.hpp:145
Definition: data.cpp:25