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-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 
23 
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 
32 
34 {
35  if (data.getContentType() != tlv::ContentType_Blob) {
36  NDN_THROW(Error("Expecting ContentType Blob, got " + to_string(data.getContentType())));
37  }
38 
39  if (!isValidName(data.getName())) {
40  NDN_THROW(Error("Name " + data.getName().toUri() + " is not a valid MetadataObject name"));
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,
52  optional<uint64_t> version,
53  time::milliseconds freshnessPeriod) const
54 {
55  if (discoveryInterestName.empty() || discoveryInterestName[-1] != KEYWORD_METADATA_COMP) {
56  NDN_THROW(Error("Name " + discoveryInterestName.toUri() +
57  " is not a valid discovery Interest name"));
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 
72 {
73  m_versionedName = name;
74  return *this;
75 }
76 
77 bool
79 {
80  return name.size() >= 3 && name[-3] == KEYWORD_METADATA_COMP &&
81  name[-2].isVersion() && name[-1].isSegment();
82 }
83 
86 {
87  return Interest(name.append(KEYWORD_METADATA_COMP))
88  .setCanBePrefix(true)
89  .setMustBeFresh(true);
90 }
91 
92 } // namespace ndn
ndn::Data::getContentType
uint32_t getContentType() const
Definition: data.hpp:204
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
ndn::MetadataObject::Error
Definition: metadata-object.hpp:49
ndn::KEYWORD_METADATA_COMP
const name::Component KEYWORD_METADATA_COMP
Definition: metadata-object.cpp:29
ndn::MetadataObject
Class for RDR-style metadata encoding/decoding.
Definition: metadata-object.hpp:46
ndn::security::SigningInfo
Signing parameters passed to KeyChain.
Definition: signing-info.hpp:42
ndn::MetadataObject::isValidName
static bool isValidName(const Name &name)
Check whether name can be a valid metadata name.
Definition: metadata-object.cpp:78
ndn::Data::getContent
const Block & getContent() const
Get Content.
Definition: data.cpp:232
ndn::Name::empty
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:145
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::Block::get
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:414
ndn::Name::wireDecode
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
ndn::Name::appendVersion
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:230
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
ndn::MetadataObject::MetadataObject
MetadataObject()
Create an empty metadata object.
ndn::Name::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
ndn::MetadataObject::makeData
NDN_CXX_NODISCARD Data makeData(Name discoveryInterestName, KeyChain &keyChain, const ndn::security::SigningInfo &si=KeyChain::getDefaultSigningInfo(), optional< uint64_t > version=nullopt, time::milliseconds freshnessPeriod=10_ms) const
Create a Data packet representing this metadata object.
Definition: metadata-object.cpp:49
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::Data::setContent
Data & setContent(const Block &block)
Set Content from a block.
Definition: data.cpp:241
ndn::Data::setFreshnessPeriod
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:296
ndn::tlv::ContentType_Blob
@ ContentType_Blob
payload
Definition: tlv.hpp:157
metadata-object.hpp
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::tlv::Name
@ Name
Definition: tlv.hpp:67
ndn::Name::toUri
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:348
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::name
Definition: name-component-types.hpp:33
ndn::MetadataObject::makeDiscoveryInterest
static NDN_CXX_NODISCARD Interest makeDiscoveryInterest(Name name)
Generate a discovery interest packet based on name.
Definition: metadata-object.cpp:85
ndn::Name::appendSegment
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:416
ndn::security::v2::KeyChain::sign
void sign(Data &data, const SigningInfo &params=getDefaultSigningInfo())
Sign data according to the supplied signing information.
Definition: key-chain.cpp:450
ndn::MetadataObject::setVersionedName
MetadataObject & setVersionedName(const Name &name)
Set the versioned name.
Definition: metadata-object.cpp:71
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34