Tatsuhiro Tsujikawa %!s(int64=18) %!d(string=hai) anos
pai
achega
079d2844ec

+ 51 - 0
src/AbstractSegmentManFactory.h

@@ -0,0 +1,51 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_ABSTRACT_SEGMENT_MAN_FACTORY_H_
+#define _D_ABSTRACT_SEGMENT_MAN_FACTORY_H_
+
+#include "SegmentManFactory.h"
+
+class AbstractSegmentManFactory : public SegmentManFactory {
+protected:
+  const Option* _option;
+public:
+  AbstractSegmentManFactory(const Option* option):_option(option) {}
+
+  virtual ~AbstractSegmentManFactory() {}
+};
+
+typedef SharedHandle<AbstractSegmentManFactory> AbstractSegmentManFactoryHandle;
+
+#endif // _D_ABSTRACT_SEGMENT_MAN_FACTORY_H_

+ 59 - 0
src/CUIDCounter.h

@@ -0,0 +1,59 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_CUID_COUNTER_H_
+#define _D_CUID_COUNTER_H_
+
+#include "common.h"
+
+typedef int32_t CUID;
+
+class CUIDCounter {
+private:
+  int32_t count;
+public:
+  CUIDCounter():count(0) {}
+
+  ~CUIDCounter() {}
+
+  CUID newID()
+  {
+    return ++count;
+  }
+};
+
+typedef SharedHandle<CUIDCounter> CUIDCounterHandle;
+typedef SingletonHolder<CUIDCounterHandle> CUIDCounterSingletonHolder;
+
+#endif // _D_CUID_COUNTER_H_

+ 90 - 0
src/CheckIntegrityCommand.cc

@@ -0,0 +1,90 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "CheckIntegrityCommand.h"
+#include "FileAllocationEntry.h"
+#include "InitiateConnectionCommandFactory.h"
+#include "DlAbortEx.h"
+#include "message.h"
+
+void CheckIntegrityCommand::initValidator()
+{
+  _validator = new IteratableChunkChecksumValidator();
+  _validator->setChunkChecksum(_requestGroup->getChunkChecksum());
+  _validator->setDiskWriter(_requestGroup->getSegmentMan()->diskWriter);
+  _validator->setBitfield(_requestGroup->getSegmentMan()->getBitfield());
+  if(!_validator->canValidate()) {
+    // insufficient checksums.
+    throw new DlAbortEx("Insufficient checksums.");
+  }
+  _validator->init();
+}
+
+bool CheckIntegrityCommand::execute()
+{
+  setStatusRealtime();
+  _e->noWait = true;
+
+  try {
+    _validator->validateChunk();
+    if(_validator->finished()) {
+      if(_requestGroup->downloadFinished()) {
+	logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, cuid, _requestGroup->getFilePath().c_str());
+	return true;
+      }
+      if(_requestGroup->needsFileAllocation()) {
+	_e->_fileAllocationMan->pushFileAllocationEntry(new FileAllocationEntry(cuid, _req, _requestGroup)); 
+      } else {
+	int32_t numCommandsToGenerate = 15;
+	Commands commands = _requestGroup->getNextCommand(_e, numCommandsToGenerate);
+	Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, _req, _requestGroup, _e);
+	commands.push_front(command);
+	_e->addCommand(commands);
+      }
+      return true;
+    } else {
+      _e->commands.push_back(this);
+      return false;
+    }
+  } catch(Exception* e) {
+    _requestGroup->getSegmentMan()->errors++;
+    logger->error("CUID#%d - Exception caught while validating file integrity.", e, cuid);
+    delete e;
+    logger->error(MSG_DOWNLOAD_NOT_COMPLETE, cuid, _requestGroup->getFilePath().c_str());
+    // TODO this is wrong. There may exist invalid chunk data before catching
+    // exception. Fix this.
+    _requestGroup->markPieceDone(_validator->getCurrentOffset());
+    return true;
+  }
+}

+ 71 - 0
src/CheckIntegrityCommand.h

@@ -0,0 +1,71 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_CHECK_INTEGRITY_COMMAND_H_
+#define _D_CHECK_INTEGRITY_COMMAND_H_
+
+#include "Command.h"
+#include "Request.h"
+#include "RequestGroup.h"
+#include "DownloadEngine.h"
+#include "IteratableChunkChecksumValidator.h"
+
+class CheckIntegrityCommand : public Command {
+private:
+  RequestHandle _req;
+  RequestGroup* _requestGroup;
+  DownloadEngine* _e;
+  IteratableChunkChecksumValidatorHandle _validator;
+public:
+  CheckIntegrityCommand(int cuid, const RequestHandle& req, RequestGroup* requestGroup, DownloadEngine* e):
+    Command(cuid),
+    _req(req),
+    _requestGroup(requestGroup),
+    _e(e),
+    _validator(0)
+  {
+    ++_requestGroup->numConnection;
+  }
+
+  virtual ~CheckIntegrityCommand()
+  {
+    --_requestGroup->numConnection;
+  }
+
+  void initValidator();
+
+  virtual bool execute();
+};
+
+#endif // _D_CHECK_INTEGRITY_COMMAND_H_

+ 96 - 0
src/ChunkChecksum.h

@@ -0,0 +1,96 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_CHUNK_CHECKSUM_H_
+#define _D_CHUNK_CHECKSUM_H_
+
+#include "common.h"
+#include "messageDigest.h"
+
+class ChunkChecksum {
+private:
+  MessageDigestContext::DigestAlgo _algo;
+  Strings _checksums;
+  int32_t _checksumLength;
+public:
+  ChunkChecksum(const MessageDigestContext::DigestAlgo& algo,
+		const Strings& checksums,
+		int32_t checksumLength):
+    _algo(algo),
+    _checksums(checksums),
+    _checksumLength(checksumLength) {}
+
+  bool validateChunk(const string& actualChecksum,
+		     int32_t checksumIndex) const
+  {
+    if(checksumIndex < (int32_t)_checksums.size()) {
+      return actualChecksum == getChecksum(checksumIndex);
+    } else {
+      return false;
+    }
+  }
+
+  int64_t getEstimatedDataLength() const
+  {
+    return ((int64_t)_checksumLength)*_checksums.size();
+  }
+
+  int32_t countChecksum() const
+  {
+    return _checksums.size();
+  }
+
+  string getChecksum(int32_t index) const
+  {
+    if(index < (int32_t)_checksums.size()) {
+      return _checksums[index];
+    } else {
+      return "";
+    }
+  }
+
+  int32_t getChecksumLength() const
+  {
+    return _checksumLength;
+  }
+
+  MessageDigestContext::DigestAlgo getAlgo() const
+  {
+    return _algo;
+  }
+};
+
+typedef SharedHandle<ChunkChecksum> ChunkChecksumHandle;
+
+#endif // _D_CHUNK_CHECKSUM_H_

