SegmentMan.cc 8.9 KB

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