NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2022 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  "MetadataObject::Error must inherit from tlv::Error");
30 
32 
34 {
35  if (!isValidName(data.getName())) {
36  NDN_THROW(Error("Name " + data.getName().toUri() + " is not a valid MetadataObject name"));
37  }
38 
39  if (data.getContentType() != tlv::ContentType_Blob) {
40  NDN_THROW(Error("MetadataObject has invalid ContentType " + to_string(data.getContentType())));
41  }
42 
43  if (data.getContent().value_size() == 0) {
44  NDN_THROW(Error("MetadataObject is empty"));
45  }
46 
47  data.getContent().parse();
48  // ignore non-Name elements before the first one
49  m_versionedName.wireDecode(data.getContent().get(tlv::Name));
50 }
51 
52 Data
53 MetadataObject::makeData(Name discoveryInterestName,
54  KeyChain& keyChain,
56  optional<uint64_t> version,
57  time::milliseconds freshnessPeriod) const
58 {
59  if (discoveryInterestName.empty() || discoveryInterestName[-1] != getKeywordComponent()) {
60  NDN_THROW(Error("Name " + discoveryInterestName.toUri() +
61  " is not a valid discovery Interest name"));
62  }
63  discoveryInterestName.appendVersion(version);
64  discoveryInterestName.appendSegment(0);
65 
66  Data data(discoveryInterestName);
67  data.setContent(m_versionedName.wireEncode());
68  data.setFreshnessPeriod(freshnessPeriod);
69  keyChain.sign(data, si);
70 
71  return data;
72 }
73 
76 {
77  m_versionedName = name;
78  return *this;
79 }
80 
81 const name::Component&
83 {
84  static const name::Component nc(tlv::KeywordNameComponent, {'m', 'e', 't', 'a', 'd', 'a', 't', 'a'});
85  return nc;
86 }
87 
88 bool
90 {
91  return name.size() >= 3 && name[-3] == getKeywordComponent() &&
92  name[-2].isVersion() && name[-1].isSegment();
93 }
94 
97 {
98  return Interest(name.append(getKeywordComponent()))
99  .setCanBePrefix(true)
100  .setMustBeFresh(true);
101 }
102 
103 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
std::string to_string(const T &val)
Definition: backports.hpp:86
ndn security KeyChain
Definition: key-chain.cpp:70
static bool isValidName(const Name &name)
Check whether name can be a valid metadata name.
MetadataObject()
Create an empty metadata object.
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:246
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Definition: block.hpp:321
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
Represents an Interest packet.
Definition: interest.hpp:48
Name & append(const Component &component)
Append a component.
Definition: name.hpp:275
Signing parameters passed to KeyChain.
Name & appendVersion(const optional< uint64_t > &version=nullopt)
Append a version component.
Definition: name.cpp:230
#define NDN_THROW(e)
Definition: exception.hpp:61
MetadataObject & setVersionedName(const Name &name)
Set the versioned name.
NDN_CXX_NODISCARD Data makeData(Name discoveryInterestName, KeyChain &keyChain, const ndn::security::SigningInfo &si=security::SigningInfo(), optional< uint64_t > version=nullopt, time::milliseconds freshnessPeriod=10_ms) const
Create a Data packet representing this metadata object.
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:412
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:143
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
Represents an absolute name.
Definition: name.hpp:41
static const name::Component & getKeywordComponent()
Returns the well-known keyword name component used for metadata objects (32=metadata) ...
size_t size() const
Returns the number of components.
Definition: name.hpp:151
Represents a name component.
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:346
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:175
static NDN_CXX_NODISCARD Interest makeDiscoveryInterest(Name name)
Generate a discovery interest packet based on name.
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:349
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
Represents a Data packet.
Definition: data.hpp:37
uint32_t getContentType() const
Definition: data.hpp:275
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:415
Class for RDR-style metadata encoding/decoding.
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48