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 #include <boost/lexical_cast.hpp>
25 
26 namespace ndn {
27 
28 static_assert(std::is_base_of<tlv::Error, MetadataObject::Error>::value,
29  "MetadataObject::Error must inherit from tlv::Error");
30 
31 const name::Component KEYWORD_METADATA_COMP = "20 08 6D65746164617461"_block; // 32=metadata
32 
34 
36 {
37  if (data.getContentType() != tlv::ContentType_Blob) {
38  BOOST_THROW_EXCEPTION(Error("Expected ContentType to be BLOB but " +
39  boost::lexical_cast<std::string>(data.getContentType()) +
40  " is provided"));
41  }
42 
43  if (!isValidName(data.getName())) {
44  BOOST_THROW_EXCEPTION(Error("Name " + data.getName().toUri() +
45  " is not a valid MetadataObject name"));
46  }
47 
48  data.getContent().parse();
49  // ignore non-Name elements before the first one
50  m_versionedName.wireDecode(data.getContent().get(tlv::Name));
51 }
52 
53 Data
54 MetadataObject::makeData(Name discoveryInterestName,
55  KeyChain& keyChain,
57  optional<uint64_t> version,
58  time::milliseconds freshnessPeriod) const
59 {
60  if (discoveryInterestName.empty() || discoveryInterestName[-1] != KEYWORD_METADATA_COMP) {
61  BOOST_THROW_EXCEPTION(Error("Name " + discoveryInterestName.toUri() +
62  " is not a valid discovery Interest name"));
63  }
64  discoveryInterestName.appendVersion(version);
65  discoveryInterestName.appendSegment(0);
66 
67  Data data(discoveryInterestName);
68  data.setContent(m_versionedName.wireEncode());
69  data.setFreshnessPeriod(freshnessPeriod);
70  keyChain.sign(data, si);
71 
72  return data;
73 }
74 
77 {
78  m_versionedName = name;
79  return *this;
80 }
81 
82 bool
84 {
85  return name.size() >= 3 && name[-3] == KEYWORD_METADATA_COMP &&
86  name[-2].isVersion() && name[-1].isSegment();
87 }
88 
91 {
92  return Interest(name.append(KEYWORD_METADATA_COMP))
93  .setCanBePrefix(true)
94  .setMustBeFresh(true);
95 }
96 
97 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:116
const Name & getName() const
Get name.
Definition: data.hpp:124
The interface of signing key management.
Definition: key-chain.hpp:46
const Block & getContent() const
Get Content.
Definition: data.cpp:233
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:242
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:333
Represents an Interest packet.
Definition: interest.hpp:44
Signing parameters passed to KeyChain.
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:214
MetadataObject & setVersionedName(const Name &name)
Set the versioned name.
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
Definition: block.cpp:422
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.
Represents an absolute name.
Definition: name.hpp:43
void sign(Data &data, const SigningInfo &params=getDefaultSigningInfo())
Sign data according to the supplied signing information.
Definition: key-chain.cpp:430
Represents a name component.
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:297
static Interest makeDiscoveryInterest(Name name)
Generate a discovery interest packet based on name.
bool empty() const
Check if name is empty.
Definition: name.hpp:139
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:125
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:158
Represents a Data packet.
Definition: data.hpp:35
uint32_t getContentType() const
Definition: data.hpp:204
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:371
const name::Component KEYWORD_METADATA_COMP
Class for RDR-style metadata encoding/decoding.