UnknownLengthPieceStorage.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "UnknownLengthPieceStorage.h"
  36. #include <cstdlib>
  37. #include "DefaultDiskWriter.h"
  38. #include "DirectDiskAdaptor.h"
  39. #include "prefs.h"
  40. #include "DefaultDiskWriterFactory.h"
  41. #include "DownloadContext.h"
  42. #include "Piece.h"
  43. #include "FileEntry.h"
  44. namespace aria2 {
  45. UnknownLengthPieceStorage::UnknownLengthPieceStorage
  46. (const SharedHandle<DownloadContext>& downloadContext,
  47. const Option* option):
  48. _downloadContext(downloadContext),
  49. _option(option),
  50. _diskWriterFactory(new DefaultDiskWriterFactory()),
  51. _totalLength(0),
  52. _downloadFinished(false) {}
  53. UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
  54. void UnknownLengthPieceStorage::initStorage()
  55. {
  56. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  57. directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
  58. directDiskAdaptor->setFileEntries(_downloadContext->getFileEntries().begin(),
  59. _downloadContext->getFileEntries().end());
  60. DiskWriterHandle writer =
  61. _diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
  62. directDiskAdaptor->setDiskWriter(writer);
  63. _diskAdaptor = directDiskAdaptor;
  64. }
  65. #ifdef ENABLE_BITTORRENT
  66. bool UnknownLengthPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)
  67. {
  68. abort();
  69. }
  70. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  71. {
  72. abort();
  73. }
  74. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece
  75. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  76. {
  77. abort();
  78. }
  79. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingFastPiece(const SharedHandle<Peer>& peer)
  80. {
  81. abort();
  82. }
  83. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingFastPiece
  84. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  85. {
  86. abort();
  87. }
  88. #endif // ENABLE_BITTORRENT
  89. SharedHandle<Piece> UnknownLengthPieceStorage::getSparseMissingUnusedPiece
  90. (const unsigned char* ignoreBitfield, size_t length)
  91. {
  92. if(_downloadFinished) {
  93. return SharedHandle<Piece>();
  94. }
  95. if(_piece.isNull()) {
  96. _piece.reset(new Piece());
  97. return _piece;
  98. } else {
  99. return SharedHandle<Piece>();
  100. }
  101. }
  102. PieceHandle UnknownLengthPieceStorage::getMissingPiece(size_t index)
  103. {
  104. if(index == 0) {
  105. return getSparseMissingUnusedPiece(0, 0);
  106. } else {
  107. return SharedHandle<Piece>();
  108. }
  109. }
  110. PieceHandle UnknownLengthPieceStorage::getPiece(size_t index)
  111. {
  112. if(index == 0) {
  113. if(_piece.isNull()) {
  114. return SharedHandle<Piece>(new Piece());
  115. } else {
  116. return _piece;
  117. }
  118. } else {
  119. return SharedHandle<Piece>();
  120. }
  121. }
  122. void UnknownLengthPieceStorage::completePiece(const PieceHandle& piece)
  123. {
  124. if(_piece == piece) {
  125. _downloadFinished = true;
  126. _totalLength = _piece->getLength();
  127. _diskAdaptor->setTotalLength(_totalLength);
  128. _piece.reset();
  129. }
  130. }
  131. void UnknownLengthPieceStorage::cancelPiece(const PieceHandle& piece)
  132. {
  133. if(_piece == piece) {
  134. _piece.reset();
  135. }
  136. }
  137. bool UnknownLengthPieceStorage::hasPiece(size_t index)
  138. {
  139. if(index == 0 && _downloadFinished) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }
  145. bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
  146. {
  147. if(index == 0 && !_piece.isNull()) {
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. DiskAdaptorHandle UnknownLengthPieceStorage::getDiskAdaptor()
  154. {
  155. return _diskAdaptor;
  156. }
  157. size_t UnknownLengthPieceStorage::getPieceLength(size_t index)
  158. {
  159. if(index == 0) {
  160. return _totalLength;
  161. } else {
  162. return 0;
  163. }
  164. }
  165. void UnknownLengthPieceStorage::markAllPiecesDone()
  166. {
  167. if(!_piece.isNull()) {
  168. _totalLength = _piece->getLength();
  169. _piece.reset();
  170. }
  171. _downloadFinished = true;
  172. }
  173. void UnknownLengthPieceStorage::markPiecesDone(uint64_t length)
  174. {
  175. // TODO not implemented yet
  176. abort();
  177. }
  178. void UnknownLengthPieceStorage::markPieceMissing(size_t index)
  179. {
  180. // TODO not implemented yet
  181. abort();
  182. }
  183. void UnknownLengthPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  184. {}
  185. void UnknownLengthPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  186. {
  187. _diskWriterFactory = diskWriterFactory;
  188. }
  189. } // namespace aria2