prefix-announcement.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 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 Teng Liang <philoliang@email.arizona.edu>
22  */
23 
24 #include "prefix-announcement.hpp"
25 #include "tlv.hpp"
26 
27 namespace ndn {
28 namespace lp {
29 
30 static const name::Component SELF_LEARNING_PREFIX("self-learning");
31 
33 
35 {
36  wireDecode(block);
37 }
38 
39 PrefixAnnouncement::PrefixAnnouncement(shared_ptr<const Data> data)
40 {
41  setData(std::move(data));
42 }
43 
44 template<encoding::Tag TAG>
45 size_t
46 PrefixAnnouncement::wireEncode(EncodingImpl<TAG>& encoder) const
47 {
48  size_t length = 0;
49  length += m_data->wireEncode(encoder);
50  length += encoder.prependVarNumber(length);
51  length += encoder.prependVarNumber(tlv::PrefixAnnouncement);
52  return length;
53 }
54 
56 
57 void
59 {
60  if (wire.type() != tlv::PrefixAnnouncement) {
61  BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(wire.type())));
62  }
63 
64  wire.parse();
65  setData(make_shared<Data>(wire.get(ndn::tlv::Data)));
66 }
67 
68 Name
70 {
71  if (m_data == nullptr) {
72  BOOST_THROW_EXCEPTION(Error("Data is unset in PrefixAnnouncement"));
73  }
74 
75  const Name& dataName = m_data->getName();
76  BOOST_ASSERT(dataName.at(0) == SELF_LEARNING_PREFIX);
77  BOOST_ASSERT(dataName.at(-1).isVersion());
78 
79  return dataName.getSubName(1, dataName.size() - 2);
80 }
81 
83 PrefixAnnouncement::setData(shared_ptr<const Data> data)
84 {
85  if (data == nullptr) {
86  BOOST_THROW_EXCEPTION(Error("Unexpected nullptr"));
87  }
88 
89  if (data->getName().at(0) != SELF_LEARNING_PREFIX) {
90  BOOST_THROW_EXCEPTION(Error("Unexpected prefix in name " + data->getName().toUri()));
91  }
92 
93  if (!data->getName().get(-1).isVersion()) {
94  BOOST_THROW_EXCEPTION(Error("Last name component of " + data->getName().toUri() +
95  " is not a version"));
96  }
97 
98  if (data->getContent().value_size() != 0 ||
99  data->getMetaInfo().wireEncode().value_size() != 0) {
100  BOOST_THROW_EXCEPTION(Error("Both Data MetaInfo and Content must be empty"));
101  }
102 
103  m_data = std::move(data);
104  return *this;
105 }
106 
107 } // namespace lp
108 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
static const name::Component SELF_LEARNING_PREFIX("self-learning")
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
PrefixAnnouncement & setData(shared_ptr< const Data > data)
Name getAnnouncedName() const
Get announced name.
bool isVersion() const
Check if the component is version per NDN naming conventions.
void wireDecode(const Block &wire)
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
Definition: block.cpp:417
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
size_t size() const
Get number of components.
Definition: name.hpp:154
Represents an absolute name.
Definition: name.hpp:42
size_t wireEncode(EncodingImpl< TAG > &encoder) const
const Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:185
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
represents a Prefix Announcement
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:199
std::string to_string(const V &v)
Definition: backports.hpp:84