2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "SegmentMan.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2006-02-22 11:18:47 +00:00
|
|
|
#include "prefs.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PiecedSegment.h"
|
|
|
|
#include "GrowSegment.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "PeerStat.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "Piece.h"
|
2008-03-15 04:19:46 +00:00
|
|
|
#include <algorithm>
|
2008-08-13 16:13:54 +00:00
|
|
|
#include <cassert>
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentEntry::SegmentEntry(int32_t cuid, const SegmentHandle& segment):
|
|
|
|
cuid(cuid), segment(segment) {}
|
|
|
|
|
|
|
|
SegmentEntry::~SegmentEntry() {}
|
|
|
|
|
|
|
|
SegmentMan::SegmentMan(const Option* option,
|
|
|
|
const DownloadContextHandle& downloadContext,
|
|
|
|
const PieceStorageHandle& pieceStorage):
|
|
|
|
_option(option),
|
|
|
|
logger(LogFactory::getInstance()),
|
|
|
|
_downloadContext(downloadContext),
|
|
|
|
_pieceStorage(pieceStorage)
|
2007-01-28 14:18:35 +00:00
|
|
|
{}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentMan::~SegmentMan() {}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool SegmentMan::downloadFinished() const
|
|
|
|
{
|
|
|
|
if(_pieceStorage.isNull()) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
return _pieceStorage->downloadFinished();
|
2007-09-01 16:10:30 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::init()
|
|
|
|
{
|
|
|
|
// TODO Do we have to do something about DownloadContext and PieceStorage here?
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
uint64_t SegmentMan::getTotalLength() const
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
|
|
|
if(_pieceStorage.isNull()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return _pieceStorage->getTotalLength();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::setPieceStorage(const PieceStorageHandle& pieceStorage)
|
|
|
|
{
|
|
|
|
_pieceStorage = pieceStorage;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::setDownloadContext(const DownloadContextHandle& downloadContext)
|
|
|
|
{
|
|
|
|
_downloadContext = downloadContext;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentHandle SegmentMan::checkoutSegment(int32_t cuid,
|
|
|
|
const PieceHandle& piece)
|
|
|
|
{
|
|
|
|
if(piece.isNull()) {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Segment>();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
logger->debug("Attach segment#%d to CUID#%d.", piece->getIndex(), cuid);
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SegmentHandle segment;
|
2007-10-11 16:58:24 +00:00
|
|
|
if(piece->getLength() == 0) {
|
2008-04-20 00:50:22 +00:00
|
|
|
segment.reset(new GrowSegment(piece));
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
2008-04-20 00:50:22 +00:00
|
|
|
segment.reset(new PiecedSegment(_downloadContext->getPieceLength(), piece));
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
SegmentEntryHandle entry(new SegmentEntry(cuid, segment));
|
2007-10-11 16:58:24 +00:00
|
|
|
usedSegmentEntries.push_back(entry);
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
logger->debug("index=%d, length=%d, segmentLength=%d, writtenLength=%d",
|
2007-10-11 16:58:24 +00:00
|
|
|
segment->getIndex(),
|
|
|
|
segment->getLength(),
|
|
|
|
segment->getSegmentLength(),
|
|
|
|
segment->getWrittenLength());
|
2006-09-19 14:52:59 +00:00
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
2008-07-20 07:55:10 +00:00
|
|
|
SegmentEntryHandle SegmentMan::findSlowerSegmentEntry
|
|
|
|
(const PeerStatHandle& peerStat)
|
|
|
|
{
|
2008-03-08 10:33:56 +00:00
|
|
|
unsigned int speed = peerStat->getAvgDownloadSpeed()*0.8;
|
2008-04-20 00:50:22 +00:00
|
|
|
SegmentEntryHandle slowSegmentEntry;
|
2008-07-20 07:55:10 +00:00
|
|
|
int startupIdleTime = _option->getAsInt(PREF_STARTUP_IDLE_TIME);
|
|
|
|
for(std::deque<SharedHandle<SegmentEntry> >::const_iterator itr =
|
|
|
|
usedSegmentEntries.begin(); itr != usedSegmentEntries.end(); ++itr) {
|
|
|
|
const SharedHandle<SegmentEntry>& segmentEntry = *itr;
|
2006-09-21 06:56:54 +00:00
|
|
|
if(segmentEntry->cuid == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-20 07:55:10 +00:00
|
|
|
SharedHandle<PeerStat> p = getPeerStat(segmentEntry->cuid);
|
|
|
|
if(p.isNull()) {
|
|
|
|
// "p is null" means that it hasn't used DownloadCommand yet, i.e. waiting
|
|
|
|
// response from HTTP server after sending HTTP request.
|
|
|
|
p.reset(new PeerStat(segmentEntry->cuid));
|
|
|
|
registerPeerStat(p);
|
2006-09-21 06:56:54 +00:00
|
|
|
slowSegmentEntry = segmentEntry;
|
2008-07-20 07:55:10 +00:00
|
|
|
} else {
|
|
|
|
if(p->getCuid() == peerStat->getCuid() ||
|
|
|
|
(p->getStatus() == PeerStat::ACTIVE &&
|
|
|
|
!p->getDownloadStartTime().elapsed(startupIdleTime))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unsigned int pSpeed = p->calculateDownloadSpeed();
|
|
|
|
if(pSpeed < speed) {
|
|
|
|
speed = pSpeed;
|
|
|
|
slowSegmentEntry = segmentEntry;
|
|
|
|
}
|
2006-09-21 06:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return slowSegmentEntry;
|
|
|
|
}
|
|
|
|
|
2008-05-11 10:46:52 +00:00
|
|
|
void SegmentMan::getInFlightSegment(std::deque<SharedHandle<Segment> >& segments,
|
|
|
|
int32_t cuid)
|
2007-10-17 16:26:51 +00:00
|
|
|
{
|
|
|
|
for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
|
|
|
|
itr != usedSegmentEntries.end(); ++itr) {
|
|
|
|
const SegmentEntryHandle& segmentEntry = *itr;
|
|
|
|
if(segmentEntry->cuid == cuid) {
|
2008-05-11 10:46:52 +00:00
|
|
|
segments.push_back(segmentEntry->segment);
|
2007-10-17 16:26:51 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2007-10-17 16:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SegmentHandle SegmentMan::getSegment(int32_t cuid) {
|
2007-10-11 16:58:24 +00:00
|
|
|
PieceHandle piece = _pieceStorage->getMissingPiece();
|
|
|
|
if(piece.isNull()) {
|
2006-09-21 06:56:54 +00:00
|
|
|
PeerStatHandle myPeerStat = getPeerStat(cuid);
|
2007-10-11 16:58:24 +00:00
|
|
|
if(myPeerStat.isNull()) {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Segment>();
|
2006-09-21 06:56:54 +00:00
|
|
|
}
|
|
|
|
SegmentEntryHandle slowSegmentEntry = findSlowerSegmentEntry(myPeerStat);
|
|
|
|
if(slowSegmentEntry.get()) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->info(MSG_SEGMENT_FORWARDING,
|
2006-09-21 06:56:54 +00:00
|
|
|
slowSegmentEntry->cuid,
|
2007-10-11 16:58:24 +00:00
|
|
|
slowSegmentEntry->segment->getIndex(),
|
2006-09-21 06:56:54 +00:00
|
|
|
cuid);
|
|
|
|
PeerStatHandle slowPeerStat = getPeerStat(slowSegmentEntry->cuid);
|
|
|
|
slowPeerStat->requestIdle();
|
|
|
|
cancelSegment(slowSegmentEntry->cuid);
|
2008-08-13 16:13:54 +00:00
|
|
|
|
|
|
|
SharedHandle<Piece> piece =
|
|
|
|
_pieceStorage->getMissingPiece(slowSegmentEntry->segment->getIndex());
|
|
|
|
assert(!piece.isNull());
|
|
|
|
|
|
|
|
return checkoutSegment(cuid, piece);
|
2006-09-21 06:56:54 +00:00
|
|
|
} else {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Segment>();
|
2006-09-21 06:56:54 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
return checkoutSegment(cuid, piece);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
SegmentHandle SegmentMan::getSegment(int32_t cuid, size_t index) {
|
|
|
|
if(_downloadContext->getNumPieces() <= index) {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Segment>();
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
return checkoutSegment(cuid, _pieceStorage->getMissingPiece(index));
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
void SegmentMan::cancelSegment(int32_t cuid) {
|
2007-10-11 16:58:24 +00:00
|
|
|
for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
|
2007-10-17 16:26:51 +00:00
|
|
|
itr != usedSegmentEntries.end();) {
|
2007-10-11 16:58:24 +00:00
|
|
|
if((*itr)->cuid == cuid) {
|
|
|
|
_pieceStorage->cancelPiece((*itr)->segment->getPiece());
|
2007-10-17 16:26:51 +00:00
|
|
|
itr = usedSegmentEntries.erase(itr);
|
|
|
|
} else {
|
|
|
|
++itr;
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 16:37:25 +00:00
|
|
|
class FindSegmentEntry {
|
|
|
|
private:
|
|
|
|
SegmentHandle _segment;
|
|
|
|
public:
|
|
|
|
FindSegmentEntry(const SegmentHandle& segment):_segment(segment) {}
|
|
|
|
|
|
|
|
bool operator()(const SegmentEntryHandle& segmentEntry) const
|
|
|
|
{
|
|
|
|
return segmentEntry->segment->getIndex() == _segment->getIndex();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool SegmentMan::completeSegment(int32_t cuid, const SegmentHandle& segment) {
|
2007-10-11 16:58:24 +00:00
|
|
|
_pieceStorage->completePiece(segment->getPiece());
|
|
|
|
_pieceStorage->advertisePiece(cuid, segment->getPiece()->getIndex());
|
2008-02-08 15:53:45 +00:00
|
|
|
SegmentEntries::iterator itr = std::find_if(usedSegmentEntries.begin(),
|
|
|
|
usedSegmentEntries.end(),
|
|
|
|
FindSegmentEntry(segment));
|
2006-09-19 14:52:59 +00:00
|
|
|
if(itr == usedSegmentEntries.end()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
usedSegmentEntries.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
bool SegmentMan::hasSegment(size_t index) const {
|
2007-10-11 16:58:24 +00:00
|
|
|
return _pieceStorage->hasPiece(index);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
uint64_t SegmentMan::getDownloadLength() const {
|
2007-10-11 16:58:24 +00:00
|
|
|
if(_pieceStorage.isNull()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return _pieceStorage->getCompletedLength();
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-20 07:55:10 +00:00
|
|
|
class FindPeerStat {
|
|
|
|
public:
|
|
|
|
bool operator()(const SharedHandle<PeerStat>& peerStat, int32_t cuid) const
|
|
|
|
{
|
|
|
|
return peerStat->getCuid() < cuid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool SegmentMan::registerPeerStat(const SharedHandle<PeerStat>& peerStat)
|
|
|
|
{
|
|
|
|
std::deque<SharedHandle<PeerStat> >::iterator i =
|
|
|
|
std::lower_bound(peerStats.begin(), peerStats.end(),peerStat->getCuid(),
|
|
|
|
FindPeerStat());
|
|
|
|
if(i == peerStats.end() || (*i)->getCuid() != peerStat->getCuid()) {
|
|
|
|
peerStats.insert(i, peerStat);
|
|
|
|
return true ;
|
|
|
|
} else {
|
|
|
|
return false;
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
PeerStatHandle SegmentMan::getPeerStat(int32_t cuid) const
|
|
|
|
{
|
2008-07-20 07:55:10 +00:00
|
|
|
std::deque<SharedHandle<PeerStat> >::const_iterator i =
|
|
|
|
std::lower_bound(peerStats.begin(), peerStats.end(), cuid, FindPeerStat());
|
|
|
|
if(i != peerStats.end() && (*i)->getCuid() == cuid) {
|
|
|
|
return *i;
|
|
|
|
} else {
|
|
|
|
return SharedHandle<PeerStat>();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-04 17:06:47 +00:00
|
|
|
const std::deque<SharedHandle<PeerStat> >& SegmentMan::getPeerStats() const
|
|
|
|
{
|
|
|
|
return peerStats;
|
|
|
|
}
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
unsigned int SegmentMan::calculateDownloadSpeed() const {
|
|
|
|
unsigned int speed = 0;
|
2008-02-09 17:14:40 +00:00
|
|
|
for(std::deque<SharedHandle<PeerStat> >::const_iterator itr = peerStats.begin(); itr != peerStats.end(); itr++) {
|
2006-09-19 14:52:59 +00:00
|
|
|
const PeerStatHandle& peerStat = *itr;
|
|
|
|
if(peerStat->getStatus() == PeerStat::ACTIVE) {
|
|
|
|
speed += peerStat->calculateDownloadSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return speed;
|
2006-04-18 17:06:17 +00:00
|
|
|
}
|
2007-01-11 16:32:31 +00:00
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
size_t SegmentMan::countFreePieceFrom(size_t index) const
|
2007-10-17 16:26:51 +00:00
|
|
|
{
|
2008-03-08 10:33:56 +00:00
|
|
|
size_t numPieces = _downloadContext->getNumPieces();
|
|
|
|
for(size_t i = index; i < numPieces; ++i) {
|
2007-10-17 16:26:51 +00:00
|
|
|
if(_pieceStorage->hasPiece(i) || _pieceStorage->isPieceUsed(i)) {
|
|
|
|
return i-index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _downloadContext->getNumPieces()-index;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|