+ 48 - 0
src/DefaultSegmentManFactory.cc

@@ -0,0 +1,48 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "DefaultSegmentManFactory.h"
+#include "prefs.h"
+#include "DefaultDiskWriter.h"
+
+SegmentManHandle DefaultSegmentManFactory::createNewInstance()
+{
+  SegmentManHandle segmentMan = new SegmentMan();
+  segmentMan->diskWriter = new DefaultDiskWriter();
+  segmentMan->dir = _option->get(PREF_DIR);
+  // TODO disable this in multi-simultaneous download mode.
+  segmentMan->ufilename = _option->get(PREF_OUT);
+  segmentMan->option = _option;
+  return segmentMan;
+}

+ 51 - 0
src/DefaultSegmentManFactory.h

@@ -0,0 +1,51 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_DEFAULT_SEGMENT_MAN_FACTORY_H_
+#define _D_DEFAULT_SEGMENT_MAN_FACTORY_H_
+
+#include "AbstractSegmentManFactory.h"
+
+class DefaultSegmentManFactory : public AbstractSegmentManFactory {
+public:
+  DefaultSegmentManFactory(const Option* option):AbstractSegmentManFactory(option) {}
+
+  virtual ~DefaultSegmentManFactory() {}
+
+  virtual SegmentManHandle createNewInstance();
+};
+
+typedef SharedHandle<DefaultSegmentManFactory> DefaultSegmentManFactoryHandle;
+
+#endif // _D_DEFAULT_SEGMENT_MAN_FACTORY_H_

+ 73 - 0
src/FileAllocationCommand.cc

@@ -0,0 +1,73 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "FileAllocationCommand.h"
+#include "InitiateConnectionCommandFactory.h"
+#include "message.h"
+
+bool FileAllocationCommand::execute()
+{
+  setStatusRealtime();
+  _e->noWait = true;
+  try {
+    _fileAllocationEntry->allocateChunk();
+
+    if(_fileAllocationEntry->allocationFinished()) {
+      int64_t totalLength = _requestGroup->getSegmentMan()->totalSize;
+      logger->debug("%d seconds to allocate %lld byte(s)",
+		    _timer.difference(), totalLength);
+    
+      _e->_fileAllocationMan->markCurrentFileAllocationEntryDone();
+
+      int32_t numCommandsToGenerate = 15;
+      Commands commands = _requestGroup->getNextCommand(_e, numCommandsToGenerate);
+
+      Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, _req, _requestGroup, _e);
+
+      commands.push_front(command);
+
+      _e->addCommand(commands);
+      return true;
+    } else {
+      _e->commands.push_back(this);
+      return false;
+    }
+  } catch(Exception* e) {
+    _requestGroup->getSegmentMan()->errors++;
+    logger->error("CUID#%d - Exception caught while allocating file space.", e, cuid);
+    delete e;
+    logger->notice(MSG_DOWNLOAD_NOT_COMPLETE, cuid, _requestGroup->getFilePath().c_str());
+    return true;
+  }
+}

+ 63 - 0
src/FileAllocationCommand.h

@@ -0,0 +1,63 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_FILE_ALLOCATION_COMMAND_H_
+#define _D_FILE_ALLOCATION_COMMAND_H_
+
+#include "Command.h"
+#include "Request.h"
+#include "RequestGroup.h"
+#include "DownloadEngine.h"
+#include "TimeA2.h"
+#include "FileAllocationEntry.h"
+
+class FileAllocationCommand : public Command {
+private:
+  RequestHandle _req;
+  RequestGroup* _requestGroup;
+  DownloadEngine* _e;
+  FileAllocationEntryHandle _fileAllocationEntry;
+  Time _timer;
+public:
+  FileAllocationCommand(int cuid, const RequestHandle& req, RequestGroup* requestGroup, DownloadEngine* e, const FileAllocationEntryHandle& fileAllocationEntry):
+    Command(cuid),
+    _req(req),
+    _requestGroup(requestGroup),
+    _e(e),
+    _fileAllocationEntry(fileAllocationEntry) {}
+
+  virtual bool execute();
+};
+
+#endif // _D_FILE_ALLOCATION_COMMAND_H_

+ 62 - 0
src/FileAllocationDispatcherCommand.cc

@@ -0,0 +1,62 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "FileAllocationDispatcherCommand.h"
+#include "FileAllocationCommand.h"
+
+bool FileAllocationDispatcherCommand::execute()
+{
+  if(_e->_requestGroupMan->downloadFinished()) {
+    return true;
+  }
+  
+  if(!_e->_fileAllocationMan->isFileAllocationBeingExecuted() &&
+     _e->_fileAllocationMan->nextFileAllocationEntryExists()) {
+    FileAllocationEntryHandle entry = _e->_fileAllocationMan->popNextFileAllocationEntry();
+    logger->info("Dispatching FileAllocationCommand for CUID#%d.",
+		 entry->getCUID());
+    FileAllocationCommand* command =
+      new FileAllocationCommand(entry->getCUID(),
+				entry->getCurrentRequest(),
+				entry->getRequestGroup(),
+				_e,
+				_e->_fileAllocationMan->getCurrentFileAllocationEntry());
+    _e->commands.push_back(command);
+    _e->noWait = true;
+  }
+
+  _e->commands.push_back(this);
+
+  return false;
+}

+ 54 - 0
src/FileAllocationDispatcherCommand.h

