blob: 4a8beb41994b9c6ddcd2e615706b822b75d54ca7 [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
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700150 /**
151 * @brief Process any data to receive or call timeout callbacks and update prefix list (if needed)
152 * By default only process pending events and return immediately, optionally specify a timeout.
153 */
154 void
155 processEvents(const time::milliseconds& timeout = time::milliseconds(-1));
156
Mickey Sweatt527b0492016-03-02 11:07:48 -0800157 protected:
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700158 /**
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700159 * \brief Write @p packet composed of torrent date to disk.
160 * @param packet The data packet to be written to the disk
161 * Write the Data packet to disk, return 'true' if data successfully written to disk 'false'
162 * otherwise. Behavior is undefined unless the corresponding file manifest has already been
163 * downloaded.
spirosmastorakisa46eee42016-04-05 14:24:45 -0700164 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700165 bool
166 writeData(const Data& packet);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700167
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700168 /*
169 * \brief Write the @p segment torrent segment to disk at the specified path.
170 * @param segment The torrent file segment to be written to disk
171 * @param path The path at which to write the torrent file segment
172 * Write the segment to disk, return 'true' if data successfully written to disk 'false'
173 * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of
174 * this manager and @p path is the directory used for all segments of this torrent file.
175 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700176 bool
177 writeTorrentSegment(const TorrentFile& segment, const std::string& path);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700178
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700179 /*
180 * \brief Write the @p manifest file manifest to disk at the specified @p path.
181 * @param manifest The file manifest to be written to disk
182 * @param path The path at which to write the file manifest
183 * Write the file manifest to disk, return 'true' if data successfully written to disk 'false'
184 * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the
185 * torrent file of this manager and @p path is the directory used for all file manifests of this
186 * torrent file.
187 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700188 bool
189 writeFileManifest(const FileManifest& manifest, const std::string& path);
Mickey Sweattafda1f12016-04-04 17:15:11 -0700190
spirosmastorakisa46eee42016-04-05 14:24:45 -0700191 /*
192 * \brief Download the segments of the torrent file
193 * @param name The name of the torrent file to be downloaded
194 * @param path The path to write the torrent file on disk
195 * @param manifestNames A vector containing the name of the manifests in the torrent file.
196 * This parameter will be updated every time we receive a torrent
197 * file segment
198 * @param async Blocking (sync) or non-blocking (async) operation
199 * @param onSuccess Optional callback to be called when all the segments of the torrent file
200 * have been downloaded. The default value is an empty callack. A callback
201 * should be specified when async is false
202 * @param onFailed Optional callback to be called when we fail to download a segment of the
203 * torrent file. The default value is an empty callack. A callback should be
204 * specified when async is false
205 *
206 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700207 void
208 downloadTorrentFileSegment(const ndn::Name& name,
209 const std::string& path,
210 std::shared_ptr<std::vector<Name>> manifestNames,
211 bool async,
212 TorrentFileReceivedCallback onSuccess,
213 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800214
spirosmastorakisa46eee42016-04-05 14:24:45 -0700215 /*
216 * \brief Download the segments of a file manifest
217 * @param manifestName The name of the file manifest to be downloaded
218 * @param path The path to write the file manifest on disk
219 * @param packetNames A vector containing the name of the data packets in the file manifest
220 * @param onSuccess Callback to be called when all the segments of the file manifest have been
221 * downloaded
222 * @param onFailed Callback to be called when we fail to download a file manifest segment
223 *
224 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700225 void
226 downloadFileManifestSegment(const Name& manifestName,
227 const std::string& path,
228 std::shared_ptr<std::vector<Name>> packetNames,
229 ManifestReceivedCallback onSuccess,
230 FailedCallback onFailed);
spirosmastorakisa46eee42016-04-05 14:24:45 -0700231
232 enum {
233 // Number of times to retry if a routable prefix fails to retrieve data
234 MAX_NUM_OF_RETRIES = 5,
235 // Number of Interests to be sent before sorting the stats table
236 SORTING_INTERVAL = 100
237 };
Mickey Sweatt527b0492016-03-02 11:07:48 -0800238
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700239 void onDataReceived(const Data& data);
240
241 void
242 onInterestReceived(const InterestFilter& filter, const Interest& interest);
243
244 void
245 onRegisterFailed(const Name& prefix, const std::string& reason);
246
Mickey Sweatt527b0492016-03-02 11:07:48 -0800247 // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data
248 // packets this manager currently has
249 mutable std::unordered_map<Name,
250 std::pair<std::shared_ptr<fs::fstream>,
251 std::vector<bool>>> m_fileStates;
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700252 // A map for each initial manifest to the size for the sub-manifest
253 std::unordered_map<std::string, size_t> m_subManifestSizes;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800254 // The segments of the TorrentFile this manager has
255 std::vector<TorrentFile> m_torrentSegments;
256 // The FileManifests this manager has
257 std::vector<FileManifest> m_fileManifests;
258 // The name of the initial segment of the torrent file for this manager
259 Name m_torrentFileName;
260 // The path to the location on disk of the Data packet for this manager
261 std::string m_dataPath;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700262
spirosmastorakis50642f82016-04-08 12:11:18 -0700263protected:
264 /*
265 * \brief Find the torrent file segment that we should download
266 * (either we have nothing or we have them all)
267 * @return A shared_ptr to the name of the segment to download or
268 * nullptr if we have all the segments
269 *
270 */
271 shared_ptr<Name>
272 findTorrentFileSegmentToDownload();
273
274 /*
275 * \brief Given a file manifest segment name, find the next file manifest segment
276 * that we should download
277 * @param manifestName The name of the file manifest segment that we want to download
278 * @return A shared_ptr to the name of the segment to download or
279 * nullptr if we have all the segments
280 *
281 */
282 shared_ptr<Name>
283 findManifestSegmentToDownload(const Name& manifestName);
284
285 /*
286 * \brief Given a data packet name, find whether we have already downloaded this packet
287 * @param dataName The name of the data packet to download
288 * @return True if we have already downloaded this packet, false otherwise
289 *
290 */
291 bool
292 dataAlreadyDownloaded(const Name& dataName);
293
294 /*
295 * \brief Find the segments of all the file manifests that we are missing
296 * @param manifestNames The name of the manifest file segments to download (currently missing)
297 * This parameter is used as an output vector of names
298 */
299 void
300 findFileManifestsToDownload(std::vector<Name>& manifestNames);
301
302 /*
303 * \brief Find the names of the data packets of a file manifest that we are currently missing
304 * @param manifestName The name of the manifest
305 * @param packetNames The name of the data packets to be downloaded
306 * (used as an output vector of names)
307 *
308 * No matter what segment of a manifest file the manifestName parameter might refer to, the
309 * missing data packets starting from the first segment of this manifest file would be returned
310 *
311 */
312 void
313 findDataPacketsToDownload(const Name& manifestName, std::vector<Name>& packetNames);
314
315 /*
316 * \brief Find all the data packets that we are currently missing
317 * @param packetNames The name of the data packets to be downloaded
318 * (used as an output vector of names)
319 *
320 */
321 void
322 findAllMissingDataPackets(std::vector<Name>& packetNames);
323
spirosmastorakisa46eee42016-04-05 14:24:45 -0700324private:
325 shared_ptr<Interest>
326 createInterest(Name name);
327
328 // Stats table where routable prefixes are stored
329 StatsTable m_statsTable;
330 // Face used for network communication
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700331 std::shared_ptr<Face> m_face;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700332 // Iterator to the routable prefix that we currently use
333 StatsTable::iterator m_stats_table_iter;
334 // Number of retries per routable prefix
335 uint64_t m_retries;
336 // Number of Interests sent since last sorting
337 uint64_t m_sortingCounter;
338 // Keychain instance
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700339 shared_ptr<KeyChain> m_keyChain;
340 // Update Handler instance
341 shared_ptr<UpdateHandler> m_updateHandler;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800342};
343
344inline
spirosmastorakisa46eee42016-04-05 14:24:45 -0700345TorrentManager::TorrentManager(const ndn::Name& torrentFileName,
346 const std::string& dataPath,
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700347 std::shared_ptr<Face> face)
Mickey Sweatt527b0492016-03-02 11:07:48 -0800348: m_fileStates()
349, m_torrentSegments()
350, m_fileManifests()
351, m_torrentFileName(torrentFileName)
352, m_dataPath(dataPath)
spirosmastorakisa46eee42016-04-05 14:24:45 -0700353, m_face(face)
354, m_retries(0)
355, m_sortingCounter(0)
356, m_keyChain(new KeyChain())
Mickey Sweatt527b0492016-03-02 11:07:48 -0800357{
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700358 if(face == nullptr) {
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700359 m_face = make_shared<Face>();
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700360 }
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700361
spirosmastorakisa46eee42016-04-05 14:24:45 -0700362 // Hardcoded prefixes for now
363 // TODO(Spyros): Think of something more clever to bootstrap...
364 m_statsTable.insert("/ucla");
365 m_statsTable.insert("/arizona");
366 m_stats_table_iter = m_statsTable.begin();
Mickey Sweatt527b0492016-03-02 11:07:48 -0800367}
368
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700369inline
370void
371TorrentManager::processEvents(const time::milliseconds& timeout)
372{
373 m_face->processEvents(timeout);
374}
375
Mickey Sweatt527b0492016-03-02 11:07:48 -0800376} // end ntorrent
377} // end ndn
378
spirosmastorakisa46eee42016-04-05 14:24:45 -0700379#endif // INCLUDED_TORRENT_FILE_MANAGER_H