Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -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 | #ifndef INCLUDED_TORRENT_FILE_MANAGER_H |
| 22 | #define INCLUDED_TORRENT_FILE_MANAGER_H |
| 23 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 24 | #include "file-manifest.hpp" |
spirosmastorakis | 9b68b53 | 2016-05-02 21:40:29 -0700 | [diff] [blame^] | 25 | #include "interest-queue.hpp" |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 26 | #include "torrent-file.hpp" |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 27 | #include "update-handler.hpp" |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 28 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 29 | #include <ndn-cxx/data.hpp> |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/face.hpp> |
| 31 | #include <ndn-cxx/interest.hpp> |
| 32 | #include <ndn-cxx/link.hpp> |
| 33 | #include <ndn-cxx/name.hpp> |
| 34 | #include <ndn-cxx/security/key-chain.hpp> |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 35 | |
| 36 | #include <boost/filesystem/fstream.hpp> |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 37 | |
| 38 | #include <functional> |
| 39 | #include <memory> |
| 40 | #include <string> |
| 41 | #include <unordered_map> |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 42 | #include <unordered_set> |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 43 | #include <vector> |
| 44 | |
| 45 | namespace fs = boost::filesystem; |
| 46 | |
| 47 | namespace ndn { |
| 48 | namespace ntorrent { |
| 49 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 50 | class TorrentManager : noncopyable { |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 51 | /** |
| 52 | * \class TorrentManager |
| 53 | * |
| 54 | * \brief A class to manage the interaction with the system in seeding/leaching a torrent |
| 55 | */ |
| 56 | public: |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 57 | typedef std::function<void(const ndn::Name&)> DataReceivedCallback; |
| 58 | typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback; |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 59 | typedef std::function<void(const std::vector<ndn::Name>&)> TorrentFileReceivedCallback; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 60 | typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 61 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 62 | /* |
| 63 | * \brief Create a new Torrent manager with the specified parameters. |
| 64 | * @param torrentFileName The full name of the initial segment of the torrent file |
| 65 | * @param dataPath The path to the location on disk to use for the torrent data |
| 66 | * @param face Optional face object to be used for data retrieval |
| 67 | * |
| 68 | * The behavior is undefined unless Initialize() is called before calling any other method on a |
| 69 | * TorrentManger object. |
| 70 | */ |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 71 | TorrentManager(const ndn::Name& torrentFileName, |
| 72 | const std::string& dataPath, |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 73 | bool seed = true, |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 74 | std::shared_ptr<Face> face = nullptr); |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 75 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 76 | /* |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 77 | * @brief Initialize the state of this object. |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 78 | * |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 79 | * Read and validate from disk all torrent file segments, file manifests, and data packets for |
| 80 | * the torrent file managed by this object initializing all state in this manager respectively. |
| 81 | * Also seeds all validated data. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 82 | */ |
| 83 | void |
| 84 | Initialize(); |
| 85 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 86 | /** |
| 87 | * brief Return 'true' if all segments of the torrent file downloaded, 'false' otherwise. |
| 88 | */ |
| 89 | bool |
| 90 | hasAllTorrentSegments() const; |
| 91 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 92 | /* |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 93 | * \brief Given a data packet name, find whether we have already downloaded this packet |
| 94 | * @param dataName The name of the data packet to download |
| 95 | * @return True if we have already downloaded this packet, false otherwise |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 96 | * |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 97 | */ |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 98 | bool |
| 99 | hasDataPacket(const Name& dataName) const; |
| 100 | |
| 101 | /* |
| 102 | * \brief Find the torrent file segment that we should download |
| 103 | * (either we have nothing or we have them all) |
| 104 | * @return A shared_ptr to the name of the segment to download or |
| 105 | * nullptr if we have all the segments |
| 106 | * |
| 107 | */ |
| 108 | shared_ptr<Name> |
| 109 | findTorrentFileSegmentToDownload() const; |
| 110 | |
| 111 | /* |
| 112 | * \brief Given a file manifest segment name, find the next file manifest segment |
| 113 | * that we should download |
| 114 | * @param manifestName The name of the file manifest segment that we want to download |
| 115 | * @return A shared_ptr to the name of the segment to download or |
| 116 | * nullptr if we have all the segments |
| 117 | * |
| 118 | */ |
| 119 | shared_ptr<Name> |
| 120 | findManifestSegmentToDownload(const Name& manifestName) const; |
| 121 | |
| 122 | /* |
| 123 | * \brief Find the segments of all the file manifests that we are missing |
| 124 | * @param manifestNames The name of the manifest file segments to download (currently missing) |
| 125 | * This parameter is used as an output vector of names |
| 126 | */ |
| 127 | void |
| 128 | findFileManifestsToDownload(std::vector<Name>& manifestNames) const; |
| 129 | |
| 130 | /* |
| 131 | * \brief Find the names of the data packets of a file manifest that we are currently missing |
| 132 | * @param manifestName The name of the manifest |
| 133 | * @param packetNames The name of the data packets to be downloaded |
| 134 | * (used as an output vector of names) |
| 135 | * |
| 136 | * No matter what segment of a manifest file the manifestName parameter might refer to, the |
| 137 | * missing data packets starting from the first segment of this manifest file would be returned |
| 138 | * |
| 139 | */ |
| 140 | void |
| 141 | findDataPacketsToDownload(const Name& manifestName, std::vector<Name>& packetNames) const; |
| 142 | |
| 143 | /* |
| 144 | * \brief Find all the data packets that we are currently missing |
| 145 | * @param packetNames The name of the data packets to be downloaded |
| 146 | * (used as an output vector of names) |
| 147 | * |
| 148 | */ |
| 149 | void |
| 150 | findAllMissingDataPackets(std::vector<Name>& packetNames) const; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 151 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 152 | /* |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 153 | * @brief Stop all network activities of this manager |
| 154 | */ |
| 155 | void |
| 156 | shutdown(); |
| 157 | /* |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 158 | * @brief Download the torrent file |
| 159 | * @param path The path to write the downloaded segments |
| 160 | * @param onSuccess Callback to be called if we successfully download all the |
| 161 | * segments of the torrent file. It passes the name of the file manifest |
| 162 | * to be downloaded to the callback |
| 163 | * @param onFailed Callaback to be called if we fail to download a segment of |
| 164 | * the torrent file. It passes the name of the torrent file segment that |
| 165 | * failed to download and a failure reason to the callback |
| 166 | * |
| 167 | * This method provides non-blocking downloading of all the torrent file segments |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 168 | */ |
| 169 | void |
| 170 | downloadTorrentFile(const std::string& path, |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 171 | TorrentFileReceivedCallback onSuccess = {}, |
| 172 | FailedCallback onFailed = {}); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 173 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 174 | /* |
| 175 | * @brief Download a file manifest |
| 176 | * @param manifestName The name of the manifest file to be downloaded |
| 177 | * @param path The path to write the downloaded segments |
| 178 | * @param onSuccess Callback to be called if we successfully download all the |
| 179 | * segments of the file manifest. It passes the names of the data packets |
| 180 | * to be downloaded to the callback |
| 181 | * @param onFailed Callaback to be called if we fail to download a segment of |
| 182 | * the file manifest. It passes the name of the data packet that failed |
| 183 | * to download and a failure reason |
| 184 | * |
| 185 | * This method provides non-blocking downloading of all the file manifest segments |
| 186 | * |
| 187 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 188 | void |
| 189 | download_file_manifest(const Name& manifestName, |
| 190 | const std::string& path, |
| 191 | ManifestReceivedCallback onSuccess, |
| 192 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 193 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 194 | /* |
| 195 | * @brief Download a data packet |
| 196 | * @param packetName The name of the data packet to be downloaded |
| 197 | * @param onSuccess Callback to be called if we successfully download the data packet. |
| 198 | * It passes the name of the data packet to the callback |
| 199 | * @param onFailed Callaback to be called if we fail to download the requested data packet |
| 200 | * It passes the name of the data packet to the callback and a failure reason |
| 201 | * |
| 202 | * This method writes the downloaded data packet to m_dataPath on disk |
| 203 | * |
| 204 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 205 | void |
| 206 | download_data_packet(const Name& packetName, |
| 207 | DataReceivedCallback onSuccess, |
| 208 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 209 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 210 | // Seed the specified 'data' to the network. |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 211 | void |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 212 | seed(const Data& data); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 213 | |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 214 | /** |
| 215 | * @brief Process any data to receive or call timeout callbacks and update prefix list (if needed) |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 216 | * By default this blocks until all operations are complete. |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 217 | */ |
| 218 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 219 | processEvents(const time::milliseconds& timeout = time::milliseconds(0)); |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 220 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 221 | protected: |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 222 | /** |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 223 | * \brief Write @p packet composed of torrent date to disk. |
| 224 | * @param packet The data packet to be written to the disk |
| 225 | * Write the Data packet to disk, return 'true' if data successfully written to disk 'false' |
| 226 | * otherwise. Behavior is undefined unless the corresponding file manifest has already been |
| 227 | * downloaded. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 228 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 229 | bool |
| 230 | writeData(const Data& packet); |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 231 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 232 | /* |
| 233 | * \brief Write the @p segment torrent segment to disk at the specified path. |
| 234 | * @param segment The torrent file segment to be written to disk |
| 235 | * @param path The path at which to write the torrent file segment |
| 236 | * Write the segment to disk, return 'true' if data successfully written to disk 'false' |
| 237 | * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of |
| 238 | * this manager and @p path is the directory used for all segments of this torrent file. |
| 239 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 240 | bool |
| 241 | writeTorrentSegment(const TorrentFile& segment, const std::string& path); |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 242 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 243 | /* |
| 244 | * \brief Write the @p manifest file manifest to disk at the specified @p path. |
| 245 | * @param manifest The file manifest to be written to disk |
| 246 | * @param path The path at which to write the file manifest |
| 247 | * Write the file manifest to disk, return 'true' if data successfully written to disk 'false' |
| 248 | * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the |
| 249 | * torrent file of this manager and @p path is the directory used for all file manifests of this |
| 250 | * torrent file. |
| 251 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 252 | bool |
| 253 | writeFileManifest(const FileManifest& manifest, const std::string& path); |
Mickey Sweatt | afda1f1 | 2016-04-04 17:15:11 -0700 | [diff] [blame] | 254 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 255 | /* |
| 256 | * \brief Download the segments of the torrent file |
| 257 | * @param name The name of the torrent file to be downloaded |
| 258 | * @param path The path to write the torrent file on disk |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 259 | * @param onSuccess Optional callback to be called when all the segments of the torrent file |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 260 | * have been downloaded. The default value is an empty callback. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 261 | * @param onFailed Optional callback to be called when we fail to download a segment of the |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 262 | * torrent file. The default value is an empty callback. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 263 | * |
| 264 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 265 | void |
| 266 | downloadTorrentFileSegment(const ndn::Name& name, |
| 267 | const std::string& path, |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 268 | TorrentFileReceivedCallback onSuccess, |
| 269 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 270 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 271 | /* |
| 272 | * \brief Download the segments of a file manifest |
| 273 | * @param manifestName The name of the file manifest to be downloaded |
| 274 | * @param path The path to write the file manifest on disk |
| 275 | * @param packetNames A vector containing the name of the data packets in the file manifest |
| 276 | * @param onSuccess Callback to be called when all the segments of the file manifest have been |
| 277 | * downloaded |
| 278 | * @param onFailed Callback to be called when we fail to download a file manifest segment |
| 279 | * |
| 280 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 281 | void |
| 282 | downloadFileManifestSegment(const Name& manifestName, |
| 283 | const std::string& path, |
| 284 | std::shared_ptr<std::vector<Name>> packetNames, |
| 285 | ManifestReceivedCallback onSuccess, |
| 286 | FailedCallback onFailed); |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 287 | |
| 288 | enum { |
| 289 | // Number of times to retry if a routable prefix fails to retrieve data |
| 290 | MAX_NUM_OF_RETRIES = 5, |
| 291 | // Number of Interests to be sent before sorting the stats table |
spirosmastorakis | 9b68b53 | 2016-05-02 21:40:29 -0700 | [diff] [blame^] | 292 | SORTING_INTERVAL = 100, |
| 293 | // Maximum window size used for sending new Interests out |
| 294 | WINDOW_SIZE = 50 |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 295 | }; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 296 | |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 297 | void onDataReceived(const Data& data); |
| 298 | |
| 299 | void |
| 300 | onInterestReceived(const InterestFilter& filter, const Interest& interest); |
| 301 | |
| 302 | void |
| 303 | onRegisterFailed(const Name& prefix, const std::string& reason); |
| 304 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 305 | protected: |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 306 | // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data |
| 307 | // packets this manager currently has |
| 308 | mutable std::unordered_map<Name, |
| 309 | std::pair<std::shared_ptr<fs::fstream>, |
| 310 | std::vector<bool>>> m_fileStates; |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 311 | // A map for each initial manifest to the size for the sub-manifest |
| 312 | std::unordered_map<std::string, size_t> m_subManifestSizes; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 313 | // The segments of the TorrentFile this manager has |
| 314 | std::vector<TorrentFile> m_torrentSegments; |
| 315 | // The FileManifests this manager has |
| 316 | std::vector<FileManifest> m_fileManifests; |
| 317 | // The name of the initial segment of the torrent file for this manager |
| 318 | Name m_torrentFileName; |
| 319 | // The path to the location on disk of the Data packet for this manager |
| 320 | std::string m_dataPath; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 321 | |
| 322 | private: |
| 323 | shared_ptr<Interest> |
| 324 | createInterest(Name name); |
| 325 | |
spirosmastorakis | 9b68b53 | 2016-05-02 21:40:29 -0700 | [diff] [blame^] | 326 | void |
| 327 | sendInterest(); |
| 328 | |
| 329 | |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 330 | // A flag to determine if upon completion we should continue seeding |
| 331 | bool m_seedFlag; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 332 | // Face used for network communication |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 333 | std::shared_ptr<Face> m_face; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 334 | // Stats table where routable prefixes are stored |
| 335 | StatsTable m_statsTable; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 336 | // Iterator to the routable prefix that we currently use |
| 337 | StatsTable::iterator m_stats_table_iter; |
| 338 | // Number of retries per routable prefix |
| 339 | uint64_t m_retries; |
| 340 | // Number of Interests sent since last sorting |
| 341 | uint64_t m_sortingCounter; |
| 342 | // Keychain instance |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 343 | shared_ptr<KeyChain> m_keyChain; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 344 | |
| 345 | std::unordered_set<ndn::Name> m_pendingInterests; |
spirosmastorakis | 9b68b53 | 2016-05-02 21:40:29 -0700 | [diff] [blame^] | 346 | |
| 347 | shared_ptr<InterestQueue> m_interestQueue; |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 348 | // TODO(spyros) Fix and reintegrate update handler |
| 349 | // // Update Handler instance |
| 350 | // shared_ptr<UpdateHandler> m_updateHandler; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | inline |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 354 | TorrentManager::TorrentManager(const ndn::Name& torrentFileName, |
| 355 | const std::string& dataPath, |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 356 | bool seed, |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 357 | std::shared_ptr<Face> face) |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 358 | : m_fileStates() |
| 359 | , m_torrentSegments() |
| 360 | , m_fileManifests() |
| 361 | , m_torrentFileName(torrentFileName) |
| 362 | , m_dataPath(dataPath) |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 363 | , m_seedFlag(seed) |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 364 | , m_face(face) |
| 365 | , m_retries(0) |
| 366 | , m_sortingCounter(0) |
| 367 | , m_keyChain(new KeyChain()) |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 368 | { |
spirosmastorakis | 9b68b53 | 2016-05-02 21:40:29 -0700 | [diff] [blame^] | 369 | m_interestQueue = make_shared<InterestQueue>(); |
| 370 | |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 371 | if(face == nullptr) { |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 372 | m_face = make_shared<Face>(); |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 373 | } |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 374 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 375 | // Hardcoded prefixes for now |
| 376 | // TODO(Spyros): Think of something more clever to bootstrap... |
| 377 | m_statsTable.insert("/ucla"); |
| 378 | m_statsTable.insert("/arizona"); |
| 379 | m_stats_table_iter = m_statsTable.begin(); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 382 | inline |
| 383 | void |
| 384 | TorrentManager::processEvents(const time::milliseconds& timeout) |
| 385 | { |
| 386 | m_face->processEvents(timeout); |
| 387 | } |
| 388 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 389 | inline |
| 390 | bool |
| 391 | TorrentManager::hasAllTorrentSegments() const |
| 392 | { |
| 393 | return findTorrentFileSegmentToDownload() == nullptr; |
| 394 | } |
| 395 | |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 396 | inline |
| 397 | void |
| 398 | TorrentManager::shutdown() |
| 399 | { |
| 400 | // TODO(msweatt) Consider unregistering all prefix to exit more gracefully |
| 401 | m_face->shutdown(); |
| 402 | } |
| 403 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 404 | } // end ntorrent |
| 405 | } // end ndn |
| 406 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 407 | #endif // INCLUDED_TORRENT_FILE_MANAGER_H |