@@ -0,0 +1,54 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_FILE_ALLOCATION_DISPATCHER_COMMAND_H_
+#define _D_FILE_ALLOCATION_DISPATCHER_COMMAND_H_
+
+#include "Command.h"
+#include "DownloadEngine.h"
+
+class FileAllocationDispatcherCommand : public Command {
+private:
+  DownloadEngine* _e;
+public:
+  FileAllocationDispatcherCommand(int cuid, DownloadEngine* e):
+    Command(cuid), _e(e)
+  {
+    setStatusRealtime();
+  }
+
+  virtual bool execute();
+};
+
+#endif // _D_FILE_ALLOCATION_DISPATCHER_COMMAND_H_

+ 54 - 0
src/FileAllocationEntry.cc

@@ -0,0 +1,54 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "FileAllocationEntry.h"
+
+#define BUFSIZE 16*1024
+
+void FileAllocationEntry::allocateChunk()
+{
+  int32_t bufSize = BUFSIZE;
+  char buf[BUFSIZE];
+  memset(buf, 0, bufSize);
+  
+  _requestGroup->getSegmentMan()->diskWriter->writeData(buf, bufSize, _offset);
+  _offset += bufSize;
+
+  int64_t totalLength = _requestGroup->getSegmentMan()->totalSize;
+  if(totalLength < _offset) {
+    _requestGroup->getSegmentMan()->diskWriter->truncate(totalLength);
+    _offset = totalLength;
+  }
+}
+

+ 102 - 0
src/FileAllocationEntry.h

@@ -0,0 +1,102 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_FILE_ALLOCATION_ENTRY_H_
+#define _D_FILE_ALLOCATION_ENTRY_H_
+
+#include "common.h"
+#include "Request.h"
+#include "RequestGroup.h"
+
+class FileAllocationEntry {
+private:
+  int _cuid;
+  RequestHandle _currentRequest;
+  RequestGroup* _requestGroup;
+  int64_t _offset;
+public:
+  FileAllocationEntry(int cuid,
+		      const RequestHandle& currentRequest,
+		      RequestGroup* requestGroup,
+		      int64_t offset = 0):
+    _cuid(cuid),
+    _currentRequest(currentRequest),
+    _requestGroup(requestGroup),
+    _offset(offset)
+  {
+    ++_requestGroup->numConnection;
+  }
+
+  ~FileAllocationEntry()
+  {
+    --_requestGroup->numConnection;
+  }
+
+  int getCUID() const
+  {
+    return _cuid;
+  }
+
+  RequestHandle getCurrentRequest() const
+  {
+    return _currentRequest;
+  }
+
+  RequestGroup* getRequestGroup() const
+  {
+    return _requestGroup;
+  }
+
+  void setOffset(int64_t offset)
+  {
+    _offset = offset;
+  }
+
+  int64_t getOffset() const
+  {
+    return _offset;
+  }
+
+  void allocateChunk();
+
+  bool allocationFinished() const
+  {
+    return _requestGroup->getSegmentMan()->totalSize <= _offset;
+  }
+};
+
+typedef SharedHandle<FileAllocationEntry> FileAllocationEntryHandle;
+typedef deque<FileAllocationEntryHandle> FileAllocationEntries;
+
+#endif // _D_FILE_ALLOCATION_ENTRY_H_

+ 89 - 0
src/FileAllocationMan.h

@@ -0,0 +1,89 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_FILE_ALLOCATION_MAN_H_
+#define _D_FILE_ALLOCATION_MAN_H_
+
+#include "common.h"
+#include "Request.h"
+#include "RequestGroup.h"
+#include "FileAllocationEntry.h"
+
+class FileAllocationMan {
+private:
+  FileAllocationEntries _fileAllocationEntries;
+  FileAllocationEntryHandle _currentFileAllocationEntry;
+public:
+  FileAllocationMan():_currentFileAllocationEntry(0) {}
+
+  bool isFileAllocationBeingExecuted() const
+  {
+    return _currentFileAllocationEntry.get() != 0;
+  }
+
+  FileAllocationEntryHandle getCurrentFileAllocationEntry()
+  {
+    return _currentFileAllocationEntry;
+  }
+
+  void markCurrentFileAllocationEntryDone()
+  {
+    _currentFileAllocationEntry = 0;
+  }
+
+  bool nextFileAllocationEntryExists() const
+  {
+    return !_fileAllocationEntries.empty();
+  }
+
+  FileAllocationEntryHandle popNextFileAllocationEntry()
+  {
+    if(!nextFileAllocationEntryExists()) {
+      return 0;
+    }
+    FileAllocationEntryHandle entry = _fileAllocationEntries.front();
+    _fileAllocationEntries.pop_front();
+    _currentFileAllocationEntry = entry;
+    return entry;
+  }
+
+  void pushFileAllocationEntry(const FileAllocationEntryHandle& entry)
+  {
+    _fileAllocationEntries.push_back(entry);
+  }
+};
+
+typedef SharedHandle<FileAllocationMan> FileAllocationManHandle;
+
+#endif // _D_FILE_ALLOCATION_MAN_H_

+ 45 - 0
src/FillRequestGroupCommand.cc

@@ -0,0 +1,45 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "FillRequestGroupCommand.h"
+
+bool FillRequestGroupCommand::execute()
+{
+  _e->_requestGroupMan->fillRequestGroupFromReserver(_e);
+  if(_e->_requestGroupMan->downloadFinished()) {
+    return true;
+  }
+  _e->commands.push_back(this);
+  return false;
+}

+ 66 - 0
src/FillRequestGroupCommand.h

@@ -0,0 +1,66 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_FILL_REQUEST_GROUP_COMMAND_H_
+#define _D_FILL_REQUEST_GROUP_COMMAND_H_
+
+#include "Command.h"
+#include "TimeA2.h"
+#include "RequestGroup.h"
+#include "DownloadEngine.h"
+
+class FillRequestGroupCommand : public Command {
+private:
+  RequestGroups _reservedRequestGroups;
+  DownloadEngine* _e;
+  int32_t _interval;
+  Time _checkPoint;
+public:
+  FillRequestGroupCommand(int cuid, DownloadEngine* e, int32_t interval):
+    Command(cuid),
+    _e(e),
+    _interval(interval)
+  {
+    setStatusRealtime();
+  }
+
+  virtual bool execute();
+
+  void setInterval(int32_t interval)
+  {
+    _interval = interval;
+  }
+};
+
+#endif // _D_FILL_REQUEST_GROUP_COMMAND_H_

