NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
23 #include "../util/concepts.hpp"
24 #include "../encoding/block-helpers.hpp"
25 
26 namespace ndn {
27 namespace security {
28 
29 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>));
30 BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>));
33 static_assert(std::is_base_of<tlv::Error, AdditionalDescription::Error>::value,
34  "AdditionalDescription::Error must inherit from tlv::Error");
35 
36 static const size_t KEY_OFFSET = 0;
37 static const size_t VALUE_OFFSET = 1;
38 
40 {
41  wireDecode(block);
42 }
43 
44 const std::string&
45 AdditionalDescription::get(const std::string& key) const
46 {
47  auto it = m_info.find(key);
48  if (it == m_info.end())
49  BOOST_THROW_EXCEPTION(Error("Entry does not exist for key (" + key + ")"));
50 
51  return it->second;
52 }
53 
54 void
55 AdditionalDescription::set(const std::string& key, const std::string& value)
56 {
57  m_info[key] = value;
58 }
59 
60 bool
61 AdditionalDescription::has(const std::string& key) const
62 {
63  return (m_info.find(key) != m_info.end());
64 }
65 
68 {
69  return m_info.begin();
70 }
71 
74 {
75  return m_info.end();
76 }
77 
80 {
81  return m_info.begin();
82 }
83 
86 {
87  return m_info.end();
88 }
89 
90 template<encoding::Tag TAG>
91 size_t
93 {
94  size_t totalLength = 0;
95 
96  for (auto it = m_info.rbegin(); it != m_info.rend(); it++) {
97  size_t entryLength = 0;
98  entryLength += prependStringBlock(encoder, tlv::DescriptionValue, it->second);
99  entryLength += prependStringBlock(encoder, tlv::DescriptionKey, it->first);
100  entryLength += encoder.prependVarNumber(entryLength);
101  entryLength += encoder.prependVarNumber(tlv::DescriptionEntry);
102 
103  totalLength += entryLength;
104  }
105 
106  totalLength += encoder.prependVarNumber(totalLength);
107  totalLength += encoder.prependVarNumber(tlv::AdditionalDescription);
108  return totalLength;
109 }
110 
111 template size_t
112 AdditionalDescription::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
113 
114 template size_t
115 AdditionalDescription::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
116 
117 const Block&
119 {
120  if (m_wire.hasWire())
121  return m_wire;
122 
123  EncodingEstimator estimator;
124  size_t estimatedSize = wireEncode(estimator);
125 
126  EncodingBuffer buffer(estimatedSize, 0);
127  wireEncode(buffer);
128 
129  m_wire = buffer.block();
130  m_wire.parse();
131 
132  return m_wire;
133 }
134 
135 void
137 {
138  if (!wire.hasWire()) {
139  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
140  }
141 
142  m_wire = wire;
143  m_wire.parse();
144 
145  if (m_wire.type() != tlv::AdditionalDescription)
146  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription"));
147 
149  while (it != m_wire.elements_end()) {
150  const Block& entry = *it;
151  entry.parse();
152 
153  if (entry.type() != tlv::DescriptionEntry)
154  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry"));
155 
156  if (entry.elements_size() != 2)
157  BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs"));
158 
159  if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
160  entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
161  BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field"));
162 
163  m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
164  it++;
165  }
166 }
167 
168 bool
170 {
171  return (m_info == other.m_info);
172 }
173 
174 bool
176 {
177  return !(*this == other);
178 }
179 
180 std::ostream&
181 operator<<(std::ostream& os, const AdditionalDescription& other)
182 {
183  size_t count = 0;
184  os << "(";
185  for (const auto& entry : other) {
186  if (count > 0)
187  os << ", ";
188  os << "(" << entry.first << ":" << entry.second << ")";
189  count++;
190  }
191  os << ")";
192 
193  return os;
194 }
195 
196 } // namespace security
197 } // namespace ndn
bool operator!=(const AdditionalDescription &other) const
element_const_iterator elements_begin() const
Definition: block.cpp:589
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
EncodingImpl< EstimatorTag > EncodingEstimator
bool operator==(const AdditionalDescription &other) const
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
element_const_iterator elements_end() const
Definition: block.cpp:595
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
AdditionalDescription()=default
Create an empty AdditionalDescription.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Helper to prepend TLV block type type with value from a string value.
std::string readString(const Block &block)
Helper to read a string value from a block.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &other)
bool has(const std::string &key) const
std::map< std::string, std::string >::iterator iterator
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
EncodingImpl< EncoderTag > EncodingBuffer
const std::string & get(const std::string &key) const
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
size_t elements_size() const
Definition: block.cpp:601
void set(const std::string &key, const std::string &value)
const element_container & elements() const
Get all subelements.
Definition: block.hpp:364
static const size_t KEY_OFFSET
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
Abstraction of AdditionalDescription.
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
static const size_t VALUE_OFFSET
uint32_t type() const
Definition: block.hpp:346
std::map< std::string, std::string >::const_iterator const_iterator