Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
rtc_indexer.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 <protocols/errors.h>
19 #include <protocols/fec_utils.h>
20 #include <protocols/indexer.h>
21 #include <protocols/rtc/rtc_consts.h>
22 #include <protocols/transport_protocol.h>
23 
24 #include <deque>
25 
26 namespace transport {
27 
28 namespace interface {
29 class ConsumerSocket;
30 }
31 
32 namespace protocol {
33 
34 namespace rtc {
35 
36 template <uint32_t LIMIT = MIN_PROBE_SEQ>
37 class RtcIndexer : public Indexer {
38  public:
41  : Indexer(icn_socket, transport),
42  first_suffix_(1),
43  next_suffix_(first_suffix_),
44  fec_type_(fec::FECType::UNKNOWN),
45  n_fec_(0),
46  n_current_fec_(n_fec_) {}
47 
48  RtcIndexer(RtcIndexer &&other) : Indexer(std::forward<Indexer>(other)) {}
49 
50  ~RtcIndexer() {}
51 
52  void reset() override {
53  next_suffix_ = first_suffix_;
54  n_fec_ = 0;
55  }
56 
57  uint32_t checkNextSuffix() override { return next_suffix_; }
58 
59  uint32_t getNextSuffix() override {
60  if (isFec(next_suffix_)) {
61  if (n_current_fec_) {
62  auto ret = next_suffix_++;
63  n_current_fec_--;
64  return ret;
65  } else {
66  n_current_fec_ = n_fec_;
67  next_suffix_ = nextSource(next_suffix_);
68  }
69  } else if (!n_current_fec_) {
70  n_current_fec_ = n_fec_;
71  }
72 
73  return (next_suffix_++ % LIMIT);
74  }
75 
76  void setFirstSuffix(uint32_t suffix) override {
77  first_suffix_ = suffix % LIMIT;
78  }
79 
80  uint32_t getFirstSuffix() override { return first_suffix_; }
81 
82  uint32_t jumpToIndex(uint32_t index) override {
83  next_suffix_ = index % LIMIT;
84  return next_suffix_;
85  }
86 
88  core::ContentObject &content_object,
89  bool reassembly) override {
90  setVerifier();
91  auto ret = verifier_->verifyPackets(&content_object);
92 
93  switch (ret) {
94  case auth::VerificationPolicy::ACCEPT: {
95  if (reassembly) {
96  reassembly_->reassemble(content_object);
97  }
98  break;
99  }
100 
101  case auth::VerificationPolicy::UNKNOWN:
102  case auth::VerificationPolicy::DROP: {
103  transport_->onPacketDropped(
104  interest, content_object,
105  make_error_code(protocol_error::verification_failed));
106  break;
107  }
108 
109  case auth::VerificationPolicy::ABORT: {
110  transport_->onContentReassembled(
111  make_error_code(protocol_error::session_aborted));
112  break;
113  }
114  }
115  }
116 
120  uint32_t getNextReassemblySegment() override {
122  "Get reassembly segment called on rtc indexer. RTC indexer does not "
123  "provide "
124  "reassembly.");
125  }
126 
127  bool isFinalSuffixDiscovered() override { return true; }
128 
129  uint32_t getFinalSuffix() override { return LIMIT; }
130 
131  void enableFec(fec::FECType fec_type) override { fec_type_ = fec_type; }
132 
133  void disableFec() override { fec_type_ = fec::FECType::UNKNOWN; }
134 
135  void setNFec(uint32_t n_fec) override {
136  n_fec_ = n_fec;
137  n_current_fec_ = n_fec_;
138  }
139 
140  uint32_t getNFec() override { return n_fec_; }
141 
142  bool isFec(uint32_t index) override {
143  return isFec(fec_type_, index, first_suffix_);
144  }
145 
146  double getFecOverhead() override {
147  if (fec_type_ == fec::FECType::UNKNOWN) {
148  return 0;
149  }
150 
151  double k = (double)fec::FECUtils::getSourceSymbols(fec_type_);
152  return (double)n_fec_ / k;
153  }
154 
155  double getMaxFecOverhead() override {
156  if (fec_type_ == fec::FECType::UNKNOWN) {
157  return 0;
158  }
159 
160  double k = (double)fec::FECUtils::getSourceSymbols(fec_type_);
161  double n = (double)fec::FECUtils::getBlockSymbols(fec_type_);
162  return (double)(n - k) / k;
163  }
164 
165  static bool isFec(fec::FECType fec_type, uint32_t index,
166  uint32_t first_suffix) {
167  if (index < LIMIT) {
168  return fec::FECUtils::isFec(fec_type, index, first_suffix);
169  }
170 
171  return false;
172  }
173 
174  static uint32_t nextSource(fec::FECType fec_type, uint32_t index,
175  uint32_t first_suffix) {
176  return fec::FECUtils::nextSource(fec_type, index, first_suffix) % LIMIT;
177  }
178 
179  private:
180  uint32_t nextSource(uint32_t index) {
181  return nextSource(fec_type_, index, first_suffix_);
182  }
183 
184  private:
185  uint32_t first_suffix_;
186  uint32_t next_suffix_;
187  fec::FECType fec_type_;
188  bool fec_enabled_;
189  uint32_t n_fec_;
190  uint32_t n_current_fec_;
191 };
192 
193 } // namespace rtc
194 } // namespace protocol
195 } // namespace transport
transport::protocol::rtc::RtcIndexer::getNextReassemblySegment
uint32_t getNextReassemblySegment() override
Definition: rtc_indexer.h:120
transport::protocol::rtc::RtcIndexer
Definition: rtc_indexer.h:37
transport::interface::ConsumerSocket
Main interface for consumer applications.
Definition: socket_consumer.h:48
transport::protocol::rtc::RtcIndexer::isFinalSuffixDiscovered
bool isFinalSuffixDiscovered() override
Definition: rtc_indexer.h:127
transport::protocol::rtc::RtcIndexer::setNFec
void setNFec(uint32_t n_fec) override
Definition: rtc_indexer.h:135
transport::protocol::Indexer::setVerifier
virtual void setVerifier()
transport::protocol::rtc::RtcIndexer::onContentObject
void onContentObject(core::Interest &interest, core::ContentObject &content_object, bool reassembly) override
Definition: rtc_indexer.h:87
transport::protocol::rtc::RtcIndexer::setFirstSuffix
void setFirstSuffix(uint32_t suffix) override
Definition: rtc_indexer.h:76
transport::core::ContentObject
Definition: content_object.h:29
transport::protocol::rtc::RtcIndexer::checkNextSuffix
uint32_t checkNextSuffix() override
Definition: rtc_indexer.h:57
transport::core::Interest
Definition: interest.h:30
transport
Definition: forwarder_config.h:32
transport::protocol::Indexer
Definition: indexer.h:35
transport::protocol::TransportProtocol
Definition: transport_protocol.h:41
errors::RuntimeException
Definition: runtime_exception.h:25
transport::protocol::rtc::RtcIndexer::reset
void reset() override
Definition: rtc_indexer.h:52
transport::protocol::Reassembly::reassemble
virtual void reassemble(core::ContentObject &content_object)=0
transport::protocol::rtc::RtcIndexer::jumpToIndex
uint32_t jumpToIndex(uint32_t index) override
Definition: rtc_indexer.h:82