blob: 8976f6a26b9791afb1d7d80c28577f78602a5a16 [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"
29#include "discover-version.hpp"
30
31namespace ndn {
32namespace chunks {
33
34Consumer::Consumer(Face& face, Validator& validator, bool isVerbose, std::ostream& os)
35 : m_face(face)
36 , m_validator(validator)
37 , m_pipeline(nullptr)
38 , m_nextToPrint(0)
39 , m_outputStream(os)
40 , m_isVerbose(isVerbose)
41{
42}
43
44void Consumer::run(DiscoverVersion& discover, PipelineInterests& pipeline)
45{
46 m_pipeline = &pipeline;
47 m_nextToPrint = 0;
48
49 discover.onDiscoverySuccess.connect(bind(&Consumer::runWithData, this, _1));
50 discover.onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1));
51
52 discover.run();
53 m_face.processEvents();
54}
55
56void Consumer::runWithData(const Data& data)
57{
58 m_validator.validate(data,
59 bind(&Consumer::onDataValidated, this, _1),
60 bind(&Consumer::onFailure, this, _2));
61
62 m_pipeline->runWithExcludedSegment(data,
63 bind(&Consumer::onData, this, _1, _2),
64 bind(&Consumer::onFailure, this, _1));
65
66}
67
68void
69Consumer::onData(const Interest& interest, const Data& data)
70{
71 m_validator.validate(data,
72 bind(&Consumer::onDataValidated, this, _1),
73 bind(&Consumer::onFailure, this, _2));
74}
75
76void
77Consumer::onDataValidated(shared_ptr<const Data> data)
78{
79 if (data->getContentType() == ndn::tlv::ContentType_Nack) {
80 if (m_isVerbose)
81 std::cerr << "Application level NACK: " << *data << std::endl;
82
83 m_pipeline->cancel();
84 throw ApplicationNackError(*data);
85 }
86
87 m_bufferedData[data->getName()[-1].toSegment()] = data;
88 writeInOrderData();
89}
90
91void
92Consumer::onFailure(const std::string& reason)
93{
94 throw std::runtime_error(reason);
95}
96
97void
98Consumer::writeInOrderData()
99{
100 for (auto it = m_bufferedData.begin();
101 it != m_bufferedData.end() && it->first == m_nextToPrint;
102 it = m_bufferedData.erase(it), ++m_nextToPrint) {
103
104 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