blob: 5524aeb008aad06ac17f43f8f87c128a781960d5 [file] [log] [blame]
Teng Liang5b323d12018-01-31 18:50:45 -07001/* -*- 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
27namespace ndn {
28namespace lp {
29
30static const name::Component SELF_LEARNING_PREFIX("self-learning");
31
32PrefixAnnouncement::PrefixAnnouncement() = default;
33
34PrefixAnnouncement::PrefixAnnouncement(const Block& block)
35{
36 wireDecode(block);
37}
38
39PrefixAnnouncement::PrefixAnnouncement(shared_ptr<const Data> data)
40{
41 setData(std::move(data));
42}
43
44template<encoding::Tag TAG>
45size_t
46PrefixAnnouncement::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
55NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(PrefixAnnouncement);
56
57void
58PrefixAnnouncement::wireDecode(const Block& wire)
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
68Name
69PrefixAnnouncement::getAnnouncedName() const
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
82PrefixAnnouncement&
83PrefixAnnouncement::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