+ 81 - 0
src/IteratableChunkChecksumValidator.cc

@@ -0,0 +1,81 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "IteratableChunkChecksumValidator.h"
+#include "Util.h"
+#include "message.h"
+
+void IteratableChunkChecksumValidator::validateChunk()
+{
+  if(!finished()) {
+    string actualChecksum = calculateActualChecksum();
+
+
+    if(!_chunkChecksum->validateChunk(actualChecksum, _currentIndex)) {
+      int64_t offset = ((int64_t)_currentIndex)*_chunkChecksum->getChecksumLength();
+      // wrong checksum
+      logger->info(EX_INVALID_CHUNK_CHECKSUM,
+		   _currentIndex,
+		   offset,
+		   _chunkChecksum->getChecksum(_currentIndex).c_str(),
+		   actualChecksum.c_str());
+      int32_t startIndex;
+      int32_t endIndex;
+      Util::indexRange(startIndex, endIndex, offset,
+		       _chunkChecksum->getChecksumLength(),
+		       _bitfield->getBlockLength());
+      _bitfield->unsetBitRange(startIndex, endIndex);
+    }
+    _currentIndex++;	  
+  }
+}
+
+string IteratableChunkChecksumValidator::calculateActualChecksum()
+{
+  int64_t offset = ((int64_t)_currentIndex)*_chunkChecksum->getChecksumLength();
+  int32_t length = _diskWriter->size() < offset+_chunkChecksum->getChecksumLength() ? _diskWriter->size()-offset : _chunkChecksum->getChecksumLength();
+  return _diskWriter->messageDigest(offset, length, _chunkChecksum->getAlgo());
+}
+
+bool IteratableChunkChecksumValidator::canValidate() const
+{
+  // We assume file is already opened using DiskWriter::open or openExistingFile.
+  return _chunkChecksum->getEstimatedDataLength() >= _diskWriter->size();
+}
+
+void IteratableChunkChecksumValidator::init()
+{
+  _bitfield->setAllBit();
+  _currentIndex = 0;
+}

+ 91 - 0
src/IteratableChunkChecksumValidator.h

@@ -0,0 +1,91 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_ITERATABLE_CHUNK_CHECKSUM_VALIDATOR_H_
+#define _D_ITERATABLE_CHUNK_CHECKSUM_VALIDATOR_H_
+
+#include "common.h"
+#include "BitfieldMan.h"
+#include "ChunkChecksum.h"
+#include "DiskWriter.h"
+#include "LogFactory.h"
+
+class IteratableChunkChecksumValidator
+{
+private:
+  DiskWriterHandle _diskWriter;
+  BitfieldMan* _bitfield;
+  int32_t _currentIndex;
+  ChunkChecksumHandle _chunkChecksum;
+  const Logger* logger;
+
+  string calculateActualChecksum();
+public:
+  IteratableChunkChecksumValidator():_diskWriter(0), _bitfield(0), _currentIndex(0), _chunkChecksum(0), logger(LogFactory::getInstance()) {}
+
+  bool canValidate() const;
+
+  void init();
+
+  void validateChunk();
+
+  bool finished() const
+  {
+    return _currentIndex >= _chunkChecksum->countChecksum();
+  }
+
+  void setDiskWriter(const DiskWriterHandle& diskWriter)
+  {
+    _diskWriter = diskWriter;
+  }
+
+  void setBitfield(BitfieldMan* bitfield)
+  {
+    _bitfield = bitfield;
+  }
+
+  void setChunkChecksum(const ChunkChecksumHandle& chunkChecksum)
+  {
+    _chunkChecksum = chunkChecksum;
+  }
+
+  int64_t getCurrentOffset() const
+  {
+    return ((int64_t)_currentIndex)*_chunkChecksum->getChecksumLength();
+  }
+};
+
+typedef SharedHandle<IteratableChunkChecksumValidator> IteratableChunkChecksumValidatorHandle;
+
+#endif // _D_ITERATABLE_CHUNK_CHECKSUM_VALIDATOR_H_

+ 119 - 0
src/MultiUrlRequestInfo.cc

@@ -0,0 +1,119 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "MultiUrlRequestInfo.h"
+#include "prefs.h"
+#include "DownloadEngineFactory.h"
+#include "RecoverableException.h"
+#include "message.h"
+#include "DNSCache.h"
+#include "TorrentRequestInfo.h"
+#include "MetalinkRequestInfo.h"
+
+extern volatile sig_atomic_t haltRequested;
+
+RequestInfoHandle MultiUrlRequestInfo::createNextRequestInfo(const string& filename) const
+{
+#ifdef ENABLE_BITTORRENT
+  if(op->getAsBool(PREF_FOLLOW_TORRENT) &&
+     Util::endsWith(fileInfo.filename, ".torrent")) {
+    return new TorrentRequestInfo(filename, op);
+  } else
+#endif // ENABLE_BITTORRENT
+#ifdef ENABLE_METALINK
+    if(op->getAsBool(PREF_FOLLOW_METALINK) &&
+       Util::endsWith(fileInfo.filename, ".metalink")) {
+      return new MetalinkRequestInfo(filename, op);
+    } else
+#endif // ENABLE_METALINK
+      {
+	return 0;
+      }
+}
+
+static void handler(int signal) {
+  haltRequested = true;
+}
+
+RequestInfos MultiUrlRequestInfo::execute() {
+  {
+    DNSCacheHandle dnsCache = new SimpleDNSCache();
+    DNSCacheSingletonHolder::instance(dnsCache);
+  }
+
+  RequestInfos nextReqInfos;
+  try {
+    SharedHandle<ConsoleDownloadEngine> e(DownloadEngineFactory::newConsoleEngine(op, _requestGroups));
+
+    e->fillCommand();
+
+    // The number of simultaneous download is specified by PREF_MAX_SIMULTANEOUS_DOWNLOADS.
+    // The remaining urls are queued into FillRequestGroupCommand.
+    // It observes the number of simultaneous downloads and if it is under
+    // the limit, it adds RequestGroup object from its queue to DownloadEngine.
+    // This is done every 1 second. At the same time, it removes finished/error
+    // RequestGroup from DownloadEngine.
+
+    Util::setGlobalSignalHandler(SIGINT, handler, 0);
+    Util::setGlobalSignalHandler(SIGTERM, handler, 0);
+    
+    e->run();
+    
+    for(RequestGroups::iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      if((*itr)->downloadFinished()) {
+	RequestInfoHandle reqInfo = createNextRequestInfo((*itr)->getFilePath());
+	if(!reqInfo.isNull()) {
+	  nextReqInfos.push_back(reqInfo);
+	}
+      }
+    }
+    RequestGroupMan rgman(_requestGroups);
+    // TODO print summary of the download result
+    rgman.showDownloadResults(cout);
+    cout << flush;
+    // TODO Do we have to print a message when some of the downloads are failed?
+    if(!rgman.downloadFinished()) {
+      printDownloadAbortMessage();
+    }
+  } catch(RecoverableException *ex) {
+    logger->error("Exception caught", ex);
+    delete ex;
+    fail = true;
+  }
+  Util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
+  Util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
+  
+  return nextReqInfos;
+}

