Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
fec_utils.h
1 /*
2  * Copyright (c) 2021 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 <hicn/transport/config.h>
19 #include <hicn/transport/core/content_object.h>
20 #include <hicn/transport/errors/not_implemented_exception.h>
21 #include <protocols/fec/rs.h>
22 
23 #if ENABLE_RELY
24 #include <protocols/fec/rely.h>
25 #endif
26 
27 #include <functional>
28 
29 namespace transport {
30 namespace protocol {
31 
32 namespace fec {
33 
34 #if ENABLE_RELY
35 #define foreach_fec_type foreach_rs_fec_type foreach_rely_fec_type
36 #else
37 #define foreach_fec_type foreach_rs_fec_type
38 #endif
39 
40 #define ENUM_FROM_MACRO(name, k, n) name##_K##k##_N##n
41 #define ENUM_FROM_MACRO_STR(name, k, n) #name "_K" #k "_N" #n
42 
43 enum class FECType : uint8_t {
44 #define _(name, k, n) ENUM_FROM_MACRO(name, k, n),
45  foreach_fec_type
46 #undef _
47  UNKNOWN
48 };
49 
50 #define ENUM_FROM_MACRO2(name, k, n) FECType::ENUM_FROM_MACRO(name, k, n)
51 
52 class FECUtils {
53  public:
54  static FECType fecTypeFromString(const char *fec_type) {
55 #define _(name, k, n) \
56  do { \
57  if (strncmp(fec_type, ENUM_FROM_MACRO_STR(name, k, n), \
58  strlen(ENUM_FROM_MACRO_STR(name, k, n))) == 0) { \
59  return ENUM_FROM_MACRO2(name, k, n); \
60  } \
61  } while (0);
62  foreach_fec_type
63 #undef _
64 
65  return FECType::UNKNOWN;
66  }
67 
68  static bool isFec(FECType fec_type, uint32_t index, uint32_t seq_offset = 0) {
69  switch (fec_type) {
70 #define _(name, k, n) \
71  case ENUM_FROM_MACRO2(name, k, n): \
72  return FecInfo<Code<k, n>>::isFec(index - (seq_offset % n));
73 
74  foreach_fec_type
75 #undef _
76  default : return false;
77  }
78  }
79 
80  static uint32_t nextSource(FECType fec_type, uint32_t index,
81  uint32_t seq_offset = 0) {
82  switch (fec_type) {
83 #define _(name, k, n) \
84  case ENUM_FROM_MACRO2(name, k, n): \
85  return FecInfo<Code<k, n>>::nextSource(index) + (seq_offset % n);
86 
87  foreach_fec_type
88 #undef _
89  default : throw std::runtime_error("Unknown fec type");
90  }
91  }
92 
93  static uint32_t getSourceSymbols(FECType fec_type) {
94  switch (fec_type) {
95 #define _(name, k, n) \
96  case ENUM_FROM_MACRO2(name, k, n): \
97  return k;
98  foreach_fec_type
99 #undef _
100  default : throw std::runtime_error("Unknown fec type");
101  }
102  }
103 
104  static uint32_t getBlockSymbols(FECType fec_type) {
105  switch (fec_type) {
106 #define _(name, k, n) \
107  case ENUM_FROM_MACRO2(name, k, n): \
108  return n;
109  foreach_fec_type
110 #undef _
111  default : throw std::runtime_error("Unknown fec type");
112  }
113  }
114 
115  static std::unique_ptr<ProducerFEC> getEncoder(FECType fec_type,
116  uint32_t seq_offset = 0) {
117  return factoryEncoder(fec_type, seq_offset);
118  }
119 
120  static std::unique_ptr<ConsumerFEC> getDecoder(FECType fec_type,
121  uint32_t seq_offset = 0) {
122  return factoryDencoder(fec_type, seq_offset);
123  }
124 
125  private:
126  static std::unique_ptr<ProducerFEC> factoryEncoder(FECType fec_type,
127  uint32_t seq_offset) {
128  switch (fec_type) {
129 #define _(name, k, n) \
130  case ENUM_FROM_MACRO2(name, k, n): \
131  return std::make_unique<name##Encoder>(k, n, seq_offset);
132 
133  foreach_fec_type
134 #undef _
135  default : throw std::runtime_error("Unknown fec type");
136  }
137  }
138 
139  static std::unique_ptr<ConsumerFEC> factoryDencoder(FECType fec_type,
140  uint32_t seq_offset) {
141  switch (fec_type) {
142 #define _(name, k, n) \
143  case ENUM_FROM_MACRO2(name, k, n): \
144  return std::make_unique<name##Decoder>(k, n, seq_offset);
145 
146  foreach_fec_type
147 #undef _
148  default : throw std::runtime_error("Unknown fec type");
149  }
150  }
151 }; // namespace fec
152 
153 } // namespace fec
154 } // namespace protocol
155 } // namespace transport
transport::protocol::fec::FECUtils
Definition: fec_utils.h:52
transport
Definition: forwarder_config.h:32