UnknownLengthPieceStorage.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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::vector<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::vector<size_t>& excludedIndexes)
  85. {
  86. abort();
  87. }
  88. #endif // ENABLE_BITTORRENT
  89. bool UnknownLengthPieceStorage::hasMissingUnusedPiece()
  90. {
  91. abort();
  92. }
  93. SharedHandle<Piece> UnknownLengthPieceStorage::getSparseMissingUnusedPiece
  94. (const unsigned char* ignoreBitfield, size_t length)
  95. {
  96. if(_downloadFinished) {
  97. return SharedHandle<Piece>();
  98. }
  99. if(_piece.isNull()) {
  100. _piece.reset(new Piece());
  101. return _piece;
  102. } else {
  103. return SharedHandle<Piece>();
  104. }
  105. }
  106. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece(size_t index)
  107. {
  108. if(index == 0) {
  109. return getSparseMissingUnusedPiece(0, 0);
  110. } else {
  111. return SharedHandle<Piece>();
  112. }
  113. }
  114. SharedHandle<Piece> UnknownLengthPieceStorage::getPiece(size_t index)
  115. {
  116. if(index == 0) {
  117. if(_piece.isNull()) {
  118. return SharedHandle<Piece>(new Piece());
  119. } else {
  120. return _piece;
  121. }
  122. } else {
  123. return SharedHandle<Piece>();
  124. }
  125. }
  126. void UnknownLengthPieceStorage::completePiece(const SharedHandle<Piece>& piece)
  127. {
  128. if(_piece == piece) {
  129. _downloadFinished = true;
  130. _totalLength = _piece->getLength();
  131. _diskAdaptor->setTotalLength(_totalLength);
  132. _piece.reset();
  133. }
  134. }
  135. void UnknownLengthPieceStorage::cancelPiece(const SharedHandle<Piece>& piece)
  136. {
  137. if(_piece == piece) {
  138. _piece.reset();
  139. }
  140. }
  141. bool UnknownLengthPieceStorage::hasPiece(size_t index)
  142. {
  143. if(index == 0 && _downloadFinished) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
  150. {
  151. if(index == 0 && !_piece.isNull()) {
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. }
  157. DiskAdaptorHandle UnknownLengthPieceStorage::getDiskAdaptor()
  158. {
  159. return _diskAdaptor;
  160. }
  161. size_t UnknownLengthPieceStorage::getPieceLength(size_t index)
  162. {
  163. if(index == 0) {
  164. return _totalLength;
  165. } else {
  166. return 0;
  167. }
  168. }
  169. void UnknownLengthPieceStorage::markAllPiecesDone()
  170. {
  171. if(!_piece.isNull()) {
  172. _totalLength = _piece->getLength();
  173. _piece.reset();
  174. }
  175. _downloadFinished = true;
  176. }
  177. void UnknownLengthPieceStorage::markPiecesDone(uint64_t length)
  178. {
  179. // TODO not implemented yet
  180. abort();
  181. }
  182. void UnknownLengthPieceStorage::markPieceMissing(size_t index)
  183. {
  184. // TODO not implemented yet
  185. abort();
  186. }
  187. void UnknownLengthPieceStorage::getInFlightPieces
  188. (std::vector<SharedHandle<Piece> >& pieces)
  189. {}
  190. void UnknownLengthPieceStorage::setDiskWriterFactory
  191. (const DiskWriterFactoryHandle& diskWriterFactory)
  192. {
  193. _diskWriterFactory = diskWriterFactory;
  194. }
  195. } // namespace aria2