Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0ebecab | 2017-09-12 15:28:42 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, Regents of the University of California, |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 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 Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 29 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/util/exception.hpp> |
| 31 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 32 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 33 | |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 34 | Consumer::Consumer(security::Validator& validator, std::ostream& os) |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 35 | : m_validator(validator) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 36 | , m_outputStream(os) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 40 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 41 | Consumer::run(std::unique_ptr<DiscoverVersion> discover, std::unique_ptr<PipelineInterests> pipeline) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 42 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 43 | m_discover = std::move(discover); |
| 44 | m_pipeline = std::move(pipeline); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | m_nextToPrint = 0; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 46 | m_bufferedData.clear(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 47 | |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 48 | m_discover->onDiscoverySuccess.connect([this] (const Name& versionedName) { |
| 49 | m_pipeline->run(versionedName, |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 50 | FORWARD_TO_MEM_FN(handleData), |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 51 | [] (const std::string& msg) { NDN_THROW(std::runtime_error(msg)); }); |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 52 | }); |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 53 | m_discover->onDiscoveryFailure.connect([] (const std::string& msg) { |
Davide Pesavento | 80baddf | 2019-02-23 15:51:59 -0500 | [diff] [blame] | 54 | NDN_THROW(std::runtime_error(msg)); |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 55 | }); |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 56 | m_discover->run(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 59 | void |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 60 | Consumer::handleData(const Data& data) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 61 | { |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 62 | auto dataPtr = data.shared_from_this(); |
| 63 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 64 | m_validator.validate(data, |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 65 | [this, dataPtr] (const Data& data) { |
| 66 | if (data.getContentType() == ndn::tlv::ContentType_Nack) { |
Davide Pesavento | 80baddf | 2019-02-23 15:51:59 -0500 | [diff] [blame] | 67 | NDN_THROW(ApplicationNackError(data)); |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 68 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 70 | // 'data' passed to callback comes from DataValidationState and was not created with make_shared |
| 71 | m_bufferedData[getSegmentFromPacket(data)] = dataPtr; |
| 72 | writeInOrderData(); |
| 73 | }, |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 74 | [] (const Data&, const security::ValidationError& error) { |
Davide Pesavento | 80baddf | 2019-02-23 15:51:59 -0500 | [diff] [blame] | 75 | NDN_THROW(DataValidationError(error)); |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 76 | }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
| 80 | Consumer::writeInOrderData() |
| 81 | { |
| 82 | for (auto it = m_bufferedData.begin(); |
| 83 | it != m_bufferedData.end() && it->first == m_nextToPrint; |
| 84 | it = m_bufferedData.erase(it), ++m_nextToPrint) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 85 | const Block& content = it->second->getContent(); |
| 86 | m_outputStream.write(reinterpret_cast<const char*>(content.value()), content.value_size()); |
| 87 | } |
| 88 | } |
| 89 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 90 | } // namespace ndn::chunks |