SegmentMan.cc 9.2 KB

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