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 | |
spirosmastorakis | d351c6b | 2016-05-06 17:02:48 -0700 | [diff] [blame^] | 24 | #include "util/shared-constants.hpp" |
| 25 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 26 | #include <cstring> |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 29 | #include <utility> |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
| 32 | #include <ndn-cxx/data.hpp> |
| 33 | #include <ndn-cxx/name.hpp> |
| 34 | |
| 35 | namespace ndn { |
| 36 | namespace ntorrent { |
| 37 | |
| 38 | class 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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 58 | // 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 91 | // CREATORS |
Mickey Sweatt | 6de5dde | 2016-03-15 16:44:56 -0700 | [diff] [blame] | 92 | FileManifest() = default; |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 93 | |
| 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 Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 146 | 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 154 | 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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 175 | set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr); |
| 176 | /// Sets the sub-manifest pointer of manifest to the specified 'subManifestPtr' |
| 177 | |
| 178 | void |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 179 | push_back(const Name& name); |
| 180 | /// Appends a Name to the catalog |
| 181 | |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 182 | void |
| 183 | reserve(size_t capacity); |
| 184 | /// Reserve memory in the catalog adequate to hold at least 'capacity' Names. |
| 185 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 186 | 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 Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame] | 198 | |
| 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 Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 207 | |
| 208 | private: |
| 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 |
| 226 | bool operator==(const FileManifest& lhs, const FileManifest& rhs); |
| 227 | /// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise. |
| 228 | |
| 229 | bool operator!=(const FileManifest& lhs, const FileManifest& rhs); |
| 230 | /// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise. |
| 231 | |
| 232 | inline |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 233 | std::vector<FileManifest> |
| 234 | FileManifest::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 | |
| 242 | inline |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 243 | FileManifest::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 Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 257 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 258 | inline |
| 259 | FileManifest::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 | |
| 273 | inline |
| 274 | FileManifest::FileManifest(const Block& block) |
Mickey Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame] | 275 | : Data() |
| 276 | , m_dataPacketSize(0) |
| 277 | , m_catalogPrefix("") |
| 278 | , m_catalog() |
| 279 | , m_submanifestPtr(nullptr) |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 280 | { |
| 281 | wireDecode(block); |
| 282 | } |
| 283 | |
| 284 | inline const Name& |
| 285 | FileManifest::name() const |
| 286 | { |
| 287 | return getName(); |
| 288 | } |
| 289 | |
| 290 | inline size_t |
| 291 | FileManifest::data_packet_size() const |
| 292 | { |
| 293 | return m_dataPacketSize; |
| 294 | } |
| 295 | |
| 296 | inline const Name& |
| 297 | FileManifest::catalog_prefix() const |
| 298 | { |
| 299 | return m_catalogPrefix; |
| 300 | } |
| 301 | |
| 302 | inline const std::vector<Name>& |
| 303 | FileManifest::catalog() const |
| 304 | { |
| 305 | return m_catalog; |
| 306 | } |
| 307 | |
| 308 | inline std::shared_ptr<Name> |
| 309 | FileManifest::submanifest_ptr() const |
| 310 | { |
| 311 | return m_submanifestPtr; |
| 312 | } |
| 313 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 314 | inline std::string |
| 315 | FileManifest::file_name() const |
| 316 | { |
spirosmastorakis | d351c6b | 2016-05-06 17:02:48 -0700 | [diff] [blame^] | 317 | Name scheme(SharedConstants::commonPrefix); |
| 318 | return name().getSubName(1 + scheme.size(), |
| 319 | name().size() - (2 + scheme.size())).toUri(); |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | inline size_t |
| 324 | FileManifest::submanifest_number() const |
| 325 | { |
| 326 | return name().get(name().size() - 1).toSequenceNumber(); |
| 327 | } |
| 328 | |
Mickey Sweatt | ebc0195 | 2016-02-19 11:38:30 -0800 | [diff] [blame] | 329 | inline void |
| 330 | FileManifest::set_submanifest_ptr(std::shared_ptr<Name> subManifestPtr) |
| 331 | { |
| 332 | m_submanifestPtr = subManifestPtr; |
| 333 | } |
| 334 | |
| 335 | inline void |
| 336 | FileManifest::reserve(size_t capacity) |
| 337 | { |
| 338 | m_catalog.reserve(capacity); |
| 339 | } |
| 340 | |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 341 | } // end ntorrent |
| 342 | } // end ndn |
| 343 | |
| 344 | #endif // INCLUDED_FILE_MANIFEST_HPP |