Sfoglia il codice sorgente

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.

Tatsuhiro Tsujikawa 19 anni fa
parent
commit
e199bf8898

+ 63 - 0
ChangeLog

@@ -1,3 +1,66 @@
+2006-04-20  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	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  <tujikawa at rednoah dot com>
 
 	To add a readout of estimated remaining time to normal HTTP/FTP

+ 8 - 9
src/AbstractDiskWriter.cc

@@ -20,14 +20,15 @@
  */
 /* copyright --> */
 #include "AbstractDiskWriter.h"
+#include "DlAbortEx.h"
+#include "File.h"
+#include "Util.h"
+#include "message.h"
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <fcntl.h>
-#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));
   }  
 }
 

+ 15 - 7
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;
 }
+

+ 1 - 4
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 ""; }
 

+ 1 - 1
src/ConsoleDownloadEngine.cc

@@ -82,7 +82,7 @@ void ConsoleDownloadEngine::calculateStatistics() {
 }
 
 void ConsoleDownloadEngine::onEndOfRun() {
-  diskWriter->closeFile();
+  segmentMan->diskWriter->closeFile();
   if(segmentMan->finished()) {
     segmentMan->remove();
   } else {

+ 2 - 2
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

+ 0 - 2
src/DownloadEngine.h

@@ -29,7 +29,6 @@
 #include "SegmentMan.h"
 #include "common.h"
 #include "Logger.h"
-#include "DiskWriter.h"
 #include "Option.h"
 #include <sys/time.h>
 
@@ -57,7 +56,6 @@ public:
   bool noWait;
   Commands commands;
   SegmentMan* segmentMan;
-  DiskWriter* diskWriter;
   const Option* option;
 
   DownloadEngine();

+ 2 - 2
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());
     }
   }
 

+ 6 - 4
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";

+ 12 - 8
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;
 }

+ 4 - 2
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;
 }

+ 1 - 1
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)

+ 3 - 3
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@

+ 1 - 4
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.

+ 1 - 0
src/SegmentMan.cc

@@ -217,4 +217,5 @@ void SegmentMan::init() {
   isSplittable = false;
   downloadStarted = false;
   segments.clear();
+  diskWriter->closeFile();
 }

+ 3 - 0
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();
 
   /**

+ 16 - 4
src/SimpleLogger.cc

@@ -21,9 +21,12 @@
 /* copyright --> */
 #include "SimpleLogger.h"
 #include "Util.h"
+#include "DlAbortEx.h"
+#include "message.h"
 #include <time.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <errno.h>
 
 #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;

+ 3 - 1
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;

+ 0 - 1
src/TorrentDownloadEngine.cc

@@ -41,5 +41,4 @@ void TorrentDownloadEngine::afterEachIteration() {
       filenameFixed = true;
     }
   }
-  torrentMan->processTrackerResponse();
 }

+ 0 - 7
src/TorrentMan.cc

@@ -567,10 +567,3 @@ void TorrentMan::onDownloadComplete() {
     finishSelectiveDownloadingMode();
   }
 }
-
-void TorrentMan::processTrackerResponse() {
-  if(responseProcessor->isFeeded()) {
-    responseProcessor->execute();
-    responseProcessor->resetTrackerResponse();
-  }
-}

+ 3 - 11
src/TorrentMan.h

@@ -32,7 +32,7 @@
 #include "Option.h"
 #include "FileEntry.h"
 #include "DiskAdaptor.h"
-#include "CompactTrackerResponseProcessor.h"
+#include "Request.h"
 #include <deque>
 #include <map>
 #include <string>
@@ -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

+ 13 - 13
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,

+ 1 - 3
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();

+ 8 - 0
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;
+}

+ 2 - 0
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_

+ 22 - 17
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 <deque>
 #include <algorithm>
@@ -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;

+ 4 - 0
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_

+ 2 - 0
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