aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/utils/suffix_strategy.h
blob: 4358d12f08977b5b095f47446fee23021ee04d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * 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_;
  }

  void setSuffixStrategy(
      transport::core::NextSegmentCalculationStrategy strategy) {
    suffix_stragegy_ = strategy;
  }

  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