diff options
Diffstat (limited to 'libtransport/src/hicn/transport/utils')
3 files changed, 210 insertions, 1 deletions
diff --git a/libtransport/src/hicn/transport/utils/CMakeLists.txt b/libtransport/src/hicn/transport/utils/CMakeLists.txt index 4ce1e537f..cbbca86ed 100644 --- a/libtransport/src/hicn/transport/utils/CMakeLists.txt +++ b/libtransport/src/hicn/transport/utils/CMakeLists.txt @@ -19,6 +19,7 @@ list(APPEND SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/signer.cc ${CMAKE_CURRENT_SOURCE_DIR}/verifier.cc ${CMAKE_CURRENT_SOURCE_DIR}/identity.cc + ${CMAKE_CURRENT_SOURCE_DIR}/suffix_strategy.cc ${CMAKE_CURRENT_SOURCE_DIR}/log.cc ${CMAKE_CURRENT_SOURCE_DIR}/membuf.cc ${CMAKE_CURRENT_SOURCE_DIR}/content_store.cc @@ -46,6 +47,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/crypto_hash.h ${CMAKE_CURRENT_SOURCE_DIR}/crypto_hash_type.h ${CMAKE_CURRENT_SOURCE_DIR}/identity.h + ${CMAKE_CURRENT_SOURCE_DIR}/suffix_strategy.h ${CMAKE_CURRENT_SOURCE_DIR}/conversions.h ${CMAKE_CURRENT_SOURCE_DIR}/linux.h ${CMAKE_CURRENT_SOURCE_DIR}/log.h @@ -78,4 +80,4 @@ if(NOT WIN32) endif() set(SOURCE_FILES ${SOURCE_FILES} PARENT_SCOPE) -set(HEADER_FILES ${HEADER_FILES} PARENT_SCOPE)
\ No newline at end of file +set(HEADER_FILES ${HEADER_FILES} PARENT_SCOPE) diff --git a/libtransport/src/hicn/transport/utils/suffix_strategy.cc b/libtransport/src/hicn/transport/utils/suffix_strategy.cc new file mode 100644 index 000000000..f3bcc4562 --- /dev/null +++ b/libtransport/src/hicn/transport/utils/suffix_strategy.cc @@ -0,0 +1,73 @@ +/* + * 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. + */ + +#include <hicn/transport/utils/suffix_strategy.h> + +using transport::core::NextSegmentCalculationStrategy; + +namespace utils { +std::uint32_t SuffixManifest::getNextSuffix() { + uint32_t next_suffix; + + switch (suffix_stragegy_) { + case NextSegmentCalculationStrategy::INCREMENTAL: + if (!nb_segments_) { + throw errors::RuntimeException( + "The number of segments in a manifest must be set " + "before assigning incremental suffixes."); + } + /* The current manifest's suffix + the number of segments in a */ + /* manifest give the suffix of the last segment in the manifest. */ + /* The next manifest's suffix is therefore that number plus one. */ + next_suffix = suffix_ + nb_segments_ + 1; + break; + + default: + throw errors::RuntimeException("Unknown suffix strategy."); + } + + return next_suffix; +} + +std::uint32_t SuffixContent::getNextSuffix() { + uint32_t next_suffix; + + switch (suffix_stragegy_) { + case NextSegmentCalculationStrategy::INCREMENTAL: + next_suffix = suffix_ + 1; + if (making_manifest_) { + if (!nb_segments_) { + throw errors::RuntimeException( + "The number of segments in a manifest must be set " + "before assigning incremental suffixes."); + } + + content_counter_++; + /* If the counter have reached the manifest's capacity, + * it means that the next suffix will be a manifest, so we skip it. */ + if (content_counter_ % nb_segments_ == 0) { + next_suffix++; + content_counter_ = 0; + } + } + break; + + default: + throw errors::RuntimeException("Unknown suffix strategy."); + } + + return next_suffix; +} +} // namespace utils 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 |