NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
strategy-choice.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
27 
28 namespace ndn {
29 namespace nfd {
30 
31 BOOST_CONCEPT_ASSERT((StatusDatasetItem<StrategyChoice>));
32 
34 
36 {
37  this->wireDecode(payload);
38 }
39 
40 template<encoding::Tag TAG>
41 size_t
43 {
44  size_t totalLength = 0;
45 
46  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
47  totalLength += m_name.wireEncode(encoder);
48 
49  totalLength += encoder.prependVarNumber(totalLength);
50  totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
51  return totalLength;
52 }
53 
55 
56 const Block&
58 {
59  if (m_wire.hasWire())
60  return m_wire;
61 
62  EncodingEstimator estimator;
63  size_t estimatedSize = wireEncode(estimator);
64 
65  EncodingBuffer buffer(estimatedSize, 0);
66  wireEncode(buffer);
67 
68  m_wire = buffer.block();
69  return m_wire;
70 }
71 
72 void
74 {
75  if (block.type() != tlv::nfd::StrategyChoice) {
76  NDN_THROW(Error("StrategyChoice", block.type()));
77  }
78 
79  m_wire = block;
80  m_wire.parse();
81  auto val = m_wire.elements_begin();
82 
83  if (val != m_wire.elements_end() && val->type() == tlv::Name) {
84  m_name.wireDecode(*val);
85  ++val;
86  }
87  else {
88  NDN_THROW(Error("missing required Name field"));
89  }
90 
91  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
92  val->parse();
93  if (val->elements().empty()) {
94  NDN_THROW(Error("expecting Strategy/Name"));
95  }
96  else {
97  m_strategy.wireDecode(*val->elements_begin());
98  }
99  ++val;
100  }
101  else {
102  NDN_THROW(Error("missing required Strategy field"));
103  }
104 }
105 
108 {
109  m_wire.reset();
110  m_name = name;
111  return *this;
112 }
113 
116 {
117  m_wire.reset();
118  m_strategy = strategy;
119  return *this;
120 }
121 
122 bool
124 {
125  return a.getName() == b.getName() && a.getStrategy() == b.getStrategy();
126 }
127 
128 std::ostream&
129 operator<<(std::ostream& os, const StrategyChoice& sc)
130 {
131  return os << "StrategyChoice("
132  << "Name: " << sc.getName() << ", "
133  << "Strategy: " << sc.getStrategy()
134  << ")";
135 }
136 
137 } // namespace nfd
138 } // namespace ndn
ndn::nfd::StrategyChoice::setStrategy
StrategyChoice & setStrategy(const Name &strategy)
Definition: strategy-choice.cpp:115
ndn::nfd::StrategyChoice::getStrategy
const Name & getStrategy() const
Definition: strategy-choice.hpp:71
ndn::nfd::StrategyChoice::wireEncode
const Block & wireEncode() const
Definition: strategy-choice.cpp:57
ndn::nfd::operator<<
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Definition: nfd-constants.cpp:36
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::nfd::StrategyChoice::StrategyChoice
StrategyChoice()
concepts.hpp
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::nfd::StrategyChoice::setName
StrategyChoice & setName(const Name &name)
Definition: strategy-choice.cpp:107
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::Name::wireDecode
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
ndn::encoding::prependNestedBlock
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
Definition: block-helpers.hpp:283
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::Name::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::tlv::nfd::StrategyChoice
@ StrategyChoice
Definition: tlv-nfd.hpp:99
ndn::tlv::Name
@ Name
Definition: tlv.hpp:67
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::nfd::StrategyChoice
represents an item in NFD StrategyChoice dataset
Definition: strategy-choice.hpp:37
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
tlv-nfd.hpp
block-helpers.hpp
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::name
Definition: name-component-types.hpp:33
ndn::tlv::nfd::Strategy
@ Strategy
Definition: tlv-nfd.hpp:44
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::nfd::StrategyChoice::getName
const Name & getName() const
Definition: strategy-choice.hpp:62
ndn::nfd::operator==
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
Definition: channel-status.cpp:97
ndn::nfd::StrategyChoice::Error
Definition: strategy-choice.hpp:40
strategy-choice.hpp
ndn::StatusDatasetItem
concept check for an item in a Status Dataset
Definition: concepts.hpp:116
ndn::nfd::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
encoding-buffer.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38
ndn::nfd::StrategyChoice::wireDecode
void wireDecode(const Block &wire)
Definition: strategy-choice.cpp:73