NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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 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 
112 template size_t
113 AdditionalDescription::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
114 
115 template size_t
116 AdditionalDescription::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
117 
118 const Block&
120 {
121  if (m_wire.hasWire())
122  return m_wire;
123 
124  EncodingEstimator estimator;
125  size_t estimatedSize = wireEncode(estimator);
126 
127  EncodingBuffer buffer(estimatedSize, 0);
128  wireEncode(buffer);
129 
130  m_wire = buffer.block();
131  m_wire.parse();
132 
133  return m_wire;
134 }
135 
136 void
138 {
139  if (!wire.hasWire()) {
140  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
141  }
142 
143  m_wire = wire;
144  m_wire.parse();
145 
146  if (m_wire.type() != tlv::AdditionalDescription)
147  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription"));
148 
150  while (it != m_wire.elements_end()) {
151  const Block& entry = *it;
152  entry.parse();
153 
154  if (entry.type() != tlv::DescriptionEntry)
155  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry"));
156 
157  if (entry.elements_size() != 2)
158  BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs"));
159 
160  if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
161  entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
162  BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field"));
163 
164  m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
165  it++;
166  }
167 }
168 
169 bool
171 {
172  return (m_info == other.m_info);
173 }
174 
175 bool
177 {
178  return !(*this == other);
179 }
180 
181 std::ostream&
182 operator<<(std::ostream& os, const AdditionalDescription& other)
183 {
184  size_t count = 0;
185  os << "(";
186  for (const auto& entry : other) {
187  if (count > 0)
188  os << ", ";
189  os << "(" << entry.first << ":" << entry.second << ")";
190  count++;
191  }
192  os << ")";
193 
194  return os;
195 }
196 
197 } // namespace v2
198 } // namespace security
199 } // namespace ndn
bool has(const std::string &key) 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.
std::map< std::string, std::string >::const_iterator const_iterator
static const size_t VALUE_OFFSET
EncodingImpl< EstimatorTag > EncodingEstimator
Abstraction of AdditionalDescription.
static const size_t KEY_OFFSET
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
element_const_iterator elements_end() const
Definition: block.cpp:595
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
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.
void set(const std::string &key, const std::string &value)
std::string readString(const Block &block)
Helper to read a string value from a block.
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
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
EncodingImpl< EncoderTag > EncodingBuffer
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
size_t elements_size() const
Definition: block.cpp:601
AdditionalDescription()=default
Create an empty AdditionalDescription.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &other)
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
bool operator!=(const AdditionalDescription &other) const
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
uint32_t type() const
Definition: block.hpp:324