2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DownloadEngine.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "Util.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2006-08-07 16:28:41 +00:00
|
|
|
#include "TimeA2.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2006-05-24 15:18:58 +00:00
|
|
|
#include <errno.h>
|
2006-02-28 02:25:45 +00:00
|
|
|
#include <algorithm>
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
DownloadEngine::DownloadEngine():noWait(false), segmentMan(0) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger = LogFactory::getInstance();
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
DownloadEngine::~DownloadEngine() {
|
2006-08-07 16:05:00 +00:00
|
|
|
delete segmentMan;
|
|
|
|
cleanQueue();
|
2006-05-09 15:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::cleanQueue() {
|
|
|
|
for_each(commands.begin(), commands.end(), Deleter());
|
|
|
|
commands.clear();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::run() {
|
2006-03-02 02:54:49 +00:00
|
|
|
initStatistics();
|
2006-06-12 16:55:08 +00:00
|
|
|
Time cp;
|
2006-08-11 12:29:55 +00:00
|
|
|
cp.setTimeInSec(0);
|
2006-08-14 15:03:38 +00:00
|
|
|
Commands activeCommands;
|
2006-02-17 13:35:04 +00:00
|
|
|
while(!commands.empty()) {
|
2006-06-12 16:55:08 +00:00
|
|
|
if(cp.elapsed(1)) {
|
|
|
|
cp.reset();
|
2006-05-24 15:18:58 +00:00
|
|
|
int max = commands.size();
|
|
|
|
for(int i = 0; i < max; i++) {
|
|
|
|
Command* com = commands.front();
|
|
|
|
commands.pop_front();
|
|
|
|
if(com->execute()) {
|
2006-07-19 17:07:45 +00:00
|
|
|
delete com;
|
2006-05-24 15:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-08-14 15:03:38 +00:00
|
|
|
for(Commands::iterator itr = activeCommands.begin();
|
|
|
|
itr != activeCommands.end(); itr++) {
|
|
|
|
Commands::iterator comItr = find(commands.begin(), commands.end(),
|
|
|
|
*itr);
|
2006-07-19 17:07:45 +00:00
|
|
|
assert(comItr != commands.end());
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command = *itr;
|
2006-07-19 17:07:45 +00:00
|
|
|
commands.erase(comItr);
|
2006-08-14 15:03:38 +00:00
|
|
|
if(command->execute()) {
|
|
|
|
delete command;
|
2006-05-24 15:18:58 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-24 11:59:18 +00:00
|
|
|
afterEachIteration();
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands.clear();
|
2006-02-17 13:35:04 +00:00
|
|
|
if(!noWait && !commands.empty()) {
|
2006-08-14 15:03:38 +00:00
|
|
|
waitData(activeCommands);
|
2006-02-21 12:27:17 +00:00
|
|
|
}
|
|
|
|
noWait = false;
|
2006-03-02 02:54:49 +00:00
|
|
|
calculateStatistics();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
onEndOfRun();
|
2006-03-02 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
void DownloadEngine::shortSleep() const {
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1000;
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
select(0, &rfds, NULL, NULL, &tv);
|
2006-03-02 02:54:49 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
class SetDescriptor {
|
|
|
|
private:
|
|
|
|
int* max_ptr;
|
2006-08-07 16:05:00 +00:00
|
|
|
fd_set* rfds_ptr;
|
|
|
|
fd_set* wfds_ptr;
|
2006-07-19 17:07:45 +00:00
|
|
|
public:
|
2006-08-07 16:05:00 +00:00
|
|
|
SetDescriptor(int* max_ptr, fd_set* rfds_ptr, fd_set* wfds_ptr):
|
|
|
|
max_ptr(max_ptr),
|
|
|
|
rfds_ptr(rfds_ptr),
|
|
|
|
wfds_ptr(wfds_ptr) {}
|
|
|
|
|
|
|
|
void operator()(const SocketEntry& entry) {
|
|
|
|
int fd = entry.socket->getSockfd();
|
|
|
|
switch(entry.type) {
|
|
|
|
case SocketEntry::TYPE_RD:
|
|
|
|
FD_SET(fd, rfds_ptr);
|
|
|
|
break;
|
|
|
|
case SocketEntry::TYPE_WR:
|
|
|
|
FD_SET(fd, wfds_ptr);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-19 17:07:45 +00:00
|
|
|
if(*max_ptr < fd) {
|
|
|
|
*max_ptr = fd;
|
|
|
|
}
|
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2006-08-11 12:29:55 +00:00
|
|
|
void operator()(const NameResolverEntry& entry) {
|
|
|
|
int tempFd = entry.nameResolver->getFds(rfds_ptr, wfds_ptr);
|
|
|
|
if(*max_ptr < tempFd) {
|
|
|
|
*max_ptr = tempFd;
|
|
|
|
}
|
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2006-07-19 17:07:45 +00:00
|
|
|
};
|
|
|
|
|
2006-08-14 15:03:38 +00:00
|
|
|
class AccumulateActiveCommand {
|
2006-07-19 17:07:45 +00:00
|
|
|
private:
|
2006-08-14 15:03:38 +00:00
|
|
|
Commands* activeCommands_ptr;
|
2006-08-07 16:05:00 +00:00
|
|
|
fd_set* rfds_ptr;
|
|
|
|
fd_set* wfds_ptr;
|
2006-07-19 17:07:45 +00:00
|
|
|
public:
|
2006-08-14 15:03:38 +00:00
|
|
|
AccumulateActiveCommand(Commands* activeCommands_ptr,
|
2006-08-07 16:05:00 +00:00
|
|
|
fd_set* rfds_ptr,
|
|
|
|
fd_set* wfds_ptr):
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands_ptr(activeCommands_ptr),
|
2006-08-07 16:05:00 +00:00
|
|
|
rfds_ptr(rfds_ptr),
|
|
|
|
wfds_ptr(wfds_ptr) {}
|
|
|
|
|
|
|
|
void operator()(const SocketEntry& entry) {
|
|
|
|
if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr) ||
|
|
|
|
FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands_ptr->push_back(entry.command);
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
/*
|
|
|
|
switch(entry.type) {
|
|
|
|
case SocketEntry::TYPE_RD:
|
|
|
|
if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr)) {
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands_ptr->push_back(entry.command);
|
2006-08-07 16:05:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SocketEntry::TYPE_WR:
|
|
|
|
if(FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands_ptr->push_back(entry.command);
|
2006-08-07 16:05:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2006-08-11 12:29:55 +00:00
|
|
|
void operator()(const NameResolverEntry& entry) {
|
|
|
|
entry.nameResolver->process(rfds_ptr, wfds_ptr);
|
|
|
|
switch(entry.nameResolver->getStatus()) {
|
|
|
|
case NameResolver::STATUS_SUCCESS:
|
|
|
|
case NameResolver::STATUS_ERROR:
|
2006-08-14 15:03:38 +00:00
|
|
|
activeCommands_ptr->push_back(entry.command);
|
2006-08-11 12:29:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2006-07-19 17:07:45 +00:00
|
|
|
};
|
|
|
|
|
2006-08-14 15:03:38 +00:00
|
|
|
void DownloadEngine::waitData(Commands& activeCommands) {
|
2006-02-17 13:35:04 +00:00
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2006-05-24 15:18:58 +00:00
|
|
|
int retval = 0;
|
2006-08-07 16:05:00 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
memcpy(&rfds, &rfdset, sizeof(fd_set));
|
|
|
|
memcpy(&wfds, &wfdset, sizeof(fd_set));
|
|
|
|
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
|
2006-05-24 15:18:58 +00:00
|
|
|
if(retval > 0) {
|
2006-08-07 16:05:00 +00:00
|
|
|
for_each(socketEntries.begin(), socketEntries.end(),
|
2006-08-14 15:03:38 +00:00
|
|
|
AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2006-08-11 12:29:55 +00:00
|
|
|
for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
|
2006-08-14 15:03:38 +00:00
|
|
|
AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2006-08-14 15:03:38 +00:00
|
|
|
sort(activeCommands.begin(), activeCommands.end());
|
|
|
|
activeCommands.erase(unique(activeCommands.begin(),
|
|
|
|
activeCommands.end()),
|
|
|
|
activeCommands.end());
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-28 14:06:47 +00:00
|
|
|
void DownloadEngine::updateFdSet() {
|
|
|
|
fdmax = 0;
|
|
|
|
FD_ZERO(&rfdset);
|
|
|
|
FD_ZERO(&wfdset);
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2006-08-11 12:29:55 +00:00
|
|
|
for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
|
|
|
|
SetDescriptor(&fdmax, &rfdset, &wfdset));
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2006-08-07 16:05:00 +00:00
|
|
|
for_each(socketEntries.begin(), socketEntries.end(),
|
|
|
|
SetDescriptor(&fdmax, &rfdset, &wfdset));
|
2006-07-28 14:06:47 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
bool DownloadEngine::addSocket(const SocketEntry& entry) {
|
|
|
|
SocketEntries::iterator itr =
|
|
|
|
find(socketEntries.begin(), socketEntries.end(), entry);
|
|
|
|
if(itr == socketEntries.end()) {
|
|
|
|
socketEntries.push_back(entry);
|
2006-07-28 14:06:47 +00:00
|
|
|
updateFdSet();
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
|
|
|
|
SocketEntries::iterator itr =
|
|
|
|
find(socketEntries.begin(), socketEntries.end(), entry);
|
|
|
|
if(itr == socketEntries.end()) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
2006-07-19 17:07:45 +00:00
|
|
|
} else {
|
2006-08-07 16:05:00 +00:00
|
|
|
socketEntries.erase(itr);
|
2006-07-28 14:06:47 +00:00
|
|
|
updateFdSet();
|
2006-07-19 17:07:45 +00:00
|
|
|
return true;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
|
2006-08-07 16:05:00 +00:00
|
|
|
return addSocket(entry);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
|
2006-08-07 16:05:00 +00:00
|
|
|
return deleteSocket(entry);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
|
2006-08-07 16:05:00 +00:00
|
|
|
return addSocket(entry);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
|
2006-08-07 16:05:00 +00:00
|
|
|
return deleteSocket(entry);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-08-11 12:29:55 +00:00
|
|
|
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2006-08-11 12:29:55 +00:00
|
|
|
bool DownloadEngine::addNameResolverCheck(const NameResolverHandle& resolver,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
NameResolverEntry entry(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
|
|
|
|
nameResolverEntries.end(),
|
|
|
|
entry);
|
|
|
|
if(itr == nameResolverEntries.end()) {
|
|
|
|
nameResolverEntries.push_back(entry);
|
|
|
|
updateFdSet();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
|
2006-08-14 15:03:38 +00:00
|
|
|
Command* command) {
|
|
|
|
NameResolverEntry entry(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
|
|
|
|
nameResolverEntries.end(),
|
|
|
|
entry);
|
|
|
|
if(itr == nameResolverEntries.end()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
nameResolverEntries.erase(itr);
|
|
|
|
updateFdSet();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|