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
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
|
|
|
|
class FindCommand {
|
|
|
|
private:
|
|
|
|
CommandUuid uuid;
|
|
|
|
public:
|
|
|
|
FindCommand(const CommandUuid& uuid):uuid(uuid) {}
|
|
|
|
|
|
|
|
bool operator()(const Command* command) {
|
|
|
|
if(command->getUuid() == uuid) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-07 16:05:00 +00:00
|
|
|
CommandUuids activeUuids;
|
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-07 16:05:00 +00:00
|
|
|
for(CommandUuids::iterator itr = activeUuids.begin();
|
|
|
|
itr != activeUuids.end(); itr++) {
|
2006-07-19 17:07:45 +00:00
|
|
|
Commands::iterator comItr = find_if(commands.begin(), commands.end(),
|
|
|
|
FindCommand(*itr));
|
|
|
|
assert(comItr != commands.end());
|
|
|
|
Command* com = *comItr;
|
|
|
|
commands.erase(comItr);
|
|
|
|
if(com->execute()) {
|
|
|
|
delete com;
|
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-07 16:05:00 +00:00
|
|
|
activeUuids.clear();
|
2006-02-17 13:35:04 +00:00
|
|
|
if(!noWait && !commands.empty()) {
|
2006-08-07 16:05:00 +00:00
|
|
|
waitData(activeUuids);
|
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-07 16:05:00 +00:00
|
|
|
class AccumulateActiveUuid {
|
2006-07-19 17:07:45 +00:00
|
|
|
private:
|
2006-08-07 16:05:00 +00:00
|
|
|
CommandUuids* activeUuids_ptr;
|
|
|
|
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
|
|
|
AccumulateActiveUuid(CommandUuids* activeUuids_ptr,
|
|
|
|
fd_set* rfds_ptr,
|
|
|
|
fd_set* wfds_ptr):
|
|
|
|
activeUuids_ptr(activeUuids_ptr),
|
|
|
|
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)) {
|
|
|
|
activeUuids_ptr->push_back(entry.commandUuid);
|
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)) {
|
|
|
|
activeUuids_ptr->push_back(entry.commandUuid);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SocketEntry::TYPE_WR:
|
|
|
|
if(FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
|
|
|
|
activeUuids_ptr->push_back(entry.commandUuid);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
void DownloadEngine::waitData(CommandUuids& activeUuids) {
|
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(),
|
|
|
|
AccumulateActiveUuid(&activeUuids, &rfds, &wfds));
|
|
|
|
|
|
|
|
sort(activeUuids.begin(), activeUuids.end());
|
|
|
|
activeUuids.erase(unique(activeUuids.begin(),
|
|
|
|
activeUuids.end()),
|
|
|
|
activeUuids.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-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,
|
|
|
|
const CommandUuid& commandUuid) {
|
2006-08-07 16:05:00 +00:00
|
|
|
SocketEntry entry(socket, commandUuid, SocketEntry::TYPE_RD);
|
|
|
|
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,
|
|
|
|
const CommandUuid& commandUuid) {
|
2006-08-07 16:05:00 +00:00
|
|
|
SocketEntry entry(socket, commandUuid, SocketEntry::TYPE_RD);
|
|
|
|
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,
|
|
|
|
const CommandUuid& commandUuid) {
|
2006-08-07 16:05:00 +00:00
|
|
|
SocketEntry entry(socket, commandUuid, SocketEntry::TYPE_WR);
|
|
|
|
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,
|
|
|
|
const CommandUuid& commandUuid) {
|
2006-08-07 16:05:00 +00:00
|
|
|
SocketEntry entry(socket, commandUuid, SocketEntry::TYPE_WR);
|
|
|
|
return deleteSocket(entry);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|