blob: ee04fdd2b2d5fba9ac947cd675e67688594166dd [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
spirosmastorakisd351c6b2016-05-06 17:02:48 -070024#include "util/shared-constants.hpp"
25
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080026#include <cstring>
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080027#include <memory>
28#include <string>
Mickey Sweattebc01952016-02-19 11:38:30 -080029#include <utility>
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080030#include <vector>
31
32#include <ndn-cxx/data.hpp>
33#include <ndn-cxx/name.hpp>
34
35namespace ndn {
36namespace ntorrent {
37
38class FileManifest : public Data {
39/**
40* \class FileManifest
41*
42* \brief A value semantic type for File manifests
43*
44*/
45 public:
46 // TYPES
47 class Error : public Data::Error
48 {
49 public:
50 explicit
51 Error(const std::string& what)
52 : Data::Error(what)
53 {
54 }
55 };
56
57 public:
Mickey Sweattebc01952016-02-19 11:38:30 -080058 // CLASS METHODS
59 static std::vector<FileManifest>
60 generate(const std::string& filePath,
61 const ndn::Name& manifestPrefix,
62 size_t subManifestSize,
63 size_t dataPacketSize);
64
65
66 static std::pair<std::vector<FileManifest>, std::vector<Data>>
67 generate(const std::string& filePath,
68 const ndn::Name& manifestPrefix,
69 size_t subManifestSize,
70 size_t dataPacketSize,
71 bool returnData);
72 /**
73 * \brief Generates the FileManifest(s) and Data packets for the file at the specified 'filePath'
74 *
75 * @param filePath The path to the file for which we are to create a manifest
76 * @param manifestPrefix The prefix to be used for the name of this manifest
77 * @param subManifestSize The maximum number of data packets to be included in a sub-manifest
78 * @param dataPacketSize The maximum number of bytes per Data packet packets for the file
79 * @param returnData If true also return the Data
80 *
81 * @throws Error if there is any I/O issue when trying to read the filePath.
82 *
83 * Generates the FileManfiest(s) for the file at the specified 'filePath', splitting the manifest
84 * into sub-manifests of size at most the specified 'subManifestSize'. Each sub-manifest is
85 * composed of a catalog of Data packets of at most the specified 'dataPacketSize'. Returns all
86 * of the manifests that were created in order. The behavior is undefined unless the
87 * trailing component of of the manifestPrefix is a subComponent filePath and
88 '* O < subManifestSize' and '0 < dataPacketSize'.
89 */
90
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080091 // CREATORS
Mickey Sweatt6de5dde2016-03-15 16:44:56 -070092 FileManifest() = default;
Mickey Sweatt3b0bea62016-01-25 22:12:27 -080093
94 ~FileManifest() = default;
95 /// Destroy this object
96
97 FileManifest(const Name& name,
98 size_t dataPacketSize,
99 const Name& commonPrefix,
100 const std::vector<Name>& catalog = std::vector<Name>(),
101 std::shared_ptr<Name> subManifestPtr = nullptr);
102 /**
103 * \brief Creates a new FileManifest with the specified values
104 *
105 * @param name The Name of this FileManifest
106 * @param dataPacketSize The size (except the last) of each data packet in this FileManifest
107 * @param commonPrefix The common prefix used for all named in the catalog
108 * @param catalog The collection of Names for this FileManifest
109 * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file
110 */
111
112 FileManifest(const Name& name,
113 size_t dataPacketSize,
114 const Name& catalogPrefix,
115 std::vector<Name>&& catalog,
116 std::shared_ptr<Name> subManifestPtr = nullptr);
117 /**
118 * \brief Creates a new FileManifest with the specified values
119 *
120 * @param name The Name of this FileManifest
121 * @param dataPacketSize The size (except the last) of each data packet in this FileManifest
122 * @param catalogPrefix the common prefix used for all named in the catalog
123 * @param catalog The collection of Names for this FileManifest
124 * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file
125 */
126
127 explicit
128 FileManifest(const Block& block);
129
130 FileManifest(const FileManifest& other) = default;
131 /// Creates a new FileManifest with same value as the specified 'other'
132
133 // ACCESSORS
134 const Name&
135 name() const;
136 /// Returns the 'name' of this FileManifest
137
138 const Name&
139 catalog_prefix() const;
140 /// Returns the 'catalogPrefix' of this FileManfiest.
141
142 size_t
143 data_packet_size() const;
144 /// Returns the 'data_packet_size' of this FileManifest
145
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700146 size_t
147 submanifest_number() const;
148 /// Return the submanifest number for this FileManifest
149
150 std::string
151 file_name() const;
152 /// Return the file name for this FileManifest
153
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800154 std::shared_ptr<Name>
155 submanifest_ptr() const;
156 /// Returns the 'submanifest_ptr' of this FileManifest, or 'nullptr' is none exists
157
158 const std::vector<Name>&
159 catalog() const;
160 /// Returns an unmodifiable reference to the 'catalog' of this FileManifest
161
162 private:
163 template<encoding::Tag TAG>
164 size_t
165 encodeContent(EncodingImpl<TAG>& encoder) const;
166 /// Encodes the contents of this object and append the contents to the 'encoder'
167
168 public:
169 // MANIPULATORS
170 FileManifest&
171 operator=(const FileManifest& rhs) = default;
172 /// Assigns the value of the specific 'rhs' object to this object.
173
174 void
Mickey Sweattebc01952016-02-19 11:38:30 -0800175 set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr);
176 /// Sets the sub-manifest pointer of manifest to the specified 'subManifestPtr'
177
178 void
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800179 push_back(const Name& name);
180 /// Appends a Name to the catalog
181
Mickey Sweattebc01952016-02-19 11:38:30 -0800182 void
183 reserve(size_t capacity);
184 /// Reserve memory in the catalog adequate to hold at least 'capacity' Names.
185
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800186 bool
187 remove(const Name& name);
188 /// If 'name' in catalog, removes first instance and returns 'true', otherwise returns 'false'.
189
190 void
191 wireDecode(const Block& wire);
192 /**
193 * \brief Decodes the wire and assign its contents to this FileManifest
194 *
195 * @param wire the write to be decoded
196 * @throws Error if decoding fails
197 */
Mickey Sweatta768b242016-02-29 20:08:05 -0800198
199 void
200 finalize();
201 /*
202 * \brief Performs all final processing on this manifest
203 *
204 * This method should be called once this manifest is populated and *must* be called before
205 * signing it or calling wireEncode().
206 */
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800207
208private:
209 void
210 decodeContent();
211 /// Decodes the contents of this Data packet, assigning its contents to this FileManifest.
212
213 void
214 encodeContent();
215 /// Encodes the contents of this FileManifest into the content section of its Data packet.
216
217// DATA
218 private:
219 size_t m_dataPacketSize;
220 Name m_catalogPrefix;
221 std::vector<Name> m_catalog;
222 std::shared_ptr<Name> m_submanifestPtr;
223};
224
225/// Non-member functions
226bool operator==(const FileManifest& lhs, const FileManifest& rhs);
227/// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise.
228
229bool operator!=(const FileManifest& lhs, const FileManifest& rhs);
230/// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise.
231
232inline
Mickey Sweattebc01952016-02-19 11:38:30 -0800233std::vector<FileManifest>
234FileManifest::generate(const std::string& filePath,
235 const ndn::Name& manifestPrefix,
236 size_t subManifestSize,
237 size_t dataPacketSize)
238{
239 return generate(filePath, manifestPrefix, subManifestSize, dataPacketSize, false).first;
240}
241
242inline
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800243FileManifest::FileManifest(
244 const Name& name,
245 size_t dataPacketSize,
246 const Name& catalogPrefix,
247 const std::vector<Name>& catalog,
248 std::shared_ptr<Name> subManifestPtr)
249: Data(name)
250, m_dataPacketSize(dataPacketSize)
251, m_catalogPrefix(catalogPrefix)
252, m_catalog(catalog)
253, m_submanifestPtr(subManifestPtr)
254{
255}
256
Mickey Sweattebc01952016-02-19 11:38:30 -0800257
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800258inline
259FileManifest::FileManifest(
260 const Name& name,
261 size_t dataPacketSize,
262 const Name& catalogPrefix,
263 std::vector<Name>&& catalog,
264 std::shared_ptr<Name> subManifestPtr)
265: Data(name)
266, m_dataPacketSize(dataPacketSize)
267, m_catalogPrefix(catalogPrefix)
268, m_catalog(catalog)
269, m_submanifestPtr(subManifestPtr)
270{
271}
272
273inline
274FileManifest::FileManifest(const Block& block)
Mickey Sweatta768b242016-02-29 20:08:05 -0800275: Data()
276, m_dataPacketSize(0)
277, m_catalogPrefix("")
278, m_catalog()
279, m_submanifestPtr(nullptr)
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800280{
281 wireDecode(block);
282}
283
284inline const Name&
285FileManifest::name() const
286{
287 return getName();
288}
289
290inline size_t
291FileManifest::data_packet_size() const
292{
293 return m_dataPacketSize;
294}
295
296inline const Name&
297FileManifest::catalog_prefix() const
298{
299 return m_catalogPrefix;
300}
301
302inline const std::vector<Name>&
303FileManifest::catalog() const
304{
305 return m_catalog;
306}
307
308inline std::shared_ptr<Name>
309FileManifest::submanifest_ptr() const
310{
311 return m_submanifestPtr;
312}
313
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700314inline std::string
315FileManifest::file_name() const
316{
spirosmastorakisd351c6b2016-05-06 17:02:48 -0700317 Name scheme(SharedConstants::commonPrefix);
318 return name().getSubName(1 + scheme.size(),
319 name().size() - (2 + scheme.size())).toUri();
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700320}
321
322
323inline size_t
324FileManifest::submanifest_number() const
325{
326 return name().get(name().size() - 1).toSequenceNumber();
327}
328
Mickey Sweattebc01952016-02-19 11:38:30 -0800329inline void
330FileManifest::set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr)
331{
332 m_submanifestPtr = subManifestPtr;
333}
334
335inline void
336FileManifest::reserve(size_t capacity)
337{
338 m_catalog.reserve(capacity);
339}
340
Mickey Sweatt3b0bea62016-01-25 22:12:27 -0800341} // end ntorrent
342} // end ndn
343
344#endif // INCLUDED_FILE_MANIFEST_HPP