NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
cache-policy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #include "cache-policy.hpp"
25 
26 namespace ndn {
27 namespace lp {
28 
29 std::ostream&
30 operator<<(std::ostream& os, CachePolicyType policy)
31 {
32  switch (policy) {
34  os << "NoCache";
35  break;
36  default:
37  os << "None";
38  break;
39  }
40 
41  return os;
42 }
43 
45  : m_policy(CachePolicyType::NONE)
46 {
47 }
48 
50 {
51  wireDecode(block);
52 }
53 
54 template<encoding::Tag TAG>
55 size_t
56 CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const
57 {
58  if (m_policy == CachePolicyType::NONE) {
59  BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set"));
60  }
61  size_t length = 0;
63  static_cast<uint32_t>(m_policy));
64  length += encoder.prependVarNumber(length);
65  length += encoder.prependVarNumber(tlv::CachePolicy);
66  return length;
67 }
68 
69 template size_t
70 CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
71 
72 template size_t
73 CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
74 
75 const Block&
77 {
78  if (m_policy == CachePolicyType::NONE) {
79  BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set"));
80  }
81 
82  if (m_wire.hasWire()) {
83  return m_wire;
84  }
85 
86  EncodingEstimator estimator;
87  size_t estimatedSize = wireEncode(estimator);
88 
89  EncodingBuffer buffer(estimatedSize, 0);
90  wireEncode(buffer);
91 
92  m_wire = buffer.block();
93 
94  return m_wire;
95 }
96 
97 void
99 {
100  if (wire.type() != tlv::CachePolicy) {
101  BOOST_THROW_EXCEPTION(Error("expecting CachePolicy block"));
102  }
103 
104  m_wire = wire;
105  m_wire.parse();
106 
108  if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
109  m_policy = static_cast<CachePolicyType>(readNonNegativeInteger(*it));
110  if (this->getPolicy() == CachePolicyType::NONE) {
111  BOOST_THROW_EXCEPTION(Error("unknown CachePolicyType"));
112  }
113  }
114  else {
115  BOOST_THROW_EXCEPTION(Error("expecting CachePolicyType block"));
116  }
117 }
118 
121 {
122  switch (m_policy) {
124  return m_policy;
125  default:
126  return CachePolicyType::NONE;
127  }
128 }
129 
132 {
133  m_policy = policy;
134  m_wire.reset();
135  return *this;
136 }
137 
138 } // namespace lp
139 } // namespace ndn
void wireDecode(const Block &wire)
get CachePolicyType from wire format
represents a CachePolicy header field
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
CachePolicyType getPolicy() const
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
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
element_const_iterator elements_begin() const
Definition: block.cpp:589
EncodingImpl< EncoderTag > EncodingBuffer
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
CachePolicyType
indicates the cache policy applied to a Data packet
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
const Block & wireEncode() const
encode CachePolicy into wire format
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471