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 | */ |
| 147 | const Block& |
| 148 | wireEncode(); |
| 149 | /// Encodes this FileManifest into the wire format and returns a Block with the contents |
| 150 | |
| 151 | private: |
| 152 | void |
| 153 | decodeContent(); |
| 154 | /// Decodes the contents of this Data packet, assigning its contents to this FileManifest. |
| 155 | |
| 156 | void |
| 157 | encodeContent(); |
| 158 | /// Encodes the contents of this FileManifest into the content section of its Data packet. |
| 159 | |
| 160 | // DATA |
| 161 | private: |
| 162 | size_t m_dataPacketSize; |
| 163 | Name m_catalogPrefix; |
| 164 | std::vector<Name> m_catalog; |
| 165 | std::shared_ptr<Name> m_submanifestPtr; |
| 166 | }; |
| 167 | |
| 168 | /// Non-member functions |
| 169 | bool operator==(const FileManifest& lhs, const FileManifest& rhs); |
| 170 | /// Returns 'true' if 'lhs' and 'rhs' have the same value, 'false' otherwise. |
| 171 | |
| 172 | bool operator!=(const FileManifest& lhs, const FileManifest& rhs); |
| 173 | /// Returns 'true' if 'lhs' and 'rhs' have different values, and 'false' otherwise. |
| 174 | |
| 175 | inline |
| 176 | FileManifest::FileManifest( |
| 177 | const Name& name, |
| 178 | size_t dataPacketSize, |
| 179 | const Name& catalogPrefix, |
| 180 | const std::vector<Name>& catalog, |
| 181 | std::shared_ptr<Name> subManifestPtr) |
| 182 | : Data(name) |
| 183 | , m_dataPacketSize(dataPacketSize) |
| 184 | , m_catalogPrefix(catalogPrefix) |
| 185 | , m_catalog(catalog) |
| 186 | , m_submanifestPtr(subManifestPtr) |
| 187 | { |
| 188 | } |
| 189 | |
| 190 | inline |
| 191 | FileManifest::FileManifest( |
| 192 | const Name& name, |
| 193 | size_t dataPacketSize, |
| 194 | const Name& catalogPrefix, |
| 195 | std::vector<Name>&& catalog, |
| 196 | std::shared_ptr<Name> subManifestPtr) |
| 197 | : Data(name) |
| 198 | , m_dataPacketSize(dataPacketSize) |
| 199 | , m_catalogPrefix(catalogPrefix) |
| 200 | , m_catalog(catalog) |
| 201 | , m_submanifestPtr(subManifestPtr) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | inline |
| 206 | FileManifest::FileManifest(const Block& block) |
| 207 | { |
| 208 | wireDecode(block); |
| 209 | } |
| 210 | |
| 211 | inline const Name& |
| 212 | FileManifest::name() const |
| 213 | { |
| 214 | return getName(); |
| 215 | } |
| 216 | |
| 217 | inline size_t |
| 218 | FileManifest::data_packet_size() const |
| 219 | { |
| 220 | return m_dataPacketSize; |
| 221 | } |
| 222 | |
| 223 | inline const Name& |
| 224 | FileManifest::catalog_prefix() const |
| 225 | { |
| 226 | return m_catalogPrefix; |
| 227 | } |
| 228 | |
| 229 | inline const std::vector<Name>& |
| 230 | FileManifest::catalog() const |
| 231 | { |
| 232 | return m_catalog; |
| 233 | } |
| 234 | |
| 235 | inline std::shared_ptr<Name> |
| 236 | FileManifest::submanifest_ptr() const |
| 237 | { |
| 238 | return m_submanifestPtr; |
| 239 | } |
| 240 | |
| 241 | } // end ntorrent |
| 242 | } // end ndn |
| 243 | |
| 244 | #endif // INCLUDED_FILE_MANIFEST_HPP |