aria2/src/SegmentMan.cc

222 lines
5.8 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 "SegmentMan.h"
#include "DlAbortEx.h"
#include "Util.h"
#include "File.h"
#include "message.h"
2006-02-22 11:18:47 +00:00
#include "prefs.h"
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
#include "LogFactory.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
2006-02-17 13:35:04 +00:00
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
SegmentMan::SegmentMan():totalSize(0),isSplittable(true),downloadStarted(false),dir(".") {
logger = LogFactory::getInstance();
}
2006-02-17 13:35:04 +00:00
SegmentMan::~SegmentMan() {}
void SegmentMan::unregisterId(int cuid) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
if((*itr).cuid == cuid) {
cuid = 0;
}
}
}
bool SegmentMan::getSegment(Segment& seg, int cuid) {
//Segment s = { 0, 0, 0, false };
if(segments.empty()) {
logger->debug(string("assign new segment { sp = 0, ep = "+(totalSize == 0 ? "0" : Util::llitos(totalSize-1))+" } to cuid "+Util::llitos(cuid)).c_str());
2006-02-17 13:35:04 +00:00
//seg = { cuid, 0, totalSize == 0 ? 0 : totalSize-1, 0, false };
seg.cuid = cuid;
seg.sp = 0;
seg.ep = totalSize == 0 ? 0 : totalSize-1;
seg.ds = 0;
2006-02-22 14:30:47 +00:00
seg.speed = 0;
2006-02-17 13:35:04 +00:00
seg.finish = false;
segments.push_back(seg);
return true;
}
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
if((*itr).cuid == cuid && !(*itr).finish) {
// logger->debug("return an existing segment { "
// "sp = "+Util::ulitos((*itr).sp)+", "+
// "ep = "+Util::ulitos((*itr).ep)+", "
// "ds = "+Util::ulitos((*itr).ds)+" } to "+
// "cuid "+Util::ulitos((*itr).cuid));
seg = *itr;
return true;
}
}
if(!isSplittable) {
return false;
}
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
Segment& s = *itr;
if(s.finish) {
continue;
}
if(s.cuid == 0) {
s.cuid = cuid;
seg = s;
return true;
}
}
2006-02-22 14:30:47 +00:00
return splitter->splitSegment(seg, cuid, segments);
2006-02-17 13:35:04 +00:00
}
void SegmentMan::updateSegment(const Segment& segment) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
if((*itr).cuid == segment.cuid &&
(*itr).sp == segment.sp &&
(*itr).ep == segment.ep) {
*itr = segment;
break;
}
}
}
2006-02-21 12:27:17 +00:00
bool SegmentMan::segmentFileExists() const {
2006-02-17 13:35:04 +00:00
if(!isSplittable) {
return false;
}
string segFilename = getSegmentFilePath();
File f(segFilename);
if(f.isFile()) {
logger->info(MSG_SEGMENT_FILE_EXISTS, segFilename.c_str());
return true;
} else {
logger->info(MSG_SEGMENT_FILE_DOES_NOT_EXIST, segFilename.c_str());
return false;
}
}
void SegmentMan::load() {
if(!isSplittable) {
return;
}
string segFilename = getSegmentFilePath();
logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "r+");
read(segFile);
fclose(segFile);
logger->info(MSG_LOADED_SEGMENT_FILE);
for(Segments::iterator itr = segments.begin(); itr != segments.end();
2006-02-17 13:35:04 +00:00
itr++) {
(*itr).cuid = 0;
}
}
2006-02-21 12:27:17 +00:00
void SegmentMan::save() const {
if(!isSplittable || totalSize == 0) {
2006-02-17 13:35:04 +00:00
return;
}
string segFilename = getSegmentFilePath();
logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "w");
2006-02-21 12:27:17 +00:00
if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) {
throw new DlAbortEx(strerror(errno));
}
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
if(fwrite(&*itr, sizeof(Segment), 1, segFile) < 1) {
throw new DlAbortEx(strerror(errno));
}
}
fclose(segFile);
logger->info(MSG_SAVED_SEGMENT_FILE);
}
2006-02-21 12:27:17 +00:00
FILE* SegmentMan::openSegFile(string segFilename, string mode) const {
2006-02-17 13:35:04 +00:00
FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
if(segFile == NULL) {
throw new DlAbortEx(strerror(errno));
}
return segFile;
}
void SegmentMan::read(FILE* file) {
assert(file != NULL);
2006-02-21 12:27:17 +00:00
if(fread(&totalSize, sizeof(totalSize), 1, file) < 1) {
throw new DlAbortEx(strerror(errno));
}
2006-02-17 13:35:04 +00:00
while(1) {
Segment seg;
if(fread(&seg, sizeof(Segment), 1, file) < 1) {
if(ferror(file)) {
throw new DlAbortEx(strerror(errno));
} else if(feof(file)) {
break;
}
}
segments.push_back(seg);
}
}
2006-02-21 12:27:17 +00:00
void SegmentMan::remove() const {
2006-02-17 13:35:04 +00:00
if(!isSplittable) {
return;
}
if(segmentFileExists()) {
File f(getSegmentFilePath());
f.remove();
}
}
2006-02-21 12:27:17 +00:00
bool SegmentMan::finished() const {
2006-02-17 13:35:04 +00:00
if(!downloadStarted || segments.size() == 0) {
return false;
}
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
if(!(*itr).finish) {
return false;
}
}
return true;
}
2006-02-21 12:27:17 +00:00
void SegmentMan::removeIfFinished() const {
2006-02-17 13:35:04 +00:00
if(finished()) {
remove();
}
}
2006-02-21 12:27:17 +00:00
long long int SegmentMan::getDownloadedSize() const {
2006-02-17 13:35:04 +00:00
long long int size = 0;
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
2006-02-17 13:35:04 +00:00
size += (*itr).ds;
}
return size;
}
2006-04-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add a readout of estimated remaining time to normal HTTP/FTP downloads: * src/ConsoleDownloadEngine.h (startup): New variable. (startupLength): New variable. (isStartupLengthSet): New variable. (avgSpeed): New variable. (eta): New variable. * src/ConsoleDownloadEngine.cc (sendStatistics): Added a readout of estimated remaining time. (initStatistics): Initialized newly added variables. (calculateStatistics): Calculate average speed and estimated remaining time. To decouple TorrentDownloadEngine from HttpResponseCommand: * src/TrackerDownloadCommand.h: Removed. * src/TrackerDownloadCommand.cc: Removed. * src/TrackerInitCommand.h: Removed. * src/TrackerInitCommand.cc: Removed. * src/TrackerUpdateCommand.h: Removed. * src/TrackerUpdateCommand.cc: Removed. * src/TrackerWatcherCommand.cc (execute): The construction of request url written in TrackerInitCommand was moved here. Do not create tracker request command if torretnMan->trackers != 0. * src/CompactTrackerResponseProcessor.h: New class. * src/CompactTrackerResponseProcessor.cc: New class. * src/message.h (MSG_TRACKER_WARNING_MESSAGE): Updated. * src/HttpResponseCommand.cc (createHttpDownloadCommand): Decoupled TorrentDownloadEngine from this. * src/SegmentMan.h (init): New function. * src/SegmentMan.cc (init): New function. * src/TorrentMan.h (responseProcessor): New variable. (trackers): New variable. (setTrackerResponseProcessor): New function. (getTrackerResponseProcessor): New function. (processTrackerResponse): New function. * src/TorrentMan.cc (Constructor): Initialized new variable trackers. (processTrackerResponse): New function. * src/main.cc (main): Use ByteArrayDiskWriter and CompactTrackerResponseProcessor. * src/TorrentDownloadEngine.cc (afterEachIteration): Call torrentMan-> processTrackerResponse(). * src/TorrentConsoleDownloadEngine.cc (printStatistics): Updated a readout. * src/TorrentDownloadEngine.cc (afterEachIteration): Added log message which indicates download has completed. * src/AbstractDiskWriter.cc (Destructor): fd >= 0, not fd >0 (closeFile): fd >= 0, not fd > 0 * src/main.cc (main): Added short cut for show-files. Added short cut for torrent-file. Added new command-line option listen-port. Updated i18n messages.
2006-04-18 17:06:17 +00:00
void SegmentMan::init() {
totalSize = 0;
isSplittable = false;
downloadStarted = false;
segments.clear();
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
diskWriter->closeFile();
2006-04-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add a readout of estimated remaining time to normal HTTP/FTP downloads: * src/ConsoleDownloadEngine.h (startup): New variable. (startupLength): New variable. (isStartupLengthSet): New variable. (avgSpeed): New variable. (eta): New variable. * src/ConsoleDownloadEngine.cc (sendStatistics): Added a readout of estimated remaining time. (initStatistics): Initialized newly added variables. (calculateStatistics): Calculate average speed and estimated remaining time. To decouple TorrentDownloadEngine from HttpResponseCommand: * src/TrackerDownloadCommand.h: Removed. * src/TrackerDownloadCommand.cc: Removed. * src/TrackerInitCommand.h: Removed. * src/TrackerInitCommand.cc: Removed. * src/TrackerUpdateCommand.h: Removed. * src/TrackerUpdateCommand.cc: Removed. * src/TrackerWatcherCommand.cc (execute): The construction of request url written in TrackerInitCommand was moved here. Do not create tracker request command if torretnMan->trackers != 0. * src/CompactTrackerResponseProcessor.h: New class. * src/CompactTrackerResponseProcessor.cc: New class. * src/message.h (MSG_TRACKER_WARNING_MESSAGE): Updated. * src/HttpResponseCommand.cc (createHttpDownloadCommand): Decoupled TorrentDownloadEngine from this. * src/SegmentMan.h (init): New function. * src/SegmentMan.cc (init): New function. * src/TorrentMan.h (responseProcessor): New variable. (trackers): New variable. (setTrackerResponseProcessor): New function. (getTrackerResponseProcessor): New function. (processTrackerResponse): New function. * src/TorrentMan.cc (Constructor): Initialized new variable trackers. (processTrackerResponse): New function. * src/main.cc (main): Use ByteArrayDiskWriter and CompactTrackerResponseProcessor. * src/TorrentDownloadEngine.cc (afterEachIteration): Call torrentMan-> processTrackerResponse(). * src/TorrentConsoleDownloadEngine.cc (printStatistics): Updated a readout. * src/TorrentDownloadEngine.cc (afterEachIteration): Added log message which indicates download has completed. * src/AbstractDiskWriter.cc (Destructor): fd >= 0, not fd >0 (closeFile): fd >= 0, not fd > 0 * src/main.cc (main): Added short cut for show-files. Added short cut for torrent-file. Added new command-line option listen-port. Updated i18n messages.
2006-04-18 17:06:17 +00:00
}