SegmentMan.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "SegmentMan.h"
  36. #include "Util.h"
  37. #include "message.h"
  38. #include "prefs.h"
  39. #include "PiecedSegment.h"
  40. #include "GrowSegment.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "PieceStorage.h"
  44. #include "PeerStat.h"
  45. #include "Option.h"
  46. #include "DownloadContext.h"
  47. #include "Piece.h"
  48. namespace aria2 {
  49. SegmentEntry::SegmentEntry(int32_t cuid, const SegmentHandle& segment):
  50. cuid(cuid), segment(segment) {}
  51. SegmentEntry::~SegmentEntry() {}
  52. SegmentMan::SegmentMan(const Option* option,
  53. const DownloadContextHandle& downloadContext,
  54. const PieceStorageHandle& pieceStorage):
  55. _option(option),
  56. logger(LogFactory::getInstance()),
  57. _downloadContext(downloadContext),
  58. _pieceStorage(pieceStorage)
  59. {}
  60. SegmentMan::~SegmentMan() {}
  61. bool SegmentMan::downloadFinished() const
  62. {
  63. if(_pieceStorage.isNull()) {
  64. return false;
  65. } else {
  66. return _pieceStorage->downloadFinished();
  67. }
  68. }
  69. void SegmentMan::init()
  70. {
  71. // TODO Do we have to do something about DownloadContext and PieceStorage here?
  72. }
  73. uint64_t SegmentMan::getTotalLength() const
  74. {
  75. if(_pieceStorage.isNull()) {
  76. return 0;
  77. } else {
  78. return _pieceStorage->getTotalLength();
  79. }
  80. }
  81. void SegmentMan::setPieceStorage(const PieceStorageHandle& pieceStorage)
  82. {
  83. _pieceStorage = pieceStorage;
  84. }
  85. void SegmentMan::setDownloadContext(const DownloadContextHandle& downloadContext)
  86. {
  87. _downloadContext = downloadContext;
  88. }
  89. SegmentHandle SegmentMan::checkoutSegment(int32_t cuid,
  90. const PieceHandle& piece)
  91. {
  92. if(piece.isNull()) {
  93. return 0;
  94. }
  95. logger->debug("Attach segment#%d to CUID#%d.", piece->getIndex(), cuid);
  96. SegmentHandle segment = 0;
  97. if(piece->getLength() == 0) {
  98. segment = new GrowSegment(piece);
  99. } else {
  100. segment = new PiecedSegment(_downloadContext->getPieceLength(), piece);
  101. }
  102. SegmentEntryHandle entry = new SegmentEntry(cuid, segment);
  103. usedSegmentEntries.push_back(entry);
  104. logger->debug("index=%d, length=%d, segmentLength=%d, writtenLength=%d",
  105. segment->getIndex(),
  106. segment->getLength(),
  107. segment->getSegmentLength(),
  108. segment->getWrittenLength());
  109. return segment;
  110. }
  111. SegmentEntryHandle SegmentMan::findSlowerSegmentEntry(const PeerStatHandle& peerStat) const {
  112. unsigned int speed = peerStat->getAvgDownloadSpeed()*0.8;
  113. SegmentEntryHandle slowSegmentEntry(0);
  114. for(SegmentEntries::const_iterator itr = usedSegmentEntries.begin();
  115. itr != usedSegmentEntries.end(); ++itr) {
  116. const SegmentEntryHandle& segmentEntry = *itr;
  117. if(segmentEntry->cuid == 0) {
  118. continue;
  119. }
  120. PeerStatHandle p = getPeerStat(segmentEntry->cuid);
  121. if(!p.get() || p->getCuid() == peerStat->getCuid() ||
  122. p->getStatus() != PeerStat::ACTIVE ||
  123. !p->getDownloadStartTime().elapsed(_option->getAsInt(PREF_STARTUP_IDLE_TIME))) {
  124. continue;
  125. }
  126. unsigned int pSpeed = p->calculateDownloadSpeed();
  127. if(pSpeed < speed) {
  128. speed = pSpeed;
  129. slowSegmentEntry = segmentEntry;
  130. }
  131. }
  132. return slowSegmentEntry;
  133. }
  134. Segments SegmentMan::getInFlightSegment(int32_t cuid)
  135. {
  136. Segments temp;
  137. for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
  138. itr != usedSegmentEntries.end(); ++itr) {
  139. const SegmentEntryHandle& segmentEntry = *itr;
  140. if(segmentEntry->cuid == cuid) {
  141. temp.push_back(segmentEntry->segment);
  142. }
  143. }
  144. return temp;
  145. }
  146. SegmentHandle SegmentMan::getSegment(int32_t cuid) {
  147. PieceHandle piece = _pieceStorage->getMissingPiece();
  148. if(piece.isNull()) {
  149. PeerStatHandle myPeerStat = getPeerStat(cuid);
  150. if(myPeerStat.isNull()) {
  151. return 0;
  152. }
  153. SegmentEntryHandle slowSegmentEntry = findSlowerSegmentEntry(myPeerStat);
  154. if(slowSegmentEntry.get()) {
  155. logger->info(MSG_SEGMENT_FORWARDING,
  156. slowSegmentEntry->cuid,
  157. slowSegmentEntry->segment->getIndex(),
  158. cuid);
  159. PeerStatHandle slowPeerStat = getPeerStat(slowSegmentEntry->cuid);
  160. slowPeerStat->requestIdle();
  161. cancelSegment(slowSegmentEntry->cuid);
  162. return checkoutSegment(cuid, slowSegmentEntry->segment->getPiece());
  163. } else {
  164. return 0;
  165. }
  166. } else {
  167. return checkoutSegment(cuid, piece);
  168. }
  169. }
  170. SegmentHandle SegmentMan::getSegment(int32_t cuid, size_t index) {
  171. if(_downloadContext->getNumPieces() <= index) {
  172. return 0;
  173. }
  174. return checkoutSegment(cuid, _pieceStorage->getMissingPiece(index));
  175. }
  176. void SegmentMan::cancelSegment(int32_t cuid) {
  177. for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
  178. itr != usedSegmentEntries.end();) {
  179. if((*itr)->cuid == cuid) {
  180. _pieceStorage->cancelPiece((*itr)->segment->getPiece());
  181. itr = usedSegmentEntries.erase(itr);
  182. } else {
  183. ++itr;
  184. }
  185. }
  186. }
  187. class FindSegmentEntry {
  188. private:
  189. SegmentHandle _segment;
  190. public:
  191. FindSegmentEntry(const SegmentHandle& segment):_segment(segment) {}
  192. bool operator()(const SegmentEntryHandle& segmentEntry) const
  193. {
  194. return segmentEntry->segment->getIndex() == _segment->getIndex();
  195. }
  196. };
  197. bool SegmentMan::completeSegment(int32_t cuid, const SegmentHandle& segment) {
  198. _pieceStorage->completePiece(segment->getPiece());
  199. _pieceStorage->advertisePiece(cuid, segment->getPiece()->getIndex());
  200. SegmentEntries::iterator itr = std::find_if(usedSegmentEntries.begin(),
  201. usedSegmentEntries.end(),
  202. FindSegmentEntry(segment));
  203. if(itr == usedSegmentEntries.end()) {
  204. return false;
  205. } else {
  206. usedSegmentEntries.erase(itr);
  207. return true;
  208. }
  209. }
  210. bool SegmentMan::hasSegment(size_t index) const {
  211. return _pieceStorage->hasPiece(index);
  212. }
  213. uint64_t SegmentMan::getDownloadLength() const {
  214. if(_pieceStorage.isNull()) {
  215. return 0;
  216. } else {
  217. return _pieceStorage->getCompletedLength();
  218. }
  219. }
  220. void SegmentMan::registerPeerStat(const PeerStatHandle& peerStat) {
  221. PeerStatHandle temp = getPeerStat(peerStat->getCuid());
  222. if(!temp.get()) {
  223. peerStats.push_back(peerStat);
  224. }
  225. }
  226. PeerStatHandle SegmentMan::getPeerStat(int32_t cuid) const
  227. {
  228. for(std::deque<SharedHandle<PeerStat> >::const_iterator itr = peerStats.begin(); itr != peerStats.end(); ++itr) {
  229. const PeerStatHandle& peerStat = *itr;
  230. if(peerStat->getCuid() == cuid) {
  231. return peerStat;
  232. }
  233. }
  234. return 0;
  235. }
  236. unsigned int SegmentMan::calculateDownloadSpeed() const {
  237. unsigned int speed = 0;
  238. for(std::deque<SharedHandle<PeerStat> >::const_iterator itr = peerStats.begin(); itr != peerStats.end(); itr++) {
  239. const PeerStatHandle& peerStat = *itr;
  240. if(peerStat->getStatus() == PeerStat::ACTIVE) {
  241. speed += peerStat->calculateDownloadSpeed();
  242. }
  243. }
  244. return speed;
  245. }
  246. size_t SegmentMan::countFreePieceFrom(size_t index) const
  247. {
  248. size_t numPieces = _downloadContext->getNumPieces();
  249. for(size_t i = index; i < numPieces; ++i) {
  250. if(_pieceStorage->hasPiece(i) || _pieceStorage->isPieceUsed(i)) {
  251. return i-index;
  252. }
  253. }
  254. return _downloadContext->getNumPieces()-index;
  255. }
  256. } // namespace aria2