Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
| 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 | |
| 30 | namespace ndn { |
| 31 | namespace chunks { |
| 32 | |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 33 | Consumer::Consumer(Validator& validator, bool isVerbose, std::ostream& os) |
| 34 | : m_validator(validator) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | , m_outputStream(os) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 36 | , m_nextToPrint(0) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | , m_isVerbose(isVerbose) |
| 38 | { |
| 39 | } |
| 40 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 41 | void |
| 42 | Consumer::run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 43 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 44 | m_discover = std::move(discover); |
| 45 | m_pipeline = std::move(pipeline); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 46 | m_nextToPrint = 0; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 47 | m_bufferedData.clear(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 48 | |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 49 | m_discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1)); |
| 50 | m_discover->onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1)); |
| 51 | m_discover->run(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 54 | void |
| 55 | Consumer::startPipeline(const Data& data) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 56 | { |
| 57 | m_validator.validate(data, |
| 58 | bind(&Consumer::onDataValidated, this, _1), |
| 59 | bind(&Consumer::onFailure, this, _2)); |
| 60 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 61 | m_pipeline->run(data, |
| 62 | bind(&Consumer::onData, this, _1, _2), |
| 63 | bind(&Consumer::onFailure, this, _1)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
| 67 | Consumer::onData(const Interest& interest, const Data& data) |
| 68 | { |
| 69 | m_validator.validate(data, |
| 70 | bind(&Consumer::onDataValidated, this, _1), |
| 71 | bind(&Consumer::onFailure, this, _2)); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | Consumer::onDataValidated(shared_ptr<const Data> data) |
| 76 | { |
| 77 | if (data->getContentType() == ndn::tlv::ContentType_Nack) { |
| 78 | if (m_isVerbose) |
| 79 | std::cerr << "Application level NACK: " << *data << std::endl; |
| 80 | |
| 81 | m_pipeline->cancel(); |
| 82 | throw ApplicationNackError(*data); |
| 83 | } |
| 84 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame^] | 85 | m_bufferedData[getSegmentFromPacket(*data)] = data; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 86 | writeInOrderData(); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Consumer::onFailure(const std::string& reason) |
| 91 | { |
| 92 | throw std::runtime_error(reason); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | Consumer::writeInOrderData() |
| 97 | { |
| 98 | for (auto it = m_bufferedData.begin(); |
| 99 | it != m_bufferedData.end() && it->first == m_nextToPrint; |
| 100 | it = m_bufferedData.erase(it), ++m_nextToPrint) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 101 | const Block& content = it->second->getContent(); |
| 102 | m_outputStream.write(reinterpret_cast<const char*>(content.value()), content.value_size()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace chunks |
| 107 | } // namespace ndn |