blob: 4483558db992240a22d921a19801a323dc7e454e [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider59ec95a2017-09-14 17:50:00 -07002/*
Davide Pesavento969cd5a2018-04-20 16:27:47 -04003 * Copyright (c) 2016-2018, Regents of the University of California,
Davide Pesavento6f76afc2017-09-19 18:43:51 -04004 * 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
Davide Pesavento6f76afc2017-09-19 18:43:51 -040026 * @author Davide Pesavento
Klaus Schneider59ec95a2017-09-14 17:50:00 -070027 * @author Klaus Schneider
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028 */
29
30#include "producer.hpp"
31
32namespace ndn {
33namespace chunks {
34
Davide Pesavento6f76afc2017-09-19 18:43:51 -040035Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is,
36 const Options& opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037 : m_face(face)
38 , m_keyChain(keyChain)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040039 , m_options(opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040{
41 if (prefix.size() > 0 && prefix[-1].isVersion()) {
42 m_prefix = prefix.getPrefix(-1);
43 m_versionedPrefix = prefix;
44 }
45 else {
46 m_prefix = prefix;
47 m_versionedPrefix = Name(m_prefix).appendVersion();
48 }
49
50 populateStore(is);
51
Davide Pesavento6f76afc2017-09-19 18:43:51 -040052 if (m_options.wantShowVersion)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053 std::cout << m_versionedPrefix[-1] << std::endl;
54
55 m_face.setInterestFilter(m_prefix,
56 bind(&Producer::onInterest, this, _2),
57 RegisterPrefixSuccessCallback(),
58 bind(&Producer::onRegisterFailed, this, _1, _2));
59
Davide Pesavento6f76afc2017-09-19 18:43:51 -040060 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 std::cerr << "Data published with name: " << m_versionedPrefix << std::endl;
62}
63
64void
65Producer::run()
66{
67 m_face.processEvents();
68}
69
70void
71Producer::onInterest(const Interest& interest)
72{
73 BOOST_ASSERT(m_store.size() > 0);
74
Davide Pesavento6f76afc2017-09-19 18:43:51 -040075 if (m_options.isVerbose)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076 std::cerr << "Interest: " << interest << std::endl;
77
78 const Name& name = interest.getName();
79 shared_ptr<Data> data;
80
81 // is this a discovery Interest or a sequence retrieval?
82 if (name.size() == m_versionedPrefix.size() + 1 && m_versionedPrefix.isPrefixOf(name) &&
83 name[-1].isSegment()) {
84 const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment());
85 // specific segment retrieval
86 if (segmentNo < m_store.size()) {
87 data = m_store[segmentNo];
88 }
89 }
90 else if (interest.matchesData(*m_store[0])) {
91 // Interest has version and is looking for the first segment or has no version
92 data = m_store[0];
93 }
94
95 if (data != nullptr) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -040096 if (m_options.isVerbose)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010097 std::cerr << "Data: " << *data << std::endl;
98
99 m_face.put(*data);
100 }
101}
102
103void
104Producer::populateStore(std::istream& is)
105{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400106 BOOST_ASSERT(m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400108 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100109 std::cerr << "Loading input ..." << std::endl;
110
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400111 std::vector<uint8_t> buffer(m_options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100112 while (is.good()) {
113 is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
114 const auto nCharsRead = is.gcount();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400115
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100116 if (nCharsRead > 0) {
117 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400118 data->setFreshnessPeriod(m_options.freshnessPeriod);
119 data->setContent(buffer.data(), static_cast<size_t>(nCharsRead));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100120 m_store.push_back(data);
121 }
122 }
123
124 if (m_store.empty()) {
125 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400126 data->setFreshnessPeriod(m_options.freshnessPeriod);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127 m_store.push_back(data);
128 }
129
130 auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
131 for (const auto& data : m_store) {
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400132 data->setFinalBlock(finalBlockId);
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400133 m_keyChain.sign(*data, m_options.signingInfo);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100134 }
135
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400136 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100137 std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl;
138}
139
140void
141Producer::onRegisterFailed(const Name& prefix, const std::string& reason)
142{
143 std::cerr << "ERROR: Failed to register prefix '"
144 << prefix << "' (" << reason << ")" << std::endl;
145 m_face.shutdown();
146}
147
148} // namespace chunks
149} // namespace ndn