NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-local-control-header.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP
23 #define NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP
24 
25 #include "../encoding/encoding-buffer.hpp"
26 #include "../encoding/tlv-nfd.hpp"
27 #include "../encoding/block-helpers.hpp"
28 
29 namespace ndn {
30 namespace nfd {
31 
38 {
39 public:
40  class Error : public std::runtime_error
41  {
42  public:
43  explicit
44  Error(const std::string& what)
45  : std::runtime_error(what)
46  {
47  }
48  };
49 
50  enum EncodeFlags : uint8_t {
53  ENCODE_NEXT_HOP = (1 << 1),
55  ENCODE_ALL = 0xff
56  };
57 
58  enum CachingPolicy : uint8_t {
61  };
62 
64  : m_incomingFaceId(INVALID_FACE_ID)
65  , m_nextHopFaceId(INVALID_FACE_ID)
66  , m_cachingPolicy(CachingPolicy::INVALID_POLICY)
67  {
68  }
69 
75  explicit
76  LocalControlHeader(const Block& wire, uint8_t encodeMask = ENCODE_ALL)
77  {
78  wireDecode(wire, encodeMask);
79  }
80 
100  template<class U>
101  inline Block
102  wireEncode(const U& payload, uint8_t encodeMask = ENCODE_ALL) const;
103 
110  inline void
111  wireDecode(const Block& wire, uint8_t encodeMask = ENCODE_ALL);
112 
113  inline static const Block&
114  getPayload(const Block& wire);
115 
119  // Getters/setters
120 
121  bool
122  empty(uint8_t encodeMask) const
123  {
124  bool needIncomingFaceId = encodeMask & ENCODE_INCOMING_FACE_ID;
125  bool needNextHopFaceId = encodeMask & ENCODE_NEXT_HOP;
126  bool needCachingPolicy = encodeMask & ENCODE_CACHING_POLICY;
127 
128  return !((needIncomingFaceId && hasIncomingFaceId()) ||
129  (needNextHopFaceId && hasNextHopFaceId()) ||
130  (needCachingPolicy && hasCachingPolicy()));
131  }
132 
133  //
134 
135  bool
137  {
138  return m_incomingFaceId != INVALID_FACE_ID;
139  }
140 
141  uint64_t
143  {
144  return m_incomingFaceId;
145  }
146 
147  void
148  setIncomingFaceId(uint64_t incomingFaceId)
149  {
150  m_incomingFaceId = incomingFaceId;
151  }
152 
153  //
154 
155  bool
157  {
158  return m_nextHopFaceId != INVALID_FACE_ID;
159  }
160 
161  uint64_t
163  {
164  return m_nextHopFaceId;
165  }
166 
167  void
168  setNextHopFaceId(uint64_t nextHopFaceId)
169  {
170  m_nextHopFaceId = nextHopFaceId;
171  }
172 
173  //
174 
175  bool
177  {
178  return m_cachingPolicy != CachingPolicy::INVALID_POLICY;
179  }
180 
183  {
184  return m_cachingPolicy;
185  }
186 
187  void
189  {
190  m_cachingPolicy = cachingPolicy;
191  }
192 
193 private:
194  template<encoding::Tag TAG>
195  inline size_t
196  wireEncode(EncodingImpl<TAG>& block, size_t payloadSize, uint8_t encodeMask) const;
197 
198 private:
199  uint64_t m_incomingFaceId;
200  uint64_t m_nextHopFaceId;
201  CachingPolicy m_cachingPolicy;
202 };
203 
204 
208 template<encoding::Tag TAG>
209 inline size_t
210 LocalControlHeader::wireEncode(EncodingImpl<TAG>& block, size_t payloadSize,
211  uint8_t encodeMask) const
212 {
213  bool needIncomingFaceId = encodeMask & ENCODE_INCOMING_FACE_ID;
214  bool needNextHopFaceId = encodeMask & ENCODE_NEXT_HOP;
215  bool needCachingPolicy = encodeMask & ENCODE_CACHING_POLICY;
216 
217  size_t totalLength = payloadSize;
218 
219  if (needIncomingFaceId && hasIncomingFaceId())
220  {
221  totalLength += prependNonNegativeIntegerBlock(block,
223  }
224 
225  if (needNextHopFaceId && hasNextHopFaceId())
226  {
227  totalLength += prependNonNegativeIntegerBlock(block,
229  }
230 
231  if (needCachingPolicy && hasCachingPolicy())
232  {
233  size_t cachingPolicyLength = 0;
234  cachingPolicyLength += block.prependVarNumber(0);
235  cachingPolicyLength += block.prependVarNumber(tlv::nfd::NoCache);
236  cachingPolicyLength += block.prependVarNumber(cachingPolicyLength);
237  cachingPolicyLength += block.prependVarNumber(tlv::nfd::CachingPolicy);
238 
239  totalLength += cachingPolicyLength;
240  }
241 
242  totalLength += block.prependVarNumber(totalLength);
243  totalLength += block.prependVarNumber(tlv::nfd::LocalControlHeader);
244  return totalLength;
245 }
246 
247 template<class U>
248 inline Block
249 LocalControlHeader::wireEncode(const U& payload, uint8_t encodeMask) const
250 {
252  if (empty(encodeMask))
253  BOOST_THROW_EXCEPTION(Error("Requested wire for LocalControlHeader, but none of the fields are "
254  "set or enabled"));
255 
256  EncodingEstimator estimator;
257  size_t length = wireEncode(estimator, payload.wireEncode().size(), encodeMask);
258 
259  EncodingBuffer buffer(length);
260  wireEncode(buffer, payload.wireEncode().size(), encodeMask);
261 
262  return buffer.block(false);
263 }
264 
265 inline void
266 LocalControlHeader::wireDecode(const Block& wire, uint8_t encodeMask)
267 {
268  bool needIncomingFaceId = encodeMask & ENCODE_INCOMING_FACE_ID;
269  bool needNextHopFaceId = encodeMask & ENCODE_NEXT_HOP;
270  bool needCachingPolicy = encodeMask & ENCODE_CACHING_POLICY;
271 
272  BOOST_ASSERT(wire.type() == tlv::nfd::LocalControlHeader);
273  wire.parse();
274 
275  m_incomingFaceId = INVALID_FACE_ID;
276  m_nextHopFaceId = INVALID_FACE_ID;
277  m_cachingPolicy = CachingPolicy::INVALID_POLICY;
278 
280  i != wire.elements_end();
281  ++i)
282  {
283  switch (i->type())
284  {
286  if (needIncomingFaceId)
287  m_incomingFaceId = readNonNegativeInteger(*i);
288  break;
290  if (needNextHopFaceId)
291  m_nextHopFaceId = readNonNegativeInteger(*i);
292  break;
294  if (needCachingPolicy) {
295  i->parse();
296  Block::element_const_iterator it = i->elements_begin();
297  if (it != i->elements_end() && it->type() == tlv::nfd::NoCache) {
298  m_cachingPolicy = CachingPolicy::NO_CACHE;
299  }
300  else {
301  BOOST_THROW_EXCEPTION(Error("CachingPolicy: Missing required NoCache field"));
302  }
303  }
304  break;
305  default:
306  // ignore all unsupported
307  break;
308  }
309  }
310 }
311 
312 inline const Block&
314 {
315  if (wire.type() == tlv::nfd::LocalControlHeader)
316  {
317  wire.parse();
318  if (wire.elements_size() < 1)
319  return wire; // don't throw an error, but don't continue processing
320 
321  return wire.elements()[wire.elements().size()-1];
322  }
323  else
324  {
325  return wire;
326  }
327 }
328 
329 } // namespace nfd
330 } // namespace ndn
331 
332 #endif // NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP
void setCachingPolicy(CachingPolicy cachingPolicy)
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
EncodingImpl< EstimatorTag > EncodingEstimator
const element_container & elements() const
Get all subelements.
Definition: block.hpp:364
STL namespace.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
element_const_iterator elements_end() const
Definition: block.cpp:595
static const Block & getPayload(const Block &wire)
element_const_iterator elements_begin() const
Definition: block.cpp:589
void wireDecode(const Block &wire, uint8_t encodeMask=ENCODE_ALL)
Decode from the wire format and set LocalControlHeader on the supplied item.
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
Class to handle work with LocalControlHeader.
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
size_t elements_size() const
Definition: block.cpp:601
bool empty(uint8_t encodeMask) const
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
void setIncomingFaceId(uint64_t incomingFaceId)
LocalControlHeader(const Block &wire, uint8_t encodeMask=ENCODE_ALL)
Create from wire encoding.
void setNextHopFaceId(uint64_t nextHopFaceId)
Block wireEncode(const U &payload, uint8_t encodeMask=ENCODE_ALL) const
Create wire encoding with options LocalControlHeader and the supplied item.
static const uint64_t INVALID_FACE_ID