aria2/src/DownloadCommand.cc

104 lines
3.1 KiB
C++
Raw Normal View History

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 "DownloadCommand.h"
#include <sys/time.h>
#include "Util.h"
#include "DlRetryEx.h"
#include "HttpInitiateConnectionCommand.h"
#include "InitiateConnectionCommandFactory.h"
#include "message.h"
DownloadCommand::DownloadCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s), lastSize(0) {
2006-02-22 14:30:47 +00:00
sw.tv_sec = 0;
sw.tv_usec = 0;
}
2006-02-17 13:35:04 +00:00
DownloadCommand::~DownloadCommand() {}
bool DownloadCommand::executeInternal(Segment seg) {
TransferEncoding* te = NULL;
if(transferEncoding.size()) {
te = getTransferEncoding(transferEncoding);
assert(te != NULL);
}
int bufSize = 4096;
char buf[bufSize];
socket->readData(buf, bufSize);
if(te != NULL) {
int infbufSize = 4096;
char infbuf[infbufSize];
te->inflate(infbuf, infbufSize, buf, bufSize);
To add TrackerUpdateCommand with which replaces CompactTrackerResponseProcessor: * src/TrackerWatcherCommand.h (req): Removed. * src/TrackerWatcherCommand.cc (execute): Send a request to tracker if the number of peer connections are less than 30. * src/ByteArrayDiskWriter.h (readData): Implemented. * src/SegmentMan.h (diskWriter): New function. * src/SegmentMan.cc (init): Added a call to diskWriter->closeFile() * src/main.cc : Removed #include "CompactTrackerResponseProcessor.h" (main): Use TrackerUpdateCommand. * src/TorrentMan.h (CompactTrackerResponseProcessor): Removed. (req): New variable. (setTrackerResponseProcessor): Removed. (getTrackerResponseProcessor): Removed. (processTrackerResponse): Removed. * src/DownloadEngine.h (diskWriter): Removed. * src/TorrentDownloadEngine.cc (afterEachIteration): Removed a call to torrentMan->processTrackerResponse(). To add Util::expandBuffer: * src/ByteArrayDiskWriter.h (expandBuffer): Removed. * src/ByteArrayDiskWriter.cc (writeData): Use Util::expandBuffer(). * src/Util.h (expandBuffer): New function. To fix the bug that causes segmentation fault when "-l ." is specified in command-line option: * src/SimpleLogger.h (SimpleLogger): Removed "filename" argument. (openFile): New function. (closeFile): New function. * src/SimpleLogger.cc (SimpleLogger): Removed fopen. (~SimpleLogger): Call closeFile(); * src/LogFactory.cc (getInstance): Added a call to slogger->openFile(). * src/main.cc (main): Added a check to see logger is configured properly. To enable HTTP authentication without specifying "--http-auth-scheme" * src/prefs.h (PREF_HTTP_AUTH_ENABLED): New definition. * src/HttpConnection.cc (createRequest): Send Authorization header if PREF_HTTP_AUTH_ENABLED == V_TRUE. * src/main.cc (main): Preset PREF_HTTP_AUTH_SCHEME to V_TRUE If "--http-user" is specified, set PREF_HTTP_AUTH_ENABLED to V_TRUE * src/Peer.cc (shouldChoke): Updated algorithm. * src/message.h (EX_AUTH_FAILED): New definition. (EX_FILE_OPEN): New definition. * src/HttpResponseCommand.cc (checkResponse): Throw DlAbortEx if status == 401. (handleDefaultEncoding): Added a call to diskWriter->initAndOpenFile() if req->isTorrent == true. * src/main.cc (handler): Removed the check to see e->diskWriter != NULL (torrentHandler): Removed the check to see diskAdaptor != NULL. * src/AbstractDiskWriter.cc (openExistingFile): Updated messsage. (createFile): Updated message.
2006-04-19 17:23:58 +00:00
e->segmentMan->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds);
seg.ds += infbufSize;
2006-02-17 13:35:04 +00:00
} else {
To add TrackerUpdateCommand with which replaces CompactTrackerResponseProcessor: * src/TrackerWatcherCommand.h (req): Removed. * src/TrackerWatcherCommand.cc (execute): Send a request to tracker if the number of peer connections are less than 30. * src/ByteArrayDiskWriter.h (readData): Implemented. * src/SegmentMan.h (diskWriter): New function. * src/SegmentMan.cc (init): Added a call to diskWriter->closeFile() * src/main.cc : Removed #include "CompactTrackerResponseProcessor.h" (main): Use TrackerUpdateCommand. * src/TorrentMan.h (CompactTrackerResponseProcessor): Removed. (req): New variable. (setTrackerResponseProcessor): Removed. (getTrackerResponseProcessor): Removed. (processTrackerResponse): Removed. * src/DownloadEngine.h (diskWriter): Removed. * src/TorrentDownloadEngine.cc (afterEachIteration): Removed a call to torrentMan->processTrackerResponse(). To add Util::expandBuffer: * src/ByteArrayDiskWriter.h (expandBuffer): Removed. * src/ByteArrayDiskWriter.cc (writeData): Use Util::expandBuffer(). * src/Util.h (expandBuffer): New function. To fix the bug that causes segmentation fault when "-l ." is specified in command-line option: * src/SimpleLogger.h (SimpleLogger): Removed "filename" argument. (openFile): New function. (closeFile): New function. * src/SimpleLogger.cc (SimpleLogger): Removed fopen. (~SimpleLogger): Call closeFile(); * src/LogFactory.cc (getInstance): Added a call to slogger->openFile(). * src/main.cc (main): Added a check to see logger is configured properly. To enable HTTP authentication without specifying "--http-auth-scheme" * src/prefs.h (PREF_HTTP_AUTH_ENABLED): New definition. * src/HttpConnection.cc (createRequest): Send Authorization header if PREF_HTTP_AUTH_ENABLED == V_TRUE. * src/main.cc (main): Preset PREF_HTTP_AUTH_SCHEME to V_TRUE If "--http-user" is specified, set PREF_HTTP_AUTH_ENABLED to V_TRUE * src/Peer.cc (shouldChoke): Updated algorithm. * src/message.h (EX_AUTH_FAILED): New definition. (EX_FILE_OPEN): New definition. * src/HttpResponseCommand.cc (checkResponse): Throw DlAbortEx if status == 401. (handleDefaultEncoding): Added a call to diskWriter->initAndOpenFile() if req->isTorrent == true. * src/main.cc (handler): Removed the check to see e->diskWriter != NULL (torrentHandler): Removed the check to see diskAdaptor != NULL. * src/AbstractDiskWriter.cc (openExistingFile): Updated messsage. (createFile): Updated message.
2006-04-19 17:23:58 +00:00
e->segmentMan->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds);
seg.ds += bufSize;
2006-02-17 13:35:04 +00:00
}
2006-02-22 14:30:47 +00:00
// calculate downloading speed
struct timeval now;
gettimeofday(&now, NULL);
if(sw.tv_sec == 0 && sw.tv_usec == 0) {
sw = now;
lastSize = seg.ds;
} else {
long long int diff = Util::difftv(now, sw);
if(diff >= 1000000) {
seg.speed = (int)((seg.ds-lastSize)/(diff/1000000.0));
sw = now;
lastSize = seg.ds;
}
}
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()
|| te == NULL && seg.ds >= seg.ep-seg.sp+1
2006-03-05 06:32:01 +00:00
|| bufSize == 0) {
2006-02-17 13:35:04 +00:00
if(te != NULL) te->end();
To add LogFactory which creates singleton logger: * src/LogFactory.h: New class. * src/LogFactory.cc: New class. * src/Command.h (logger): New variable. (Constructor): Use LogFactory. * src/AbstractCommand.cc: Use Command::logger * src/PeerConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SegmentSplitter.h : Made logger protected. * src/SegmentSplitter.cc (Constructor): Use LogFactory. * src/SegmentMan.cc (Constructor): Use LogFactory. * src/DownloadEngine.h : Made logger protected. * src/DownloadEngine.cc (Constructor): Use LogFactory. * src/PeerInteractionCommand.cc : Use Command::logger. * src/HttpResponseCommand.cc : Use Command::logger. * src/SegmentMan.h : Made logger private. * src/TorrentMan.h : Made logger private. * src/TorrentMan.cc : Use LogFactory. * src/FtpNegotiateCommand.cc : Use Command::logger. * src/HttpConnection.h (Constructor): Deleted the argument logger. * src/HttpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/FtpConnection.h (Constructor): Deleted the argument logger. * src/FtpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/DownloadCommand.cc : Use Command::logger. * src/PeerAbstractCommand.cc : Use Command::logger. * src/PeerListenCommand.cc : Use Command::logger. * src/PeerInitiateConnectionCommand.cc : Use Command::logger. * src/HttpInitiateConnectionCommand.cc : Use Command::logger. * src/FtpInitiateConnectionCommand.cc : Use Command::logger. * src/TrackerWatcherCommand.cc : Use Command::logger. * src/TrackerUpdateCommand.cc : Use Command::logger. * src/TrackerDownloadCommand.cc : Use Command::logger. * src/RequestSlotMan.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SendMessageQueue.h (Constructor): Deleted the argument logger. * src/SendMessageQueue.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/main.cc (main): Use LogFactory. * src/DiskAdaptor.h (logger): New variable. * src/DiskAdaptor.cc (Constructor): Use LogFactory. * src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
2006-04-17 16:17:20 +00:00
logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
2006-02-17 13:35:04 +00:00
seg.ds = seg.ep-seg.sp+1;
seg.finish = true;
e->segmentMan->updateSegment(seg);
// this unit is going to download another segment.
return prepareForNextSegment();
} else {
e->segmentMan->updateSegment(seg);
e->commands.push(this);
return false;
}
}
bool DownloadCommand::prepareForRetry(int wait) {
//req->resetUrl();
return AbstractCommand::prepareForRetry(wait);
2006-02-17 13:35:04 +00:00
}
bool DownloadCommand::prepareForNextSegment() {
req->resetUrl();
if(!e->segmentMan->finished()) {
return AbstractCommand::prepareForRetry(0);
2006-02-17 13:35:04 +00:00
} else {
return true;
}
}