blob: 5e50900491b7a345c9b3598f66b30ba1e72b9b42 [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/*
3 * Copyright (c) 2016-2017, Regents of the University of California,
Andrea Tosatto672b9a72016-01-05 16:18:20 +01004 * 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
Klaus Schneider59ec95a2017-09-14 17:50:00 -070026 * @author Klaus Schneider
Andrea Tosatto672b9a72016-01-05 16:18:20 +010027 */
28
29#include "producer.hpp"
30
31namespace ndn {
32namespace chunks {
33
34Producer::Producer(const Name& prefix,
35 Face& face,
36 KeyChain& keyChain,
37 const security::SigningInfo& signingInfo,
38 time::milliseconds freshnessPeriod,
39 size_t maxSegmentSize,
Klaus Schneider59ec95a2017-09-14 17:50:00 -070040 bool isQuiet,
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041 bool isVerbose,
42 bool needToPrintVersion,
43 std::istream& is)
44 : m_face(face)
45 , m_keyChain(keyChain)
46 , m_signingInfo(signingInfo)
47 , m_freshnessPeriod(freshnessPeriod)
48 , m_maxSegmentSize(maxSegmentSize)
Klaus Schneider59ec95a2017-09-14 17:50:00 -070049 , m_isQuiet(isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010050 , m_isVerbose(isVerbose)
51{
52 if (prefix.size() > 0 && prefix[-1].isVersion()) {
53 m_prefix = prefix.getPrefix(-1);
54 m_versionedPrefix = prefix;
55 }
56 else {
57 m_prefix = prefix;
58 m_versionedPrefix = Name(m_prefix).appendVersion();
59 }
60
61 populateStore(is);
62
63 if (needToPrintVersion)
64 std::cout << m_versionedPrefix[-1] << std::endl;
65
66 m_face.setInterestFilter(m_prefix,
67 bind(&Producer::onInterest, this, _2),
68 RegisterPrefixSuccessCallback(),
69 bind(&Producer::onRegisterFailed, this, _1, _2));
70
Klaus Schneider59ec95a2017-09-14 17:50:00 -070071 if (!m_isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 std::cerr << "Data published with name: " << m_versionedPrefix << std::endl;
73}
74
75void
76Producer::run()
77{
78 m_face.processEvents();
79}
80
81void
82Producer::onInterest(const Interest& interest)
83{
84 BOOST_ASSERT(m_store.size() > 0);
85
86 if (m_isVerbose)
87 std::cerr << "Interest: " << interest << std::endl;
88
89 const Name& name = interest.getName();
90 shared_ptr<Data> data;
91
92 // is this a discovery Interest or a sequence retrieval?
93 if (name.size() == m_versionedPrefix.size() + 1 && m_versionedPrefix.isPrefixOf(name) &&
94 name[-1].isSegment()) {
95 const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment());
96 // specific segment retrieval
97 if (segmentNo < m_store.size()) {
98 data = m_store[segmentNo];
99 }
100 }
101 else if (interest.matchesData(*m_store[0])) {
102 // Interest has version and is looking for the first segment or has no version
103 data = m_store[0];
104 }
105
106 if (data != nullptr) {
107 if (m_isVerbose)
108 std::cerr << "Data: " << *data << std::endl;
109
110 m_face.put(*data);
111 }
112}
113
114void
115Producer::populateStore(std::istream& is)
116{
117 BOOST_ASSERT(m_store.size() == 0);
118
Klaus Schneider59ec95a2017-09-14 17:50:00 -0700119 if (!m_isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100120 std::cerr << "Loading input ..." << std::endl;
121
122 std::vector<uint8_t> buffer(m_maxSegmentSize);
123 while (is.good()) {
124 is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
125 const auto nCharsRead = is.gcount();
126 if (nCharsRead > 0) {
127 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
128 data->setFreshnessPeriod(m_freshnessPeriod);
129 data->setContent(&buffer[0], nCharsRead);
130
131 m_store.push_back(data);
132 }
133 }
134
135 if (m_store.empty()) {
136 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
137 data->setFreshnessPeriod(m_freshnessPeriod);
138 m_store.push_back(data);
139 }
140
141 auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
142 for (const auto& data : m_store) {
143 data->setFinalBlockId(finalBlockId);
144 m_keyChain.sign(*data, m_signingInfo);
145 }
146
Klaus Schneider59ec95a2017-09-14 17:50:00 -0700147 if (!m_isQuiet)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100148 std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl;
149}
150
151void
152Producer::onRegisterFailed(const Name& prefix, const std::string& reason)
153{
154 std::cerr << "ERROR: Failed to register prefix '"
155 << prefix << "' (" << reason << ")" << std::endl;
156 m_face.shutdown();
157}
158
159} // namespace chunks
160} // namespace ndn