blob: 431f058722802bef79f5c14f042faecf4461363c [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventobf1c0692017-01-15 19:15:09 -05003 * Copyright (c) 2016-2017, Regents of the University of California,
4 * 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
30namespace ndn {
31namespace chunks {
32
Junxiao Shif8606492017-07-23 03:44:34 +000033Consumer::Consumer(security::v2::Validator& validator, bool isVerbose, std::ostream& os)
Weiwei Liu05d92092016-07-19 17:34:33 -070034 : m_validator(validator)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035 , m_outputStream(os)
Weiwei Liue4765012016-06-01 00:10:29 -070036 , m_nextToPrint(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037 , m_isVerbose(isVerbose)
38{
39}
40
Weiwei Liue4765012016-06-01 00:10:29 -070041void
42Consumer::run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043{
Weiwei Liue4765012016-06-01 00:10:29 -070044 m_discover = std::move(discover);
45 m_pipeline = std::move(pipeline);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010046 m_nextToPrint = 0;
Weiwei Liue4765012016-06-01 00:10:29 -070047 m_bufferedData.clear();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048
Weiwei Liu05d92092016-07-19 17:34:33 -070049 m_discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1));
Junxiao Shif8606492017-07-23 03:44:34 +000050 m_discover->onDiscoveryFailure.connect([] (const std::string& msg) {
51 BOOST_THROW_EXCEPTION(std::runtime_error(msg));
52 });
Weiwei Liu05d92092016-07-19 17:34:33 -070053 m_discover->run();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054}
55
Weiwei Liue4765012016-06-01 00:10:29 -070056void
57Consumer::startPipeline(const Data& data)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058{
Junxiao Shif8606492017-07-23 03:44:34 +000059 this->handleData(data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060
Weiwei Liue4765012016-06-01 00:10:29 -070061 m_pipeline->run(data,
Junxiao Shif8606492017-07-23 03:44:34 +000062 [this] (const Interest&, const Data& data) { this->handleData(data); },
63 [] (const std::string& msg) { BOOST_THROW_EXCEPTION(std::runtime_error(msg)); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064}
65
66void
Junxiao Shif8606492017-07-23 03:44:34 +000067Consumer::handleData(const Data& data)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068{
Junxiao Shif8606492017-07-23 03:44:34 +000069 auto dataPtr = data.shared_from_this();
70
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071 m_validator.validate(data,
Junxiao Shif8606492017-07-23 03:44:34 +000072 [this, dataPtr] (const Data& data) {
73 if (data.getContentType() == ndn::tlv::ContentType_Nack) {
74 if (m_isVerbose) {
75 std::cerr << "Application level NACK: " << data << std::endl;
76 }
77 m_pipeline->cancel();
78 BOOST_THROW_EXCEPTION(ApplicationNackError(data));
79 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080
Junxiao Shif8606492017-07-23 03:44:34 +000081 // 'data' passed to callback comes from DataValidationState and was not created with make_shared
82 m_bufferedData[getSegmentFromPacket(data)] = dataPtr;
83 writeInOrderData();
84 },
85 [this] (const Data& data, const security::v2::ValidationError& error) {
86 BOOST_THROW_EXCEPTION(DataValidationError(error));
87 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088}
89
90void
91Consumer::writeInOrderData()
92{
93 for (auto it = m_bufferedData.begin();
94 it != m_bufferedData.end() && it->first == m_nextToPrint;
95 it = m_bufferedData.erase(it), ++m_nextToPrint) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096 const Block& content = it->second->getContent();
97 m_outputStream.write(reinterpret_cast<const char*>(content.value()), content.value_size());
98 }
99}
100
101} // namespace chunks
102} // namespace ndn