spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -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 | |
| 22 | #ifndef TORRENT_FILE_HPP |
| 23 | #define TORRENT_FILE_HPP |
| 24 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 25 | #include "file-manifest.hpp" |
| 26 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 27 | #include <ndn-cxx/name.hpp> |
| 28 | #include <ndn-cxx/encoding/block.hpp> |
| 29 | #include <ndn-cxx/data.hpp> |
| 30 | |
| 31 | #include <memory> |
| 32 | |
| 33 | namespace ndn { |
| 34 | |
| 35 | namespace ntorrent { |
| 36 | |
| 37 | class TorrentFile : public Data { |
| 38 | public: |
| 39 | class Error : public Data::Error |
| 40 | { |
| 41 | public: |
| 42 | explicit |
| 43 | Error(const std::string& what) |
| 44 | : Data::Error(what) |
| 45 | { |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 50 | * @brief Create a new empty TorrentFile. |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 51 | */ |
| 52 | TorrentFile() = default; |
| 53 | |
| 54 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 55 | * @brief Create a new TorrentFile. |
| 56 | * @param torrentFileName The name of the torrent-file |
| 57 | * @param torrentFilePtr A pointer (name) to the next segment of the torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 58 | * @param commonPrefix The common name prefix of the manifest file names included in the catalog |
| 59 | * @param catalog The catalog containing the name of each file manifest |
| 60 | */ |
| 61 | TorrentFile(const Name& torrentFileName, |
| 62 | const Name& torrentFilePtr, |
| 63 | const Name& commonPrefix, |
| 64 | const std::vector<ndn::Name>& catalog); |
| 65 | |
| 66 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 67 | * @brief Create a new TorrentFile. |
| 68 | * @param torrentFileName The name of the torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 69 | * @param commonPrefix The common name prefix of the manifest file names included in the catalog |
| 70 | * @param catalog The catalog containing the name of each file manifest |
| 71 | */ |
| 72 | TorrentFile(const Name& torrentFileName, |
| 73 | const Name& commonPrefix, |
| 74 | const std::vector<ndn::Name>& catalog); |
| 75 | |
| 76 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 77 | * @brief Create a new TorrentFile |
| 78 | * @param block The block format of the torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 79 | */ |
| 80 | explicit |
| 81 | TorrentFile(const Block& block); |
| 82 | |
| 83 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 84 | * @brief Get the name of the TorrentFile |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 85 | */ |
| 86 | const Name& |
| 87 | getName() const; |
| 88 | |
| 89 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 90 | * @brief Get the common prefix of the file manifest names of this torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 91 | */ |
| 92 | const Name& |
| 93 | getCommonPrefix() const; |
| 94 | |
| 95 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 96 | * @brief Get a shared pointer to the name of the next segment of the torrent-file. |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 97 | * |
| 98 | * If there is no next segment, it returns a nullptr |
| 99 | */ |
| 100 | shared_ptr<Name> |
| 101 | getTorrentFilePtr() const; |
| 102 | |
| 103 | /** |
| 104 | * @brief Get the catalog of names of the file manifests |
| 105 | */ |
| 106 | const std::vector<Name>& |
| 107 | getCatalog() const; |
| 108 | |
| 109 | /** |
| 110 | * @brief Decode from wire format |
| 111 | */ |
| 112 | void |
| 113 | wireDecode(const Block& wire); |
| 114 | |
| 115 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 116 | * @brief Finalize torrent-file before signing the data packet |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 117 | * |
| 118 | * This method has to be called (every time) right before signing or encoding |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 119 | * the torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 120 | */ |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 121 | void |
| 122 | finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * @brief Insert a name to the catalog of file manifest names |
| 126 | */ |
| 127 | void |
| 128 | insert(const Name& name); |
| 129 | |
| 130 | /** |
| 131 | * @brief Erase a name from the catalog of file manifest names |
| 132 | */ |
| 133 | bool |
| 134 | erase(const Name& name); |
| 135 | |
| 136 | /** |
| 137 | * @brief Get the size of the catalog of file manifest names |
| 138 | */ |
| 139 | size_t |
| 140 | catalogSize() const; |
| 141 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 142 | /** |
| 143 | * @brief Given a directory path for the torrent file, it generates the torrent file |
| 144 | * |
| 145 | * @param directoryPath The path to the directory for which we are to create a torrent-file |
| 146 | * @param torrentFilePrefix The prefix to be used for the name of this torrent-file |
| 147 | * @param namesPerSegment The number of manifest names to be included in each segment of the |
| 148 | * torrent-file |
| 149 | * @param returnData Determines whether the data would be returned in memory or it will be |
| 150 | * stored on disk without being returned |
| 151 | * |
| 152 | * Generates the torrent-file for the directory at the specified 'directoryPath', |
| 153 | * splitting the torrent-file into multiple segments, each one of which contains |
| 154 | * at most 'namesPerSegment' number of manifest names |
| 155 | * |
| 156 | **/ |
| 157 | static std::pair<std::vector<TorrentFile>, |
| 158 | std::vector<std::pair<std::vector<FileManifest>, |
| 159 | std::vector<Data>>>> |
| 160 | generate(const std::string& directoryPath, |
| 161 | size_t namesPerSegment, |
| 162 | size_t subManifestSize, |
| 163 | size_t dataPacketSize, |
| 164 | bool returnData = false); |
| 165 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 166 | protected: |
| 167 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 168 | * @brief prepend torrent file as a Content block to the encoder |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 169 | */ |
| 170 | template<encoding::Tag TAG> |
| 171 | size_t |
| 172 | encodeContent(EncodingImpl<TAG>& encoder) const; |
| 173 | |
| 174 | void |
| 175 | encodeContent(); |
| 176 | |
| 177 | void |
| 178 | decodeContent(); |
| 179 | |
| 180 | private: |
| 181 | /** |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 182 | * @brief Check whether the torrent-file has a pointer to the next segment |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 183 | */ |
| 184 | bool |
| 185 | hasTorrentFilePtr() const; |
| 186 | |
| 187 | /** |
| 188 | * @brief Create a catalog of suffixes for the names of the file manifests |
| 189 | * |
| 190 | * To optimize encoding and decoding, we encode the name of the file manifests |
| 191 | * as suffixes along with their common prefix. |
| 192 | * |
| 193 | */ |
| 194 | void |
| 195 | createSuffixCatalog(); |
| 196 | |
| 197 | /** |
| 198 | * @brief Construct the catalog of long names from a catalog of suffixes for the file |
| 199 | * manifests' name |
| 200 | * |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 201 | * After decoding a torrent-file from its wire format, we construct the catalog of |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 202 | * long names from the decoded common prefix and suffixes |
| 203 | * |
| 204 | */ |
| 205 | void |
| 206 | constructLongNames(); |
| 207 | |
| 208 | /** |
| 209 | * @brief Insert a suffix to the suffix catalog |
| 210 | */ |
| 211 | void |
| 212 | insertToSuffixCatalog(const PartialName& suffix); |
| 213 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 214 | /** |
| 215 | * @brief Set the pointer of the current torrent-file segment to the next segment |
| 216 | */ |
| 217 | void |
| 218 | setTorrentFilePtr(const Name& ptrName); |
| 219 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 220 | private: |
| 221 | Name m_commonPrefix; |
| 222 | Name m_torrentFilePtr; |
| 223 | std::vector<ndn::Name> m_suffixCatalog; |
| 224 | std::vector<ndn::Name> m_catalog; |
| 225 | }; |
| 226 | |
| 227 | inline bool |
| 228 | TorrentFile::hasTorrentFilePtr() const |
| 229 | { |
| 230 | return !m_torrentFilePtr.empty(); |
| 231 | } |
| 232 | |
| 233 | inline const std::vector<Name>& |
| 234 | TorrentFile::getCatalog() const |
| 235 | { |
| 236 | return m_catalog; |
| 237 | } |
| 238 | |
| 239 | inline size_t |
| 240 | TorrentFile::catalogSize() const |
| 241 | { |
| 242 | return m_catalog.size(); |
| 243 | } |
| 244 | |
| 245 | inline void |
| 246 | TorrentFile::insert(const Name& name) |
| 247 | { |
| 248 | m_catalog.push_back(name); |
| 249 | } |
| 250 | |
| 251 | inline void |
| 252 | TorrentFile::insertToSuffixCatalog(const PartialName& suffix) |
| 253 | { |
| 254 | m_suffixCatalog.push_back(suffix); |
| 255 | } |
| 256 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 257 | inline void |
| 258 | TorrentFile::setTorrentFilePtr(const Name& ptrName) |
| 259 | { |
| 260 | m_torrentFilePtr = ptrName; |
| 261 | } |
| 262 | |
| 263 | inline const Name& |
| 264 | TorrentFile::getName() const |
| 265 | { |
| 266 | return Data::getName(); |
| 267 | } |
| 268 | |
| 269 | inline const Name& |
| 270 | TorrentFile::getCommonPrefix() const |
| 271 | { |
| 272 | return m_commonPrefix; |
| 273 | } |
| 274 | |
| 275 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 276 | } // namespace ntorrent |
| 277 | |
| 278 | } // namespace ndn |
| 279 | |
| 280 | #endif // TORRENT_FILE_HPP |