keker123

Untitled

May 14th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include "piece_storage.h"
  2. #include <iostream>
  3.  
  4. PieceStorage::PieceStorage(const TorrentFile& tf, const std::filesystem::path& outputDirectory) :
  5. tf_(tf),
  6. outputDirectory_(outputDirectory),
  7. out_(outputDirectory_ / tf_.name, std::ios::binary | std::ios::out),
  8. isOutputFileOpen_(true) {
  9. out_.seekp(tf.length - 1);
  10. out_.write("\0", 1);
  11. std::cout << "OPENED STREAM: " << out_.is_open() << std::endl;
  12. std::cout << "NAME IS " << tf_.name << std::endl;
  13. for (size_t i = 0; i < tf.length / tf.pieceLength; ++i) {
  14. size_t length = (i == tf.length / tf.pieceLength - 1) ? tf.length % tf.pieceLength : tf.pieceLength;
  15. remainPieces_.push(std::make_shared<Piece>(i, length, tf.pieceHashes[i]));
  16. }
  17. }
  18.  
  19.  
  20. PiecePtr PieceStorage::GetNextPieceToDownload() {
  21. std::lock_guard lock(mutex_);
  22. if (remainPieces_.empty()) {
  23. return nullptr;
  24. }
  25. auto piece = remainPieces_.front();
  26. remainPieces_.pop();
  27. return piece;
  28. }
  29. void PieceStorage::AddPieceToQueue(const PiecePtr &piece) {
  30. std::lock_guard lock(mutex_);
  31. remainPieces_.push(piece);
  32. }
  33. void PieceStorage::PieceProcessed(const PiecePtr& piece) {
  34. // хз, что будет если пир постоянно будет отправлять бракованный кусок,peer_connect будет же крутиться в бесконечном цикле
  35. std::lock_guard lock(mutex_);
  36. if (!piece->HashMatches()) {
  37. piece->Reset();
  38. std::cerr << "Piece " << piece->GetIndex() << " hash doesn't match" << std::endl;
  39. return;
  40. }
  41. SavePieceToDisk(piece);
  42. }
  43.  
  44. bool PieceStorage::QueueIsEmpty() const {
  45. std::lock_guard lock(mutex_);
  46. return remainPieces_.empty();
  47. }
  48.  
  49. size_t PieceStorage::TotalPiecesCount() const {
  50. return tf_.length / tf_.pieceLength;
  51. }
  52.  
  53. size_t PieceStorage::PiecesInProgressCount() const {
  54. std::lock_guard lock(mutex_);
  55. return tf_.length / tf_.pieceLength - remainPieces_.size();
  56. }
  57.  
  58. void PieceStorage::CloseOutputFile() {
  59. std::lock_guard lock(mutex_);
  60. if (!isOutputFileOpen_) {
  61. std::cerr << "Output file is already closed" << std::endl;
  62. return;
  63. }
  64. out_.close();
  65. isOutputFileOpen_ = false;
  66. }
  67.  
  68. const std::vector<size_t>& PieceStorage::GetPiecesSavedToDiscIndices() const { // проблемный метод
  69. std::lock_guard lock(mutex_);
  70. return piecesSavedToDiscIndicesVector_;
  71. }
  72.  
  73. size_t PieceStorage::PiecesSavedToDiscCount() const {
  74. std::lock_guard lock(mutex_);
  75. return piecesSavedToDiscIndicesSet_.size();
  76. }
  77.  
  78.  
  79. void PieceStorage::SavePieceToDisk(const PiecePtr &piece) {
  80. if (!isOutputFileOpen_) {
  81. std::cerr << "Output file is already closed" << std::endl;
  82. return;
  83. }
  84. if(piecesSavedToDiscIndicesSet_.find(piece->GetIndex()) != piecesSavedToDiscIndicesSet_.end()) {
  85. std::cerr << "Piece " << piece->GetIndex() << " is already saved to disc" << std::endl;
  86. return;
  87. }
  88. piecesSavedToDiscIndicesSet_.insert(piece->GetIndex());
  89. piecesSavedToDiscIndicesVector_.push_back(piece->GetIndex());
  90. out_.seekp(piece->GetIndex() * tf_.pieceLength);
  91. out_.write(piece->GetData().data(), piece->GetData().size());
  92. std::cout << "Saved to Disk piece " << piece->GetIndex() << std::endl;
  93. // std::cout << "Piece data length: " << piece->GetData().size() << std::endl;
  94. std::cout << "PieceQueue size: " << remainPieces_.size() << std::endl;
  95. }
  96.  
Add Comment
Please, Sign In to add comment