Furrent
furrent.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <mt/group.hpp>
5 #include <mt/sharing_queue.hpp>
6 #include <shared_mutex>
7 #include <torrent.hpp>
8 #include <types.hpp>
9 #include <unordered_map>
10 #include <util/singleton.hpp>
11 
12 namespace fur {
13 
17  std::string filename;
18 
20  int64_t pieces_count;
21 };
22 
27  bool completed;
28 };
29 
31 class PieceTask {
32  // Downloaded piece content
33  std::optional<download::Downloaded> _data;
34 
35  public:
42 
43  public:
45  explicit PieceTask();
48 
51  PieceTaskStats process(const peer::Peer& peer);
52 
53  private:
55  [[nodiscard]] bool download(const peer::Peer& peer);
57  [[nodiscard]] bool save() const;
58 };
59 
62 class Furrent : public Singleton<Furrent> {
64  struct WorkerState {
66  int64_t piece_processed = 0;
67  };
68 
73 
75  mutable std::shared_mutex _mtx;
77  std::unordered_map<TorrentID, Torrent> _torrents;
79  TorrentID _descriptor_next_uid;
80 
82  std::string _download_folder;
83 
84  public:
87 
89  template <typename R>
91  using Empty = util::Empty;
92 
93  public:
94  Furrent();
95  virtual ~Furrent();
96 
98  Result<Empty> set_download_folder(const std::string& folder);
99 
103  Result<TorrentID> add_torrent(const std::string& filename);
104 
107  void remove_torrent(TorrentID tid);
108 
111 
112  private:
114  void thread_main(mt::Runner runner, WorkerState& state, int64_t index);
115 
118  bool prepare_torrent_files(TorrentFile& descriptor);
119 };
120 
121 } // namespace fur
fur::Furrent::remove_torrent
void remove_torrent(TorrentID tid)
Removes a torrent descriptor and all of his tasks.
Definition: furrent.cpp:367
fur::Furrent::Error::LoadingTorrentFailed
@ LoadingTorrentFailed
downloader.hpp
fur::PieceTask::tid
TorrentID tid
Identifier of the owner torrent.
Definition: furrent.hpp:37
fur::mt::ThreadGroup< WorkerState >
sharing_queue.hpp
torrent.hpp
types.hpp
fur::Furrent::Error::GenericError
@ GenericError
fur::Furrent::add_torrent
Result< TorrentID > add_torrent(const std::string &filename)
Begin download of a torrent.
Definition: furrent.cpp:305
fur::TorrentGuiData::tid
TorrentID tid
Definition: furrent.hpp:15
fur::Furrent::get_gui_data
TorrentGuiData get_gui_data(TorrentID tid) const
Extract torrents stats.
Definition: furrent.cpp:382
fur::PieceTask
Class responsible for processing a piece.
Definition: furrent.hpp:31
fur::PieceTask::process
PieceTaskStats process(const peer::Peer &peer)
Process piece, downloads it from a peer and saves it to file.
Definition: furrent.cpp:24
group.hpp
fur::Furrent::~Furrent
virtual ~Furrent()
Definition: furrent.cpp:102
fur::util::Result
Used to handle errors without using exceptions.
Definition: result.hpp:18
fur::TorrentGuiData::state
TorrentState state
Definition: furrent.hpp:16
fur::TorrentID
int64_t TorrentID
Definition: types.hpp:8
fur::Furrent::Error
Error
All possible Furrent errors.
Definition: furrent.hpp:86
fur::Furrent
Definition: furrent.hpp:62
fur::TorrentGuiData::pieces_count
int64_t pieces_count
Definition: furrent.hpp:20
fur::PieceTaskStats
Definition: furrent.hpp:25
fur::Furrent::Furrent
Furrent()
Definition: furrent.cpp:82
fur::PieceTask::descriptor
TorrentFile descriptor
.torrent descriptor
Definition: furrent.hpp:41
fur::PieceTask::piece
Piece piece
Piece to process.
Definition: furrent.hpp:39
fur::peer::Peer
Represents a single peer as given by the tracker.
Definition: peer.hpp:19
fur::util::Empty
Definition: result.hpp:51
fur::TorrentGuiData
Definition: furrent.hpp:14
fur::TorrentGuiData::filename
std::string filename
Definition: furrent.hpp:17
fur::TorrentFile
Represents a parsed .torrent file.
Definition: torrent.hpp:32
fur::TorrentState
TorrentState
Definition: torrent.hpp:82
fur::PieceTaskStats::completed
bool completed
True if the operation was successfull.
Definition: furrent.hpp:27
fur::Furrent::set_download_folder
Result< Empty > set_download_folder(const std::string &folder)
Set the download folder.
Definition: furrent.cpp:107
singleton.hpp
fur::util::Singleton
Definition: singleton.hpp:6
fur::TorrentGuiData::pieces_processed
int64_t pieces_processed
Definition: furrent.hpp:19
fur::mt::Runner
Used to control threads execution.
Definition: group.hpp:19
fur::PieceTask::PieceTask
PieceTask()
Constructs an empty temporary piece task.
Definition: furrent.cpp:14
fur::Piece
Definition: torrent.hpp:75
fur::mt::SharedQueue
Definition: sharing_queue.hpp:26
fur
Common types used in all the project.
Definition: bencode_parser.cpp:7