blob: a1c2edfbde2436ce21d71f7afb38a14721a74711 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0ebecab2017-09-12 15:28:42 -04002/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Davide Pesaventobf1c0692017-01-15 19:15:09 -05004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
26 */
27
28#include "consumer.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029
Davide Pesaventob3570c62022-02-19 19:19:00 -050030namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010031
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040032Consumer::Consumer(security::Validator& validator, std::ostream& os)
Weiwei Liu05d92092016-07-19 17:34:33 -070033 : m_validator(validator)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034 , m_outputStream(os)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035{
36}
37
Weiwei Liue4765012016-06-01 00:10:29 -070038void
39Consumer::run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040{
Weiwei Liue4765012016-06-01 00:10:29 -070041 m_discover = std::move(discover);
42 m_pipeline = std::move(pipeline);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043 m_nextToPrint = 0;
Weiwei Liue4765012016-06-01 00:10:29 -070044 m_bufferedData.clear();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080046 m_discover->onDiscoverySuccess.connect([this] (const Name& versionedName) {
47 m_pipeline->run(versionedName,
Davide Pesaventof8d9a532021-07-03 16:04:12 -040048 FORWARD_TO_MEM_FN(handleData),
Davide Pesaventof8a14d82021-03-12 00:42:03 -050049 [] (const std::string& msg) { NDN_THROW(std::runtime_error(msg)); });
Davide Pesaventoe9c69852017-11-04 18:08:37 -040050 });
Junxiao Shif8606492017-07-23 03:44:34 +000051 m_discover->onDiscoveryFailure.connect([] (const std::string& msg) {
Davide Pesavento80baddf2019-02-23 15:51:59 -050052 NDN_THROW(std::runtime_error(msg));
Junxiao Shif8606492017-07-23 03:44:34 +000053 });
Weiwei Liu05d92092016-07-19 17:34:33 -070054 m_discover->run();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055}
56
Weiwei Liue4765012016-06-01 00:10:29 -070057void
Junxiao Shif8606492017-07-23 03:44:34 +000058Consumer::handleData(const Data& data)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059{
Junxiao Shif8606492017-07-23 03:44:34 +000060 auto dataPtr = data.shared_from_this();
61
Andrea Tosatto672b9a72016-01-05 16:18:20 +010062 m_validator.validate(data,
Junxiao Shif8606492017-07-23 03:44:34 +000063 [this, dataPtr] (const Data& data) {
64 if (data.getContentType() == ndn::tlv::ContentType_Nack) {
Davide Pesavento80baddf2019-02-23 15:51:59 -050065 NDN_THROW(ApplicationNackError(data));
Junxiao Shif8606492017-07-23 03:44:34 +000066 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067
Junxiao Shif8606492017-07-23 03:44:34 +000068 // 'data' passed to callback comes from DataValidationState and was not created with make_shared
69 m_bufferedData[getSegmentFromPacket(data)] = dataPtr;
70 writeInOrderData();
71 },
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040072 [] (const Data&, const security::ValidationError& error) {
Davide Pesavento80baddf2019-02-23 15:51:59 -050073 NDN_THROW(DataValidationError(error));
Junxiao Shif8606492017-07-23 03:44:34 +000074 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075}
76
77void
78Consumer::writeInOrderData()
79{
80 for (auto it = m_bufferedData.begin();
81 it != m_bufferedData.end() && it->first == m_nextToPrint;
82 it = m_bufferedData.erase(it), ++m_nextToPrint) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083 const Block& content = it->second->getContent();
84 m_outputStream.write(reinterpret_cast<const char*>(content.value()), content.value_size());
85 }
86}
87
Davide Pesaventob3570c62022-02-19 19:19:00 -050088} // namespace ndn::chunks