+ 61 - 0
src/MultiUrlRequestInfo.h

@@ -0,0 +1,61 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_MULTI_URL_REQUEST_INFO_H_
+#define _D_MULTI_URL_REQUEST_INFO_H_
+
+#include "RequestInfo.h"
+
+class MultiUrlRequestInfo : public RequestInfo {
+private:
+  RequestGroups _requestGroups;
+
+  RequestInfoHandle createNextRequestInfo(const string& filename) const;
+public:
+  MultiUrlRequestInfo(const RequestGroups& requestGroups, Option* op):
+    RequestInfo(op),
+    _requestGroups(requestGroups) {}
+
+  MultiUrlRequestInfo(const Strings& uris, Option* op):RequestInfo(op)
+  {
+    _requestGroups.push_back(new RequestGroup(uris, op));
+  }
+
+  virtual ~MultiUrlRequestInfo() {}
+
+  virtual RequestInfos execute();
+};
+
+typedef SharedHandle<MultiUrlRequestInfo> MultiUrlRequestInfoHandle;
+#endif // _D_MULTI_URL_REQUEST_INFO_H_

+ 258 - 0
src/RequestGroup.cc

@@ -0,0 +1,258 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "RequestGroup.h"
+#include "DownloadEngine.h"
+#include "prefs.h"
+#include "DefaultDiskWriter.h"
+#include "RequestFactory.h"
+#include "InitiateConnectionCommandFactory.h"
+#include "CUIDCounter.h"
+#include "DlAbortEx.h"
+#include "File.h"
+#include "message.h"
+#include "DlAbortEx.h"
+#include "Util.h"
+#include "CheckIntegrityCommand.h"
+#include "FatalException.h"
+
+SegmentManHandle RequestGroup::initSegmentMan()
+{
+  _segmentMan = _segmentManFactory->createNewInstance();
+  /*
+  _segmentMan = new SegmentMan();
+  _segmentMan->diskWriter = new DefaultDiskWriter();// DefaultDiskWriter::createNewDiskWriter(_option);
+  _segmentMan->dir = _option->get(PREF_DIR);
+  // TODO disable this in multi-simultaneous download mode.
+  _segmentMan->ufilename = _option->get(PREF_OUT);
+  _segmentMan->option = _option;
+  */
+  return _segmentMan;
+}
+
+Commands RequestGroup::getNextCommand(DownloadEngine* e, int32_t maxNum, const string& method)
+{
+  Commands commands;
+  for(;!_uris.empty() && commands.size() < (size_t)maxNum; _uris.pop_front()) {
+    string uri = _uris.front();
+    _spentUris.push_back(uri);
+    RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest();
+    req->setReferer(_option->get(PREF_REFERER));
+    req->setMethod(method);
+    if(req->setUrl(uri)) {
+      commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
+    } else {
+      logger->info(_("Unrecognized URL or unsupported protocol: %s\n"),
+		   req->getUrl().c_str());
+    }
+  }
+  return commands;
+}
+
+void RequestGroup::initBitfield()
+{
+  _segmentMan->initBitfield(_option->getAsInt(PREF_SEGMENT_SIZE),
+			    _segmentMan->totalSize);
+}
+
+void RequestGroup::openExistingFile()
+{
+  _segmentMan->diskWriter->openExistingFile(_segmentMan->getFilePath());
+}
+
+void RequestGroup::markAllPiecesDone()
+{
+  _segmentMan->markAllPiecesDone();
+}
+
+void RequestGroup::markExistingPiecesDone()
+{
+  _segmentMan->markPieceDone(File(_segmentMan->getFilePath()).size());
+}
+
+void RequestGroup::markPieceDone(int64_t length)
+{
+  _segmentMan->markPieceDone(length);
+}
+
+void RequestGroup::shouldCancelDownloadForSafety()
+{
+  if(_segmentMan->shouldCancelDownloadForSafety()) {
+    logger->notice(MSG_FILE_ALREADY_EXISTS,
+		   _segmentMan->getFilePath().c_str(),
+		   _segmentMan->getSegmentFilePath().c_str());
+    
+    throw new FatalException(EX_FILE_ALREADY_EXISTS,
+			     _segmentMan->getFilePath().c_str(),
+			     _segmentMan->getSegmentFilePath().c_str());
+  }
+}
+
+void RequestGroup::initAndOpenFile()
+{
+  _segmentMan->diskWriter->initAndOpenFile(_segmentMan->getFilePath());
+}
+  
+bool RequestGroup::needsFileAllocation() const
+{
+  return _option->get(PREF_FILE_ALLOCATION) == V_PREALLOC
+    && File(_segmentMan->getFilePath()).size() < _segmentMan->totalSize;
+}
+  
+bool RequestGroup::fileExists() const
+{
+  return _segmentMan->fileExists();
+}
+
+bool RequestGroup::segmentFileExists() const
+{
+  return _segmentMan->segmentFileExists();
+}
+
+string RequestGroup::getFilePath() const
+{
+  if(_segmentMan.isNull()) {
+    return "";
+  } else {
+    return _segmentMan->getFilePath();
+  }
+}
+
+void RequestGroup::loadAndOpenFile()
+{
+  bool segFileExists = segmentFileExists();
+  if(segFileExists) {
+    load();
+    openExistingFile();
+  } else if(fileExists() && _option->get(PREF_CONTINUE) == V_TRUE) {
+    File existingFile(getFilePath());
+    if(getTotalLength() < existingFile.size()) {
+      throw new DlAbortEx("Invalid file length. Cannot continue download %s: local %s, remote %s",
+		    getFilePath().c_str(),
+		    Util::llitos(existingFile.size()).c_str(),
+		    Util::llitos(getTotalLength()).c_str());
+    }
+    initBitfield();
+    openExistingFile();
+  } else {
+    shouldCancelDownloadForSafety();
+    initBitfield();
+    if(fileExists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
+      openExistingFile();
+    } else {
+      initAndOpenFile();
+    }
+  }
+}
+
+bool RequestGroup::downloadFinishedByFileLength()
+{
+  if(_segmentMan->segmentFileExists()) {
+    return false;
+  }
+  File existingFile(getFilePath());
+  if(existingFile.exists() &&
+     getTotalLength() == existingFile.size()) {
+    _segmentMan->downloadStarted = true;
+    initBitfield();
+    _segmentMan->markAllPiecesDone();
+    return true;
+  } else {
+    return false;
+  }
+}
+
+void RequestGroup::prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e)
+{
+  File existingFile(getFilePath());
+  if(existingFile.size() > 0 && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
+    CheckIntegrityCommand* command = new CheckIntegrityCommand(cuid, req, this, e);
+    command->initValidator();
+    e->commands.push_back(command);
+  } else if(needsFileAllocation()) {
+    e->_fileAllocationMan->pushFileAllocationEntry(new FileAllocationEntry(cuid, req, this));
+  } else {
+    int32_t numCommandsToGenerate = 15;
+    Commands commands = getNextCommand(e, numCommandsToGenerate);
+    Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, this, e);
+    commands.push_front(command);
+    e->addCommand(commands);
+  }
+}
+
+void RequestGroup::validateFilename(const string& expectedFilename,
+				    const string& actualFilename) const
+{
+  if(expectedFilename.empty()) {
+    return;
+  }
+  if(expectedFilename != actualFilename) {
+    throw new DlAbortEx(EX_FILENAME_MISMATCH,
+			expectedFilename.c_str(),
+			actualFilename.c_str());
+  }
+}
+
+void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
+				       int64_t actualTotalLength) const
+{
+  if(expectedTotalLength <= 0) {
+    return;
+  }
+  if(expectedTotalLength != actualTotalLength) {
+    throw new DlAbortEx(EX_SIZE_MISMATCH,
+			expectedTotalLength,
+			actualTotalLength);
+  }
+}
+
+void RequestGroup::validateFilename(const string& actualFilename) const
+{
+  validateFilename(_segmentMan->filename, actualFilename);
+}
+
+void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
+{
+  validateTotalLength(_segmentMan->totalSize, actualTotalLength);
+}
+
+void RequestGroup::validateFilenameByHint(const string& actualFilename) const
+{
+  validateFilename(_hintFilename, actualFilename);
+}
+
+void RequestGroup::validateTotalLengthByHint(int64_t actualTotalLength) const
+{
+  validateTotalLength(_hintTotalLength, actualTotalLength);
+}

