Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
manifest.h
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <core/manifest_format.h>
19 #include <hicn/transport/core/content_object.h>
20 #include <hicn/transport/core/name.h>
21 
22 #include <set>
23 
24 namespace transport {
25 
26 namespace core {
27 
28 using typename core::Name;
29 using typename core::Packet;
30 using typename core::PayloadType;
31 
32 template <typename Base, typename FormatTraits, typename ManifestImpl>
33 class Manifest : public Base {
34  static_assert(std::is_base_of<Packet, Base>::value,
35  "Base must inherit from packet!");
36 
37  public:
38  // core::ContentObjectManifest::Ptr
39 
40  using Encoder = typename FormatTraits::Encoder;
41  using Decoder = typename FormatTraits::Decoder;
42 
43  Manifest(std::size_t signature_size = 0)
44  : Base(HF_INET6_TCP_AH, signature_size),
45  encoder_(*this, signature_size),
46  decoder_(*this) {
47  Base::setPayloadType(PayloadType::MANIFEST);
48  }
49 
50  Manifest(const core::Name &name, std::size_t signature_size = 0)
51  : Base(name, HF_INET6_TCP_AH, signature_size),
52  encoder_(*this, signature_size),
53  decoder_(*this) {
54  Base::setPayloadType(PayloadType::MANIFEST);
55  }
56 
57  template <typename T>
58  Manifest(T &&base)
59  : Base(std::forward<T &&>(base)),
60  encoder_(*this, 0, false),
61  decoder_(*this) {
62  Base::setPayloadType(PayloadType::MANIFEST);
63  }
64 
65  virtual ~Manifest() = default;
66 
67  std::size_t estimateManifestSize(std::size_t additional_entries = 0) {
68  return static_cast<ManifestImpl &>(*this).estimateManifestSizeImpl(
69  additional_entries);
70  }
71 
72  /*
73  * After the call to encode, users MUST call clear before adding data
74  * to the manifest.
75  */
76  Manifest &encode() { return static_cast<ManifestImpl &>(*this).encodeImpl(); }
77 
78  Manifest &decode() {
79  Manifest::decoder_.decode();
80 
81  manifest_type_ = decoder_.getManifestType();
82  hash_algorithm_ = decoder_.getHashAlgorithm();
83  is_last_ = decoder_.getIsFinalManifest();
84 
85  return static_cast<ManifestImpl &>(*this).decodeImpl();
86  }
87 
88  static std::size_t getManifestHeaderSize() {
89  return Encoder::getManifestHeaderSize();
90  }
91 
92  static std::size_t getManifestEntrySize() {
93  return Encoder::getManifestEntrySize();
94  }
95 
96  Manifest &setManifestType(ManifestType type) {
97  manifest_type_ = type;
98  encoder_.setManifestType(manifest_type_);
99  return *this;
100  }
101 
102  Manifest &setHashAlgorithm(auth::CryptoHashType hash_algorithm) {
103  hash_algorithm_ = hash_algorithm;
104  encoder_.setHashAlgorithm(hash_algorithm_);
105  return *this;
106  }
107 
108  auth::CryptoHashType getHashAlgorithm() { return hash_algorithm_; }
109 
110  ManifestType getManifestType() const { return manifest_type_; }
111 
112  bool isFinalManifest() const { return is_last_; }
113 
114  Manifest &setVersion(ManifestVersion version) {
115  encoder_.setVersion(version);
116  return *this;
117  }
118 
119  Manifest &setFinalBlockNumber(std::uint32_t final_block_number) {
120  encoder_.setFinalBlockNumber(final_block_number);
121  return *this;
122  }
123 
124  uint32_t getFinalBlockNumber() const {
125  return decoder_.getFinalBlockNumber();
126  }
127 
128  ManifestVersion getVersion() const { return decoder_.getVersion(); }
129 
130  Manifest &setFinalManifest(bool is_final_manifest) {
131  encoder_.setIsFinalManifest(is_final_manifest);
132  is_last_ = is_final_manifest;
133  return *this;
134  }
135 
136  Manifest &clear() {
137  encoder_.clear();
138  decoder_.clear();
139  return *this;
140  }
141 
142  protected:
143  ManifestType manifest_type_;
144  auth::CryptoHashType hash_algorithm_;
145  bool is_last_;
146 
147  Encoder encoder_;
148  Decoder decoder_;
149 };
150 
151 } // end namespace core
152 
153 } // end namespace transport
transport::core::Name
Definition: name.h:45
transport::core::Manifest
Definition: manifest.h:33
transport
Definition: forwarder_config.h:32