Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
rtc_ldr.h
1 /*
2  * Copyright (c) 2017-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 #include <hicn/transport/config.h>
18 #include <hicn/transport/core/asio_wrapper.h>
19 #include <hicn/transport/core/content_object.h>
20 #include <hicn/transport/core/name.h>
21 #include <protocols/indexer.h>
22 #include <protocols/rtc/rtc_consts.h>
23 #include <protocols/rtc/rtc_state.h>
24 
25 #include <functional>
26 #include <map>
27 #include <unordered_map>
28 
29 namespace transport {
30 
31 namespace protocol {
32 
33 namespace rtc {
34 
36  : public std::enable_shared_from_this<RTCLossDetectionAndRecovery> {
37  struct rtx_state_ {
38  uint64_t first_send_;
39  uint64_t next_send_;
40  uint32_t rtx_count_;
41  };
42 
43  using rtxState = struct rtx_state_;
44  using SendRtxCallback = std::function<void(uint32_t)>;
45 
46  public:
47  RTCLossDetectionAndRecovery(Indexer *indexer, SendRtxCallback &&callback,
48  asio::io_service &io_service);
49 
51 
52  void setState(std::shared_ptr<RTCState> state) { state_ = state; }
53  void setFecParams(uint32_t n, uint32_t k) {
54  n_ = n;
55  k_ = k;
56  }
57  void turnOnRTX();
58  void turnOffRTX();
59  bool isRtxOn() { return rtx_on_; }
60 
61  void onNewRound(bool in_sync);
62  void onTimeout(uint32_t seq);
63  void onPacketRecoveredFec(uint32_t seq);
64  void onDataPacketReceived(const core::ContentObject &content_object);
65  void onNackPacketReceived(const core::ContentObject &nack);
66  void onProbePacketReceived(const core::ContentObject &probe);
67 
68  void clear();
69 
70  bool isRtx(uint32_t seq) {
71  if (rtx_state_.find(seq) != rtx_state_.end()) return true;
72  return false;
73  }
74 
75  private:
76  void addToRetransmissions(uint32_t start, uint32_t stop);
77  uint64_t computeNextSend(uint32_t seq, bool new_rtx);
78  void retransmit();
79  void scheduleNextRtx();
80  bool deleteRtx(uint32_t seq);
81  void scheduleSentinelTimer(uint64_t expires_from_now);
82  void sentinelTimer();
83  uint32_t computeFecPacketsToAsk(bool in_sync);
84 
85  uint64_t getNow() {
86  using namespace std::chrono;
87  uint64_t now =
88  duration_cast<milliseconds>(steady_clock::now().time_since_epoch())
89  .count();
90  return now;
91  }
92 
93  // this map keeps track of the retransmitted interest, ordered from the oldest
94  // to the newest one. the state contains the timer of the first send of the
95  // interest (from pendingIntetests_), the timer of the next send (key of the
96  // multimap) and the number of rtx
97  std::map<uint32_t, rtxState> rtx_state_;
98  // this map stored the rtx by timer. The key is the time at which the rtx
99  // should be sent, and the val is the interest seq number
100  std::multimap<uint64_t, uint32_t> rtx_timers_;
101 
102  // lost packets that will be recovered with fec
103  std::unordered_set<uint32_t> recover_with_fec_;
104 
105  bool rtx_on_;
106  bool fec_on_;
107  uint64_t next_rtx_timer_;
108  uint64_t last_event_;
109  uint64_t sentinel_timer_interval_;
110 
111  // fec params
112  uint32_t n_;
113  uint32_t k_;
114 
115  std::unique_ptr<asio::steady_timer> timer_;
116  std::unique_ptr<asio::steady_timer> sentinel_timer_;
117  std::shared_ptr<RTCState> state_;
118 
119  Indexer *indexer_;
120 
121  SendRtxCallback send_rtx_callback_;
122 };
123 
124 } // end namespace rtc
125 
126 } // end namespace protocol
127 
128 } // end namespace transport
transport::core::ContentObject
Definition: content_object.h:29
transport
Definition: forwarder_config.h:32
transport::protocol::rtc::RTCLossDetectionAndRecovery
Definition: rtc_ldr.h:35
transport::protocol::Indexer
Definition: indexer.h:35