aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/protocols/prod_protocol_rtc.cc
blob: cb8dff6e49bdb3d9c5317482d20beaea46d8e4da (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
 * 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.
 */

#include <glog/logging.h>
#include <hicn/transport/core/global_object_pool.h>
#include <implementation/socket_producer.h>
#include <protocols/prod_protocol_rtc.h>
#include <protocols/rtc/probe_handler.h>
#include <protocols/rtc/rtc_consts.h>
#include <stdlib.h>
#include <time.h>

#include <unordered_set>

namespace transport {
namespace protocol {

using Format = core::Packet::Format;

RTCProductionProtocol::RTCProductionProtocol(
    implementation::ProducerSocket *icn_socket)
    : ProductionProtocol(icn_socket),
      current_seg_(1),
      prev_produced_bytes_(0),
      prev_produced_packets_(0),
      produced_bytes_(0),
      produced_packets_(0),
      max_packet_production_(UINT32_MAX),
      bytes_production_rate_(UINT32_MAX),
      packets_production_rate_(0),
      last_produced_data_ts_(0),
      last_round_(utils::SteadyTime::nowMs().count()),
      allow_delayed_nacks_(false),
      pending_fec_pace_(false),
      max_len_(0),
      queue_len_(0),
      data_aggregation_(true),
      data_aggregation_timer_switch_(false) {
  std::uniform_int_distribution<> dis(0, 255);
  prod_label_ = dis(gen_);
  cache_label_ = (prod_label_ + 1) % 256;
  round_timer_ =
      std::make_unique<asio::steady_timer>(portal_->getThread().getIoService());
  fec_pacing_timer_ =
      std::make_unique<asio::steady_timer>(portal_->getThread().getIoService());
  app_packets_timer_ =
      std::make_unique<asio::steady_timer>(portal_->getThread().getIoService());
  setOutputBufferSize(10000);
}

RTCProductionProtocol::~RTCProductionProtocol() {}

void RTCProductionProtocol::setProducerParam() {
  // Flow name: here we assume there is only one prefix registered in the portal
  flow_name_ = portal_->getServedNamespaces().begin()->makeName();

  // Default format
  core::Packet::Format default_format;
  socket_->getSocketOption(interface::GeneralTransportOptions::PACKET_FORMAT,
                           default_format);

  // FEC
  using namespace std::placeholders;
  enableFEC(std::bind(&RTCProductionProtocol::onFecPackets, this, _1),
            std::bind(&RTCProductionProtocol::getBuffer, this, _1));

  // Aggregated data
  socket_->getSocketOption(interface::RtcTransportOptions::AGGREGATED_DATA,
                           data_aggregation_);

  size_t signature_size = signer_->getSignatureFieldSize();
  data_header_format_ = {!manifest_max_capacity_
                             ? Packet::toAHFormat(default_format)
                             : default_format,
                         !manifest_max_capacity_ ? signature_size : 0};
  manifest_header_format_ = {Packet::toAHFormat(default_format),
                             signature_size};
  nack_header_format_ = {Packet::toAHFormat(default_format), signature_size};
  fec_header_format_ = {Packet::toAHFormat(default_format), signature_size};

  // Initialize verifier for aggregated interests
  std::shared_ptr<auth::Verifier> verifier;
  socket_->getSocketOption(implementation::GeneralTransportOptions::VERIFIER,
                           verifier);
  verifier_ = std::make_shared<rtc::RTCVerifier>(verifier, 0, 0);

  // Schedule round timer
  scheduleRoundTimer();
}

void RTCProductionProtocol::scheduleRoundTimer() {
  round_timer_->expires_from_now(
      std::chrono::milliseconds(rtc::PRODUCER_STATS_INTERVAL));
  std::weak_ptr<RTCProductionProtocol> self = shared_from_this();
  round_timer_->async_wait([self](const std::error_code &ec) {
    if (ec) return;

    auto sp = self.lock();
    if (sp && sp->isRunning()) {
      sp->updateStats(true);
    }
  });
}

void RTCProductionProtocol::updateStats(bool new_round) {
  uint64_t now = utils::SteadyTime::nowMs().count();
  uint64_t duration = now - last_round_;
  if (!new_round) {
    duration += rtc::PRODUCER_STATS_INTERVAL;
  } else {
    prev_produced_bytes_ = 0;
    prev_produced_packets_ = 0;
  }

  double per_second = rtc::MILLI_IN_A_SEC / duration;

  uint32_t prev_packets_production_rate = packets_production_rate_;

  // bytes_production_rate_ does not take into account FEC!!! this is because
  // each client requests a differen amount of FEC packet so the client itself
  // increase the production rate in the right way
  bytes_production_rate_ =
      ceil((double)(produced_bytes_ + prev_produced_bytes_) * per_second);
  packets_production_rate_ =
      ceil((double)(produced_packets_ + prev_produced_packets_) * per_second);

  if (fec_encoder_ && fec_type_ != fec::FECType::UNKNOWN) {
    // add fec packets looking at the fec code. we don't use directly the number
    // of fec packets produced in 1 round because it may happen that different
    // numbers of blocks are generated during the rounds and this creates
    // inconsistencies in the estimation of the production rate
    uint32_t k = fec::FECUtils::getSourceSymbols(fec_type_);
    uint32_t n = fec::FECUtils::getBlockSymbols(fec_type_);

    packets_production_rate_ +=
        ceil((double)packets_production_rate_ / (double)k) * (n - k);
  }

  // update the production rate as soon as it increases by 10% with respect to
  // the last round
  max_packet_production_ =
      produced_packets_ + ceil((double)produced_packets_ * 0.10);
  if (max_packet_production_ < rtc::WIN_MIN)
    max_packet_production_ = rtc::WIN_MIN;

  if (packets_production_rate_ <= rtc::MIN_PRODUCTION_RATE ||
      prev_packets_production_rate <= rtc::MIN_PRODUCTION_RATE) {
    allow_delayed_nacks_ = true;
  } else {
    // at least 2 rounds with enough packets
    allow_delayed_nacks_ = false;
  }

  if (new_round) {
    prev_produced_bytes_ = produced_bytes_;
    prev_produced_packets_ = produced_packets_;
    produced_bytes_ = 0;
    produced_packets_ = 0;
    last_round_ = now;
    scheduleRoundTimer();
  }
}

uint32_t RTCProductionProtocol::produceStream(
    const Name &content_name, std::unique_ptr<utils::MemBuf> &&buffer,
    bool is_last, uint32_t start_offset) {
  throw errors::NotImplementedException();
}

uint32_t RTCProductionProtocol::produceStream(const Name &content_name,
                                              const uint8_t *buffer,
                                              size_t buffer_size, bool is_last,
                                              uint32_t start_offset) {
  throw errors::NotImplementedException();
}

void RTCProductionProtocol::produce(ContentObject &content_object) {
  throw errors::NotImplementedException();
}

uint32_t RTCProductionProtocol::produceDatagram(
    const Name &content_name, std::unique_ptr<utils::MemBuf> &&buffer) {
  std::size_t buffer_size = buffer->length();
  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Maybe Sending content object: " << content_name;

  if (TRANSPORT_EXPECT_FALSE(buffer_size == 0)) return 0;

  DLOG_IF(INFO, VLOG_IS_ON(3)) << "Sending content object: " << content_name;

  uint32_t data_packet_size;
  socket_->getSocketOption(interface::GeneralTransportOptions::DATA_PACKET_SIZE,
                           data_packet_size);
  // this is a source packet but we check the fec header size of FEC packet in
  // order to leave room for the header when FEC packets will be generated
  uint32_t fec_header = 0;
  if (fec_encoder_) fec_encoder_->getFecHeaderSize(true);
  uint32_t headers_size =
      (uint32_t)Packet::getHeaderSizeFromFormat(data_header_format_.first,
                                                data_header_format_.second) +
      rtc::DATA_HEADER_SIZE + fec_header;
  if (TRANSPORT_EXPECT_FALSE((headers_size + buffer_size) > data_packet_size)) {
    return 0;
  }

  if (!data_aggregation_) {
    // if data aggregation is off emptyQueue will always return doing nothing
    emptyQueue();

    sendManifest(content_name);

    // create content object
    auto content_object =
        core::PacketManager<>::getInstance().getPacket<ContentObject>(
            data_header_format_.first, data_header_format_.second);

    // add rtc header to the payload
    struct rtc::data_packet_t header;
    content_object->appendPayload((const uint8_t *)&header,
                                  rtc::DATA_HEADER_SIZE);
    content_object->appendPayload(buffer->data(), buffer->length());

    // schedule actual sending on internal thread
    portal_->getThread().tryRunHandlerNow(
        [this, content_object{std::move(content_object)},
         content_name]() mutable {
          produceInternal(std::move(content_object), content_name);
        });
  } else {
    // XXX here we assume that all the packets that we push to the queue have
    // the same name
    auto app_pkt = utils::MemBuf::copyBuffer(buffer->data(), buffer->length());
    addPacketToQueue(std::move(app_pkt));
  }

  return 1;
}

void RTCProductionProtocol::addPacketToQueue(
    std::unique_ptr<utils::MemBuf> &&buffer) {
  std::size_t buffer_size = buffer->length();
  if ((queue_len_ + buffer_size) > rtc::MAX_RTC_PAYLOAD_SIZE) {
    emptyQueue();  // this should guaranty that the generated packet will never
                   // be larger than an MTU
  }

  waiting_app_packets_.push(std::move(buffer));
  if (max_len_ < buffer_size) max_len_ = buffer_size;
  queue_len_ += buffer_size;

  if (waiting_app_packets_.size() >= rtc::MAX_AGGREGATED_PACKETS) {
    emptyQueue();
  }

  if (waiting_app_packets_.size() >= 1 && !data_aggregation_timer_switch_) {
    data_aggregation_timer_switch_ = true;
    app_packets_timer_->expires_from_now(
        std::chrono::milliseconds(rtc::AGGREGATED_PACKETS_TIMER));
    std::weak_ptr<RTCProductionProtocol> self = shared_from_this();
    app_packets_timer_->async_wait([self](const std::error_code &ec) {
      if (ec) return;

      auto ptr = self.lock();
      if (ptr && ptr->isRunning()) {
        if (!ptr->data_aggregation_timer_switch_) return;
        ptr->emptyQueue();
      }
    });
  }
}

void RTCProductionProtocol::emptyQueue() {
  if (waiting_app_packets_.size() == 0) return;  // queue is empty

  Name n(flow_name_);

  // cancel timer is scheduled
  if (data_aggregation_timer_switch_) {
    data_aggregation_timer_switch_ = false;
    app_packets_timer_->cancel();
  }

  // send a manifest beforehand if the hash buffer if full
  sendManifest(n);

  // create content object
  auto content_object =
      core::PacketManager<>::getInstance().getPacket<ContentObject>(
          data_header_format_.first, data_header_format_.second);

  // add rtc header to the payload
  struct rtc::data_packet_t header;
  content_object->appendPayload((const uint8_t *)&header,
                                rtc::DATA_HEADER_SIZE);

  // init aggregated header
  rtc::AggrPktHeader hdr(
      (uint8_t *)(content_object->getPayload()->data() + rtc::DATA_HEADER_SIZE),
      max_len_, waiting_app_packets_.size());
  uint32_t header_size = hdr.getHeaderLen();
  content_object->append(header_size);  // leave space for the aggregated header

  uint8_t index = 0;
  while (waiting_app_packets_.size() != 0) {
    std::unique_ptr<utils::MemBuf> pkt =
        std::move(waiting_app_packets_.front());
    waiting_app_packets_.pop();
    // XXX for the moment we have a single name, so this works, otherwise we
    // need to do something else
    hdr.addPacketToHeader(index, pkt->length());
    // append packet
    content_object->appendPayload(pkt->data(), pkt->length());
    index++;
  }

  // reset queue values
  max_len_ = 0;
  queue_len_ = 0;

  // the packet is ready we need to send it
  portal_->getThread().tryRunHandlerNow(
      [this, content_object{std::move(content_object)}, n]() mutable {
        produceInternal(std::move(content_object), n);
      });
}

void RTCProductionProtocol::sendManifest(const Name &name) {
  if (!manifest_max_capacity_) {
    return;
  }

  Name manifest_name = name;

  // If there is not enough hashes to fill a manifest, return early
  if (manifest_entries_.size() < manifest_max_capacity_) {
    return;
  }

  // Create a new manifest
  std::shared_ptr<core::ContentObjectManifest> manifest =
      createManifest(manifest_name.setSuffix(current_seg_));
  auto manifest_co =
      std::dynamic_pointer_cast<ContentObject>(manifest->getPacket());

  // Fill the manifest with packet hashes that were previously saved
  uint32_t nb_entries;
  for (nb_entries = 0; nb_entries < manifest_max_capacity_; ++nb_entries) {
    if (manifest_entries_.empty()) {
      break;
    }
    std::pair<uint32_t, auth::CryptoHash> front = manifest_entries_.front();
    manifest->addEntry(front.first, front.second);
    manifest_entries_.pop();
  }

  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Sending manifest " << manifest_co->getName().getSuffix()
      << " of size " << nb_entries;

  // Encode and send the manifest
  manifest->encode();
  portal_->getThread().tryRunHandlerNow(
      [this, content_object{std::move(manifest_co)}, manifest_name]() mutable {
        produceInternal(std::move(content_object), manifest_name);
      });
}

std::shared_ptr<core::ContentObjectManifest>
RTCProductionProtocol::createManifest(const Name &content_name) const {
  Name name(content_name);

  auth::CryptoHashType hash_algo;
  socket_->getSocketOption(interface::GeneralTransportOptions::HASH_ALGORITHM,
                           hash_algo);

  uint64_t now = utils::SteadyTime::nowMs().count();

  // Create a new manifest
  std::shared_ptr<core::ContentObjectManifest> manifest =
      ContentObjectManifest::createContentManifest(
          manifest_header_format_.first, name, manifest_header_format_.second);
  manifest->setHeaders(core::ManifestType::INLINE_MANIFEST,
                       manifest_max_capacity_, hash_algo, false /* is_last */,
                       name);

  // Set connection parameters
  manifest->setParamsRTC(ParamsRTC{
      .timestamp = now,
      .prod_rate = bytes_production_rate_,
      .prod_seg = current_seg_,
      .fec_type = fec_type_,
  });

  return manifest;
}

void RTCProductionProtocol::produceInternal(
    std::shared_ptr<ContentObject> &&content_object, const Name &content_name,
    bool fec) {
  uint64_t now = utils::SteadyTime::nowMs().count();

  if (fec && (now - last_produced_data_ts_) < rtc::FEC_PACING_TIME) {
    paced_fec_packets_.push(std::pair<uint64_t, ContentObject::Ptr>(
        now, std::move(content_object)));
    postponeFecPacket();
  } else {
    // need to check if there are FEC packets waiting to be sent
    flushFecPkts(current_seg_);
    producePktInternal(std::move(content_object), content_name, fec);
  }
}

void RTCProductionProtocol::producePktInternal(
    std::shared_ptr<ContentObject> &&content_object, const Name &content_name,
    bool fec) {
  bool is_manifest = content_object->getPayloadType() == PayloadType::MANIFEST;
  uint64_t now = utils::SteadyTime::nowMs().count();

  // set rtc header
  if (!is_manifest) {
    struct rtc::data_packet_t *data_pkt =
        (struct rtc::data_packet_t *)content_object->getPayload()->data();
    data_pkt->setTimestamp(now);
    data_pkt->setProductionRate(bytes_production_rate_);
  }

  // set hicn stuff
  Name n(content_name);
  content_object->setName(n.setSuffix(current_seg_));

  uint32_t expiry_time = 0;
  socket_->getSocketOption(
      interface::GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME,
      expiry_time);
  if (expiry_time == interface::default_values::content_object_expiry_time)
    expiry_time = 500;  // the data expiration time should be set by the App. if
                        // the App does not specify it the default is 500ms
  content_object->setLifetime(expiry_time);
  content_object->setPathLabel(prod_label_);

  // update stats
  if (!fec) {
    produced_bytes_ +=
        content_object->headerSize() + content_object->payloadSize();
    produced_packets_++;
  }

  if (!data_aggregation_ && produced_packets_ >= max_packet_production_) {
    // in this case all the pending interests may be used to accomodate the
    // sudden increase in the production rate. calling the updateStats we will
    // notify all the clients
    updateStats(false);
  }

  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Sending content object: " << n << ", is fec: " << fec;

  // pass packet to FEC encoder
  if (fec_encoder_ && !fec) {
    uint32_t offset = is_manifest ? (uint32_t)content_object->headerSize()
                                  : (uint32_t)content_object->headerSize() +
                                        rtc::DATA_HEADER_SIZE;
    uint32_t metadata = static_cast<uint32_t>(content_object->getPayloadType());

    fec_encoder_->onPacketProduced(*content_object, offset, metadata);
  }

  output_buffer_.insert(content_object);

  if (*on_content_object_in_output_buffer_) {
    on_content_object_in_output_buffer_->operator()(*socket_->getInterface(),
                                                    *content_object);
  }

  // TODO we may want to send FEC only if an interest is pending in the pit in
  sendContentObject(content_object, false, fec);

  if (*on_content_object_output_) {
    on_content_object_output_->operator()(*socket_->getInterface(),
                                          *content_object);
  }

  if (!fec) last_produced_data_ts_ = now;

  // Update current segment
  current_seg_ = (current_seg_ + 1) % rtc::MIN_PROBE_SEQ;

  // Publish FEC packets if available
  if (fec_encoder_ && !fec) {
    while (!fec && pending_fec_packets_.size()) {
      auto &co = pending_fec_packets_.front();
      produceInternal(std::move(co), flow_name_, true);
      pending_fec_packets_.pop();
    }
  }
}

void RTCProductionProtocol::flushFecPkts(uint32_t current_seq_num) {
  // Currently we immediately send all the pending fec packets
  // A pacing policy may be helpful, but we do not want to delay too much
  // the packets at this moment.
  while (paced_fec_packets_.size() > 0) {
    producePktInternal(std::move(paced_fec_packets_.front().second), flow_name_,
                       true);
    paced_fec_packets_.pop();
  }
  fec_pacing_timer_->cancel();
  pending_fec_pace_ = false;
  postponeFecPacket();
}

void RTCProductionProtocol::postponeFecPacket() {
  if (paced_fec_packets_.size() == 0) return;
  if (pending_fec_pace_) {
    return;
  }

  uint64_t produced_time = paced_fec_packets_.front().first;
  uint64_t now = utils::SteadyTime::nowMs().count();

  uint64_t wait_time = 0;
  if ((produced_time + rtc::FEC_PACING_TIME) > now)
    wait_time = produced_time + rtc::FEC_PACING_TIME - now;

  fec_pacing_timer_->expires_from_now(std::chrono::milliseconds(wait_time));
  pending_fec_pace_ = true;

  std::weak_ptr<RTCProductionProtocol> self = shared_from_this();
  fec_pacing_timer_->async_wait([self](const std::error_code &ec) {
    if (ec) return;

    auto sp = self.lock();
    if (sp && sp->isRunning()) {
      if (!sp->pending_fec_pace_) return;

      if (sp->paced_fec_packets_.size() > 0) {
        sp->producePktInternal(std::move(sp->paced_fec_packets_.front().second),
                               sp->flow_name_, true);
        sp->paced_fec_packets_.pop();
      }
      sp->pending_fec_pace_ = false;
      sp->postponeFecPacket();
    }
  });
}

void RTCProductionProtocol::onInterest(Interest &interest) {
  if (*on_interest_input_) {
    on_interest_input_->operator()(*socket_->getInterface(), interest);
  }

  if (!interest.isValid()) throw std::runtime_error("Bad interest format");
  if (interest.hasManifest() &&
      verifier_->verify(interest) != auth::VerificationPolicy::ACCEPT)
    throw std::runtime_error("Interset manifest verification failed");

  uint32_t *suffix = interest.firstSuffix();
  uint32_t n_suffixes_in_manifest = interest.numberOfSuffixes();
  uint32_t *request_bitmap = interest.getRequestBitmap();

  Name name = interest.getName();
  uint32_t pos = 0;  // Position of current suffix in manifest

  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Received interest " << name << " (" << n_suffixes_in_manifest
      << " suffixes in manifest)";

  // Process the suffix in the interest header
  // (first loop iteration), then suffixes in the manifest
  do {
    if (!interest.hasManifest() || is_bit_set(request_bitmap, pos)) {
      const std::shared_ptr<ContentObject> content_object =
          output_buffer_.find(name);

      if (content_object) {
        if (*on_interest_satisfied_output_buffer_) {
          on_interest_satisfied_output_buffer_->operator()(
              *socket_->getInterface(), interest);
        }

        if (*on_content_object_output_) {
          on_content_object_output_->operator()(*socket_->getInterface(),
                                                *content_object);
        }

        DLOG_IF(INFO, VLOG_IS_ON(3))
            << "Send content %u (onInterest) " << content_object->getName();
        content_object->setPathLabel(cache_label_);
        sendContentObject(content_object);
      } else {
        if (*on_interest_process_) {
          on_interest_process_->operator()(*socket_->getInterface(), interest);
        }
        processInterest(name.getSuffix(), interest.getLifetime());
      }
    }

    // Retrieve next suffix in the manifest
    if (interest.hasManifest()) {
      uint32_t seq = *suffix;
      suffix++;

      name.setSuffix(seq);
      interest.setName(name);
    }
  } while (pos++ < n_suffixes_in_manifest);
}

void RTCProductionProtocol::processInterest(uint32_t interest_seg,
                                            uint32_t lifetime) {
  switch (rtc::ProbeHandler::getProbeType(interest_seg)) {
    case rtc::ProbeType::INIT:
      DLOG_IF(INFO, VLOG_IS_ON(3)) << "Received init probe " << interest_seg;
      sendManifestProbe(interest_seg);
      return;
    case rtc::ProbeType::RTT:
      DLOG_IF(INFO, VLOG_IS_ON(3)) << "Received RTT probe " << interest_seg;
      sendNack(interest_seg);
      return;
    default:
      break;
  }

  if (interest_seg < current_seg_) sendNack(interest_seg);
}

void RTCProductionProtocol::sendManifestProbe(uint32_t sequence) {
  Name manifest_name(flow_name_);
  manifest_name.setSuffix(sequence);

  std::shared_ptr<core::ContentObjectManifest> manifest_probe =
      createManifest(manifest_name);
  auto manifest_probe_co =
      std::dynamic_pointer_cast<ContentObject>(manifest_probe->getPacket());

  manifest_probe_co->setLifetime(0);
  manifest_probe_co->setPathLabel(prod_label_);
  manifest_probe->encode();

  if (*on_content_object_output_) {
    on_content_object_output_->operator()(*socket_->getInterface(),
                                          *manifest_probe_co);
  }

  DLOG_IF(INFO, VLOG_IS_ON(3)) << "Send init probe " << sequence;
  sendContentObject(manifest_probe_co, true, false);
}

void RTCProductionProtocol::sendNack(uint32_t sequence) {
  auto nack = core::PacketManager<>::getInstance().getPacket<ContentObject>(
      nack_header_format_.first, nack_header_format_.second);
  uint64_t now = utils::SteadyTime::nowMs().count();
  uint32_t next_packet = current_seg_;
  uint32_t prod_rate = bytes_production_rate_;

  struct rtc::nack_packet_t header;
  header.setTimestamp(now);
  header.setProductionRate(prod_rate);
  header.setProductionSegment(next_packet);
  nack->appendPayload((const uint8_t *)&header, rtc::NACK_HEADER_SIZE);

  Name n(flow_name_);
  n.setSuffix(sequence);
  nack->setName(n);
  nack->setLifetime(0);
  nack->setPathLabel(prod_label_);

  if (*on_content_object_output_) {
    on_content_object_output_->operator()(*socket_->getInterface(), *nack);
  }

  DLOG_IF(INFO, VLOG_IS_ON(3)) << "Send nack " << sequence;
  sendContentObject(nack, true, false);
}

void RTCProductionProtocol::sendContentObject(
    std::shared_ptr<ContentObject> content_object, bool nack, bool fec) {
  bool is_ah = _is_ah(content_object->getFormat());

  // Compute signature
  if (is_ah) {
    signer_->signPacket(content_object.get());
  }

  portal_->sendContentObject(*content_object);

  // Compute and save data packet digest
  if (manifest_max_capacity_ && !is_ah) {
    auth::CryptoHashType hash_algo;
    socket_->getSocketOption(interface::GeneralTransportOptions::HASH_ALGORITHM,
                             hash_algo);
    manifest_entries_.push({content_object->getName().getSuffix(),
                            content_object->computeDigest(hash_algo)});
  }
}

void RTCProductionProtocol::onFecPackets(fec::BufferArray &packets) {
  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Produced " << packets.size() << " FEC packets";

  for (auto &packet : packets) {
    auto content_object =
        std::static_pointer_cast<ContentObject>(packet.getBuffer());
    content_object->prepend(content_object->headerSize() +
                            rtc::DATA_HEADER_SIZE);
    pending_fec_packets_.push(std::move(content_object));
  }
}

fec::buffer RTCProductionProtocol::getBuffer(std::size_t size) {
  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Asked buffer for FEC symbol of size " << size;

  auto ret = core::PacketManager<>::getInstance().getPacket<ContentObject>(
      fec_header_format_.first, fec_header_format_.second);

  ret->updateLength(rtc::DATA_HEADER_SIZE + size);
  ret->append(rtc::DATA_HEADER_SIZE + size);
  ret->trimStart(ret->headerSize() + rtc::DATA_HEADER_SIZE);

  DLOG_IF(INFO, VLOG_IS_ON(3))
      << "Responding with buffer of length " << ret->length();
  DCHECK(ret->length() >= size);

  return ret;
}

}  // namespace protocol

}  // namespace transport