aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/protocols/rtc/rtc_indexer.h
blob: cda156b22ecd1cef9ea184a8aeff0816190bcc3b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2021 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 <protocols/errors.h>
#include <protocols/fec_utils.h>
#include <protocols/indexer.h>
#include <protocols/rtc/probe_handler.h>
#include <protocols/rtc/rtc_consts.h>
#include <protocols/transport_protocol.h>
#include <utils/suffix_strategy.h>

#include <deque>

namespace transport {

namespace interface {
class ConsumerSocket;
}

namespace protocol {

namespace rtc {

template <uint32_t LIMIT = MIN_PROBE_SEQ>
class RtcIndexer : public Indexer {
 public:
  RtcIndexer(implementation::ConsumerSocket *icn_socket,
             TransportProtocol *transport)
      : Indexer(icn_socket, transport),
        first_suffix_(1),
        next_suffix_(first_suffix_),
        fec_type_(fec::FECType::UNKNOWN),
        n_fec_(0),
        n_current_fec_(n_fec_) {}

  RtcIndexer(RtcIndexer &&other) : Indexer(std::forward<Indexer>(other)) {}

  ~RtcIndexer() {}

  void reset() override {
    next_suffix_ = first_suffix_;
    n_fec_ = 0;
  }

  uint32_t checkNextSuffix() const override { return next_suffix_; }

  uint32_t getNextSuffix() override {
    if (isFec(next_suffix_)) {
      if (n_current_fec_) {
        auto ret = next_suffix_++;
        n_current_fec_--;
        return ret;
      } else {
        n_current_fec_ = n_fec_;
        next_suffix_ = nextSource(next_suffix_);
      }
    } else if (!n_current_fec_) {
      n_current_fec_ = n_fec_;
    }

    return (next_suffix_++ % LIMIT);
  }

  void setFirstSuffix(uint32_t suffix) override {
    first_suffix_ = suffix % LIMIT;
  }

  uint32_t getFirstSuffix() const override { return first_suffix_; }

  uint32_t jumpToIndex(uint32_t index) override {
    next_suffix_ = index % LIMIT;
    return next_suffix_;
  }

  void onContentObject(core::Interest &interest,
                       core::ContentObject &content_object,
                       bool reassembly) override {
    if (reassembly) {
      reassembly_->reassemble(content_object);
    }
  }

  /**
   * Retrieve the next segment to be reassembled.
   */
  uint32_t getNextReassemblySegment() override {
    throw errors::RuntimeException(
        "Get reassembly segment called on rtc indexer. RTC indexer does not "
        "provide reassembly.");
  }

  bool isFinalSuffixDiscovered() override { return true; }

  uint32_t getFinalSuffix() const override { return LIMIT; }

  void enableFec(fec::FECType fec_type) override { fec_type_ = fec_type; }

  void disableFec() override { fec_type_ = fec::FECType::UNKNOWN; }

  void setNFec(uint32_t n_fec) override {
    n_fec_ = n_fec;
    n_current_fec_ = n_fec_;
  }

  uint32_t getNFec() const override { return n_fec_; }

  bool isFec(uint32_t index) override {
    return isFec(fec_type_, index, first_suffix_);
  }

  double getFecOverhead() const override {
    if (fec_type_ == fec::FECType::UNKNOWN) {
      return 0;
    }

    double k = (double)fec::FECUtils::getSourceSymbols(fec_type_);
    return (double)n_fec_ / k;
  }

  double getMaxFecOverhead() const override {
    if (fec_type_ == fec::FECType::UNKNOWN) {
      return 0;
    }

    double k = (double)fec::FECUtils::getSourceSymbols(fec_type_);
    double n = (double)fec::FECUtils::getBlockSymbols(fec_type_);
    return (double)(n - k) / k;
  }

  static bool isFec(fec::FECType fec_type, uint32_t index,
                    uint32_t first_suffix) {
    if (index < LIMIT) {
      return fec::FECUtils::isFec(fec_type, index, first_suffix);
    }

    return false;
  }

  static uint32_t nextSource(fec::FECType fec_type, uint32_t index,
                             uint32_t first_suffix) {
    return fec::FECUtils::nextSource(fec_type, index, first_suffix) % LIMIT;
  }

 private:
  uint32_t nextSource(uint32_t index) {
    return nextSource(fec_type_, index, first_suffix_);
  }

 private:
  uint32_t first_suffix_;
  uint32_t next_suffix_;
  fec::FECType fec_type_;
  bool fec_enabled_;
  uint32_t n_fec_;
  uint32_t n_current_fec_;
};

}  // namespace rtc
}  // namespace protocol
}  // namespace transport