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 "DownloadCommand.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "DlRetryEx.h"
|
2006-08-07 16:05:00 +00:00
|
|
|
#include "DlAbortEx.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#include "HttpInitiateConnectionCommand.h"
|
|
|
|
#include "InitiateConnectionCommandFactory.h"
|
|
|
|
#include "message.h"
|
2006-08-07 16:05:00 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include <sys/time.h>
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-10-18 14:57:00 +00:00
|
|
|
DownloadCommand::DownloadCommand(int cuid,
|
|
|
|
const RequestHandle req,
|
|
|
|
DownloadEngine* e,
|
2006-07-19 17:07:45 +00:00
|
|
|
const SocketHandle& s):
|
|
|
|
AbstractCommand(cuid, req, e, s), lastSize(0) {
|
2006-09-19 14:52:59 +00:00
|
|
|
PeerStatHandle peerStat = PeerStatHandle(new PeerStat(cuid));
|
|
|
|
peerStat->downloadStart();
|
|
|
|
this->e->segmentMan->registerPeerStat(peerStat);
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
DownloadCommand::~DownloadCommand() {
|
|
|
|
PeerStatHandle peerStat = e->segmentMan->getPeerStat(cuid);
|
|
|
|
assert(peerStat.get());
|
|
|
|
peerStat->downloadStop();
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
bool DownloadCommand::executeInternal(Segment& segment) {
|
2006-09-21 14:37:15 +00:00
|
|
|
int maxSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
|
2006-09-19 14:52:59 +00:00
|
|
|
if(maxSpeedLimit > 0 &&
|
|
|
|
maxSpeedLimit < e->segmentMan->calculateDownloadSpeed()) {
|
|
|
|
usleep(1);
|
|
|
|
e->commands.push_back(this);
|
|
|
|
return false;
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
TransferEncoding* te = NULL;
|
|
|
|
if(transferEncoding.size()) {
|
|
|
|
te = getTransferEncoding(transferEncoding);
|
|
|
|
assert(te != NULL);
|
|
|
|
}
|
|
|
|
int bufSize = 4096;
|
|
|
|
char buf[bufSize];
|
|
|
|
socket->readData(buf, bufSize);
|
2006-09-19 14:52:59 +00:00
|
|
|
PeerStatHandle peerStat = e->segmentMan->getPeerStat(cuid);
|
|
|
|
assert(peerStat.get());
|
2006-02-17 13:35:04 +00:00
|
|
|
if(te != NULL) {
|
|
|
|
int infbufSize = 4096;
|
|
|
|
char infbuf[infbufSize];
|
|
|
|
te->inflate(infbuf, infbufSize, buf, bufSize);
|
2006-09-19 14:52:59 +00:00
|
|
|
e->segmentMan->diskWriter->writeData(infbuf, infbufSize,
|
|
|
|
segment.getPosition()+segment.writtenLength);
|
|
|
|
segment.writtenLength += infbufSize;
|
|
|
|
peerStat->updateDownloadLength(infbufSize);
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2006-09-19 14:52:59 +00:00
|
|
|
e->segmentMan->diskWriter->writeData(buf, bufSize,
|
|
|
|
segment.getPosition()+segment.writtenLength);
|
|
|
|
segment.writtenLength += bufSize;
|
|
|
|
peerStat->updateDownloadLength(bufSize);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-02-22 14:30:47 +00:00
|
|
|
// calculate downloading speed
|
2006-09-21 06:56:54 +00:00
|
|
|
if(peerStat->getDownloadStartTime().elapsed(e->option->getAsInt(PREF_STARTUP_IDLE_TIME))) {
|
2006-08-07 16:05:00 +00:00
|
|
|
int lowestLimit = e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT);
|
2006-09-19 14:52:59 +00:00
|
|
|
int nowSpeed = peerStat->calculateDownloadSpeed();
|
|
|
|
if(lowestLimit > 0 && nowSpeed <= lowestLimit) {
|
2006-08-07 16:05:00 +00:00
|
|
|
throw new DlAbortEx("CUID#%d - Too slow Downloading speed: %d <= %d(B/s)",
|
|
|
|
cuid,
|
2006-09-19 14:52:59 +00:00
|
|
|
nowSpeed,
|
2006-08-07 16:05:00 +00:00
|
|
|
lowestLimit);
|
|
|
|
}
|
2006-02-22 14:30:47 +00:00
|
|
|
}
|
2006-03-05 06:32:01 +00:00
|
|
|
if(e->segmentMan->totalSize != 0 && bufSize == 0) {
|
|
|
|
throw new DlRetryEx(EX_GOT_EOF);
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
if(te != NULL && te->finished()
|
2006-09-19 14:52:59 +00:00
|
|
|
|| te == NULL && segment.complete()
|
2006-03-05 06:32:01 +00:00
|
|
|
|| bufSize == 0) {
|
2006-02-17 13:35:04 +00:00
|
|
|
if(te != NULL) te->end();
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
|
2006-09-19 14:52:59 +00:00
|
|
|
e->segmentMan->completeSegment(cuid, segment);
|
2006-02-17 13:35:04 +00:00
|
|
|
// this unit is going to download another segment.
|
2006-09-19 14:52:59 +00:00
|
|
|
return prepareForNextSegment(segment);
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2006-09-19 14:52:59 +00:00
|
|
|
e->segmentMan->updateSegment(cuid, segment);
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
bool DownloadCommand::prepareForNextSegment(const Segment& currentSegment) {
|
|
|
|
if(e->segmentMan->finished()) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
|
|
|
// Merge segment with next segment, if segment.index+1 == nextSegment.index
|
|
|
|
Segment tempSegment = currentSegment;
|
|
|
|
while(1) {
|
|
|
|
Segment nextSegment;
|
|
|
|
if(e->segmentMan->getSegment(nextSegment, cuid, tempSegment.index+1)) {
|
|
|
|
if(nextSegment.writtenLength > 0) {
|
|
|
|
return prepareForRetry(0);
|
|
|
|
}
|
|
|
|
nextSegment.writtenLength = tempSegment.writtenLength-tempSegment.length;
|
|
|
|
if(nextSegment.complete()) {
|
|
|
|
e->segmentMan->completeSegment(cuid, nextSegment);
|
|
|
|
tempSegment = nextSegment;
|
|
|
|
} else {
|
|
|
|
e->segmentMan->updateSegment(cuid, nextSegment);
|
|
|
|
e->commands.push_back(this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prepareForRetry(0);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|