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 | e9c6985 | 2017-11-04 18:08:37 -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, |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [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 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 28 | #ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP |
| 29 | #define NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP |
| 30 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 31 | #include "discover-version.hpp" |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 32 | #include "pipeline-interests.hpp" |
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 | #include <ndn-cxx/security/validation-error.hpp> |
| 35 | #include <ndn-cxx/security/validator.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 36 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 37 | #include <boost/lexical_cast.hpp> |
| 38 | #include <iostream> |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 39 | #include <map> |
| 40 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 41 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 42 | |
| 43 | /** |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 44 | * @brief Segmented version consumer. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | * |
| 46 | * Discover the latest version of the data published under a specified prefix, and retrieve all the |
| 47 | * segments associated to that version. The segments are fetched in order and written to a |
| 48 | * user-specified stream in the same order. |
| 49 | */ |
| 50 | class Consumer : noncopyable |
| 51 | { |
| 52 | public: |
| 53 | class ApplicationNackError : public std::runtime_error |
| 54 | { |
| 55 | public: |
| 56 | explicit |
| 57 | ApplicationNackError(const Data& data) |
| 58 | : std::runtime_error("Application generated Nack: " + boost::lexical_cast<std::string>(data)) |
| 59 | { |
| 60 | } |
| 61 | }; |
| 62 | |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 63 | class DataValidationError : public std::runtime_error |
| 64 | { |
| 65 | public: |
| 66 | explicit |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 67 | DataValidationError(const security::ValidationError& error) |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 68 | : std::runtime_error(boost::lexical_cast<std::string>(error)) |
| 69 | { |
| 70 | } |
| 71 | }; |
| 72 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | /** |
| 74 | * @brief Create the consumer |
| 75 | */ |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 76 | explicit |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 77 | Consumer(security::Validator& validator, std::ostream& os = std::cout); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Run the consumer |
| 81 | */ |
| 82 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 83 | run(std::unique_ptr<DiscoverVersion> discover, std::unique_ptr<PipelineInterests> pipeline); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | void |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 87 | handleData(const Data& data); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | |
| 89 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 90 | void |
| 91 | writeInOrderData(); |
| 92 | |
| 93 | private: |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 94 | security::Validator& m_validator; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 95 | std::ostream& m_outputStream; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 96 | std::unique_ptr<DiscoverVersion> m_discover; |
| 97 | std::unique_ptr<PipelineInterests> m_pipeline; |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 98 | uint64_t m_nextToPrint = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 99 | |
| 100 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 101 | std::map<uint64_t, std::shared_ptr<const Data>> m_bufferedData; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 104 | } // namespace ndn::chunks |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 105 | |
| 106 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP |