aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/utils/suffix_strategy.h
diff options
context:
space:
mode:
authorOlivier Roques <oroques@cisco.com>2019-11-07 10:05:52 +0000
committerAlberto Compagno <acompagn+fdio@cisco.com>2019-11-14 08:59:00 +0100
commitf2b7325ae6114b6b5b9e4d32c0a7cdc07576f224 (patch)
tree0413a1475a3cba5bba7db872cf0a4a5defaf47d5 /libtransport/src/hicn/transport/utils/suffix_strategy.h
parent4f57ca72e8131e5cfb023b26417b924e774d5e73 (diff)
[HICN-392] Assign independent suffixes for manifests/contents
This patch introduces a new class, SuffixStrategy and two sub-classes, SuffixContent and SuffixManifest which allow to independently assign suffixes to contents and manifests respectively. The produce() function in socket_producer.cc has also been changed to use them. Given a strategy and an offset (and optionally the capacity of a manifest), these classes automatically compute the correct next suffixes for both type of data (manifest or content). This removes the burden of having to manage suffixes for instance when producing or when retrieving content, and could be expanded to add more strategy in the future. Currently the only existing strategy is "INCREMENTAL": manifests with capacity N have a suffix multiple of N+1: 0, N+1, 2(N+1) etc. Contents have a suffix incremented by 1 except when it conflicts with a manifest: 1, 2, ..., N, N+2, N+3, ..., 2N+1, 2N+3... Signed-off-by: Olivier Roques <olvrqs@gmail.com> Change-Id: Ia7692d7325240de7bea6e38b668077042e5f8758 Signed-off-by: Alberto Compagno <acompagn+fdio@cisco.com>
Diffstat (limited to 'libtransport/src/hicn/transport/utils/suffix_strategy.h')
-rw-r--r--libtransport/src/hicn/transport/utils/suffix_strategy.h134
1 files changed, 134 insertions, 0 deletions
diff --git a/libtransport/src/hicn/transport/utils/suffix_strategy.h b/libtransport/src/hicn/transport/utils/suffix_strategy.h
new file mode 100644
index 000000000..99e557380
--- /dev/null
+++ b/libtransport/src/hicn/transport/utils/suffix_strategy.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <hicn/transport/core/manifest_format.h>
+
+namespace utils {
+class SuffixStrategy {
+ public:
+ SuffixStrategy(
+ transport::core::NextSegmentCalculationStrategy suffix_stragegy,
+ std::uint32_t start_offset)
+ : suffix_stragegy_(suffix_stragegy),
+ suffix_(start_offset),
+ nb_segments_(0) {}
+
+ transport::core::NextSegmentCalculationStrategy getSuffixStrategy() {
+ return suffix_stragegy_;
+ }
+
+ std::uint32_t getSuffix() { return suffix_; }
+
+ virtual std::uint32_t getNextSuffix() = 0;
+
+ void updateSuffix(std::uint32_t new_suffix) { suffix_ = new_suffix; }
+
+ std::size_t getNbSegments() { return nb_segments_; }
+
+ void setNbSegments(std::size_t nb_segments) { nb_segments_ = nb_segments; }
+
+ void reset(std::uint32_t reset_suffix) {
+ suffix_ = reset_suffix;
+ nb_segments_ = 0;
+ }
+
+ ~SuffixStrategy() {}
+
+ protected:
+ transport::core::NextSegmentCalculationStrategy suffix_stragegy_;
+ std::uint32_t suffix_;
+ std::size_t nb_segments_;
+};
+
+class SuffixManifest : public SuffixStrategy {
+ public:
+ SuffixManifest(
+ transport::core::NextSegmentCalculationStrategy suffix_stragegy,
+ std::uint32_t start_offset)
+ : SuffixStrategy(suffix_stragegy, start_offset) {}
+
+ std::uint32_t getNextSuffix();
+
+ SuffixManifest operator++() {
+ uint32_t next_suffix = getNextSuffix();
+ updateSuffix(next_suffix);
+ return SuffixManifest(suffix_stragegy_, next_suffix);
+ }
+
+ SuffixManifest operator++(int) {
+ SuffixManifest temp_suffix(suffix_stragegy_, suffix_);
+ uint32_t next_suffix = getNextSuffix();
+ updateSuffix(next_suffix);
+ return temp_suffix;
+ }
+
+ SuffixManifest operator+(uint32_t shift) {
+ for (uint32_t i = 0; i < shift; i++) {
+ updateSuffix(getNextSuffix());
+ }
+ return SuffixManifest(suffix_stragegy_, getSuffix());
+ }
+};
+
+class SuffixContent : public SuffixStrategy {
+ public:
+ SuffixContent(transport::core::NextSegmentCalculationStrategy suffix_stragegy,
+ std::uint32_t start_offset, bool making_manifest)
+ : SuffixStrategy(suffix_stragegy, start_offset),
+ making_manifest_(making_manifest),
+ content_counter_(0) {}
+
+ SuffixContent(transport::core::NextSegmentCalculationStrategy suffix_stragegy,
+ std::uint32_t start_offset)
+ : SuffixContent(suffix_stragegy, start_offset, false) {}
+
+ std::uint32_t getNextSuffix();
+
+ SuffixContent operator++() {
+ uint32_t next_suffix = getNextSuffix();
+ updateSuffix(next_suffix);
+ return SuffixContent(suffix_stragegy_, next_suffix, making_manifest_);
+ }
+
+ SuffixContent operator++(int) {
+ SuffixContent temp_suffix(suffix_stragegy_, suffix_, making_manifest_);
+ uint32_t next_suffix = getNextSuffix();
+ updateSuffix(next_suffix);
+ return temp_suffix;
+ }
+
+ SuffixContent operator+(uint32_t shift) {
+ for (uint32_t i = 0; i < shift; i++) {
+ updateSuffix(getNextSuffix());
+ }
+ return SuffixContent(suffix_stragegy_, getSuffix(), making_manifest_);
+ }
+
+ void setUsingManifest(bool value) { making_manifest_ = value; }
+
+ void reset(std::uint32_t reset_suffix) {
+ SuffixStrategy::reset(reset_suffix);
+ content_counter_ = 0;
+ }
+
+ protected:
+ bool making_manifest_;
+ /* content_counter_ keeps track of the number of segments */
+ /* between two manifests */
+ uint32_t content_counter_;
+};
+} // namespace utils