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 | |
| 25 | #include <ndn-cxx/name.hpp> |
| 26 | #include <ndn-cxx/encoding/block.hpp> |
| 27 | #include <ndn-cxx/data.hpp> |
| 28 | |
| 29 | #include <memory> |
| 30 | |
| 31 | namespace ndn { |
| 32 | |
| 33 | namespace ntorrent { |
| 34 | |
| 35 | class TorrentFile : public Data { |
| 36 | public: |
| 37 | class Error : public Data::Error |
| 38 | { |
| 39 | public: |
| 40 | explicit |
| 41 | Error(const std::string& what) |
| 42 | : Data::Error(what) |
| 43 | { |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * @brief Create a new empty .TorrentFile. |
| 49 | */ |
| 50 | TorrentFile() = default; |
| 51 | |
| 52 | /** |
| 53 | * @brief Create a new .TorrentFile. |
| 54 | * @param torrentFileName The name of the .torrent-file |
| 55 | * @param torrentFilePtr A pointer (name) to the next segment of the .torrent-file |
| 56 | * @param commonPrefix The common name prefix of the manifest file names included in the catalog |
| 57 | * @param catalog The catalog containing the name of each file manifest |
| 58 | */ |
| 59 | TorrentFile(const Name& torrentFileName, |
| 60 | const Name& torrentFilePtr, |
| 61 | const Name& commonPrefix, |
| 62 | const std::vector<ndn::Name>& catalog); |
| 63 | |
| 64 | /** |
| 65 | * @brief Create a new .TorrentFile. |
| 66 | * @param torrentFileName The name of the .torrent-file |
| 67 | * @param commonPrefix The common name prefix of the manifest file names included in the catalog |
| 68 | * @param catalog The catalog containing the name of each file manifest |
| 69 | */ |
| 70 | TorrentFile(const Name& torrentFileName, |
| 71 | const Name& commonPrefix, |
| 72 | const std::vector<ndn::Name>& catalog); |
| 73 | |
| 74 | /** |
| 75 | * @brief Create a new .TorrentFile |
| 76 | * @param block The block format of the .torrent-file |
| 77 | */ |
| 78 | explicit |
| 79 | TorrentFile(const Block& block); |
| 80 | |
| 81 | /** |
| 82 | * @brief Get the name of the .TorrentFile |
| 83 | */ |
| 84 | const Name& |
| 85 | getName() const; |
| 86 | |
| 87 | /** |
| 88 | * @brief Get the common prefix of the file manifest names of this .torrent-file |
| 89 | */ |
| 90 | const Name& |
| 91 | getCommonPrefix() const; |
| 92 | |
| 93 | /** |
| 94 | * @brief Get a shared pointer to the name of the next segment of the .torrent-file. |
| 95 | * |
| 96 | * If there is no next segment, it returns a nullptr |
| 97 | */ |
| 98 | shared_ptr<Name> |
| 99 | getTorrentFilePtr() const; |
| 100 | |
| 101 | /** |
| 102 | * @brief Get the catalog of names of the file manifests |
| 103 | */ |
| 104 | const std::vector<Name>& |
| 105 | getCatalog() const; |
| 106 | |
| 107 | /** |
| 108 | * @brief Decode from wire format |
| 109 | */ |
| 110 | void |
| 111 | wireDecode(const Block& wire); |
| 112 | |
| 113 | /** |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame^] | 114 | * @brief Finalize .torrent-file before signing the data packet |
| 115 | * |
| 116 | * This method has to be called (every time) right before signing or encoding |
| 117 | * the .torrent-file |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 118 | */ |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame^] | 119 | void |
| 120 | finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Insert a name to the catalog of file manifest names |
| 124 | */ |
| 125 | void |
| 126 | insert(const Name& name); |
| 127 | |
| 128 | /** |
| 129 | * @brief Erase a name from the catalog of file manifest names |
| 130 | */ |
| 131 | bool |
| 132 | erase(const Name& name); |
| 133 | |
| 134 | /** |
| 135 | * @brief Get the size of the catalog of file manifest names |
| 136 | */ |
| 137 | size_t |
| 138 | catalogSize() const; |
| 139 | |
| 140 | protected: |
| 141 | /** |
| 142 | * @brief prepend .torrent file as a Content block to the encoder |
| 143 | */ |
| 144 | template<encoding::Tag TAG> |
| 145 | size_t |
| 146 | encodeContent(EncodingImpl<TAG>& encoder) const; |
| 147 | |
| 148 | void |
| 149 | encodeContent(); |
| 150 | |
| 151 | void |
| 152 | decodeContent(); |
| 153 | |
| 154 | private: |
| 155 | /** |
| 156 | * @brief Check whether the .torrent-file has a pointer to the next segment |
| 157 | */ |
| 158 | bool |
| 159 | hasTorrentFilePtr() const; |
| 160 | |
| 161 | /** |
| 162 | * @brief Create a catalog of suffixes for the names of the file manifests |
| 163 | * |
| 164 | * To optimize encoding and decoding, we encode the name of the file manifests |
| 165 | * as suffixes along with their common prefix. |
| 166 | * |
| 167 | */ |
| 168 | void |
| 169 | createSuffixCatalog(); |
| 170 | |
| 171 | /** |
| 172 | * @brief Construct the catalog of long names from a catalog of suffixes for the file |
| 173 | * manifests' name |
| 174 | * |
| 175 | * After decoding a .torrent-file from its wire format, we construct the catalog of |
| 176 | * long names from the decoded common prefix and suffixes |
| 177 | * |
| 178 | */ |
| 179 | void |
| 180 | constructLongNames(); |
| 181 | |
| 182 | /** |
| 183 | * @brief Insert a suffix to the suffix catalog |
| 184 | */ |
| 185 | void |
| 186 | insertToSuffixCatalog(const PartialName& suffix); |
| 187 | |
| 188 | private: |
| 189 | Name m_commonPrefix; |
| 190 | Name m_torrentFilePtr; |
| 191 | std::vector<ndn::Name> m_suffixCatalog; |
| 192 | std::vector<ndn::Name> m_catalog; |
| 193 | }; |
| 194 | |
| 195 | inline bool |
| 196 | TorrentFile::hasTorrentFilePtr() const |
| 197 | { |
| 198 | return !m_torrentFilePtr.empty(); |
| 199 | } |
| 200 | |
| 201 | inline const std::vector<Name>& |
| 202 | TorrentFile::getCatalog() const |
| 203 | { |
| 204 | return m_catalog; |
| 205 | } |
| 206 | |
| 207 | inline size_t |
| 208 | TorrentFile::catalogSize() const |
| 209 | { |
| 210 | return m_catalog.size(); |
| 211 | } |
| 212 | |
| 213 | inline void |
| 214 | TorrentFile::insert(const Name& name) |
| 215 | { |
| 216 | m_catalog.push_back(name); |
| 217 | } |
| 218 | |
| 219 | inline void |
| 220 | TorrentFile::insertToSuffixCatalog(const PartialName& suffix) |
| 221 | { |
| 222 | m_suffixCatalog.push_back(suffix); |
| 223 | } |
| 224 | |
| 225 | } // namespace ntorrent |
| 226 | |
| 227 | } // namespace ndn |
| 228 | |
| 229 | #endif // TORRENT_FILE_HPP |