keker123

Untitled

May 7th, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include "piece_storage.h"
  2. #include <iostream>
  3.  
  4. PieceStorage::PieceStorage(const TorrentFile &tf) {
  5.     for (size_t i = 0; i < tf.length / tf.pieceLength; ++i) {
  6.         size_t length = (i == tf.length / tf.pieceLength - 1) ? tf.length % tf.pieceLength : tf.pieceLength;
  7.         remainPieces_.push(std::make_shared<Piece>(i, length, tf.pieceHashes[i]));
  8.     }
  9. }
  10.  
  11. PiecePtr PieceStorage::GetNextPieceToDownload() {
  12.     if (remainPieces_.empty()) {
  13.         return nullptr;
  14.     }
  15.     auto piece = remainPieces_.front();
  16.     remainPieces_.pop();
  17.     return piece;
  18. }
  19.  
  20. void PieceStorage::PieceProcessed(const PiecePtr& piece) {
  21.     // clear queue?
  22.     while (!remainPieces_.empty()) {
  23.         remainPieces_.pop();
  24.     }
  25.     SavePieceToDisk(piece);
  26. }
  27.  
  28. bool PieceStorage::QueueIsEmpty() const {
  29.     return remainPieces_.empty();
  30. }
  31.  
  32. size_t PieceStorage::TotalPiecesCount() const {
  33.     // TODO хз тут оставшихся частей или всего?
  34.     return remainPieces_.size();
  35. }
  36.  
  37. void PieceStorage::SavePieceToDisk(PiecePtr piece) {
  38.     // Эта функция будет переопределена при запуске вашего решения в проверяющей системе
  39.     // Вместо сохранения на диск там пока что будет проверка, что часть файла скачалась правильно
  40.     std::cout << "Downloaded piece " << piece->GetIndex() << std::endl;
  41.     std::cerr << "Clear pieces list, don't want to download all of them" << std::endl;
  42.     while (!remainPieces_.empty()) {
  43.         remainPieces_.pop();
  44.     }
  45. }
  46.  
Add Comment
Please, Sign In to add comment