2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DownloadEngine.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "StatCalc.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DownloadResult.h"
|
|
|
|
#include "StatCalc.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2006-08-07 16:28:41 +00:00
|
|
|
#include "TimeA2.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "a2time.h"
|
|
|
|
#include "Socket.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-05-11 09:36:39 +00:00
|
|
|
#include "a2functional.h"
|
2008-05-31 10:31:07 +00:00
|
|
|
#include "DlAbortEx.h"
|
2008-08-04 17:06:47 +00:00
|
|
|
#include "ServerStatMan.h"
|
2008-09-01 13:46:03 +00:00
|
|
|
#include "CookieStorage.h"
|
2008-09-24 17:01:57 +00:00
|
|
|
#include "A2STR.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
|
|
|
#include "AuthConfig.h"
|
2008-11-05 12:30:22 +00:00
|
|
|
#include "Request.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "EventPoll.h"
|
|
|
|
#include "Command.h"
|
2009-01-24 09:00:36 +00:00
|
|
|
#include "FileAllocationEntry.h"
|
2009-01-24 10:40:42 +00:00
|
|
|
#include "CheckIntegrityEntry.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
#include "BtProgressInfoFile.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
# include "BtRegistry.h"
|
|
|
|
# include "PeerStorage.h"
|
|
|
|
# include "PieceStorage.h"
|
|
|
|
# include "BtAnnounce.h"
|
|
|
|
# include "BtRuntime.h"
|
|
|
|
#endif // ENABLE_BITTORRENT
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
namespace global {
|
|
|
|
|
|
|
|
// Global clock, this clock is reseted before executeCommand() call to
|
|
|
|
// reduce the call gettimeofday() system call.
|
|
|
|
Time wallclock;
|
|
|
|
|
2007-12-07 13:33:59 +00:00
|
|
|
// 0 ... running
|
|
|
|
// 1 ... stop signal detected
|
|
|
|
// 2 ... stop signal processed by DownloadEngine
|
|
|
|
// 3 ... 2nd stop signal(force shutdown) detected
|
|
|
|
// 4 ... 2nd stop signal processed by DownloadEngine
|
|
|
|
volatile sig_atomic_t globalHaltRequested = 0;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
} // namespace global
|
|
|
|
|
2009-01-15 15:23:16 +00:00
|
|
|
DownloadEngine::DownloadEngine(const SharedHandle<EventPoll>& eventPoll):
|
|
|
|
_eventPoll(eventPoll),
|
|
|
|
logger(LogFactory::getInstance()),
|
|
|
|
_haltRequested(false),
|
|
|
|
_noWait(false),
|
|
|
|
_refreshInterval(DEFAULT_REFRESH_INTERVAL),
|
|
|
|
_cookieStorage(new CookieStorage()),
|
2009-07-25 14:07:46 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2009-01-15 15:23:16 +00:00
|
|
|
_btRegistry(new BtRegistry()),
|
2009-07-25 14:07:46 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2009-07-02 15:18:13 +00:00
|
|
|
_dnsCache(new DNSCache())
|
2010-01-17 11:55:22 +00:00
|
|
|
{
|
|
|
|
unsigned char sessionId[20];
|
|
|
|
util::generateRandomKey(sessionId);
|
|
|
|
_sessionId = std::string(&sessionId[0], & sessionId[sizeof(sessionId)]);
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
DownloadEngine::~DownloadEngine() {
|
2006-08-07 16:05:00 +00:00
|
|
|
cleanQueue();
|
2006-05-09 15:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::cleanQueue() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::for_each(commands.begin(), commands.end(), Deleter());
|
2006-05-09 15:54:14 +00:00
|
|
|
commands.clear();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-20 05:42:15 +00:00
|
|
|
static void executeCommand(std::deque<Command*>& commands,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command::STATUS statusFilter)
|
2007-03-16 14:21:29 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t max = commands.size();
|
2009-06-06 12:33:07 +00:00
|
|
|
for(size_t i = 0; i < max; ++i) {
|
2007-03-16 14:21:29 +00:00
|
|
|
Command* com = commands.front();
|
|
|
|
commands.pop_front();
|
|
|
|
if(com->statusMatch(statusFilter)) {
|
2008-05-09 14:36:06 +00:00
|
|
|
com->transitStatus();
|
2007-03-16 14:21:29 +00:00
|
|
|
if(com->execute()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
delete com;
|
|
|
|
com = 0;
|
2007-03-16 14:21:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commands.push_back(com);
|
|
|
|
}
|
2008-05-31 10:31:07 +00:00
|
|
|
if(com) {
|
|
|
|
com->clearIOEvents();
|
|
|
|
}
|
2007-03-16 14:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 15:23:16 +00:00
|
|
|
void DownloadEngine::run()
|
|
|
|
{
|
2006-06-12 16:55:08 +00:00
|
|
|
Time cp;
|
2006-08-11 12:29:55 +00:00
|
|
|
cp.setTimeInSec(0);
|
2008-04-23 10:03:52 +00:00
|
|
|
while(!commands.empty() || !_routineCommands.empty()) {
|
2010-03-06 08:29:53 +00:00
|
|
|
global::wallclock.reset();
|
|
|
|
if(cp.difference(global::wallclock) >= _refreshInterval) {
|
2008-11-11 16:05:42 +00:00
|
|
|
_refreshInterval = DEFAULT_REFRESH_INTERVAL;
|
2010-03-06 08:29:53 +00:00
|
|
|
cp = global::wallclock;
|
2008-04-20 05:42:15 +00:00
|
|
|
executeCommand(commands, Command::STATUS_ALL);
|
2006-05-24 15:18:58 +00:00
|
|
|
} else {
|
2008-04-20 05:42:15 +00:00
|
|
|
executeCommand(commands, Command::STATUS_ACTIVE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2008-04-20 05:42:15 +00:00
|
|
|
executeCommand(_routineCommands, Command::STATUS_ALL);
|
2006-03-24 11:59:18 +00:00
|
|
|
afterEachIteration();
|
2007-05-20 13:51:52 +00:00
|
|
|
if(!commands.empty()) {
|
2007-03-16 14:21:29 +00:00
|
|
|
waitData();
|
2006-02-21 12:27:17 +00:00
|
|
|
}
|
2008-04-20 05:42:15 +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
|
|
|
}
|
|
|
|
|
2008-05-31 10:31:07 +00:00
|
|
|
void DownloadEngine::waitData()
|
|
|
|
{
|
2006-08-07 16:05:00 +00:00
|
|
|
struct timeval tv;
|
2009-01-15 15:23:16 +00:00
|
|
|
if(_noWait) {
|
|
|
|
tv.tv_sec = tv.tv_usec = 0;
|
2006-07-19 17:07:45 +00:00
|
|
|
} else {
|
2009-01-15 15:23:16 +00:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2008-05-31 10:31:07 +00:00
|
|
|
}
|
2009-01-15 15:23:16 +00:00
|
|
|
_eventPoll->poll(tv);
|
2008-05-31 10:31:07 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->addEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_READ);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->deleteEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_READ);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->addEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_WRITE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->deleteEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_WRITE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-08-11 12:29:55 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void DownloadEngine::calculateStatistics()
|
|
|
|
{
|
|
|
|
if(!_statCalc.isNull()) {
|
2009-05-19 11:29:23 +00:00
|
|
|
_statCalc->calculateStat(this);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::onEndOfRun()
|
|
|
|
{
|
2008-08-04 17:06:47 +00:00
|
|
|
_requestGroupMan->updateServerStat();
|
2007-10-11 16:58:24 +00:00
|
|
|
_requestGroupMan->closeFile();
|
|
|
|
_requestGroupMan->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::afterEachIteration()
|
|
|
|
{
|
2009-03-19 13:42:10 +00:00
|
|
|
_requestGroupMan->calculateStat();
|
2010-03-06 08:29:53 +00:00
|
|
|
if(global::globalHaltRequested == 1) {
|
2007-12-07 13:33:59 +00:00
|
|
|
logger->notice(_("Shutdown sequence commencing... Press Ctrl-C again for emergency shutdown."));
|
2008-02-11 05:07:08 +00:00
|
|
|
requestHalt();
|
2010-03-06 08:29:53 +00:00
|
|
|
global::globalHaltRequested = 2;
|
2008-11-11 16:18:23 +00:00
|
|
|
setNoWait(true);
|
|
|
|
setRefreshInterval(0);
|
2010-03-06 08:29:53 +00:00
|
|
|
} else if(global::globalHaltRequested == 3) {
|
2007-12-07 13:33:59 +00:00
|
|
|
logger->notice(_("Emergency shutdown sequence commencing..."));
|
|
|
|
_requestGroupMan->forceHalt();
|
2010-03-06 08:29:53 +00:00
|
|
|
global::globalHaltRequested = 4;
|
2008-11-11 16:18:23 +00:00
|
|
|
setNoWait(true);
|
|
|
|
setRefreshInterval(0);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-11 05:07:08 +00:00
|
|
|
void DownloadEngine::requestHalt()
|
|
|
|
{
|
|
|
|
_haltRequested = true;
|
|
|
|
_requestGroupMan->halt();
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void DownloadEngine::setStatCalc(const StatCalcHandle& statCalc)
|
|
|
|
{
|
|
|
|
_statCalc = statCalc;
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void DownloadEngine::addCommand(const std::vector<Command*>& commands)
|
2007-11-24 11:32:22 +00:00
|
|
|
{
|
|
|
|
this->commands.insert(this->commands.end(), commands.begin(), commands.end());
|
|
|
|
}
|
|
|
|
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2008-05-08 11:18:36 +00:00
|
|
|
bool DownloadEngine::addNameResolverCheck
|
2008-05-31 10:31:07 +00:00
|
|
|
(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->addNameResolver(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
bool DownloadEngine::deleteNameResolverCheck
|
2008-05-31 10:31:07 +00:00
|
|
|
(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2009-01-15 15:23:16 +00:00
|
|
|
return _eventPoll->deleteNameResolver(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-04-20 05:42:15 +00:00
|
|
|
void DownloadEngine::setNoWait(bool b)
|
|
|
|
{
|
|
|
|
_noWait = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::addRoutineCommand(Command* command)
|
|
|
|
{
|
|
|
|
_routineCommands.push_back(command);
|
|
|
|
}
|
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::string& ipaddr,
|
2010-01-05 16:01:46 +00:00
|
|
|
uint16_t port,
|
|
|
|
const SocketPoolEntry& entry)
|
2008-04-22 08:52:47 +00:00
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
std::string addr = strconcat(ipaddr, ":", util::uitos(port));
|
2008-04-22 08:52:47 +00:00
|
|
|
logger->info("Pool socket for %s", addr.c_str());
|
2008-09-24 17:01:57 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry>::value_type p(addr, entry);
|
2008-05-08 13:18:25 +00:00
|
|
|
_socketPool.insert(p);
|
2008-09-24 17:01:57 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
if(_lastSocketPoolScan.difference(global::wallclock) >= 60) {
|
2008-09-24 17:01:57 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry> newPool;
|
2010-02-09 12:20:20 +00:00
|
|
|
if(logger->debug()) {
|
|
|
|
logger->debug("Scaning SocketPool and erasing timed out entry.");
|
|
|
|
}
|
2010-03-06 08:29:53 +00:00
|
|
|
_lastSocketPoolScan = global::wallclock;
|
2008-09-24 17:01:57 +00:00
|
|
|
for(std::multimap<std::string, SocketPoolEntry>::iterator i =
|
2010-02-28 16:04:52 +00:00
|
|
|
_socketPool.begin(), eoi = _socketPool.end(); i != eoi; ++i) {
|
2008-09-24 17:01:57 +00:00
|
|
|
if(!(*i).second.isTimeout()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
newPool.insert(*i);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-09 12:20:20 +00:00
|
|
|
if(logger->debug()) {
|
|
|
|
logger->debug
|
|
|
|
("%lu entries removed.",
|
|
|
|
static_cast<unsigned long>(_socketPool.size()-newPool.size()));
|
|
|
|
}
|
2008-09-24 17:01:57 +00:00
|
|
|
_socketPool = newPool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::poolSocket
|
|
|
|
(const std::string& ipaddr,
|
|
|
|
uint16_t port,
|
|
|
|
const SharedHandle<SocketCore>& sock,
|
|
|
|
const std::map<std::string, std::string>& options,
|
|
|
|
time_t timeout)
|
|
|
|
{
|
|
|
|
SocketPoolEntry e(sock, options, timeout);
|
|
|
|
poolSocket(ipaddr, port, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::poolSocket
|
|
|
|
(const std::string& ipaddr,
|
|
|
|
uint16_t port,
|
|
|
|
const SharedHandle<SocketCore>& sock,
|
|
|
|
time_t timeout)
|
|
|
|
{
|
|
|
|
SocketPoolEntry e(sock, std::map<std::string, std::string>(), timeout);
|
|
|
|
poolSocket(ipaddr, port, e);
|
|
|
|
}
|
|
|
|
|
2008-11-05 12:30:22 +00:00
|
|
|
void DownloadEngine::poolSocket(const SharedHandle<Request>& request,
|
2010-01-05 16:01:46 +00:00
|
|
|
bool proxyDefined,
|
|
|
|
const SharedHandle<SocketCore>& socket,
|
|
|
|
time_t timeout)
|
2008-11-05 12:30:22 +00:00
|
|
|
{
|
|
|
|
if(proxyDefined) {
|
|
|
|
// If proxy is defined, then pool socket with its hostname.
|
|
|
|
poolSocket(request->getHost(), request->getPort(), socket);
|
|
|
|
} else {
|
|
|
|
std::pair<std::string, uint16_t> peerInfo;
|
|
|
|
socket->getPeerInfo(peerInfo);
|
|
|
|
poolSocket(peerInfo.first, peerInfo.second, socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::poolSocket
|
|
|
|
(const SharedHandle<Request>& request,
|
|
|
|
bool proxyDefined,
|
|
|
|
const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::map<std::string, std::string>& options,
|
2008-11-05 12:30:22 +00:00
|
|
|
time_t timeout)
|
|
|
|
{
|
|
|
|
if(proxyDefined) {
|
|
|
|
// If proxy is defined, then pool socket with its hostname.
|
|
|
|
poolSocket(request->getHost(), request->getPort(), socket, options);
|
|
|
|
} else {
|
|
|
|
std::pair<std::string, uint16_t> peerInfo;
|
|
|
|
socket->getPeerInfo(peerInfo);
|
|
|
|
poolSocket(peerInfo.first, peerInfo.second, socket, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
std::multimap<std::string, DownloadEngine::SocketPoolEntry>::iterator
|
|
|
|
DownloadEngine::findSocketPoolEntry(const std::string& ipaddr, uint16_t port)
|
|
|
|
{
|
2009-06-06 12:33:07 +00:00
|
|
|
std::string addr = ipaddr;
|
2009-10-22 15:09:00 +00:00
|
|
|
strappend(addr, ":", util::uitos(port));
|
2008-09-24 17:01:57 +00:00
|
|
|
std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
|
|
|
|
std::multimap<std::string, SocketPoolEntry>::iterator> range =
|
|
|
|
_socketPool.equal_range(addr);
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::multimap<std::string, SocketPoolEntry>::iterator i =
|
|
|
|
range.first, eoi = range.second; i != eoi; ++i) {
|
2008-09-24 17:01:57 +00:00
|
|
|
const SocketPoolEntry& e = (*i).second;
|
|
|
|
if(!e.isTimeout()) {
|
|
|
|
logger->info("Found socket for %s", addr.c_str());
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _socketPool.end();
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedHandle<SocketCore>
|
|
|
|
DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port)
|
|
|
|
{
|
2008-05-08 13:18:25 +00:00
|
|
|
SharedHandle<SocketCore> s;
|
2008-09-24 17:01:57 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry>::iterator i =
|
|
|
|
findSocketPoolEntry(ipaddr, port);
|
|
|
|
if(i != _socketPool.end()) {
|
|
|
|
s = (*i).second.getSocket();
|
|
|
|
_socketPool.erase(i);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
SharedHandle<SocketCore>
|
|
|
|
DownloadEngine::popPooledSocket(std::map<std::string, std::string>& options,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& ipaddr, uint16_t port)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
|
|
|
SharedHandle<SocketCore> s;
|
|
|
|
std::multimap<std::string, SocketPoolEntry>::iterator i =
|
|
|
|
findSocketPoolEntry(ipaddr, port);
|
|
|
|
if(i != _socketPool.end()) {
|
|
|
|
s = (*i).second.getSocket();
|
|
|
|
options = (*i).second.getOptions();
|
|
|
|
_socketPool.erase(i);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedHandle<SocketCore>
|
|
|
|
DownloadEngine::popPooledSocket
|
2010-02-28 12:30:11 +00:00
|
|
|
(const std::vector<std::string>& ipaddrs, uint16_t port)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
|
|
|
SharedHandle<SocketCore> s;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = ipaddrs.begin(),
|
|
|
|
eoi = ipaddrs.end(); i != eoi; ++i) {
|
2008-09-24 17:01:57 +00:00
|
|
|
s = popPooledSocket(*i, port);
|
|
|
|
if(!s.isNull()) {
|
2008-05-08 13:18:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
return s;
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
SharedHandle<SocketCore>
|
|
|
|
DownloadEngine::popPooledSocket
|
2008-09-24 17:01:57 +00:00
|
|
|
(std::map<std::string, std::string>& options,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& ipaddrs, uint16_t port)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2008-09-24 17:01:57 +00:00
|
|
|
SharedHandle<SocketCore> s;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = ipaddrs.begin(),
|
|
|
|
eoi = ipaddrs.end(); i != eoi; ++i) {
|
2008-09-24 17:01:57 +00:00
|
|
|
s = popPooledSocket(options, *i, port);
|
2008-05-08 11:18:36 +00:00
|
|
|
if(!s.isNull()) {
|
2008-09-24 17:01:57 +00:00
|
|
|
break;
|
2008-05-08 11:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-24 17:01:57 +00:00
|
|
|
return s;
|
2008-05-08 11:18:36 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 13:18:25 +00:00
|
|
|
DownloadEngine::SocketPoolEntry::SocketPoolEntry
|
|
|
|
(const SharedHandle<SocketCore>& socket,
|
2008-09-24 17:01:57 +00:00
|
|
|
const std::map<std::string, std::string>& options,
|
2008-05-08 13:18:25 +00:00
|
|
|
time_t timeout):
|
|
|
|
_socket(socket),
|
2008-09-24 17:01:57 +00:00
|
|
|
_options(options),
|
2008-05-08 13:18:25 +00:00
|
|
|
_timeout(timeout) {}
|
|
|
|
|
|
|
|
DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {}
|
|
|
|
|
|
|
|
bool DownloadEngine::SocketPoolEntry::isTimeout() const
|
|
|
|
{
|
2010-03-06 08:29:53 +00:00
|
|
|
return _registeredTime.difference(global::wallclock) >= _timeout;
|
2008-05-08 13:18:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-19 13:54:09 +00:00
|
|
|
cuid_t DownloadEngine::newCUID()
|
2008-11-03 07:49:13 +00:00
|
|
|
{
|
|
|
|
return _cuidCounter.newID();
|
|
|
|
}
|
|
|
|
|
2008-11-03 08:18:58 +00:00
|
|
|
const std::string& DownloadEngine::findCachedIPAddress
|
2009-07-02 15:18:13 +00:00
|
|
|
(const std::string& hostname, uint16_t port) const
|
2008-11-03 08:18:58 +00:00
|
|
|
{
|
2009-07-02 15:18:13 +00:00
|
|
|
return _dnsCache->find(hostname, port);
|
2008-11-03 08:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::cacheIPAddress
|
2009-07-02 15:18:13 +00:00
|
|
|
(const std::string& hostname, const std::string& ipaddr, uint16_t port)
|
2008-11-03 08:18:58 +00:00
|
|
|
{
|
2009-07-02 15:18:13 +00:00
|
|
|
_dnsCache->put(hostname, ipaddr, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::markBadIPAddress
|
|
|
|
(const std::string& hostname, const std::string& ipaddr, uint16_t port)
|
|
|
|
{
|
|
|
|
_dnsCache->markBad(hostname, ipaddr, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::removeCachedIPAddress
|
|
|
|
(const std::string& hostname, uint16_t port)
|
|
|
|
{
|
|
|
|
_dnsCache->remove(hostname, port);
|
2008-11-03 08:18:58 +00:00
|
|
|
}
|
|
|
|
|
2008-11-03 10:06:25 +00:00
|
|
|
void DownloadEngine::setAuthConfigFactory
|
|
|
|
(const SharedHandle<AuthConfigFactory>& factory)
|
|
|
|
{
|
|
|
|
_authConfigFactory = factory;
|
|
|
|
}
|
|
|
|
|
2008-11-11 16:05:42 +00:00
|
|
|
void DownloadEngine::setRefreshInterval(time_t interval)
|
|
|
|
{
|
|
|
|
_refreshInterval = interval;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|