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> |
| 25 | #include <list> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 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: |
| 56 | // CREATORS |
| 57 | FileManifest() = delete; |
| 58 | |
| 59 | ~FileManifest() = default; |
| 60 | /// Destroy this object |
| 61 | |
| 62 | FileManifest(const Name& name, |
| 63 | size_t dataPacketSize, |
| 64 | const Name& commonPrefix, |
| 65 | const std::vector<Name>& catalog = std::vector<Name>(), |
| 66 | std::shared_ptr<Name> subManifestPtr = nullptr); |
| 67 | /** |
| 68 | * \brief Creates a new FileManifest with the specified values |
| 69 | * |
| 70 | * @param name The Name of this FileManifest |
| 71 | * @param dataPacketSize The size (except the last) of each data packet in this FileManifest |
| 72 | * @param commonPrefix The common prefix used for all named in the catalog |
| 73 | * @param catalog The collection of Names for this FileManifest |
| 74 | * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file |
| 75 | */ |
| 76 | |
| 77 | FileManifest(const Name& name, |
| 78 | size_t dataPacketSize, |
| 79 | const Name& catalogPrefix, |
| 80 | std::vector<Name>&& catalog, |
| 81 | std::shared_ptr<Name> subManifestPtr = nullptr); |
| 82 | /** |
| 83 | * \brief Creates a new FileManifest with the specified values |
| 84 | * |
| 85 | * @param name The Name of this FileManifest |
| 86 | * @param dataPacketSize The size (except the last) of each data packet in this FileManifest |
| 87 | * @param catalogPrefix the common prefix used for all named in the catalog |
| 88 | * @param catalog The collection of Names for this FileManifest |
| 89 | * @param subManifestPtr (optional) The Name for the sub-manifest in this for this file |
| 90 | */ |
| 91 | |
| 92 | explicit |
| 93 | FileManifest(const Block& block); |
| 94 | |
| 95 | FileManifest(const FileManifest& other) = default; |
| 96 | /// Creates a new FileManifest with same value as the specified 'other' |
| 97 | |
| 98 | // ACCESSORS |
| 99 | const Name& |
| 100 | name() const; |
| 101 | /// Returns the 'name' of this FileManifest |
| 102 | |
| 103 | const Name& |
| 104 | catalog_prefix() const; |
| 105 | /// Returns the 'catalogPrefix' of this FileManfiest. |
| 106 | |
| 107 | size_t |
| 108 | data_packet_size() const; |
| 109 | /// Returns the 'data_packet_size' of this FileManifest |
| 110 | |
| 111 | std::shared_ptr<Name> |
| 112 | submanifest_ptr() const; |
| 113 | /// Returns the 'submanifest_ptr' of this FileManifest, or 'nullptr' is none exists |
| 114 | |
| 115 | const std::vector<Name>& |
| 116 | catalog() const; |
| 117 | /// Returns an unmodifiable reference to the 'catalog' of this FileManifest |
| 118 | |
| 119 | private: |
| 120 | template<encoding::Tag TAG> |
| 121 | size_t |
| 122 | encodeContent(EncodingImpl<TAG>& encoder) const; |
| 123 | /// Encodes the contents of this object and append the contents to the 'encoder' |
| 124 | |
| 125 | public: |
| 126 | // MANIPULATORS |
| 127 | FileManifest& |
| 128 | operator=(const FileManifest& rhs) = default; |
| 129 | /// Assigns the value of the specific 'rhs' object to this object. |
| 130 | |
| 131 | void |
| 132 | push_back(const Name& name); |
| 133 | /// Appends a Name to the catalog |
| 134 | |
| 135 | bool |
| 136 | remove(const Name& name); |
| 137 | /// If 'name' in catalog, removes first instance and returns 'true', otherwise returns 'false'. |
| 138 | |
| 139 | void |
| 140 | wireDecode(const Block& wire); |
| 141 | /** |
| 142 | * \brief Decodes the wire and assign its contents to this FileManifest |
| 143 | * |
| 144 | * @param wire the write to be decoded |
| 145 | * @throws Error if decoding fails |
| 146 | */ |
Mickey Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame^] | 147 | |
| 148 | void |
| 149 | finalize(); |
| 150 | /* |
| 151 | * \brief Performs all final processing on this manifest |
| 152 | * |
| 153 | * This method should be called once this manifest is populated and *must* be called before |
| 154 | * signing it or calling wireEncode(). |
| 155 | */ |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | void |
| 159 | decodeContent(); |
| 160 | /// Decodes the contents of this Data packet, assigning its contents to this FileManifest. |
| 161 | |
| 162 | void |
| 163 | encodeContent(); |
| 164 | /// Encodes the contents of this FileManifest into the content section of its Data packet. |
| 165 | |
| 166 | // DATA |
| 167 | private: |
| 168 | size_t m_dataPacketSize; |
| 169 | Name m_catalogPrefix; |
| 170 | std::vector<Name> m_catalog; |
| 171 | std::shared_ptr<Name> m_submanifestPtr; |
| 172 | }; |
| 173 | |
| 174 | /// Non-member functions |
| 175 | bool operator==(const FileManifest& lhs, const FileManifest& rhs); |
| 176 | /// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise. |
| 177 | |
| 178 | bool operator!=(const FileManifest& lhs, const FileManifest& rhs); |
| 179 | /// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise. |
| 180 | |
| 181 | inline |
| 182 | FileManifest::FileManifest( |
| 183 | const Name& name, |
| 184 | size_t dataPacketSize, |
| 185 | const Name& catalogPrefix, |
| 186 | const std::vector<Name>& catalog, |
| 187 | std::shared_ptr<Name> subManifestPtr) |
| 188 | : Data(name) |
| 189 | , m_dataPacketSize(dataPacketSize) |
| 190 | , m_catalogPrefix(catalogPrefix) |
| 191 | , m_catalog(catalog) |
| 192 | , m_submanifestPtr(subManifestPtr) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | inline |
| 197 | FileManifest::FileManifest( |
| 198 | const Name& name, |
| 199 | size_t dataPacketSize, |
| 200 | const Name& catalogPrefix, |
| 201 | std::vector<Name>&& catalog, |
| 202 | std::shared_ptr<Name> subManifestPtr) |
| 203 | : Data(name) |
| 204 | , m_dataPacketSize(dataPacketSize) |
| 205 | , m_catalogPrefix(catalogPrefix) |
| 206 | , m_catalog(catalog) |
| 207 | , m_submanifestPtr(subManifestPtr) |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | inline |
| 212 | FileManifest::FileManifest(const Block& block) |
Mickey Sweatt | a768b24 | 2016-02-29 20:08:05 -0800 | [diff] [blame^] | 213 | : Data() |
| 214 | , m_dataPacketSize(0) |
| 215 | , m_catalogPrefix("") |
| 216 | , m_catalog() |
| 217 | , m_submanifestPtr(nullptr) |
Mickey Sweatt | 3b0bea6 | 2016-01-25 22:12:27 -0800 | [diff] [blame] | 218 | { |
| 219 | wireDecode(block); |
| 220 | } |
| 221 | |
| 222 | inline const Name& |
| 223 | FileManifest::name() const |
| 224 | { |
| 225 | return getName(); |
| 226 | } |
| 227 | |
| 228 | inline size_t |
| 229 | FileManifest::data_packet_size() const |
| 230 | { |
| 231 | return m_dataPacketSize; |
| 232 | } |
| 233 | |
| 234 | inline const Name& |
| 235 | FileManifest::catalog_prefix() const |
| 236 | { |
| 237 | return m_catalogPrefix; |
| 238 | } |
| 239 | |
| 240 | inline const std::vector<Name>& |
| 241 | FileManifest::catalog() const |
| 242 | { |
| 243 | return m_catalog; |
| 244 | } |
| 245 | |
| 246 | inline std::shared_ptr<Name> |
| 247 | FileManifest::submanifest_ptr() const |
| 248 | { |
| 249 | return m_submanifestPtr; |
| 250 | } |
| 251 | |
| 252 | } // end ntorrent |
| 253 | } // end ndn |
| 254 | |
| 255 | #endif // INCLUDED_FILE_MANIFEST_HPP |