blob: 98e222cddc9d8a85a451574b0b041ac57370e305 [file] [log] [blame]
spirosmastorakisa6057f52016-01-28 13:34:41 -08001/* -*- 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
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080025#include "file-manifest.hpp"
26
spirosmastorakisa6057f52016-01-28 13:34:41 -080027#include <ndn-cxx/name.hpp>
28#include <ndn-cxx/encoding/block.hpp>
29#include <ndn-cxx/data.hpp>
30
31#include <memory>
32
33namespace ndn {
34
35namespace ntorrent {
36
37class TorrentFile : public Data {
38public:
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 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080050 * @brief Create a new empty TorrentFile.
spirosmastorakisa6057f52016-01-28 13:34:41 -080051 */
52 TorrentFile() = default;
53
54 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080055 * @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
spirosmastorakisa6057f52016-01-28 13:34:41 -080058 * @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 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080067 * @brief Create a new TorrentFile.
68 * @param torrentFileName The name of the torrent-file
spirosmastorakisa6057f52016-01-28 13:34:41 -080069 * @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 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080077 * @brief Create a new TorrentFile
78 * @param block The block format of the torrent-file
spirosmastorakisa6057f52016-01-28 13:34:41 -080079 */
80 explicit
81 TorrentFile(const Block& block);
82
83 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080084 * @brief Get the name of the TorrentFile
spirosmastorakisa6057f52016-01-28 13:34:41 -080085 */
86 const Name&
87 getName() const;
88
89 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080090 * @brief Get the common prefix of the file manifest names of this torrent-file
spirosmastorakisa6057f52016-01-28 13:34:41 -080091 */
92 const Name&
93 getCommonPrefix() const;
94
95 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -080096 * @brief Get a shared pointer to the name of the next segment of the torrent-file.
spirosmastorakisa6057f52016-01-28 13:34:41 -080097 *
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 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800116 * @brief Finalize torrent-file before signing the data packet
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800117 *
118 * This method has to be called (every time) right before signing or encoding
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800119 * the torrent-file
spirosmastorakisa6057f52016-01-28 13:34:41 -0800120 */
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800121 void
122 finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800123
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
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800142 /**
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
spirosmastorakisa6057f52016-01-28 13:34:41 -0800166protected:
167 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800168 * @brief prepend torrent file as a Content block to the encoder
spirosmastorakisa6057f52016-01-28 13:34:41 -0800169 */
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
180private:
181 /**
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800182 * @brief Check whether the torrent-file has a pointer to the next segment
spirosmastorakisa6057f52016-01-28 13:34:41 -0800183 */
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 *
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800201 * After decoding a torrent-file from its wire format, we construct the catalog of
spirosmastorakisa6057f52016-01-28 13:34:41 -0800202 * 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
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800214 /**
215 * @brief Set the pointer of the current torrent-file segment to the next segment
216 */
217 void
218 setTorrentFilePtr(const Name& ptrName);
219
spirosmastorakisa6057f52016-01-28 13:34:41 -0800220private:
221 Name m_commonPrefix;
222 Name m_torrentFilePtr;
223 std::vector<ndn::Name> m_suffixCatalog;
224 std::vector<ndn::Name> m_catalog;
225};
226
227inline bool
228TorrentFile::hasTorrentFilePtr() const
229{
230 return !m_torrentFilePtr.empty();
231}
232
233inline const std::vector<Name>&
234TorrentFile::getCatalog() const
235{
236 return m_catalog;
237}
238
239inline size_t
240TorrentFile::catalogSize() const
241{
242 return m_catalog.size();
243}
244
245inline void
246TorrentFile::insert(const Name& name)
247{
248 m_catalog.push_back(name);
249}
250
251inline void
252TorrentFile::insertToSuffixCatalog(const PartialName& suffix)
253{
254 m_suffixCatalog.push_back(suffix);
255}
256
spirosmastorakisf5d1b6c2016-02-25 12:49:56 -0800257inline void
258TorrentFile::setTorrentFilePtr(const Name& ptrName)
259{
260 m_torrentFilePtr = ptrName;
261}
262
263inline const Name&
264TorrentFile::getName() const
265{
266 return Data::getName();
267}
268
269inline const Name&
270TorrentFile::getCommonPrefix() const
271{
272 return m_commonPrefix;
273}
274
275
spirosmastorakisa6057f52016-01-28 13:34:41 -0800276} // namespace ntorrent
277
278} // namespace ndn
279
280#endif // TORRENT_FILE_HPP