Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
manifest_inline.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.h>
19 #include <core/manifest_format.h>
20 #include <hicn/transport/portability/portability.h>
21 
22 #include <set>
23 
24 namespace transport {
25 
26 namespace core {
27 
28 template <typename Base, typename FormatTraits>
30  : public Manifest<Base, FormatTraits, ManifestInline<Base, FormatTraits>> {
31  using ManifestBase =
33 
34  using Hash = typename FormatTraits::Hash;
35  using HashType = typename FormatTraits::HashType;
36  using Suffix = typename FormatTraits::Suffix;
37  using SuffixList = typename FormatTraits::SuffixList;
38  using HashEntry = std::pair<auth::CryptoHashType, std::vector<uint8_t>>;
39 
40  public:
42 
43  ManifestInline(const core::Name &name, std::size_t signature_size = 0)
44  : ManifestBase(name, signature_size) {}
45 
46  template <typename T>
47  ManifestInline(T &&base) : ManifestBase(std::forward<T &&>(base)) {}
48 
49  static TRANSPORT_ALWAYS_INLINE ManifestInline *createManifest(
50  const core::Name &manifest_name, ManifestVersion version,
51  ManifestType type, HashType algorithm, bool is_last,
52  const Name &base_name, NextSegmentCalculationStrategy strategy,
53  std::size_t signature_size) {
54  auto manifest = new ManifestInline(manifest_name, signature_size);
55  manifest->setVersion(version);
56  manifest->setManifestType(type);
57  manifest->setHashAlgorithm(algorithm);
58  manifest->setFinalManifest(is_last);
59  manifest->setBaseName(base_name);
60  manifest->setNextSegmentCalculationStrategy(strategy);
61 
62  return manifest;
63  }
64 
65  ManifestInline &encodeImpl() {
66  ManifestBase::encoder_.encode();
67  return *this;
68  }
69 
70  ManifestInline &decodeImpl() {
71  base_name_ = ManifestBase::decoder_.getBaseName();
72  next_segment_strategy_ =
73  ManifestBase::decoder_.getNextSegmentCalculationStrategy();
74  suffix_hash_map_ = ManifestBase::decoder_.getSuffixHashList();
75 
76  return *this;
77  }
78 
79  std::size_t estimateManifestSizeImpl(std::size_t additional_entries = 0) {
80  return ManifestBase::encoder_.estimateSerializedLength(additional_entries);
81  }
82 
83  ManifestInline &setBaseName(const Name &name) {
84  base_name_ = name;
85  ManifestBase::encoder_.setBaseName(base_name_);
86  return *this;
87  }
88 
89  const Name &getBaseName() { return base_name_; }
90 
91  ManifestInline &addSuffixHash(Suffix suffix, const Hash &hash) {
92  ManifestBase::encoder_.addSuffixAndHash(suffix, hash);
93  return *this;
94  }
95 
96  // Call this function only after the decode function!
97  const SuffixList &getSuffixList() { return suffix_hash_map_; }
98 
99  ManifestInline &setNextSegmentCalculationStrategy(
100  NextSegmentCalculationStrategy strategy) {
101  next_segment_strategy_ = strategy;
102  ManifestBase::encoder_.setNextSegmentCalculationStrategy(
103  next_segment_strategy_);
104  return *this;
105  }
106 
107  NextSegmentCalculationStrategy getNextSegmentCalculationStrategy() {
108  return next_segment_strategy_;
109  }
110 
111  // Convert several manifests into a single map from suffixes to packet hashes.
112  // All manifests must have been decoded beforehand.
113  static std::unordered_map<Suffix, Hash> getSuffixMap(
114  const std::vector<ManifestInline *> &manifests) {
115  std::unordered_map<Suffix, Hash> suffix_map;
116 
117  for (auto manifest_ptr : manifests) {
118  HashType hash_type = manifest_ptr->getHashAlgorithm();
119  SuffixList suffix_list = manifest_ptr->getSuffixList();
120 
121  for (auto it = suffix_list.begin(); it != suffix_list.end(); ++it) {
122  Hash hash(it->second, Hash::getSize(hash_type), hash_type);
123  suffix_map[it->first] = hash;
124  }
125  }
126 
127  return suffix_map;
128  }
129 
130  static std::unordered_map<Suffix, Hash> getSuffixMap(
131  ManifestInline *manifest) {
132  return getSuffixMap(std::vector<ManifestInline *>{manifest});
133  }
134 
135  private:
136  core::Name base_name_;
137  NextSegmentCalculationStrategy next_segment_strategy_;
138  SuffixList suffix_hash_map_;
139 };
140 
141 } // namespace core
142 } // namespace transport
transport::core::Name
Definition: name.h:45
transport::core::hash
Definition: name.h:118
transport::core::Manifest
Definition: manifest.h:33
transport
Definition: forwarder_config.h:32
transport::core::ManifestInline
Definition: manifest_inline.h:29