blob: 3c4b92336149a630fa14e597c48ece08259bfce5 [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
24#include "torrent-file.hpp"
25#include "file-manifest.hpp"
26
27#include <ndn-cxx/name.hpp>
28#include <ndn-cxx/data.hpp>
29
30#include <boost/filesystem/fstream.hpp>
31#include <boost/utility.hpp>
32
33#include <functional>
34#include <memory>
35#include <string>
36#include <unordered_map>
37#include <vector>
38
39namespace fs = boost::filesystem;
40
41namespace ndn {
42namespace ntorrent {
43
44class TorrentManager : boost::noncopyable {
45 /**
46 * \class TorrentManager
47 *
48 * \brief A class to manage the interaction with the system in seeding/leaching a torrent
49 */
50 public:
51 typedef std::function<void(const ndn::Name&)> DataReceivedCallback;
52 typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback;
53 typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback;
54
55 TorrentManager(const ndn::Name& torrentFileName,
56 const std::string& dataPath);
57 /*
58 * \brief Create a new Torrent manager with the specified parameters.
59 * @param torrentFileName The full name of the initial segment of the torrent file
60 * @param filePath The path to the location on disk to use for the torrent data
61 *
62 * The behavior is undefined unless Initialize() is called before calling any other method on a
63 * TorrentManger object.
64 */
65
66 void Initialize();
67 /*
68 * \brief Initialize the state of this object.
69 * Read and validate from disk all torrent file segments, file manifests, and data packets for
70 * the torrent file managed by this object initializing all state in this manager respectively.
71 * Also seeds all validated data.
72 */
73
74 std::vector<Name> downloadTorrentFile(const std::string& path = "");
75 // Request from the network the all segments of the 'torrentFileName' and write onto local disk at
76 // the specified 'path'.
77
78 void download_file_manifest(const Name& manifestName,
79 const std::string& path,
80 ManifestReceivedCallback onSuccess,
81 FailedCallback onFailed) const;
82 // Request from the network the all segments of the 'manifestName' and write onto local disk at
83 // the specified 'path'.
84
85 void download_data_packet(const Name& packetName,
86 DataReceivedCallback onSuccess,
87 FailedCallback onFailed) const;
88 // Request from the network the Data with the specified 'packetName' and write onto local disk at
89 // 'm_dataPath'.
90
91 void seed(const Data& data) const;
92 // Seed the specified 'data' to the network.
93
94 protected:
95 void onDataReceived(const Data& data);
96
97 void onInterestReceived(const Name& name);
98
99 // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data
100 // packets this manager currently has
101 mutable std::unordered_map<Name,
102 std::pair<std::shared_ptr<fs::fstream>,
103 std::vector<bool>>> m_fileStates;
104 // The segments of the TorrentFile this manager has
105 std::vector<TorrentFile> m_torrentSegments;
106 // The FileManifests this manager has
107 std::vector<FileManifest> m_fileManifests;
108 // The name of the initial segment of the torrent file for this manager
109 Name m_torrentFileName;
110 // The path to the location on disk of the Data packet for this manager
111 std::string m_dataPath;
112};
113
114inline
115TorrentManager::TorrentManager(const ndn::Name& torrentFileName,
116 const std::string& dataPath)
117: m_fileStates()
118, m_torrentSegments()
119, m_fileManifests()
120, m_torrentFileName(torrentFileName)
121, m_dataPath(dataPath)
122{
123}
124
125} // end ntorrent
126} // end ndn
127
128#endif // INCLUDED_TORRENT_FILE_MANAGER_H