blob: fe30f7539b48eb2eda3b38d94fab2bfa77cb7a61 [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
28#include "consumer.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029
30namespace ndn {
31namespace chunks {
32
33Consumer::Consumer(Face& face, Validator& validator, bool isVerbose, std::ostream& os)
34 : m_face(face)
35 , m_validator(validator)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036 , m_outputStream(os)
Weiwei Liue4765012016-06-01 00:10:29 -070037 , m_nextToPrint(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038 , m_isVerbose(isVerbose)
39{
40}
41
Weiwei Liue4765012016-06-01 00:10:29 -070042void
43Consumer::run(unique_ptr<DiscoverVersion> discover, unique_ptr<PipelineInterests> pipeline)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044{
Weiwei Liue4765012016-06-01 00:10:29 -070045 m_discover = std::move(discover);
46 m_pipeline = std::move(pipeline);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010047 m_nextToPrint = 0;
Weiwei Liue4765012016-06-01 00:10:29 -070048 m_bufferedData.clear();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049
Weiwei Liue4765012016-06-01 00:10:29 -070050 discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1));
51 discover->onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1));
52 discover->run();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054 m_face.processEvents();
55}
56
Weiwei Liue4765012016-06-01 00:10:29 -070057void
58Consumer::startPipeline(const Data& data)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059{
60 m_validator.validate(data,
61 bind(&Consumer::onDataValidated, this, _1),
62 bind(&Consumer::onFailure, this, _2));
63
Weiwei Liue4765012016-06-01 00:10:29 -070064 m_pipeline->run(data,
65 bind(&Consumer::onData, this, _1, _2),
66 bind(&Consumer::onFailure, this, _1));
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067}
68
69void
70Consumer::onData(const Interest& interest, const Data& data)
71{
72 m_validator.validate(data,
73 bind(&Consumer::onDataValidated, this, _1),
74 bind(&Consumer::onFailure, this, _2));
75}
76
77void
78Consumer::onDataValidated(shared_ptr<const Data> data)
79{
80 if (data->getContentType() == ndn::tlv::ContentType_Nack) {
81 if (m_isVerbose)
82 std::cerr << "Application level NACK: " << *data << std::endl;
83
84 m_pipeline->cancel();
85 throw ApplicationNackError(*data);
86 }
87
88 m_bufferedData[data->getName()[-1].toSegment()] = data;
89 writeInOrderData();
90}
91
92void
93Consumer::onFailure(const std::string& reason)
94{
95 throw std::runtime_error(reason);
96}
97
98void
99Consumer::writeInOrderData()
100{
101 for (auto it = m_bufferedData.begin();
102 it != m_bufferedData.end() && it->first == m_nextToPrint;
103 it = m_bufferedData.erase(it), ++m_nextToPrint) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104 const Block& content = it->second->getContent();
105 m_outputStream.write(reinterpret_cast<const char*>(content.value()), content.value_size());
106 }
107}
108
109} // namespace chunks
110} // namespace ndn