blob: ff4bafbec14d75ca97ed045c73fb0f80a69fda63 [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/*
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -08003 * Copyright (c) 2016-2019, 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
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080028 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029 */
30
31#include "producer.hpp"
32
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080033#include <ndn-cxx/metadata-object.hpp>
34
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035namespace ndn {
36namespace chunks {
37
Davide Pesavento6f76afc2017-09-19 18:43:51 -040038Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is,
39 const Options& opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040 : m_face(face)
41 , m_keyChain(keyChain)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040042 , m_options(opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043{
44 if (prefix.size() > 0 && prefix[-1].isVersion()) {
45 m_prefix = prefix.getPrefix(-1);
46 m_versionedPrefix = prefix;
47 }
48 else {
49 m_prefix = prefix;
50 m_versionedPrefix = Name(m_prefix).appendVersion();
51 }
52
53 populateStore(is);
54
Davide Pesavento6f76afc2017-09-19 18:43:51 -040055 if (m_options.wantShowVersion)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 std::cout << m_versionedPrefix[-1] << std::endl;
57
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080058 // register m_prefix without interest handler
59 m_face.registerPrefix(m_prefix, nullptr, bind(&Producer::onRegisterFailed, this, _1, _2));
60
61 // match Interests whose name starts with m_versionedPrefix
62 face.setInterestFilter(m_versionedPrefix, bind(&Producer::processSegmentInterest, this, _2));
63
64 // match Interests whose name is exactly m_prefix
65 face.setInterestFilter(InterestFilter(m_prefix, ""),
66 bind(&Producer::processSegmentInterest, this, _2));
67
68 // match discovery Interests
69 face.setInterestFilter(MetadataObject::makeDiscoveryInterest(m_prefix).getName(),
70 bind(&Producer::processDiscoveryInterest, this, _2));
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071
Davide Pesavento6f76afc2017-09-19 18:43:51 -040072 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 std::cerr << "Data published with name: " << m_versionedPrefix << std::endl;
74}
75
76void
77Producer::run()
78{
79 m_face.processEvents();
80}
81
82void
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080083Producer::processDiscoveryInterest(const Interest& interest)
84{
85 if (m_options.isVerbose)
86 std::cerr << "Discovery Interest: " << interest << std::endl;
87
88 if (!interest.getCanBePrefix()) {
89 if (m_options.isVerbose)
90 std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack" << std::endl;
91 m_face.put(lp::Nack(interest));
92 return;
93 }
94
95 MetadataObject mobject;
96 mobject.setVersionedName(m_versionedPrefix);
97
98 // make a metadata packet based on the received discovery Interest name
99 Data mdata(mobject.makeData(interest.getName(), m_keyChain, m_options.signingInfo));
100
101 if (m_options.isVerbose)
102 std::cerr << "Sending metadata: " << mdata << std::endl;
103
104 m_face.put(mdata);
105}
106
107void
108Producer::processSegmentInterest(const Interest& interest)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100109{
110 BOOST_ASSERT(m_store.size() > 0);
111
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400112 if (m_options.isVerbose)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100113 std::cerr << "Interest: " << interest << std::endl;
114
115 const Name& name = interest.getName();
116 shared_ptr<Data> data;
117
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800118 if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100119 const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment());
120 // specific segment retrieval
121 if (segmentNo < m_store.size()) {
122 data = m_store[segmentNo];
123 }
124 }
125 else if (interest.matchesData(*m_store[0])) {
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800126 // unspecified version or segment number, return first segment
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127 data = m_store[0];
128 }
129
130 if (data != nullptr) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400131 if (m_options.isVerbose)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100132 std::cerr << "Data: " << *data << std::endl;
133
134 m_face.put(*data);
135 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800136 else {
137 if (m_options.isVerbose)
138 std::cerr << "Interest cannot be satisfied, sending Nack" << std::endl;
139 m_face.put(lp::Nack(interest));
140 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141}
142
143void
144Producer::populateStore(std::istream& is)
145{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400146 BOOST_ASSERT(m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100147
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400148 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149 std::cerr << "Loading input ..." << std::endl;
150
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400151 std::vector<uint8_t> buffer(m_options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100152 while (is.good()) {
153 is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
154 const auto nCharsRead = is.gcount();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400155
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100156 if (nCharsRead > 0) {
157 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400158 data->setFreshnessPeriod(m_options.freshnessPeriod);
159 data->setContent(buffer.data(), static_cast<size_t>(nCharsRead));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100160 m_store.push_back(data);
161 }
162 }
163
164 if (m_store.empty()) {
165 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400166 data->setFreshnessPeriod(m_options.freshnessPeriod);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100167 m_store.push_back(data);
168 }
169
170 auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
171 for (const auto& data : m_store) {
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400172 data->setFinalBlock(finalBlockId);
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400173 m_keyChain.sign(*data, m_options.signingInfo);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100174 }
175
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400176 if (!m_options.isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100177 std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl;
178}
179
180void
181Producer::onRegisterFailed(const Name& prefix, const std::string& reason)
182{
183 std::cerr << "ERROR: Failed to register prefix '"
184 << prefix << "' (" << reason << ")" << std::endl;
185 m_face.shutdown();
186}
187
188} // namespace chunks
189} // namespace ndn