blob: b3cf3ffdf275cdf83134cde39b00fb2f4854adbd [file] [log] [blame]
Mickey Sweatt3b0bea62016-01-25 22:12:27 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent is free software: you can redistribute it and/or modify it under the
8* terms of the GNU Lesser General Public License as published by the Free Software
9* Foundation, either version 3 of the License, or (at your option) any later version.
10*
11* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
12* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14*
15* You should have received copies of the GNU General Public License and GNU Lesser
16* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21#ifndef INCLUDED_FILE_MANIFEST_HPP
22#define INCLUDED_FILE_MANIFEST_HPP
23
24#include <cstring>
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080025#include <memory>
26#include <string>
Mickey Sweattebc01952016-02-19 11:38:30 -080027#include <utility>
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080028#include <vector>
29
30#include <ndn-cxx/data.hpp>
31#include <ndn-cxx/name.hpp>
32
33namespace ndn {
34namespace ntorrent {
35
36class FileManifest : public Data {
37/**
38* \class FileManifest
39*
40* \brief A value semantic type for File manifests
41*
42*/
43 public:
44 // TYPES
45 class Error : public Data::Error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : Data::Error(what)
51 {
52 }
53 };
54
55 public:
Mickey Sweattebc01952016-02-19 11:38:30 -080056 // CLASS METHODS
57 static std::vector<FileManifest>
58 generate(const std::string& filePath,
59 const ndn::Name& manifestPrefix,
60 size_t subManifestSize,
61 size_t dataPacketSize);
62
63
64 static std::pair<std::vector<FileManifest>, std::vector<Data>>
65 generate(const std::string& filePath,
66 const ndn::Name& manifestPrefix,
67 size_t subManifestSize,
68 size_t dataPacketSize,
69 bool returnData);
70 /**
71 * \brief Generates the FileManifest(s) and Data packets for the file at the specified 'filePath'
72 *
73 * @param filePath The path to the file for which we are to create a manifest
74 * @param manifestPrefix The prefix to be used for the name of this manifest
75 * @param subManifestSize The maximum number of data packets to be included in a sub-manifest
76 * @param dataPacketSize The maximum number of bytes per Data packet packets for the file
77 * @param returnData If true also return the Data
78 *
79 * @throws Error if there is any I/O issue when trying to read the filePath.
80 *
81 * Generates the FileManfiest(s) for the file at the specified 'filePath', splitting the manifest
82 * into sub-manifests of size at most the specified 'subManifestSize'. Each sub-manifest is
83 * composed of a catalog of Data packets of at most the specified 'dataPacketSize'. Returns all
84 * of the manifests that were created in order. The behavior is undefined unless the
85 * trailing component of of the manifestPrefix is a subComponent filePath and
86 '* O < subManifestSize' and '0 < dataPacketSize'.
87 */
88
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080089 // CREATORS
90 FileManifest() = delete;
91
92 ~FileManifest() = default;
93 /// Destroy this object
94
95 FileManifest(const Name& name,
96 size_t dataPacketSize,
97 const Name& commonPrefix,
98 const std::vector<Name>& catalog = std::vector<Name>(),
99 std::shared_ptr<Name> subManifestPtr = nullptr);
100 /**
101 * \brief Creates a new FileManifest with the specified values
102 *
103 * @param name The Name of this FileManifest
104 * @param dataPacketSize The size (except the last) of each data packet in this FileManifest
105 * @param commonPrefix The common prefix used for all named in the catalog
106 * @param catalog The collection of Names for this FileManifest
107 * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file
108 */
109
110 FileManifest(const Name& name,
111 size_t dataPacketSize,
112 const Name& catalogPrefix,
113 std::vector<Name>&& catalog,
114 std::shared_ptr<Name> subManifestPtr = nullptr);
115 /**
116 * \brief Creates a new FileManifest with the specified values
117 *
118 * @param name The Name of this FileManifest
119 * @param dataPacketSize The size (except the last) of each data packet in this FileManifest
120 * @param catalogPrefix the common prefix used for all named in the catalog
121 * @param catalog The collection of Names for this FileManifest
122 * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file
123 */
124
125 explicit
126 FileManifest(const Block& block);
127
128 FileManifest(const FileManifest& other) = default;
129 /// Creates a new FileManifest with same value as the specified 'other'
130
131 // ACCESSORS
132 const Name&
133 name() const;
134 /// Returns the 'name' of this FileManifest
135
136 const Name&
137 catalog_prefix() const;
138 /// Returns the 'catalogPrefix' of this FileManfiest.
139
140 size_t
141 data_packet_size() const;
142 /// Returns the 'data_packet_size' of this FileManifest
143
144 std::shared_ptr<Name>
145 submanifest_ptr() const;
146 /// Returns the 'submanifest_ptr' of this FileManifest, or 'nullptr' is none exists
147
148 const std::vector<Name>&
149 catalog() const;
150 /// Returns an unmodifiable reference to the 'catalog' of this FileManifest
151
152 private:
153 template<encoding::Tag TAG>
154 size_t
155 encodeContent(EncodingImpl<TAG>& encoder) const;
156 /// Encodes the contents of this object and append the contents to the 'encoder'
157
158 public:
159 // MANIPULATORS
160 FileManifest&
161 operator=(const FileManifest& rhs) = default;
162 /// Assigns the value of the specific 'rhs' object to this object.
163
164 void
Mickey Sweattebc01952016-02-19 11:38:30 -0800165 set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr);
166 /// Sets the sub-manifest pointer of manifest to the specified 'subManifestPtr'
167
168 void
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800169 push_back(const Name& name);
170 /// Appends a Name to the catalog
171
Mickey Sweattebc01952016-02-19 11:38:30 -0800172 void
173 reserve(size_t capacity);
174 /// Reserve memory in the catalog adequate to hold at least 'capacity' Names.
175
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800176 bool
177 remove(const Name& name);
178 /// If 'name' in catalog, removes first instance and returns 'true', otherwise returns 'false'.
179
180 void
181 wireDecode(const Block& wire);
182 /**
183 * \brief Decodes the wire and assign its contents to this FileManifest
184 *
185 * @param wire the write to be decoded
186 * @throws Error if decoding fails
187 */
Mickey Sweatta768b242016-02-29 20:08:05 -0800188
189 void
190 finalize();
191 /*
192 * \brief Performs all final processing on this manifest
193 *
194 * This method should be called once this manifest is populated and *must* be called before
195 * signing it or calling wireEncode().
196 */
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800197
198private:
199 void
200 decodeContent();
201 /// Decodes the contents of this Data packet, assigning its contents to this FileManifest.
202
203 void
204 encodeContent();
205 /// Encodes the contents of this FileManifest into the content section of its Data packet.
206
207// DATA
208 private:
209 size_t m_dataPacketSize;
210 Name m_catalogPrefix;
211 std::vector<Name> m_catalog;
212 std::shared_ptr<Name> m_submanifestPtr;
213};
214
215/// Non-member functions
216bool operator==(const FileManifest& lhs, const FileManifest& rhs);
217/// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise.
218
219bool operator!=(const FileManifest& lhs, const FileManifest& rhs);
220/// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise.
221
222inline
Mickey Sweattebc01952016-02-19 11:38:30 -0800223std::vector<FileManifest>
224FileManifest::generate(const std::string& filePath,
225 const ndn::Name& manifestPrefix,
226 size_t subManifestSize,
227 size_t dataPacketSize)
228{
229 return generate(filePath, manifestPrefix, subManifestSize, dataPacketSize, false).first;
230}
231
232inline
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800233FileManifest::FileManifest(
234 const Name& name,
235 size_t dataPacketSize,
236 const Name& catalogPrefix,
237 const std::vector<Name>& catalog,
238 std::shared_ptr<Name> subManifestPtr)
239: Data(name)
240, m_dataPacketSize(dataPacketSize)
241, m_catalogPrefix(catalogPrefix)
242, m_catalog(catalog)
243, m_submanifestPtr(subManifestPtr)
244{
245}
246
Mickey Sweattebc01952016-02-19 11:38:30 -0800247
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800248inline
249FileManifest::FileManifest(
250 const Name& name,
251 size_t dataPacketSize,
252 const Name& catalogPrefix,
253 std::vector<Name>&& catalog,
254 std::shared_ptr<Name> subManifestPtr)
255: Data(name)
256, m_dataPacketSize(dataPacketSize)
257, m_catalogPrefix(catalogPrefix)
258, m_catalog(catalog)
259, m_submanifestPtr(subManifestPtr)
260{
261}
262
263inline
264FileManifest::FileManifest(const Block& block)
Mickey Sweatta768b242016-02-29 20:08:05 -0800265: Data()
266, m_dataPacketSize(0)
267, m_catalogPrefix("")
268, m_catalog()
269, m_submanifestPtr(nullptr)
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800270{
271 wireDecode(block);
272}
273
274inline const Name&
275FileManifest::name() const
276{
277 return getName();
278}
279
280inline size_t
281FileManifest::data_packet_size() const
282{
283 return m_dataPacketSize;
284}
285
286inline const Name&
287FileManifest::catalog_prefix() const
288{
289 return m_catalogPrefix;
290}
291
292inline const std::vector<Name>&
293FileManifest::catalog() const
294{
295 return m_catalog;
296}
297
298inline std::shared_ptr<Name>
299FileManifest::submanifest_ptr() const
300{
301 return m_submanifestPtr;
302}
303
Mickey Sweattebc01952016-02-19 11:38:30 -0800304inline void
305FileManifest::set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr)
306{
307 m_submanifestPtr = subManifestPtr;
308}
309
310inline void
311FileManifest::reserve(size_t capacity)
312{
313 m_catalog.reserve(capacity);
314}
315
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800316} // end ntorrent
317} // end ndn
318
319#endif // INCLUDED_FILE_MANIFEST_HPP