blob: 9972d767d84a703e8c99c08d3ed084d73b62dcba [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe9c69852017-11-04 18:08:37 -04002/*
Alexander Afanasyev28181ee2020-06-03 13:58:47 -04003 * Copyright (c) 2016-2020, Regents of the University of California,
Junxiao Shif8606492017-07-23 03:44:34 +00004 * 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
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP
29#define NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP
30
Andrea Tosatto672b9a72016-01-05 16:18:20 +010031#include "discover-version.hpp"
Weiwei Liue4765012016-06-01 00:10:29 -070032#include "pipeline-interests.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010033
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040034#include <ndn-cxx/security/validation-error.hpp>
35#include <ndn-cxx/security/validator.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036
37namespace ndn {
38namespace chunks {
39
40/**
41 * @brief Segmented version consumer
42 *
43 * Discover the latest version of the data published under a specified prefix, and retrieve all the
44 * segments associated to that version. The segments are fetched in order and written to a
45 * user-specified stream in the same order.
46 */
47class Consumer : noncopyable
48{
49public:
50 class ApplicationNackError : public std::runtime_error
51 {
52 public:
53 explicit
54 ApplicationNackError(const Data& data)
55 : std::runtime_error("Application generated Nack: " + boost::lexical_cast<std::string>(data))
56 {
57 }
58 };
59
Junxiao Shif8606492017-07-23 03:44:34 +000060 class DataValidationError : public std::runtime_error
61 {
62 public:
63 explicit
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040064 DataValidationError(const security::ValidationError& error)
Junxiao Shif8606492017-07-23 03:44:34 +000065 : std::runtime_error(boost::lexical_cast<std::string>(error))
66 {
67 }
68 };
69
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 /**
71 * @brief Create the consumer
72 */
Davide Pesaventof6991e12018-01-08 20:58:50 -050073 explicit
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040074 Consumer(security::Validator& validator, std::ostream& os = std::cout);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075
76 /**
77 * @brief Run the consumer
78 */
79 void
Weiwei Liue4765012016-06-01 00:10:29 -070080 run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010081
82private:
83 void
Junxiao Shif8606492017-07-23 03:44:34 +000084 handleData(const Data& data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010085
86PUBLIC_WITH_TESTS_ELSE_PRIVATE:
87 void
88 writeInOrderData();
89
90private:
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040091 security::Validator& m_validator;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010092 std::ostream& m_outputStream;
Weiwei Liue4765012016-06-01 00:10:29 -070093 unique_ptr<DiscoverVersion> m_discover;
94 unique_ptr<PipelineInterests> m_pipeline;
95 uint64_t m_nextToPrint;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096
97PUBLIC_WITH_TESTS_ELSE_PRIVATE:
98 std::map<uint64_t, shared_ptr<const Data>> m_bufferedData;
99};
100
101} // namespace chunks
102} // namespace ndn
103
104#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP