Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 1 | /* -*- 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 27 | #include <utility> |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include <ndn-cxx/data.hpp> |
| 31 | #include <ndn-cxx/name.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace ntorrent { |
| 35 | |
| 36 | class 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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 56 | // 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 89 | // CREATORS |
Mickey Sweatt | 6de5dde | 2016-03-15 16:44:56 -0700 | [diff] [blame^] | 90 | FileManifest() = default; |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 165 | set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr); |
| 166 | /// Sets the sub-manifest pointer of manifest to the specified 'subManifestPtr' |
| 167 | |
| 168 | void |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 169 | push_back(const Name& name); |
| 170 | /// Appends a Name to the catalog |
| 171 | |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 172 | void |
| 173 | reserve(size_t capacity); |
| 174 | /// Reserve memory in the catalog adequate to hold at least 'capacity' Names. |
| 175 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 176 | 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 Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame] | 188 | |
| 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 197 | |
| 198 | private: |
| 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 |
| 216 | bool operator==(const FileManifest& lhs, const FileManifest& rhs); |
| 217 | /// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise. |
| 218 | |
| 219 | bool operator!=(const FileManifest& lhs, const FileManifest& rhs); |
| 220 | /// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise. |
| 221 | |
| 222 | inline |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 223 | std::vector<FileManifest> |
| 224 | FileManifest::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 | |
| 232 | inline |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 233 | FileManifest::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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 247 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 248 | inline |
| 249 | FileManifest::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 | |
| 263 | inline |
| 264 | FileManifest::FileManifest(const Block& block) |
Mickey Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame] | 265 | : Data() |
| 266 | , m_dataPacketSize(0) |
| 267 | , m_catalogPrefix("") |
| 268 | , m_catalog() |
| 269 | , m_submanifestPtr(nullptr) |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 270 | { |
| 271 | wireDecode(block); |
| 272 | } |
| 273 | |
| 274 | inline const Name& |
| 275 | FileManifest::name() const |
| 276 | { |
| 277 | return getName(); |
| 278 | } |
| 279 | |
| 280 | inline size_t |
| 281 | FileManifest::data_packet_size() const |
| 282 | { |
| 283 | return m_dataPacketSize; |
| 284 | } |
| 285 | |
| 286 | inline const Name& |
| 287 | FileManifest::catalog_prefix() const |
| 288 | { |
| 289 | return m_catalogPrefix; |
| 290 | } |
| 291 | |
| 292 | inline const std::vector<Name>& |
| 293 | FileManifest::catalog() const |
| 294 | { |
| 295 | return m_catalog; |
| 296 | } |
| 297 | |
| 298 | inline std::shared_ptr<Name> |
| 299 | FileManifest::submanifest_ptr() const |
| 300 | { |
| 301 | return m_submanifestPtr; |
| 302 | } |
| 303 | |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 304 | inline void |
| 305 | FileManifest::set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr) |
| 306 | { |
| 307 | m_submanifestPtr = subManifestPtr; |
| 308 | } |
| 309 | |
| 310 | inline void |
| 311 | FileManifest::reserve(size_t capacity) |
| 312 | { |
| 313 | m_catalog.reserve(capacity); |
| 314 | } |
| 315 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 316 | } // end ntorrent |
| 317 | } // end ndn |
| 318 | |
| 319 | #endif // INCLUDED_FILE_MANIFEST_HPP |