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-06-12 16:55:08 +00:00
|
|
|
#include "Time.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-04-17 16:17:20 +00:00
|
|
|
DownloadEngine::DownloadEngine():noWait(false), segmentMan(NULL) {
|
|
|
|
logger = LogFactory::getInstance();
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
DownloadEngine::~DownloadEngine() {
|
2006-05-09 15:54:14 +00:00
|
|
|
if(segmentMan != NULL) {
|
|
|
|
delete segmentMan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-19 17:07:45 +00:00
|
|
|
CommandUuids activeCommandUuids;
|
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-07-19 17:07:45 +00:00
|
|
|
for(CommandUuids::iterator itr = activeCommandUuids.begin();
|
|
|
|
itr != activeCommandUuids.end(); itr++) {
|
|
|
|
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-07-19 17:07:45 +00:00
|
|
|
activeCommandUuids.clear();
|
2006-02-17 13:35:04 +00:00
|
|
|
if(!noWait && !commands.empty()) {
|
2006-07-19 17:07:45 +00:00
|
|
|
waitData(activeCommandUuids);
|
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:
|
|
|
|
fd_set* fds_ptr;
|
|
|
|
int* max_ptr;
|
|
|
|
public:
|
|
|
|
SetDescriptor(int* max_ptr, fd_set* fds_ptr)
|
|
|
|
:fds_ptr(fds_ptr), max_ptr(max_ptr) {}
|
|
|
|
|
2006-07-28 14:06:47 +00:00
|
|
|
void operator()(const SockCmdMap::value_type& pa) {
|
2006-07-19 17:07:45 +00:00
|
|
|
int fd = pa.first->getSockfd();
|
|
|
|
FD_SET(fd, fds_ptr);
|
|
|
|
if(*max_ptr < fd) {
|
|
|
|
*max_ptr = fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AccumulateActiveCommandUuid {
|
|
|
|
private:
|
|
|
|
CommandUuids* activeCommandUuids_ptr;
|
|
|
|
fd_set* fds_ptr;
|
|
|
|
public:
|
|
|
|
AccumulateActiveCommandUuid(CommandUuids* activeCommandUuids_ptr,
|
|
|
|
fd_set* fds_ptr)
|
|
|
|
:activeCommandUuids_ptr(activeCommandUuids_ptr), fds_ptr(fds_ptr) {}
|
|
|
|
|
2006-07-28 14:06:47 +00:00
|
|
|
void operator()(const SockCmdMap::value_type& pa) {
|
2006-07-19 17:07:45 +00:00
|
|
|
if(FD_ISSET(pa.first->getSockfd(), fds_ptr)) {
|
|
|
|
activeCommandUuids_ptr->push_back(pa.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DownloadEngine::waitData(CommandUuids& activeCommandUuids) {
|
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;
|
|
|
|
while(1) {
|
|
|
|
struct timeval tv;
|
|
|
|
|
2006-07-28 14:06:47 +00:00
|
|
|
memcpy(&rfds, &rfdset, sizeof(fd_set));
|
|
|
|
memcpy(&wfds, &wfdset, sizeof(fd_set));
|
2006-07-19 17:07:45 +00:00
|
|
|
|
2006-05-24 15:18:58 +00:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2006-07-28 14:06:47 +00:00
|
|
|
retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
|
2006-05-24 15:18:58 +00:00
|
|
|
if(retval != -1 || errno != EINTR) {
|
|
|
|
break;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-24 15:18:58 +00:00
|
|
|
if(retval > 0) {
|
2006-07-19 17:07:45 +00:00
|
|
|
for_each(rsockmap.begin(), rsockmap.end(),
|
|
|
|
AccumulateActiveCommandUuid(&activeCommandUuids, &rfds));
|
|
|
|
for_each(wsockmap.begin(), wsockmap.end(),
|
|
|
|
AccumulateActiveCommandUuid(&activeCommandUuids, &wfds));
|
|
|
|
sort(activeCommandUuids.begin(), activeCommandUuids.end());
|
|
|
|
activeCommandUuids.erase(unique(activeCommandUuids.begin(),
|
|
|
|
activeCommandUuids.end()),
|
|
|
|
activeCommandUuids.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);
|
|
|
|
for_each(rsockmap.begin(), rsockmap.end(), SetDescriptor(&fdmax, &rfdset));
|
|
|
|
for_each(wsockmap.begin(), wsockmap.end(), SetDescriptor(&fdmax, &wfdset));
|
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::addSocket(SockCmdMap& sockmap,
|
|
|
|
const SocketHandle& socket,
|
|
|
|
const CommandUuid& commandUuid) {
|
|
|
|
SockCmdMap::iterator itr = find_if(sockmap.begin(), sockmap.end(),
|
|
|
|
PairFind<SocketHandle, CommandUuid>(socket, commandUuid));
|
|
|
|
if(itr == sockmap.end()) {
|
|
|
|
SockCmdMap::value_type vt(socket, commandUuid);
|
|
|
|
sockmap.insert(vt);
|
2006-07-28 14:06:47 +00:00
|
|
|
updateFdSet();
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::deleteSocket(SockCmdMap& sockmap,
|
|
|
|
const SocketHandle& socket,
|
|
|
|
const CommandUuid& commandUuid) {
|
|
|
|
SockCmdMap::iterator itr = find_if(sockmap.begin(), sockmap.end(),
|
|
|
|
PairFind<SocketHandle, CommandUuid>(socket, commandUuid));
|
|
|
|
if(itr == sockmap.end()) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
2006-07-19 17:07:45 +00:00
|
|
|
} else {
|
|
|
|
sockmap.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) {
|
|
|
|
return addSocket(rsockmap, socket, commandUuid);
|
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) {
|
|
|
|
return deleteSocket(rsockmap, socket, commandUuid);
|
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) {
|
|
|
|
return addSocket(wsockmap, socket, commandUuid);
|
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) {
|
|
|
|
return deleteSocket(wsockmap, socket, commandUuid);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|