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