+ 245 - 0
src/RequestGroup.h

@@ -0,0 +1,245 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_REQUEST_GROUP_H_
+#define _D_REQUEST_GROUP_H_
+
+#include "common.h"
+#include "SegmentMan.h"
+#include "LogFactory.h"
+#include "Command.h"
+#include "ChunkChecksum.h"
+#include "Checksum.h"
+#include "SegmentManFactory.h"
+#include "DefaultSegmentManFactory.h"
+
+class DownloadEngine;
+
+class RequestGroup {
+private:
+  int64_t _hintTotalLength;
+  string _hintFilename;
+  Strings _uris;
+  Strings _spentUris;
+  SegmentManHandle _segmentMan;
+  SegmentManFactoryHandle _segmentManFactory;
+  const Option* _option;
+  const Logger* logger;
+  ChunkChecksumHandle _chunkChecksum;
+  Checksum _checksum;
+
+  void validateFilename(const string& expectedFilename,
+			const string& actualFilename) const;
+
+  void validateTotalLength(int64_t expectedTotalLength,
+			   int64_t actualTotalLength) const;
+
+public:
+
+  int32_t numConnection;
+
+  bool isTorrent;
+
+  RequestGroup(const Strings& uris, const Option* option):
+    _uris(uris), _segmentMan(0), _segmentManFactory(new DefaultSegmentManFactory(option)), _option(option), logger(LogFactory::getInstance()), _chunkChecksum(0), numConnection(0), isTorrent(false) {}
+
+  RequestGroup(const string& uri, const Option* option):
+    _segmentMan(0), _segmentManFactory(new DefaultSegmentManFactory(option)), _option(option), logger(LogFactory::getInstance()), _chunkChecksum(0), numConnection(0), isTorrent(false)
+  {
+    _uris.push_back(uri);
+  }
+
+  /**
+   * Reinitializes SegmentMan based on current property values and
+   * returns new one.
+   */
+  SegmentManHandle initSegmentMan();
+
+  SegmentManHandle getSegmentMan() const
+  {
+    return _segmentMan;
+  }
+
+  Commands getNextCommand(DownloadEngine* e, int32_t maxNum, const string& method = "GET");
+  
+  void addURI(const string& uri)
+  {
+    _uris.push_back(uri);
+  }
+
+  void setChunkChecksum(const ChunkChecksumHandle& chunkChecksum)
+  {
+    _chunkChecksum = chunkChecksum;
+  }
+
+  ChunkChecksumHandle getChunkChecksum() const
+  {
+    return _chunkChecksum;
+  }
+
+  void initBitfield();
+
+  void openExistingFile();
+
+  void markAllPiecesDone();
+
+  void markExistingPiecesDone();
+
+  void markPieceDone(int64_t length);
+
+  void shouldCancelDownloadForSafety();
+
+  void initAndOpenFile();
+  
+  bool needsFileAllocation() const;
+  
+  bool downloadFinished() const
+  {
+    if(_segmentMan.isNull()) {
+      return false;
+    } else {
+      return _segmentMan->finished();
+    }
+  }
+
+  void load()
+  {
+    _segmentMan->load();
+  }
+
+  void save()
+  {
+    _segmentMan->save();
+  }
+
+  void remove()
+  {
+    _segmentMan->remove();
+  }
+
+  void closeFile()
+  {
+    _segmentMan->diskWriter->closeFile();
+  }
+
+  bool fileExists() const;
+
+  bool segmentFileExists() const;
+
+  string getFilePath() const;
+
+  int64_t getTotalLength() const
+  {
+    return _segmentMan->totalSize;
+  }
+
+  int64_t getDownloadLength() const
+  {
+    return _segmentMan->getDownloadLength();
+  }
+
+  void loadAndOpenFile();
+
+  void prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e);
+
+  bool downloadFinishedByFileLength();
+
+  void setChecksum(const Checksum& checksum)
+  {
+    _checksum = checksum;
+  }
+
+  const string& getHintFilename() const
+  {
+    return _hintFilename;
+  }
+
+  void setHintFilename(const string& filename)
+  {
+    _hintFilename = filename;
+  }
+
+  int64_t getHintTotalLength() const
+  {
+    return _hintTotalLength;
+  }
+
+  void setHintTotalLength(int64_t totalLength)
+  {
+    _hintTotalLength = totalLength;
+  }
+
+  const Strings& getRemainingUris() const
+  {
+    return _uris;
+  }
+
+  const Strings& getSpentUris() const
+  {
+    return _spentUris;
+  }
+
+  Strings getUris() const
+  {
+    Strings temp(_spentUris.begin(), _spentUris.end());
+    temp.insert(temp.end(), _uris.begin(), _uris.end());
+    return temp;
+  }
+
+  /**
+   * Compares expected filename with specified actualFilename.
+   * The expected filename refers to SegmentMan::filename.
+   */
+  void validateFilename(const string& actualFilename) const;
+
+  void validateTotalLength(int64_t actualTotalLength) const;
+
+  /**
+   * Compares expected filename with specified actualFilename.
+   * The expected filename refers to RequestGroup::hintFilename.
+   */
+  void validateFilenameByHint(const string& actualFilename) const;
+
+  void validateTotalLengthByHint(int64_t actualTotalLength) const;
+
+  void setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
+  {
+    _segmentManFactory = segmentManFactory;
+  }
+};
+
+typedef SharedHandle<RequestGroup> RequestGroupHandle;
+typedef deque<RequestGroupHandle> RequestGroups;
+
+#endif // _D_REQUEST_GROUP_H_

