From e199bf88986ce441c3383e3c00ce4864a77023b2 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 19 Apr 2006 17:23:58 +0000 Subject: [PATCH] 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. --- ChangeLog | 63 +++++++++++++++++++++++++++++ src/AbstractDiskWriter.cc | 17 ++++---- src/ByteArrayDiskWriter.cc | 22 ++++++---- src/ByteArrayDiskWriter.h | 5 +-- src/ConsoleDownloadEngine.cc | 2 +- src/DownloadCommand.cc | 4 +- src/DownloadEngine.h | 2 - src/FtpInitiateConnectionCommand.cc | 4 +- src/HttpConnection.cc | 10 +++-- src/HttpResponseCommand.cc | 20 +++++---- src/LogFactory.cc | 6 ++- src/Makefile.am | 2 +- src/Makefile.in | 6 +-- src/Peer.cc | 5 +-- src/SegmentMan.cc | 1 + src/SegmentMan.h | 3 ++ src/SimpleLogger.cc | 20 +++++++-- src/SimpleLogger.h | 4 +- src/TorrentDownloadEngine.cc | 1 - src/TorrentMan.cc | 7 ---- src/TorrentMan.h | 14 ++----- src/TrackerWatcherCommand.cc | 26 ++++++------ src/TrackerWatcherCommand.h | 4 +- src/Util.cc | 8 ++++ src/Util.h | 2 + src/main.cc | 39 ++++++++++-------- src/message.h | 4 ++ src/prefs.h | 2 + 28 files changed, 197 insertions(+), 106 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3c12fe48..44569268 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,66 @@ +2006-04-20 Tatsuhiro Tsujikawa + + 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 Tatsuhiro Tsujikawa To add a readout of estimated remaining time to normal HTTP/FTP diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index b5199587..4d67ecb2 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -20,14 +20,15 @@ */ /* copyright --> */ #include "AbstractDiskWriter.h" +#include "DlAbortEx.h" +#include "File.h" +#include "Util.h" +#include "message.h" #include #include #include #include #include -#include "DlAbortEx.h" -#include "File.h" -#include "Util.h" AbstractDiskWriter::AbstractDiskWriter():fd(0) { #ifdef ENABLE_SHA1DIGEST @@ -36,9 +37,7 @@ AbstractDiskWriter::AbstractDiskWriter():fd(0) { } AbstractDiskWriter::~AbstractDiskWriter() { - if(fd >= 0) { - close(fd); - } + closeFile(); #ifdef ENABLE_SHA1DIGEST sha1DigestFree(ctx); #endif // ENABLE_SHA1DIGEST @@ -63,11 +62,11 @@ void AbstractDiskWriter::closeFile() { void AbstractDiskWriter::openExistingFile(string filename) { File f(filename); if(!f.isFile()) { - throw new DlAbortEx(strerror(errno)); + throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), "file not found"); } if((fd = open(filename.c_str(), O_RDWR, S_IRUSR|S_IWUSR)) < 0) { - throw new DlAbortEx(strerror(errno)); + throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } } @@ -78,7 +77,7 @@ void AbstractDiskWriter::createFile(string filename, int addFlags) { // filename = "index.html"; // } if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|addFlags, S_IRUSR|S_IWUSR)) < 0) { - throw new DlAbortEx(strerror(errno)); + throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } } diff --git a/src/ByteArrayDiskWriter.cc b/src/ByteArrayDiskWriter.cc index dfc94ede..e51d500b 100644 --- a/src/ByteArrayDiskWriter.cc +++ b/src/ByteArrayDiskWriter.cc @@ -20,6 +20,7 @@ */ /* copyright --> */ #include "ByteArrayDiskWriter.h" +#include "Util.h" ByteArrayDiskWriter::ByteArrayDiskWriter():buf(NULL) { } @@ -65,16 +66,23 @@ void ByteArrayDiskWriter::openExistingFile(string filename) { void ByteArrayDiskWriter::writeData(const char* data, int dataLength, long long int position) { if(bufLength+dataLength >= maxBufLength) { - expandBuffer(bufLength+dataLength); + maxBufLength = Util::expandBuffer(&buf, bufLength, bufLength+dataLength); } memcpy(buf+bufLength, data, dataLength); bufLength += dataLength; } -void ByteArrayDiskWriter::expandBuffer(int newSize) { - char* newbuf = new char[newSize]; - memcpy(newbuf, buf, bufLength); - delete [] buf; - buf = newbuf; - maxBufLength = newSize; +int ByteArrayDiskWriter::readData(char* data, int len, long long int position) { + if(position >= bufLength) { + return 0; + } + int readLength; + if(position+len <= bufLength) { + readLength = len; + } else { + readLength = bufLength-position; + } + memcpy(data, buf+position, readLength); + return readLength; } + diff --git a/src/ByteArrayDiskWriter.h b/src/ByteArrayDiskWriter.h index c5abe81c..ee522e4f 100644 --- a/src/ByteArrayDiskWriter.h +++ b/src/ByteArrayDiskWriter.h @@ -32,12 +32,10 @@ private: void init(); void clear(); - void expandBuffer(int newSize); public: ByteArrayDiskWriter(); virtual ~ByteArrayDiskWriter(); - virtual void initAndOpenFile(string filename); virtual void openFile(const string& filename); @@ -48,8 +46,7 @@ public: // position is ignored virtual void writeData(const char* data, int len, long long int position = 0); - // not implemented yet - virtual int readData(char* data, int len, long long int position) { return 0; } + virtual int readData(char* data, int len, long long int position); // not implemented yet virtual string sha1Sum(long long int offset, long long int length) { return ""; } diff --git a/src/ConsoleDownloadEngine.cc b/src/ConsoleDownloadEngine.cc index 3ceba824..24941553 100644 --- a/src/ConsoleDownloadEngine.cc +++ b/src/ConsoleDownloadEngine.cc @@ -82,7 +82,7 @@ void ConsoleDownloadEngine::calculateStatistics() { } void ConsoleDownloadEngine::onEndOfRun() { - diskWriter->closeFile(); + segmentMan->diskWriter->closeFile(); if(segmentMan->finished()) { segmentMan->remove(); } else { diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index 2a2a97d7..fc9c396e 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -47,10 +47,10 @@ bool DownloadCommand::executeInternal(Segment seg) { int infbufSize = 4096; char infbuf[infbufSize]; te->inflate(infbuf, infbufSize, buf, bufSize); - e->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds); + e->segmentMan->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds); seg.ds += infbufSize; } else { - e->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds); + e->segmentMan->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds); seg.ds += bufSize; } // calculate downloading speed diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 50c81518..995adaf9 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -29,7 +29,6 @@ #include "SegmentMan.h" #include "common.h" #include "Logger.h" -#include "DiskWriter.h" #include "Option.h" #include @@ -57,7 +56,6 @@ public: bool noWait; Commands commands; SegmentMan* segmentMan; - DiskWriter* diskWriter; const Option* option; DownloadEngine(); diff --git a/src/FtpInitiateConnectionCommand.cc b/src/FtpInitiateConnectionCommand.cc index 70373ec1..e527a78a 100644 --- a/src/FtpInitiateConnectionCommand.cc +++ b/src/FtpInitiateConnectionCommand.cc @@ -37,10 +37,10 @@ bool FtpInitiateConnectionCommand::executeInternal(Segment segment) { bool segFileExists = e->segmentMan->segmentFileExists(); if(segFileExists) { e->segmentMan->load(); - e->diskWriter->openExistingFile(e->segmentMan->getFilePath()); + e->segmentMan->diskWriter->openExistingFile(e->segmentMan->getFilePath()); e->segmentMan->downloadStarted = true; } else { - e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); + e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); } } diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index 3338b06a..8fe6cc9a 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -83,10 +83,12 @@ string HttpConnection::createRequest(const Segment& segment) const { request += "Proxy-Connection: close\r\n"; request += getProxyAuthString(); } - if(option->get(PREF_HTTP_AUTH_SCHEME) == V_BASIC) { - request += "Authorization: Basic "+ - Base64::encode(option->get(PREF_HTTP_USER)+":"+ - option->get(PREF_HTTP_PASSWD))+"\r\n"; + if(option->get(PREF_HTTP_AUTH_ENABLED) == V_TRUE) { + if(option->get(PREF_HTTP_AUTH_SCHEME) == V_BASIC) { + request += "Authorization: Basic "+ + Base64::encode(option->get(PREF_HTTP_USER)+":"+ + option->get(PREF_HTTP_PASSWD))+"\r\n"; + } } if(req->getPreviousUrl().size()) { request += "Referer: "+req->getPreviousUrl()+"\r\n"; diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index dd27ab57..f3dcb0e7 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -74,11 +74,14 @@ bool HttpResponseCommand::executeInternal(Segment seg) { } void HttpResponseCommand::checkResponse(int status, const Segment& segment) { - if(!(status < 400 && status >= 300 || - (segment.sp+segment.ds == 0 && status == 200) - || (segment.sp+segment.ds > 0 && status == 206))) { - throw new DlRetryEx(EX_BAD_STATUS, status); - } + if(status == 401) { + throw new DlAbortEx(EX_AUTH_FAILED); + } + if(!(300 <= status && status < 400 || + (segment.sp+segment.ds == 0 && status == 200) + || (segment.sp+segment.ds > 0 && status == 206))) { + throw new DlRetryEx(EX_BAD_STATUS, status); + } } bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers) { @@ -95,6 +98,7 @@ bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) { e->segmentMan->totalSize = size; e->segmentMan->isSplittable = false; e->segmentMan->downloadStarted = true; + e->segmentMan->diskWriter->initAndOpenFile("/tmp/aria2"+Util::itos(getpid())); createHttpDownloadCommand(); return true; } @@ -109,14 +113,14 @@ bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) { e->segmentMan->downloadStarted = true; if(segFileExists) { e->segmentMan->load(); - e->diskWriter->openExistingFile(e->segmentMan->getFilePath()); + e->segmentMan->diskWriter->openExistingFile(e->segmentMan->getFilePath()); // send request again to the server with Range header return prepareForRetry(0); } else { e->segmentMan->totalSize = size; Segment seg; e->segmentMan->getSegment(seg, cuid); - e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); + e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); createHttpDownloadCommand(); return true; } @@ -130,7 +134,7 @@ bool HttpResponseCommand::handleOtherEncoding(string transferEncoding, const Htt e->segmentMan->totalSize = 0; Segment seg; e->segmentMan->getSegment(seg, cuid); - e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); + e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath()); createHttpDownloadCommand(transferEncoding); return true; } diff --git a/src/LogFactory.cc b/src/LogFactory.cc index 87d4b0de..6ce7b9d5 100644 --- a/src/LogFactory.cc +++ b/src/LogFactory.cc @@ -27,11 +27,13 @@ Logger* LogFactory::logger = NULL; Logger* LogFactory::getInstance() { if(logger == NULL) { + SimpleLogger* slogger = new SimpleLogger(); if(filename.empty()) { - logger = new SimpleLogger("/dev/null"); + slogger->openFile("/dev/null"); } else { - logger = new SimpleLogger(filename); + slogger->openFile(filename); } + logger = slogger; } return logger; } diff --git a/src/Makefile.am b/src/Makefile.am index b599564c..06853374 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -83,7 +83,7 @@ SRCS = Socket.cc Socket.h\ MultiDiskAdaptor.cc MultiDiskAdaptor.h\ FileEntry.h\ LogFactory.cc LogFactory.h\ - CompactTrackerResponseProcessor.cc CompactTrackerResponseProcessor.h\ + TrackerUpdateCommand.cc TrackerUpdateCommand.h\ ByteArrayDiskWriter.cc ByteArrayDiskWriter.h noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) diff --git a/src/Makefile.in b/src/Makefile.in index b7bca333..860e76ee 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -99,7 +99,7 @@ am__objects_1 = Socket.$(OBJEXT) SocketCore.$(OBJEXT) \ SendMessageQueue.$(OBJEXT) MultiDiskWriter.$(OBJEXT) \ DiskAdaptor.$(OBJEXT) CopyDiskAdaptor.$(OBJEXT) \ DirectDiskAdaptor.$(OBJEXT) MultiDiskAdaptor.$(OBJEXT) \ - LogFactory.$(OBJEXT) CompactTrackerResponseProcessor.$(OBJEXT) \ + LogFactory.$(OBJEXT) TrackerUpdateCommand.$(OBJEXT) \ ByteArrayDiskWriter.$(OBJEXT) am_libaria2c_a_OBJECTS = $(am__objects_1) libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS) @@ -333,7 +333,7 @@ SRCS = Socket.cc Socket.h\ MultiDiskAdaptor.cc MultiDiskAdaptor.h\ FileEntry.h\ LogFactory.cc LogFactory.h\ - CompactTrackerResponseProcessor.cc CompactTrackerResponseProcessor.h\ + TrackerUpdateCommand.cc TrackerUpdateCommand.h\ ByteArrayDiskWriter.cc ByteArrayDiskWriter.h noinst_LIBRARIES = libaria2c.a @@ -426,7 +426,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldMan.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ByteArrayDiskWriter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChunkedEncoding.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CompactTrackerResponseProcessor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConsoleDownloadEngine.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBox.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CopyDiskAdaptor.Po@am__quote@ @@ -488,6 +487,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentConsoleDownloadEngine.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentDownloadEngine.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentMan.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TrackerUpdateCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TrackerWatcherCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Util.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ diff --git a/src/Peer.cc b/src/Peer.cc index f9b8fbe5..cd68c87d 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -34,10 +34,7 @@ void Peer::updateBitfield(int index, int operation) { #define THRESHOLD 1024*1024*2 bool Peer::shouldChoke() const { - if(bitfield->countBlock()*0.7 < bitfield->countMissingBlock()) { - return false; - } - if(peerDownload <= pieceLength*10) { + if(bitfield->countBlock()*0.95 < bitfield->countMissingBlock()) { return false; } // we are always optimistic. diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc index 886b05a8..2087e717 100644 --- a/src/SegmentMan.cc +++ b/src/SegmentMan.cc @@ -217,4 +217,5 @@ void SegmentMan::init() { isSplittable = false; downloadStarted = false; segments.clear(); + diskWriter->closeFile(); } diff --git a/src/SegmentMan.h b/src/SegmentMan.h index 139f6e8e..6f1c04dd 100644 --- a/src/SegmentMan.h +++ b/src/SegmentMan.h @@ -27,6 +27,7 @@ #include "Segment.h" #include "Option.h" #include "SegmentSplitter.h" +#include "DiskWriter.h" using namespace std; @@ -85,11 +86,13 @@ public: const Option* option; SegmentSplitter* splitter; + DiskWriter* diskWriter; SegmentMan(); ~SegmentMan(); // Initializes totalSize, isSplittable, downloadStarted. + // Clears command queue. Also, closes diskWriter. void init(); /** diff --git a/src/SimpleLogger.cc b/src/SimpleLogger.cc index e807d6ec..f09e456e 100644 --- a/src/SimpleLogger.cc +++ b/src/SimpleLogger.cc @@ -21,9 +21,12 @@ /* copyright --> */ #include "SimpleLogger.h" #include "Util.h" +#include "DlAbortEx.h" +#include "message.h" #include #include #include +#include #define WRITE_LOG(LEVEL, MSG) \ va_list ap;\ @@ -37,20 +40,29 @@ va_start(ap, EX);\ writeLog(Logger::LEVEL, MSG, ap, EX);\ va_end(ap); -SimpleLogger::SimpleLogger(string filename) { - file = fopen(filename.c_str(), "a"); -} +SimpleLogger::SimpleLogger():file(NULL) {} SimpleLogger::SimpleLogger(FILE* logfile) { file = logfile; } SimpleLogger::~SimpleLogger() { + closeFile(); +} + +void SimpleLogger::openFile(const string& filename) { + file = fopen(filename.c_str(), "a"); + if(file == NULL) { + throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); + } +} + +void SimpleLogger::closeFile() { if(file != NULL) { fclose(file); } } - + void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e) const { string levelStr; diff --git a/src/SimpleLogger.h b/src/SimpleLogger.h index 517ad26b..82c0cf4c 100644 --- a/src/SimpleLogger.h +++ b/src/SimpleLogger.h @@ -29,10 +29,12 @@ private: void writeLog(int level, const char* msg, va_list ap, Exception* e = NULL) const; FILE* file; public: - SimpleLogger(string filename); + SimpleLogger(); SimpleLogger(FILE* logfile); ~SimpleLogger(); + void openFile(const string& filename); + void closeFile(); void debug(const char* msg, ...) const; void debug(const char* msg, Exception* ex, ...) const; void info(const char* msg, ...) const; diff --git a/src/TorrentDownloadEngine.cc b/src/TorrentDownloadEngine.cc index 6e5b9cbb..5c837f50 100644 --- a/src/TorrentDownloadEngine.cc +++ b/src/TorrentDownloadEngine.cc @@ -41,5 +41,4 @@ void TorrentDownloadEngine::afterEachIteration() { filenameFixed = true; } } - torrentMan->processTrackerResponse(); } diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc index 7cdd647c..47a00864 100644 --- a/src/TorrentMan.cc +++ b/src/TorrentMan.cc @@ -567,10 +567,3 @@ void TorrentMan::onDownloadComplete() { finishSelectiveDownloadingMode(); } } - -void TorrentMan::processTrackerResponse() { - if(responseProcessor->isFeeded()) { - responseProcessor->execute(); - responseProcessor->resetTrackerResponse(); - } -} diff --git a/src/TorrentMan.h b/src/TorrentMan.h index 32118028..c6bd346b 100644 --- a/src/TorrentMan.h +++ b/src/TorrentMan.h @@ -32,7 +32,7 @@ #include "Option.h" #include "FileEntry.h" #include "DiskAdaptor.h" -#include "CompactTrackerResponseProcessor.h" +#include "Request.h" #include #include #include @@ -75,7 +75,6 @@ private: UsedPieces usedPieces; bool setupComplete; const Logger* logger; - CompactTrackerResponseProcessor* responseProcessor; FILE* openSegFile(string segFilename, string mode) const; void read(FILE* file); @@ -101,6 +100,8 @@ public: int connections; // The number of tracker request command currently in the command queue. int trackers; + // tracker request + Request* req; public: TorrentMan(); ~TorrentMan(); @@ -231,15 +232,6 @@ public: void onDownloadComplete(); - void setTrackerResponseProcessor(CompactTrackerResponseProcessor* proc) { - this->responseProcessor = proc; - } - CompactTrackerResponseProcessor* getTrackerResponseProcessor() const { - return this->responseProcessor; - } - - void processTrackerResponse(); - enum FILE_MODE { SINGLE, MULTI diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 7ff43ee4..aaeb5258 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -24,30 +24,30 @@ #include "InitiateConnectionCommandFactory.h" #include "Util.h" -TrackerWatcherCommand::TrackerWatcherCommand(int cuid, Request* req, +TrackerWatcherCommand::TrackerWatcherCommand(int cuid, TorrentDownloadEngine* e): - Command(cuid), req(req), e(e) { + Command(cuid), e(e) { } TrackerWatcherCommand::~TrackerWatcherCommand() {} bool TrackerWatcherCommand::execute() { - if(e->torrentMan->trackers == 0) { - req->resetTryCount(); + if(e->torrentMan->trackers == 0 && e->torrentMan->connections < 30) { + e->torrentMan->req->resetTryCount(); if(e->torrentMan->downloadComplete()) { - if(req->getTrackerEvent() == Request::COMPLETED) { - req->setTrackerEvent(Request::AFTER_COMPLETED); + if(e->torrentMan->req->getTrackerEvent() == Request::COMPLETED) { + e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED); } else { - if(req->getTrackerEvent() == Request::STARTED) { - req->setTrackerEvent(Request::AFTER_COMPLETED); - } else if(req->getTrackerEvent() != Request::AFTER_COMPLETED) { - req->setTrackerEvent(Request::COMPLETED); + if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) { + e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED); + } else if(e->torrentMan->req->getTrackerEvent() != Request::AFTER_COMPLETED) { + e->torrentMan->req->setTrackerEvent(Request::COMPLETED); } } } string event; - switch(req->getTrackerEvent()) { + switch(e->torrentMan->req->getTrackerEvent()) { case Request::STARTED: event = "started"; break; @@ -74,8 +74,8 @@ bool TrackerWatcherCommand::execute() { if(!e->torrentMan->trackerId.empty()) { url += string("&")+"trackerid="+e->torrentMan->trackerId; } - req->setUrl(url); - Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(e->torrentMan->getNewCuid(), req, e); + e->torrentMan->req->setUrl(url); + Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(e->torrentMan->getNewCuid(), e->torrentMan->req, e); e->commands.push(command); e->torrentMan->trackers++; logger->info("CUID#%d - creating new tracker request command #%d", cuid, diff --git a/src/TrackerWatcherCommand.h b/src/TrackerWatcherCommand.h index 7ca0719c..1151a7b0 100644 --- a/src/TrackerWatcherCommand.h +++ b/src/TrackerWatcherCommand.h @@ -24,14 +24,12 @@ #include "Command.h" #include "TorrentDownloadEngine.h" -#include "Request.h" class TrackerWatcherCommand : public Command { private: - Request* req; TorrentDownloadEngine* e; public: - TrackerWatcherCommand(int cuid, Request* req, TorrentDownloadEngine* e); + TrackerWatcherCommand(int cuid, TorrentDownloadEngine* e); ~TrackerWatcherCommand(); bool execute(); diff --git a/src/Util.cc b/src/Util.cc index c7d36c1f..d1405a61 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -286,3 +286,11 @@ string Util::secfmt(int sec) { str += itos(sec)+"s"; return str; } + +int Util::expandBuffer(char** pbuf, int curLength, int newLength) { + char* newbuf = new char[newLength]; + memcpy(newbuf, *pbuf, curLength); + delete [] *pbuf; + *pbuf = newbuf; + return newLength; +} diff --git a/src/Util.h b/src/Util.h index eead20b4..c209afd1 100644 --- a/src/Util.h +++ b/src/Util.h @@ -71,6 +71,8 @@ public: static bool isPowerOf(int num, int base); static string secfmt(int sec); + + static int expandBuffer(char** pbuf, int curLength, int newLength); }; #endif // _D_UTIL_H_ diff --git a/src/main.cc b/src/main.cc index 2e1cb6de..bb3582bd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -34,7 +34,7 @@ #include "PeerListenCommand.h" #include "TorrentAutoSaveCommand.h" #include "TrackerWatcherCommand.h" -#include "CompactTrackerResponseProcessor.h" +#include "TrackerUpdateCommand.h" #include "ByteArrayDiskWriter.h" #include #include @@ -91,9 +91,7 @@ void handler(int signal) { printf(_("\nstopping application...\n")); fflush(stdout); e->segmentMan->save(); - if(e->diskWriter != NULL) { - e->diskWriter->closeFile(); - } + e->segmentMan->diskWriter->closeFile(); printf(_("done\n")); exit(0); } @@ -101,9 +99,7 @@ void handler(int signal) { void torrentHandler(int signal) { printf(_("\nstopping application...\n")); fflush(stdout); - if(te->torrentMan->diskAdaptor != NULL) { - te->torrentMan->diskAdaptor->closeFile(); - } + te->torrentMan->diskAdaptor->closeFile(); if(te->torrentMan->downloadComplete() && te->isFilenameFixed()) { te->torrentMan->remove(); //te->torrentMan->deleteTempFile(); @@ -276,6 +272,7 @@ int main(int argc, char* argv[]) { op->put(PREF_PEER_CONNECTION_TIMEOUT, "60"); op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M op->put(PREF_MAX_TRIES, "5"); + op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC); op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL); op->put(PREF_FTP_USER, "anonymous"); op->put(PREF_FTP_PASSWD, "ARIA2USER@"); @@ -346,6 +343,7 @@ int main(int argc, char* argv[]) { } case 2: op->put(PREF_HTTP_USER, optarg); + op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE); break; case 3: op->put(PREF_HTTP_PASSWD, optarg); @@ -562,7 +560,15 @@ int main(int argc, char* argv[]) { } else if(logfile.size()) { LogFactory::setLogFile(logfile); } - + // make sure logger is configured properly. + try { + LogFactory::getInstance(); + } catch(Exception* ex) { + cerr << ex->getMsg() << endl; + delete ex; + exit(1); + } + SegmentSplitter* splitter = new SplitSlowestSegmentSplitter(); splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE)); @@ -576,8 +582,8 @@ int main(int argc, char* argv[]) { e = new ConsoleDownloadEngine(); e->option = op; - e->diskWriter = new DefaultDiskWriter(); e->segmentMan = new SegmentMan(); + e->segmentMan->diskWriter = new DefaultDiskWriter(); e->segmentMan->dir = dir; e->segmentMan->ufilename = ufilename; e->segmentMan->option = op; @@ -607,7 +613,7 @@ int main(int argc, char* argv[]) { requests.clear(); delete(e->segmentMan); - delete(e->diskWriter); + delete(e->segmentMan->diskWriter); delete(e); } if(!torrentFile.empty() || followTorrent && readyToTorrentMode) { @@ -622,16 +628,14 @@ int main(int argc, char* argv[]) { te = new TorrentConsoleDownloadEngine(); te->option = op; ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter(); - te->diskWriter = byteArrayDiskWriter; te->segmentMan = new SegmentMan(); + te->segmentMan->diskWriter = byteArrayDiskWriter; te->segmentMan->option = op; te->segmentMan->splitter = splitter; te->torrentMan = new TorrentMan(); te->torrentMan->setStoreDir(dir); te->torrentMan->option = op; - CompactTrackerResponseProcessor* responseProcessor = - new CompactTrackerResponseProcessor(byteArrayDiskWriter, te, req); - te->torrentMan->setTrackerResponseProcessor(responseProcessor); + te->torrentMan->req = req; string targetTorrentFile = torrentFile.empty() ? downloadedTorrentFile : torrentFile; if(op->get(PREF_SHOW_FILES) == V_TRUE) { @@ -666,7 +670,9 @@ int main(int argc, char* argv[]) { te->torrentMan->setPort(port); te->commands.push(listenCommand); te->commands.push(new TrackerWatcherCommand(te->torrentMan->getNewCuid(), - req, te)); + te)); + te->commands.push(new TrackerUpdateCommand(te->torrentMan->getNewCuid(), + te)); te->commands.push(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(), te, op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); @@ -678,10 +684,9 @@ int main(int argc, char* argv[]) { printDownloadAbortMessage(); } delete(req); - delete(responseProcessor); delete(te->segmentMan); + delete(te->segmentMan->diskWriter); delete(te->torrentMan); - delete(te->diskWriter); delete(te); } catch(Exception* ex) { cerr << ex->getMsg() << endl; diff --git a/src/message.h b/src/message.h index c54a4fc3..a7123d96 100644 --- a/src/message.h +++ b/src/message.h @@ -67,7 +67,11 @@ #define EX_TRANSFER_ENCODING_NOT_SUPPORTED _("Transfer encoding %s is not supported.") #define EX_SSL_INIT_FAILURE _("SSL initialization failed.") #define EX_SIZE_MISMATCH _("Size mismatch %lld != %lld") +#define EX_AUTH_FAILED "Authorization failed." #define EX_GOT_EOF _("Got EOF from the server.") #define EX_EOF_FROM_PEER "Got EOF from peer." #define EX_MULFORMED_META_INFO "Mulformed meta info." + +#define EX_FILE_OPEN "Error occurred while opening <%s>, cause: %s" + #endif // _D_MESSAGE_H_ diff --git a/src/prefs.h b/src/prefs.h index f4173412..5ac0d353 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -68,6 +68,8 @@ // values: basic #define PREF_HTTP_AUTH_SCHEME "http_auth_scheme" # define V_BASIC "basic" +// values: true | false +#define PREF_HTTP_AUTH_ENABLED "http_auth_enabled" /** * HTTP proxy related preferences