NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
additional-description.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 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 
23 #include "../../util/concepts.hpp"
24 #include "../../encoding/block-helpers.hpp"
25 
26 namespace ndn {
27 namespace security {
28 namespace v2 {
29 
30 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>));
33 BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>));
34 static_assert(std::is_base_of<tlv::Error, AdditionalDescription::Error>::value,
35  "AdditionalDescription::Error must inherit from tlv::Error");
36 
37 static const size_t KEY_OFFSET = 0;
38 static const size_t VALUE_OFFSET = 1;
39 
41 {
42  wireDecode(block);
43 }
44 
45 const std::string&
46 AdditionalDescription::get(const std::string& key) const
47 {
48  auto it = m_info.find(key);
49  if (it == m_info.end())
50  BOOST_THROW_EXCEPTION(Error("Entry does not exist for key (" + key + ")"));
51 
52  return it->second;
53 }
54 
55 void
56 AdditionalDescription::set(const std::string& key, const std::string& value)
57 {
58  m_info[key] = value;
59 }
60 
61 bool
62 AdditionalDescription::has(const std::string& key) const
63 {
64  return (m_info.find(key) != m_info.end());
65 }
66 
69 {
70  return m_info.begin();
71 }
72 
75 {
76  return m_info.end();
77 }
78 
81 {
82  return m_info.begin();
83 }
84 
87 {
88  return m_info.end();
89 }
90 
91 template<encoding::Tag TAG>
92 size_t
94 {
95  size_t totalLength = 0;
96 
97  for (auto it = m_info.rbegin(); it != m_info.rend(); it++) {
98  size_t entryLength = 0;
99  entryLength += prependStringBlock(encoder, tlv::DescriptionValue, it->second);
100  entryLength += prependStringBlock(encoder, tlv::DescriptionKey, it->first);
101  entryLength += encoder.prependVarNumber(entryLength);
102  entryLength += encoder.prependVarNumber(tlv::DescriptionEntry);
103 
104  totalLength += entryLength;
105  }
106 
107  totalLength += encoder.prependVarNumber(totalLength);
108  totalLength += encoder.prependVarNumber(tlv::AdditionalDescription);
109  return totalLength;
110 }
111 
113 
114 const Block&
116 {
117  if (m_wire.hasWire())
118  return m_wire;
119 
120  EncodingEstimator estimator;
121  size_t estimatedSize = wireEncode(estimator);
122 
123  EncodingBuffer buffer(estimatedSize, 0);
124  wireEncode(buffer);
125 
126  m_wire = buffer.block();
127  m_wire.parse();
128 
129  return m_wire;
130 }
131 
132 void
134 {
135  if (!wire.hasWire()) {
136  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
137  }
138 
139  m_wire = wire;
140  m_wire.parse();
141 
142  if (m_wire.type() != tlv::AdditionalDescription)
143  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription"));
144 
146  while (it != m_wire.elements_end()) {
147  const Block& entry = *it;
148  entry.parse();
149 
150  if (entry.type() != tlv::DescriptionEntry)
151  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry"));
152 
153  if (entry.elements_size() != 2)
154  BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs"));
155 
156  if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
157  entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
158  BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field"));
159 
160  m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
161  it++;
162  }
163 }
164 
165 bool
167 {
168  return (m_info == other.m_info);
169 }
170 
171 bool
173 {
174  return !(*this == other);
175 }
176 
177 std::ostream&
178 operator<<(std::ostream& os, const AdditionalDescription& other)
179 {
180  size_t count = 0;
181  os << "(";
182  for (const auto& entry : other) {
183  if (count > 0)
184  os << ", ";
185  os << "(" << entry.first << ":" << entry.second << ")";
186  count++;
187  }
188  os << ")";
189 
190  return os;
191 }
192 
193 } // namespace v2
194 } // namespace security
195 } // namespace ndn
bool has(const std::string &key) const
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
Copyright (c) 2011-2015 Regents of the University of California.
std::map< std::string, std::string >::const_iterator const_iterator
static const size_t VALUE_OFFSET
Abstraction of AdditionalDescription.
static const size_t KEY_OFFSET
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:336
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
void set(const std::string &key, const std::string &value)
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
std::map< std::string, std::string >::iterator iterator
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
const std::string & get(const std::string &key) const
bool operator==(const AdditionalDescription &other) const
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:371
AdditionalDescription()=default
Create an empty AdditionalDescription.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &other)
bool operator!=(const AdditionalDescription &other) const
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdditionalDescription)
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
EncodingImpl< EstimatorTag > EncodingEstimator