UnknownLengthPieceStorage.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "DefaultDiskWriter.h"
  37. #include "DirectDiskAdaptor.h"
  38. #include "prefs.h"
  39. #include "DefaultDiskWriterFactory.h"
  40. #include "DownloadContext.h"
  41. #include "Piece.h"
  42. #include "FileEntry.h"
  43. #include <stdlib.h>
  44. namespace aria2 {
  45. UnknownLengthPieceStorage::UnknownLengthPieceStorage(const DownloadContextHandle& downloadContext,
  46. const Option* option):
  47. _downloadContext(downloadContext),
  48. _option(option),
  49. _diskWriterFactory(new DefaultDiskWriterFactory()),
  50. _totalLength(0),
  51. _downloadFinished(false) {}
  52. UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
  53. void UnknownLengthPieceStorage::initStorage()
  54. {
  55. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  56. directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
  57. directDiskAdaptor->setFileEntries(_downloadContext->getFileEntries());
  58. DiskWriterHandle writer =
  59. _diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
  60. directDiskAdaptor->setDiskWriter(writer);
  61. _diskAdaptor = directDiskAdaptor;
  62. }
  63. bool UnknownLengthPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)
  64. {
  65. abort();
  66. }
  67. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  68. {
  69. abort();
  70. }
  71. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece
  72. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  73. {
  74. abort();
  75. }
  76. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingFastPiece(const SharedHandle<Peer>& peer)
  77. {
  78. abort();
  79. }
  80. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingFastPiece
  81. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  82. {
  83. abort();
  84. }
  85. PieceHandle UnknownLengthPieceStorage::getMissingPiece()
  86. {
  87. if(_downloadFinished) {
  88. return SharedHandle<Piece>();
  89. }
  90. if(_piece.isNull()) {
  91. _piece.reset(new Piece());
  92. return _piece;
  93. } else {
  94. return SharedHandle<Piece>();
  95. }
  96. }
  97. PieceHandle UnknownLengthPieceStorage::getMissingPiece(size_t index)
  98. {
  99. if(index == 0) {
  100. return getMissingPiece();
  101. } else {
  102. return SharedHandle<Piece>();
  103. }
  104. }
  105. PieceHandle UnknownLengthPieceStorage::getPiece(size_t index)
  106. {
  107. if(index == 0) {
  108. if(_piece.isNull()) {
  109. return SharedHandle<Piece>(new Piece());
  110. } else {
  111. return _piece;
  112. }
  113. } else {
  114. return SharedHandle<Piece>();
  115. }
  116. }
  117. void UnknownLengthPieceStorage::completePiece(const PieceHandle& piece)
  118. {
  119. if(_piece == piece) {
  120. _downloadFinished = true;
  121. _totalLength = _piece->getLength();
  122. _diskAdaptor->setTotalLength(_totalLength);
  123. _piece.reset();
  124. }
  125. }
  126. void UnknownLengthPieceStorage::cancelPiece(const PieceHandle& piece)
  127. {
  128. if(_piece == piece) {
  129. _piece.reset();
  130. }
  131. }
  132. bool UnknownLengthPieceStorage::hasPiece(size_t index)
  133. {
  134. if(index == 0 && _downloadFinished) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
  141. {
  142. if(index == 0 && !_piece.isNull()) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. DiskAdaptorHandle UnknownLengthPieceStorage::getDiskAdaptor()
  149. {
  150. return _diskAdaptor;
  151. }
  152. size_t UnknownLengthPieceStorage::getPieceLength(size_t index)
  153. {
  154. if(index == 0) {
  155. return _totalLength;
  156. } else {
  157. return 0;
  158. }
  159. }
  160. void UnknownLengthPieceStorage::markAllPiecesDone()
  161. {
  162. if(!_piece.isNull()) {
  163. _totalLength = _piece->getLength();
  164. _piece.reset();
  165. }
  166. _downloadFinished = true;
  167. }
  168. void UnknownLengthPieceStorage::markPiecesDone(uint64_t length)
  169. {
  170. // TODO not implemented yet
  171. abort();
  172. }
  173. void UnknownLengthPieceStorage::markPieceMissing(size_t index)
  174. {
  175. // TODO not implemented yet
  176. abort();
  177. }
  178. void UnknownLengthPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  179. {}
  180. void UnknownLengthPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  181. {
  182. _diskWriterFactory = diskWriterFactory;
  183. }
  184. } // namespace aria2