NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
link.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  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #include "ndn-cxx/link.hpp"
23 
24 namespace ndn {
25 
26 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Link>));
27 BOOST_CONCEPT_ASSERT((WireEncodable<Link>));
28 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Link>));
29 BOOST_CONCEPT_ASSERT((WireDecodable<Link>));
30 static_assert(std::is_base_of<Data::Error, Link::Error>::value,
31  "Link::Error should inherit from Data::Error");
32 
33 Link::Link() = default;
34 
35 Link::Link(const Block& wire, bool wantSort)
36 {
37  this->wireDecode(wire, wantSort);
38 }
39 
40 Link::Link(const Name& name, std::initializer_list<Delegation> dels)
41  : Data(name)
42  , m_delList(dels)
43 {
44  encodeContent();
45 }
46 
47 void
48 Link::encodeContent()
49 {
51 
52  if (m_delList.size() > 0) {
53  EncodingEstimator estimator;
54  size_t estimatedSize = m_delList.wireEncode(estimator, tlv::Content);
55 
56  EncodingBuffer buffer(estimatedSize, 0);
57  m_delList.wireEncode(buffer, tlv::Content);
58 
59  setContent(buffer.block());
60  }
61  else {
62  setContent(nullptr, 0);
63  }
64 }
65 
66 void
67 Link::wireDecode(const Block& wire, bool wantSort)
68 {
69  Data::wireDecode(wire);
70 
72  NDN_THROW(Error("Expecting ContentType Link, got " + to_string(getContentType())));
73  }
74 
75  m_delList.wireDecode(getContent(), wantSort);
76 }
77 
78 void
80 {
81  m_delList = dels;
82  encodeContent();
83 }
84 
85 void
86 Link::addDelegation(uint32_t preference, const Name& name)
87 {
88  m_delList.insert(preference, name, DelegationList::INS_REPLACE);
89  encodeContent();
90 }
91 
92 bool
94 {
95  size_t nErased = m_delList.erase(name);
96  if (nErased > 0) {
97  encodeContent();
98  }
99  return nErased > 0;
100 }
101 
102 } // namespace ndn
ndn::Data::getContentType
uint32_t getContentType() const
Definition: data.hpp:204
ndn::DelegationList::size
size_t size() const noexcept
Definition: delegation-list.hpp:108
ndn::DelegationList::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
Definition: delegation-list.cpp:63
ndn::Data::setContentType
Data & setContentType(uint32_t type)
Definition: data.cpp:288
ndn::Data::getContent
const Block & getContent() const
Get Content.
Definition: data.cpp:232
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition: concepts.hpp:81
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::DelegationList::insert
bool insert(uint64_t preference, const Name &name, InsertConflictResolution onConflict=INS_REPLACE)
insert Delegation
Definition: delegation-list.cpp:170
ndn::tlv::ContentType_Link
@ ContentType_Link
another name that identifies the actual data content
Definition: tlv.hpp:158
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:45
ndn::Data::wireDecode
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: data.cpp:122
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::DelegationList
represents a list of Delegations
Definition: delegation-list.hpp:38
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::DelegationList::INS_REPLACE
@ INS_REPLACE
existing delegation(s) with the same name are replaced with the new delegation
Definition: delegation-list.hpp:153
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::name
Definition: name-component-types.hpp:33
ndn::DelegationList::wireDecode
void wireDecode(const Block &block, bool wantSort=true)
decode a DelegationList
Definition: delegation-list.cpp:105
ndn::WireEncodableWithEncodingBuffer
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38
ndn::tlv::Content
@ Content
Definition: tlv.hpp:79
ndn::DelegationList::erase
size_t erase(uint64_t preference, const Name &name)
delete Delegation(s) with specified preference and name
Definition: delegation-list.hpp:185