+ 120 - 0
src/RequestGroupMan.cc

@@ -0,0 +1,120 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "RequestGroupMan.h"
+#include "DownloadEngine.h"
+#include <iomanip>
+
+void RequestGroupMan::removeStoppedGroup()
+{
+  int32_t count = 0;
+  RequestGroups temp;
+  for(RequestGroups::iterator itr = _requestGroups.begin();
+      itr != _requestGroups.end(); ++itr) {
+    if((*itr)->numConnection > 0) {
+      temp.push_back(*itr);
+    } else {
+      (*itr)->closeFile();
+      if((*itr)->downloadFinished()) {
+	_logger->notice("Download complete: %s", (*itr)->getFilePath().c_str());
+	(*itr)->remove();
+      } else {
+	(*itr)->save();
+      }
+      ++count;
+    }
+  }
+  _requestGroups = temp;
+  if(count > 0) {
+    _logger->debug("%d RequestGroup(s) deleted.", count);
+  }
+}
+
+void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
+{
+  removeStoppedGroup();
+
+  int32_t count = 0;
+  for(int32_t num = _maxSimultaneousDownloads-_requestGroups.size();
+      num > 0 && _reservedGroups.size() > 0; --num) {
+
+    RequestGroupHandle groupToAdd = _reservedGroups.front();
+    _reservedGroups.pop_front();
+    
+    _requestGroups.push_back(groupToAdd);
+    groupToAdd->initSegmentMan();
+    Commands commands = groupToAdd->getNextCommand(e, 1);
+    count += commands.size();
+    e->addCommand(commands);
+  }
+  if(count > 0) {
+    _logger->debug("%d RequestGroup(s) added.", count);
+  }
+}
+
+void RequestGroupMan::showDownloadResults(ostream& o) const
+{
+  // Download Results:
+  // idx|stat|path/length
+  // ===+====+=======================================================================
+  o << "\n"
+    <<_("Download Results:") << "\n"
+    << "idx|stat|path/URI" << "\n"
+    << "===+====+======================================================================" << "\n";
+  int32_t count = 0;
+  for(RequestGroups::const_iterator itr = _requestGroups.begin();
+      itr != _requestGroups.end(); ++itr) {
+    o << setw(3) << ++count << "|";
+    if((*itr)->downloadFinished()) {
+      o << "OK  ";
+    } else {
+      o << "ERR ";
+    }
+    o << "|";
+    if((*itr)->downloadFinished()) {
+      o << (*itr)->getFilePath();
+    } else {
+      Strings uris = (*itr)->getUris();
+      if(uris.empty()) {
+	o << "n/a";
+      } else {
+	o << uris.front();
+	if(uris.size() > 1) {
+	  o << " (" << uris.size()-1 << "more)";
+	}
+      }
+    }
+    o << "\n";
+  }
+}

+ 164 - 0
src/RequestGroupMan.h

