blob: 17e59db43c440b69ac42313ff65258489d255c32 [file] [log] [blame]
Mickey Sweatt527b0492016-03-02 11:07:48 -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#ifndef INCLUDED_TORRENT_FILE_MANAGER_H
22#define INCLUDED_TORRENT_FILE_MANAGER_H
23
Mickey Sweatt527b0492016-03-02 11:07:48 -080024#include "file-manifest.hpp"
spirosmastorakisa46eee42016-04-05 14:24:45 -070025#include "torrent-file.hpp"
spirosmastorakis4ff8c872016-04-14 09:51:38 -070026#include "update-handler.hpp"
Mickey Sweatt527b0492016-03-02 11:07:48 -080027
Mickey Sweatt527b0492016-03-02 11:07:48 -080028#include <ndn-cxx/data.hpp>
spirosmastorakisa46eee42016-04-05 14:24:45 -070029#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/interest.hpp>
31#include <ndn-cxx/link.hpp>
32#include <ndn-cxx/name.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
Mickey Sweatt527b0492016-03-02 11:07:48 -080034
35#include <boost/filesystem/fstream.hpp>
Mickey Sweatt527b0492016-03-02 11:07:48 -080036
37#include <functional>
38#include <memory>
39#include <string>
40#include <unordered_map>
41#include <vector>
42
43namespace fs = boost::filesystem;
44
45namespace ndn {
46namespace ntorrent {
47
Mickey Sweatt599bfef2016-04-05 19:11:20 -070048class TorrentManager : noncopyable {
Mickey Sweatt527b0492016-03-02 11:07:48 -080049 /**
50 * \class TorrentManager
51 *
52 * \brief A class to manage the interaction with the system in seeding/leaching a torrent
53 */
54 public:
spirosmastorakisa46eee42016-04-05 14:24:45 -070055 typedef std::function<void(const ndn::Name&)> DataReceivedCallback;
56 typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback;
spirosmastorakis50642f82016-04-08 12:11:18 -070057 typedef std::function<void(const std::vector<ndn::Name>&)> TorrentFileReceivedCallback;
spirosmastorakisa46eee42016-04-05 14:24:45 -070058 typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback;
Mickey Sweatt527b0492016-03-02 11:07:48 -080059
spirosmastorakisa46eee42016-04-05 14:24:45 -070060 /*
61 * \brief Create a new Torrent manager with the specified parameters.
62 * @param torrentFileName The full name of the initial segment of the torrent file
63 * @param dataPath The path to the location on disk to use for the torrent data
64 * @param face Optional face object to be used for data retrieval
65 *
66 * The behavior is undefined unless Initialize() is called before calling any other method on a
67 * TorrentManger object.
68 */
Mickey Sweatte908a5c2016-04-08 14:10:45 -070069 TorrentManager(const ndn::Name& torrentFileName,
70 const std::string& dataPath,
71 std::shared_ptr<Face> face = nullptr);
spirosmastorakisa46eee42016-04-05 14:24:45 -070072
Mickey Sweatt527b0492016-03-02 11:07:48 -080073 /*
spirosmastorakisa46eee42016-04-05 14:24:45 -070074 * @brief Initialize the state of this object.
Mickey Sweatt527b0492016-03-02 11:07:48 -080075 *
Mickey Sweatt527b0492016-03-02 11:07:48 -080076 * Read and validate from disk all torrent file segments, file manifests, and data packets for
77 * the torrent file managed by this object initializing all state in this manager respectively.
78 * Also seeds all validated data.
spirosmastorakisa46eee42016-04-05 14:24:45 -070079 */
80 void
81 Initialize();
82
83 /*
84 * @brief Download the torrent file
85 * @param path The path to write the downloaded segments
86 * @return A vector of the file manifest names contained in the torrent file
87 *
Mickey Sweatt527b0492016-03-02 11:07:48 -080088 */
spirosmastorakisa46eee42016-04-05 14:24:45 -070089 std::vector<Name>
90 downloadTorrentFile(const std::string& path);
Mickey Sweatt527b0492016-03-02 11:07:48 -080091
spirosmastorakisa46eee42016-04-05 14:24:45 -070092 /*
93 * @brief Download the torrent file
94 * @param path The path to write the downloaded segments
95 * @param onSuccess Callback to be called if we successfully download all the
96 * segments of the torrent file. It passes the name of the file manifest
97 * to be downloaded to the callback
98 * @param onFailed Callaback to be called if we fail to download a segment of
99 * the torrent file. It passes the name of the torrent file segment that
100 * failed to download and a failure reason to the callback
101 *
102 * This method provides non-blocking downloading of all the torrent file segments
103 *
104 */
105 void
106 downloadTorrentFile(const std::string& path,
spirosmastorakis50642f82016-04-08 12:11:18 -0700107 TorrentFileReceivedCallback onSuccess,
spirosmastorakisa46eee42016-04-05 14:24:45 -0700108 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800109
spirosmastorakisa46eee42016-04-05 14:24:45 -0700110 /*
111 * @brief Download a file manifest
112 * @param manifestName The name of the manifest file to be downloaded
113 * @param path The path to write the downloaded segments
114 * @param onSuccess Callback to be called if we successfully download all the
115 * segments of the file manifest. It passes the names of the data packets
116 * to be downloaded to the callback
117 * @param onFailed Callaback to be called if we fail to download a segment of
118 * the file manifest. It passes the name of the data packet that failed
119 * to download and a failure reason
120 *
121 * This method provides non-blocking downloading of all the file manifest segments
122 *
123 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700124 void
125 download_file_manifest(const Name& manifestName,
126 const std::string& path,
127 ManifestReceivedCallback onSuccess,
128 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800129
spirosmastorakisa46eee42016-04-05 14:24:45 -0700130 /*
131 * @brief Download a data packet
132 * @param packetName The name of the data packet to be downloaded
133 * @param onSuccess Callback to be called if we successfully download the data packet.
134 * It passes the name of the data packet to the callback
135 * @param onFailed Callaback to be called if we fail to download the requested data packet
136 * It passes the name of the data packet to the callback and a failure reason
137 *
138 * This method writes the downloaded data packet to m_dataPath on disk
139 *
140 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700141 void
142 download_data_packet(const Name& packetName,
143 DataReceivedCallback onSuccess,
144 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800145
Mickey Sweatt527b0492016-03-02 11:07:48 -0800146 // Seed the specified 'data' to the network.
spirosmastorakis50642f82016-04-08 12:11:18 -0700147 void
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700148 seed(const Data& data);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800149
150 protected:
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700151 /*
152 * \brief Write @p packet composed of torrent date to disk.
153 * @param packet The data packet to be written to the disk
154 * Write the Data packet to disk, return 'true' if data successfully written to disk 'false'
155 * otherwise. Behavior is undefined unless the corresponding file manifest has already been
156 * downloaded.
spirosmastorakisa46eee42016-04-05 14:24:45 -0700157 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700158 bool
159 writeData(const Data& packet);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700160
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700161 /*
162 * \brief Write the @p segment torrent segment to disk at the specified path.
163 * @param segment The torrent file segment to be written to disk
164 * @param path The path at which to write the torrent file segment
165 * Write the segment to disk, return 'true' if data successfully written to disk 'false'
166 * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of
167 * this manager and @p path is the directory used for all segments of this torrent file.
168 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700169 bool
170 writeTorrentSegment(const TorrentFile& segment, const std::string& path);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700171
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700172 /*
173 * \brief Write the @p manifest file manifest to disk at the specified @p path.
174 * @param manifest The file manifest to be written to disk
175 * @param path The path at which to write the file manifest
176 * Write the file manifest to disk, return 'true' if data successfully written to disk 'false'
177 * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the
178 * torrent file of this manager and @p path is the directory used for all file manifests of this
179 * torrent file.
180 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700181 bool
182 writeFileManifest(const FileManifest& manifest, const std::string& path);
Mickey Sweattafda1f12016-04-04 17:15:11 -0700183
spirosmastorakisa46eee42016-04-05 14:24:45 -0700184 /*
185 * \brief Download the segments of the torrent file
186 * @param name The name of the torrent file to be downloaded
187 * @param path The path to write the torrent file on disk
188 * @param manifestNames A vector containing the name of the manifests in the torrent file.
189 * This parameter will be updated every time we receive a torrent
190 * file segment
191 * @param async Blocking (sync) or non-blocking (async) operation
192 * @param onSuccess Optional callback to be called when all the segments of the torrent file
193 * have been downloaded. The default value is an empty callack. A callback
194 * should be specified when async is false
195 * @param onFailed Optional callback to be called when we fail to download a segment of the
196 * torrent file. The default value is an empty callack. A callback should be
197 * specified when async is false
198 *
199 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700200 void
201 downloadTorrentFileSegment(const ndn::Name& name,
202 const std::string& path,
203 std::shared_ptr<std::vector<Name>> manifestNames,
204 bool async,
205 TorrentFileReceivedCallback onSuccess,
206 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800207
spirosmastorakisa46eee42016-04-05 14:24:45 -0700208 /*
209 * \brief Download the segments of a file manifest
210 * @param manifestName The name of the file manifest to be downloaded
211 * @param path The path to write the file manifest on disk
212 * @param packetNames A vector containing the name of the data packets in the file manifest
213 * @param onSuccess Callback to be called when all the segments of the file manifest have been
214 * downloaded
215 * @param onFailed Callback to be called when we fail to download a file manifest segment
216 *
217 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700218 void
219 downloadFileManifestSegment(const Name& manifestName,
220 const std::string& path,
221 std::shared_ptr<std::vector<Name>> packetNames,
222 ManifestReceivedCallback onSuccess,
223 FailedCallback onFailed);
spirosmastorakisa46eee42016-04-05 14:24:45 -0700224
225 enum {
226 // Number of times to retry if a routable prefix fails to retrieve data
227 MAX_NUM_OF_RETRIES = 5,
228 // Number of Interests to be sent before sorting the stats table
229 SORTING_INTERVAL = 100
230 };
Mickey Sweatt527b0492016-03-02 11:07:48 -0800231
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700232 void onDataReceived(const Data& data);
233
234 void
235 onInterestReceived(const InterestFilter& filter, const Interest& interest);
236
237 void
238 onRegisterFailed(const Name& prefix, const std::string& reason);
239
Mickey Sweatt527b0492016-03-02 11:07:48 -0800240 // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data
241 // packets this manager currently has
242 mutable std::unordered_map<Name,
243 std::pair<std::shared_ptr<fs::fstream>,
244 std::vector<bool>>> m_fileStates;
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700245 // A map for each initial manifest to the size for the sub-manifest
246 std::unordered_map<std::string, size_t> m_subManifestSizes;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800247 // The segments of the TorrentFile this manager has
248 std::vector<TorrentFile> m_torrentSegments;
249 // The FileManifests this manager has
250 std::vector<FileManifest> m_fileManifests;
251 // The name of the initial segment of the torrent file for this manager
252 Name m_torrentFileName;
253 // The path to the location on disk of the Data packet for this manager
254 std::string m_dataPath;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700255
spirosmastorakis50642f82016-04-08 12:11:18 -0700256protected:
257 /*
258 * \brief Find the torrent file segment that we should download
259 * (either we have nothing or we have them all)
260 * @return A shared_ptr to the name of the segment to download or
261 * nullptr if we have all the segments
262 *
263 */
264 shared_ptr<Name>
265 findTorrentFileSegmentToDownload();
266
267 /*
268 * \brief Given a file manifest segment name, find the next file manifest segment
269 * that we should download
270 * @param manifestName The name of the file manifest segment that we want to download
271 * @return A shared_ptr to the name of the segment to download or
272 * nullptr if we have all the segments
273 *
274 */
275 shared_ptr<Name>
276 findManifestSegmentToDownload(const Name& manifestName);
277
278 /*
279 * \brief Given a data packet name, find whether we have already downloaded this packet
280 * @param dataName The name of the data packet to download
281 * @return True if we have already downloaded this packet, false otherwise
282 *
283 */
284 bool
285 dataAlreadyDownloaded(const Name& dataName);
286
287 /*
288 * \brief Find the segments of all the file manifests that we are missing
289 * @param manifestNames The name of the manifest file segments to download (currently missing)
290 * This parameter is used as an output vector of names
291 */
292 void
293 findFileManifestsToDownload(std::vector<Name>& manifestNames);
294
295 /*
296 * \brief Find the names of the data packets of a file manifest that we are currently missing
297 * @param manifestName The name of the manifest
298 * @param packetNames The name of the data packets to be downloaded
299 * (used as an output vector of names)
300 *
301 * No matter what segment of a manifest file the manifestName parameter might refer to, the
302 * missing data packets starting from the first segment of this manifest file would be returned
303 *
304 */
305 void
306 findDataPacketsToDownload(const Name& manifestName, std::vector<Name>& packetNames);
307
308 /*
309 * \brief Find all the data packets that we are currently missing
310 * @param packetNames The name of the data packets to be downloaded
311 * (used as an output vector of names)
312 *
313 */
314 void
315 findAllMissingDataPackets(std::vector<Name>& packetNames);
316
spirosmastorakisa46eee42016-04-05 14:24:45 -0700317private:
318 shared_ptr<Interest>
319 createInterest(Name name);
320
321 // Stats table where routable prefixes are stored
322 StatsTable m_statsTable;
323 // Face used for network communication
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700324 std::shared_ptr<Face> m_face;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700325 // Iterator to the routable prefix that we currently use
326 StatsTable::iterator m_stats_table_iter;
327 // Number of retries per routable prefix
328 uint64_t m_retries;
329 // Number of Interests sent since last sorting
330 uint64_t m_sortingCounter;
331 // Keychain instance
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700332 shared_ptr<KeyChain> m_keyChain;
333 // Update Handler instance
334 shared_ptr<UpdateHandler> m_updateHandler;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800335};
336
337inline
spirosmastorakisa46eee42016-04-05 14:24:45 -0700338TorrentManager::TorrentManager(const ndn::Name& torrentFileName,
339 const std::string& dataPath,
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700340 std::shared_ptr<Face> face)
Mickey Sweatt527b0492016-03-02 11:07:48 -0800341: m_fileStates()
342, m_torrentSegments()
343, m_fileManifests()
344, m_torrentFileName(torrentFileName)
345, m_dataPath(dataPath)
spirosmastorakisa46eee42016-04-05 14:24:45 -0700346, m_face(face)
347, m_retries(0)
348, m_sortingCounter(0)
349, m_keyChain(new KeyChain())
Mickey Sweatt527b0492016-03-02 11:07:48 -0800350{
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700351 if(face == nullptr) {
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700352 m_face = make_shared<Face>();
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700353 }
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700354
spirosmastorakisa46eee42016-04-05 14:24:45 -0700355 // Hardcoded prefixes for now
356 // TODO(Spyros): Think of something more clever to bootstrap...
357 m_statsTable.insert("/ucla");
358 m_statsTable.insert("/arizona");
359 m_stats_table_iter = m_statsTable.begin();
Mickey Sweatt527b0492016-03-02 11:07:48 -0800360}
361
362} // end ntorrent
363} // end ndn
364
spirosmastorakisa46eee42016-04-05 14:24:45 -0700365#endif // INCLUDED_TORRENT_FILE_MANAGER_H