blob: 9996c14279d13f042fafb3bf16a676038fd141ad [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
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 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
34#include <ndn-cxx/security/validator.hpp>
35
36namespace ndn {
37namespace chunks {
38
39/**
40 * @brief Segmented version consumer
41 *
42 * Discover the latest version of the data published under a specified prefix, and retrieve all the
43 * segments associated to that version. The segments are fetched in order and written to a
44 * user-specified stream in the same order.
45 */
46class Consumer : noncopyable
47{
48public:
49 class ApplicationNackError : public std::runtime_error
50 {
51 public:
52 explicit
53 ApplicationNackError(const Data& data)
54 : std::runtime_error("Application generated Nack: " + boost::lexical_cast<std::string>(data))
55 {
56 }
57 };
58
59 /**
60 * @brief Create the consumer
61 */
Weiwei Liu05d92092016-07-19 17:34:33 -070062 Consumer(Validator& validator, bool isVerbose, std::ostream& os = std::cout);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010063
64 /**
65 * @brief Run the consumer
66 */
67 void
Weiwei Liue4765012016-06-01 00:10:29 -070068 run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069
70private:
71 void
Weiwei Liue4765012016-06-01 00:10:29 -070072 startPipeline(const Data& data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073
74 void
75 onData(const Interest& interest, const Data& data);
76
77 void
78 onDataValidated(shared_ptr<const Data> data);
79
80 void
81 onFailure(const std::string& reason);
82
83PUBLIC_WITH_TESTS_ELSE_PRIVATE:
84 void
85 writeInOrderData();
86
87private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088 Validator& m_validator;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010089 std::ostream& m_outputStream;
Weiwei Liue4765012016-06-01 00:10:29 -070090 unique_ptr<DiscoverVersion> m_discover;
91 unique_ptr<PipelineInterests> m_pipeline;
92 uint64_t m_nextToPrint;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093 bool m_isVerbose;
94
95PUBLIC_WITH_TESTS_ELSE_PRIVATE:
96 std::map<uint64_t, shared_ptr<const Data>> m_bufferedData;
97};
98
99} // namespace chunks
100} // namespace ndn
101
102#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_CONSUMER_HPP