@@ -0,0 +1,164 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_REQUEST_GROUP_MAN_H_
+#define _D_REQUEST_GROUP_MAN_H_
+
+#include "common.h"
+#include "RequestGroup.h"
+#include "LogFactory.h"
+
+class DownloadEngine;
+
+class RequestGroupMan {
+private:
+  RequestGroups _requestGroups;
+  RequestGroups _reservedGroups;
+  const Logger* _logger;
+  int32_t _maxSimultaneousDownloads;
+public:
+  RequestGroupMan(const RequestGroups& requestGroups = RequestGroups(), int32_t maxSimultaneousDownloads = 1):
+    _requestGroups(requestGroups),
+    _logger(LogFactory::getInstance()),
+    _maxSimultaneousDownloads(maxSimultaneousDownloads) {}
+
+  bool downloadFinished()
+  {
+    for(RequestGroups::iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      if((*itr)->numConnection > 0 || !(*itr)->getSegmentMan()->finished()) {
+	return false;
+      }
+    }
+    return true;
+  }
+
+  void save()
+  {
+    for(RequestGroups::iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      if(!(*itr)->getSegmentMan()->finished()) {
+	(*itr)->getSegmentMan()->save();
+      }
+    }
+  }
+
+  void closeFile()
+  {
+    for(RequestGroups::iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      (*itr)->getSegmentMan()->diskWriter->closeFile();
+    }
+  }
+  
+  int64_t getDownloadLength() const
+  {
+    int64_t downloadLength = 0;
+    for(RequestGroups::const_iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      downloadLength += (*itr)->getSegmentMan()->getDownloadLength();
+    }
+    return downloadLength;
+  }
+
+  int64_t getTotalLength() const
+  {
+    int64_t totalLength = 0;
+    for(RequestGroups::const_iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      totalLength += (*itr)->getSegmentMan()->totalSize;
+    }
+    return totalLength;
+  }
+
+  Commands getInitialCommands(DownloadEngine* e) const
+  {
+    Commands commands;
+    for(RequestGroups::const_iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      (*itr)->initSegmentMan();
+      commands.push_back((*itr)->getNextCommand(e, 1).front());
+    }
+    return commands;
+  }
+
+  void removeStoppedGroup();
+
+  void fillRequestGroupFromReserver(DownloadEngine* e);
+
+  void addRequestGroup(const RequestGroupHandle& group)
+  {
+    _requestGroups.push_back(group);
+  }
+
+  void addReservedGroup(const RequestGroups& groups)
+  {
+    _reservedGroups.insert(_reservedGroups.end(), groups.begin(), groups.end());
+  }
+
+  void addReservedGroup(const RequestGroupHandle& group)
+  {
+    _reservedGroups.push_back(group);
+  }
+
+  int32_t countRequestGroup() const
+  {
+    return _requestGroups.size();
+  }
+		  
+  RequestGroupHandle getRequestGroup(int32_t index) const
+  {
+    if(index < (int32_t)_requestGroups.size()) {
+      return _requestGroups[index];
+    } else {
+      return 0;
+    }
+  }
+		  
+  void showDownloadResults(ostream& o) const;
+
+  int32_t getErrors() const
+  {
+    int32_t errors = 0;
+    for(RequestGroups::const_iterator itr = _requestGroups.begin();
+	itr != _requestGroups.end(); ++itr) {
+      errors += (*itr)->getSegmentMan()->errors;
+    }
+    return errors;
+  }
+};
+
+typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
+
+#endif // _D_REQUEST_GROUP_MAN_H_

+ 50 - 0
src/SegmentManFactory.h

@@ -0,0 +1,50 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_SEGMENT_MAN_FACTORY_H_
+#define _D_SEGMENT_MAN_FACTORY_H_
+
+#include "common.h"
+#include "SegmentMan.h"
+
+class SegmentManFactory {
+public:
+  virtual ~SegmentManFactory() {}
+
+  virtual SegmentManHandle createNewInstance() = 0;
+};
+
+typedef SharedHandle<SegmentManFactory> SegmentManFactoryHandle;
+
+#endif // _D_SEGMENT_MAN_FACTORY_H_

+ 45 - 0
src/TrackerSegmentManFactory.cc

@@ -0,0 +1,45 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "TrackerSegmentManFactory.h"
+#include "ByteArrayDiskWriter.h"
+
+SegmentManHandle TrackerSegmentManFactory::createNewInstance()
+{
+  SegmentManHandle segmentMan = new SegmentMan();
+  segmentMan->diskWriter = new ByteArrayDiskWriter();
+  segmentMan->option = _option;
+  segmentMan->ufilename = "[tracker.announce]";
+  return segmentMan;
+}

+ 51 - 0
src/TrackerSegmentManFactory.h

@@ -0,0 +1,51 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_TRACKER_SEGMENT_MAN_FACTORY_H_
+#define _D_TRACKER_SEGMENT_MAN_FACTORY_H_
+
+#include "AbstractSegmentManFactory.h"
+
+class TrackerSegmentManFactory : public AbstractSegmentManFactory {
+public:
+  TrackerSegmentManFactory(const Option* option):AbstractSegmentManFactory(option) {}
+
+  virtual ~TrackerSegmentManFactory() {}
+
+  virtual SegmentManHandle createNewInstance();
+};
+
+typedef SharedHandle<TrackerSegmentManFactory> TrackerSegmentManFactoryHandle;
+
+#endif // _D_TRACKER_SEGMENT_MAN_FACTORY_H_

+ 54 - 0
src/UriFileListParser.cc

@@ -0,0 +1,54 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "UriFileListParser.h"
+#include "Util.h"
+
+bool UriFileListParser::hasNext() const
+{
+  return _ifs;
+}
+
+Strings UriFileListParser::next()
+{
+  string line;
+  while(getline(_ifs, line)) {
+    if(Util::trim(line) != "") {
+      Strings uris;
+      Util::slice(uris, line, '\t', true);
+      return uris;
+    }
+  }
+  return Strings();
+}

+ 53 - 0
src/UriFileListParser.h

@@ -0,0 +1,53 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_URI_FILE_LIST_PARSER_H_
+#define _D_URI_FILE_LIST_PARSER_H_
+
+#include "common.h"
+#include <fstream>
+
+class UriFileListParser {
+private:
+  string _filename;
+  ifstream _ifs;
+public:
+  UriFileListParser(const string& filename):_filename(filename), _ifs(filename.c_str()) {}
+
+  bool hasNext() const;
+
+  Strings next();
+};
+
+#endif // _D_URI_FILE_LIST_PARSER_H_