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 --> */
|
|
|
|
#ifndef _D_DOWNLOAD_ENGINE_H_
|
|
|
|
#define _D_DOWNLOAD_ENGINE_H_
|
|
|
|
|
|
|
|
#include "common.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <deque>
|
|
|
|
#include <map>
|
2010-02-28 12:30:11 +00:00
|
|
|
#include <vector>
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "SharedHandle.h"
|
2008-05-08 11:18:36 +00:00
|
|
|
#include "a2netcompat.h"
|
2008-05-08 13:18:25 +00:00
|
|
|
#include "TimeA2.h"
|
2008-05-31 10:31:07 +00:00
|
|
|
#include "a2io.h"
|
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
|
|
|
# include "AsyncNameResolver.h"
|
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2008-11-03 07:49:13 +00:00
|
|
|
#include "CUIDCounter.h"
|
2009-01-24 09:00:36 +00:00
|
|
|
#include "FileAllocationMan.h"
|
2009-01-24 10:40:42 +00:00
|
|
|
#include "CheckIntegrityMan.h"
|
2010-01-06 14:31:41 +00:00
|
|
|
#include "DNSCache.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
class Logger;
|
|
|
|
class Option;
|
|
|
|
class RequestGroupMan;
|
|
|
|
class StatCalc;
|
2008-02-08 15:53:45 +00:00
|
|
|
class SocketCore;
|
2008-09-01 13:46:03 +00:00
|
|
|
class CookieStorage;
|
2008-11-03 10:06:25 +00:00
|
|
|
class AuthConfigFactory;
|
|
|
|
class Request;
|
2009-01-15 15:23:16 +00:00
|
|
|
class EventPoll;
|
|
|
|
class Command;
|
2009-07-25 14:07:46 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
class BtRegistry;
|
|
|
|
#endif // ENABLE_BITTORRENT
|
2006-08-11 12:29:55 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
class DownloadEngine {
|
|
|
|
private:
|
2007-03-16 14:21:29 +00:00
|
|
|
void waitData();
|
2008-05-31 10:31:07 +00:00
|
|
|
|
2010-01-17 11:55:22 +00:00
|
|
|
std::string _sessionId;
|
|
|
|
|
2009-01-15 15:23:16 +00:00
|
|
|
SharedHandle<EventPoll> _eventPoll;
|
2008-05-31 10:31:07 +00:00
|
|
|
|
2008-04-26 05:58:49 +00:00
|
|
|
Logger* logger;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<StatCalc> _statCalc;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
bool _haltRequested;
|
|
|
|
|
2008-05-08 13:18:25 +00:00
|
|
|
class SocketPoolEntry {
|
|
|
|
private:
|
|
|
|
SharedHandle<SocketCore> _socket;
|
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
std::map<std::string, std::string> _options;
|
|
|
|
|
2008-05-08 13:18:25 +00:00
|
|
|
time_t _timeout;
|
|
|
|
|
|
|
|
Time _registeredTime;
|
|
|
|
public:
|
|
|
|
SocketPoolEntry(const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::map<std::string, std::string>& option,
|
|
|
|
time_t timeout);
|
2008-05-08 13:18:25 +00:00
|
|
|
|
|
|
|
~SocketPoolEntry();
|
|
|
|
|
|
|
|
bool isTimeout() const;
|
|
|
|
|
2009-05-29 12:12:22 +00:00
|
|
|
const SharedHandle<SocketCore>& getSocket() const
|
|
|
|
{
|
|
|
|
return _socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, std::string>& getOptions() const
|
|
|
|
{
|
|
|
|
return _options;
|
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// key = IP address:port, value = SocketPoolEntry
|
|
|
|
std::multimap<std::string, SocketPoolEntry> _socketPool;
|
2008-04-22 08:52:47 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
Time _lastSocketPoolScan;
|
|
|
|
|
2008-09-01 13:46:03 +00:00
|
|
|
bool _noWait;
|
|
|
|
|
2008-11-11 16:05:42 +00:00
|
|
|
static const time_t DEFAULT_REFRESH_INTERVAL = 1;
|
|
|
|
|
|
|
|
time_t _refreshInterval;
|
|
|
|
|
2008-09-01 13:46:03 +00:00
|
|
|
std::deque<Command*> _routineCommands;
|
|
|
|
|
|
|
|
SharedHandle<CookieStorage> _cookieStorage;
|
|
|
|
|
2009-07-25 14:07:46 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2008-11-03 06:49:02 +00:00
|
|
|
SharedHandle<BtRegistry> _btRegistry;
|
2009-07-25 14:07:46 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-11-03 07:49:13 +00:00
|
|
|
CUIDCounter _cuidCounter;
|
|
|
|
|
2008-11-03 08:18:58 +00:00
|
|
|
SharedHandle<DNSCache> _dnsCache;
|
|
|
|
|
2008-11-03 10:06:25 +00:00
|
|
|
SharedHandle<AuthConfigFactory> _authConfigFactory;
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
/**
|
|
|
|
* Delegates to StatCalc
|
|
|
|
*/
|
|
|
|
void calculateStatistics();
|
|
|
|
|
|
|
|
void onEndOfRun();
|
|
|
|
|
|
|
|
void afterEachIteration();
|
2008-09-24 17:01:57 +00:00
|
|
|
|
|
|
|
void poolSocket(const std::string& ipaddr,
|
2010-01-05 16:01:46 +00:00
|
|
|
uint16_t port,
|
|
|
|
const SocketPoolEntry& entry);
|
2008-09-24 17:01:57 +00:00
|
|
|
|
|
|
|
std::multimap<std::string, SocketPoolEntry>::iterator
|
|
|
|
findSocketPoolEntry(const std::string& ipaddr, uint16_t port);
|
2006-02-17 13:35:04 +00:00
|
|
|
public:
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<Command*> commands;
|
|
|
|
SharedHandle<RequestGroupMan> _requestGroupMan;
|
|
|
|
SharedHandle<FileAllocationMan> _fileAllocationMan;
|
|
|
|
SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
|
2009-10-04 09:01:11 +00:00
|
|
|
Option* option;
|
2006-08-11 12:29:55 +00:00
|
|
|
|
2009-01-15 15:23:16 +00:00
|
|
|
DownloadEngine(const SharedHandle<EventPoll>& eventPoll);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2006-03-02 02:54:49 +00:00
|
|
|
virtual ~DownloadEngine();
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
void run();
|
|
|
|
|
2006-05-09 15:54:14 +00:00
|
|
|
void cleanQueue();
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2008-02-08 15:53:45 +00:00
|
|
|
bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2008-02-08 15:53:45 +00:00
|
|
|
bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2008-02-08 15:53:45 +00:00
|
|
|
bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2008-05-31 10:31:07 +00:00
|
|
|
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2008-05-31 10:31:07 +00:00
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2008-05-08 11:18:36 +00:00
|
|
|
bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
|
2010-01-05 16:01:46 +00:00
|
|
|
Command* command);
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2007-05-20 13:51:52 +00:00
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void addCommand(const std::vector<Command*>& commands);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void setStatCalc(const SharedHandle<StatCalc>& statCalc);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
bool isHaltRequested() const
|
2007-05-20 13:51:52 +00:00
|
|
|
{
|
2007-10-11 16:58:24 +00:00
|
|
|
return _haltRequested;
|
2007-05-20 13:51:52 +00:00
|
|
|
}
|
2008-02-11 05:07:08 +00:00
|
|
|
|
|
|
|
void requestHalt();
|
2008-04-20 05:42:15 +00:00
|
|
|
|
|
|
|
void setNoWait(bool b);
|
|
|
|
|
|
|
|
void addRoutineCommand(Command* command);
|
2008-04-22 08:52:47 +00:00
|
|
|
|
|
|
|
void poolSocket(const std::string& ipaddr, uint16_t port,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<SocketCore>& sock,
|
|
|
|
const std::map<std::string, std::string>& options,
|
|
|
|
time_t timeout = 15);
|
2008-04-22 08:52:47 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
void poolSocket(const std::string& ipaddr, uint16_t port,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<SocketCore>& sock,
|
|
|
|
time_t timeout = 15);
|
2008-09-24 17:01:57 +00:00
|
|
|
|
2008-11-05 12:30:22 +00:00
|
|
|
void poolSocket(const SharedHandle<Request>& request,
|
2010-01-05 16:01:46 +00:00
|
|
|
bool proxyDefined,
|
|
|
|
const SharedHandle<SocketCore>& socket,
|
|
|
|
const std::map<std::string, std::string>& options,
|
|
|
|
time_t timeout = 15);
|
2008-11-05 12:30:22 +00:00
|
|
|
|
|
|
|
void poolSocket(const SharedHandle<Request>& request,
|
2010-01-05 16:01:46 +00:00
|
|
|
bool proxyDefined,
|
|
|
|
const SharedHandle<SocketCore>& socket,
|
|
|
|
time_t timeout = 15);
|
2008-11-05 12:30:22 +00:00
|
|
|
|
2008-04-22 08:52:47 +00:00
|
|
|
SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
|
2010-01-05 16:01:46 +00:00
|
|
|
uint16_t port);
|
2008-05-08 11:18:36 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
SharedHandle<SocketCore> popPooledSocket
|
|
|
|
(std::map<std::string, std::string>& options,
|
|
|
|
const std::string& ipaddr,
|
|
|
|
uint16_t port);
|
2008-05-08 11:18:36 +00:00
|
|
|
|
|
|
|
SharedHandle<SocketCore>
|
2010-02-28 12:30:11 +00:00
|
|
|
popPooledSocket(const std::vector<std::string>& ipaddrs, uint16_t port);
|
2008-09-01 13:46:03 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
SharedHandle<SocketCore>
|
|
|
|
popPooledSocket
|
|
|
|
(std::map<std::string, std::string>& options,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& ipaddrs,
|
2008-09-24 17:01:57 +00:00
|
|
|
uint16_t port);
|
|
|
|
|
2009-05-29 12:12:22 +00:00
|
|
|
const SharedHandle<CookieStorage>& getCookieStorage() const
|
|
|
|
{
|
|
|
|
return _cookieStorage;
|
|
|
|
}
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2009-07-25 14:07:46 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2009-05-29 12:12:22 +00:00
|
|
|
const SharedHandle<BtRegistry>& getBtRegistry() const
|
|
|
|
{
|
|
|
|
return _btRegistry;
|
|
|
|
}
|
2009-07-25 14:07:46 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2008-11-03 07:49:13 +00:00
|
|
|
|
2009-03-19 13:54:09 +00:00
|
|
|
cuid_t newCUID();
|
2008-11-03 08:18:58 +00:00
|
|
|
|
2009-07-02 15:18:13 +00:00
|
|
|
const std::string& findCachedIPAddress
|
|
|
|
(const std::string& hostname, uint16_t port) const;
|
2008-11-03 08:18:58 +00:00
|
|
|
|
2010-01-06 14:31:41 +00:00
|
|
|
template<typename OutputIterator>
|
|
|
|
void findAllCachedIPAddresses
|
|
|
|
(OutputIterator out, const std::string& hostname, uint16_t port) const
|
|
|
|
{
|
|
|
|
_dnsCache->findAll(out, hostname, port);
|
|
|
|
}
|
|
|
|
|
2009-07-02 15:18:13 +00:00
|
|
|
void cacheIPAddress
|
|
|
|
(const std::string& hostname, const std::string& ipaddr, uint16_t port);
|
|
|
|
|
|
|
|
void markBadIPAddress
|
|
|
|
(const std::string& hostname, const std::string& ipaddr, uint16_t port);
|
|
|
|
|
|
|
|
void removeCachedIPAddress(const std::string& hostname, uint16_t port);
|
2008-11-03 10:06:25 +00:00
|
|
|
|
|
|
|
void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
|
|
|
|
|
2009-05-29 12:12:22 +00:00
|
|
|
const SharedHandle<AuthConfigFactory>& getAuthConfigFactory() const
|
|
|
|
{
|
|
|
|
return _authConfigFactory;
|
|
|
|
}
|
2008-11-11 16:05:42 +00:00
|
|
|
|
|
|
|
void setRefreshInterval(time_t interval);
|
2010-01-17 11:55:22 +00:00
|
|
|
|
|
|
|
const std::string getSessionId() const
|
|
|
|
{
|
|
|
|
return _sessionId;
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
};
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
#endif // _D_DOWNLOAD_ENGINE_H_
|
|
|
|
|