2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "AbstractCommand.h"
|
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "DlRetryEx.h"
|
|
|
|
#include "InitiateConnectionCommandFactory.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2006-02-18 05:13:21 +00:00
|
|
|
#include "SleepCommand.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include <sys/time.h>
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-03-31 14:46:48 +00:00
|
|
|
AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
|
2006-02-17 13:35:04 +00:00
|
|
|
Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) {
|
2006-02-21 12:27:17 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
if(s != NULL) {
|
|
|
|
socket = new Socket(*s);
|
2006-02-21 12:27:17 +00:00
|
|
|
setReadCheckSocket(socket);
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
|
|
|
socket = NULL;
|
|
|
|
}
|
|
|
|
this->checkPoint.tv_sec = 0;
|
|
|
|
this->checkPoint.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractCommand::~AbstractCommand() {
|
2006-02-21 12:27:17 +00:00
|
|
|
setReadCheckSocket(NULL);
|
|
|
|
setWriteCheckSocket(NULL);
|
2006-02-17 13:35:04 +00:00
|
|
|
if(socket != NULL) {
|
|
|
|
delete(socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCommand::updateCheckPoint() {
|
|
|
|
gettimeofday(&checkPoint, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractCommand::isTimeoutDetected() {
|
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if(checkPoint.tv_sec == 0 && checkPoint.tv_usec == 0) {
|
2006-02-21 12:27:17 +00:00
|
|
|
checkPoint = now;
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
long long int elapsed = Util::difftv(now, checkPoint);
|
2006-03-23 10:47:25 +00:00
|
|
|
if(elapsed >= e->option->getAsLLInt(PREF_TIMEOUT)*1000000) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractCommand::execute() {
|
|
|
|
try {
|
2006-02-21 12:27:17 +00:00
|
|
|
if(checkSocketIsReadable && !readCheckTarget->isReadable(0)
|
|
|
|
|| checkSocketIsWritable && !writeCheckTarget->isWritable(0)) {
|
2006-02-17 13:35:04 +00:00
|
|
|
if(isTimeoutDetected()) {
|
|
|
|
throw new DlRetryEx(EX_TIME_OUT);
|
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
updateCheckPoint();
|
|
|
|
Segment seg = { 0, 0, 0, false };
|
|
|
|
if(e->segmentMan->downloadStarted) {
|
|
|
|
// get segment information in order to set Range header.
|
|
|
|
if(!e->segmentMan->getSegment(seg, cuid)) {
|
|
|
|
// no segment available
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return executeInternal(seg);
|
|
|
|
} catch(DlAbortEx* err) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
|
2006-02-21 12:27:17 +00:00
|
|
|
onAbort(err);
|
2006-02-17 13:35:04 +00:00
|
|
|
delete(err);
|
|
|
|
req->resetUrl();
|
|
|
|
return true;
|
|
|
|
} catch(DlRetryEx* err) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
|
2006-02-21 12:27:17 +00:00
|
|
|
req->addTryCount();
|
2006-03-01 02:26:29 +00:00
|
|
|
bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
|
|
|
|
req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
|
|
|
|
if(isAbort) {
|
|
|
|
onAbort(err);
|
|
|
|
}
|
|
|
|
delete(err);
|
|
|
|
if(isAbort) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2006-02-21 14:00:58 +00:00
|
|
|
return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-18 05:13:21 +00:00
|
|
|
bool AbstractCommand::prepareForRetry(int wait) {
|
|
|
|
Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
|
|
|
|
if(wait == 0) {
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(command);
|
2006-02-18 05:13:21 +00:00
|
|
|
} else {
|
|
|
|
SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(scom);
|
2006-02-18 05:13:21 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-03-01 02:26:29 +00:00
|
|
|
void AbstractCommand::onAbort(Exception* ex) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->debug(MSG_UNREGISTER_CUID, cuid);
|
2006-03-01 02:26:29 +00:00
|
|
|
e->segmentMan->unregisterId(cuid);
|
2006-02-21 12:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCommand::setReadCheckSocket(Socket* socket) {
|
|
|
|
if(socket == NULL) {
|
|
|
|
if(checkSocketIsReadable) {
|
|
|
|
e->deleteSocketForReadCheck(readCheckTarget);
|
|
|
|
checkSocketIsReadable = false;
|
|
|
|
readCheckTarget = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(checkSocketIsReadable) {
|
|
|
|
if(readCheckTarget != socket) {
|
|
|
|
e->deleteSocketForReadCheck(readCheckTarget);
|
|
|
|
e->addSocketForReadCheck(socket);
|
|
|
|
readCheckTarget = socket;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e->addSocketForReadCheck(socket);
|
|
|
|
checkSocketIsReadable = true;
|
|
|
|
readCheckTarget = socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCommand::setWriteCheckSocket(Socket* socket) {
|
|
|
|
if(socket == NULL) {
|
|
|
|
if(checkSocketIsWritable) {
|
|
|
|
e->deleteSocketForWriteCheck(writeCheckTarget);
|
|
|
|
checkSocketIsWritable = false;
|
|
|
|
writeCheckTarget = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(checkSocketIsWritable) {
|
|
|
|
if(writeCheckTarget != socket) {
|
|
|
|
e->deleteSocketForWriteCheck(writeCheckTarget);
|
|
|
|
e->addSocketForWriteCheck(socket);
|
|
|
|
writeCheckTarget = socket;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e->addSocketForWriteCheck(socket);
|
|
|
|
checkSocketIsWritable = true;
|
|
|
|
writeCheckTarget = socket;
|
|
|
|
}
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|