| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * 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.
- */
- /* copyright --> */
- #include "DownloadEngine.h"
- #ifdef ENABLE_ASYNC_DNS
- #include "AsyncNameResolver.h"
- #endif // ENABLE_ASYNC_DNS
- #include "StatCalc.h"
- #include "RequestGroup.h"
- #include "RequestGroupMan.h"
- #include "FileAllocationMan.h"
- #include "CheckIntegrityMan.h"
- #include "DownloadResult.h"
- #include "StatCalc.h"
- #include "LogFactory.h"
- #include "Logger.h"
- #include "TimeA2.h"
- #include "a2time.h"
- #include "Socket.h"
- #include "Util.h"
- #include "a2functional.h"
- #include <signal.h>
- #include <cstring>
- #include <algorithm>
- namespace aria2 {
- // 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;
- SocketEntry::SocketEntry(const SocketHandle& socket,
- Command* command,
- TYPE type):
- socket(socket), command(command), type(type) {}
- bool SocketEntry::operator==(const SocketEntry& entry)
- {
- return socket == entry.socket &&
- command == entry.command &&
- type == entry.type;
- }
- #ifdef ENABLE_ASYNC_DNS
- AsyncNameResolverEntry::AsyncNameResolverEntry
- (const SharedHandle<AsyncNameResolver>& nameResolver,
- Command* command):
- nameResolver(nameResolver), command(command) {}
- bool AsyncNameResolverEntry::operator==(const AsyncNameResolverEntry& entry)
- {
- return nameResolver == entry.nameResolver &&
- command == entry.command;
- }
- #endif // ENABLE_ASYNC_DNS
- DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()),
- _haltRequested(false),
- _noWait(false)
- {
- updateFdSet();
- }
- DownloadEngine::~DownloadEngine() {
- cleanQueue();
- }
- void DownloadEngine::cleanQueue() {
- std::for_each(commands.begin(), commands.end(), Deleter());
- commands.clear();
- }
- static void executeCommand(std::deque<Command*>& commands,
- Command::STATUS statusFilter)
- {
- size_t max = commands.size();
- for(size_t i = 0; i < max; i++) {
- Command* com = commands.front();
- commands.pop_front();
- if(com->statusMatch(statusFilter)) {
- com->transitStatus();
- if(com->execute()) {
- delete com;
- }
- } else {
- commands.push_back(com);
- }
- }
- }
- void DownloadEngine::run() {
- Time cp;
- cp.setTimeInSec(0);
- while(!commands.empty() || !_routineCommands.empty()) {
- if(cp.elapsed(1)) {
- cp.reset();
- executeCommand(commands, Command::STATUS_ALL);
- } else {
- executeCommand(commands, Command::STATUS_ACTIVE);
- }
- executeCommand(_routineCommands, Command::STATUS_ALL);
- afterEachIteration();
- if(!commands.empty()) {
- waitData();
- }
- _noWait = false;
- calculateStatistics();
- }
- onEndOfRun();
- }
- 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);
- }
- void DownloadEngine::waitData() {
- fd_set rfds;
- fd_set wfds;
- struct timeval tv;
-
- memcpy(&rfds, &rfdset, sizeof(fd_set));
- memcpy(&wfds, &wfdset, sizeof(fd_set));
-
- #ifdef ENABLE_ASYNC_DNS
- for(AsyncNameResolverEntries::iterator itr = nameResolverEntries.begin();
- itr != nameResolverEntries.end(); ++itr) {
- AsyncNameResolverEntry& entry = *itr;
- int fd = entry.nameResolver->getFds(&rfds, &wfds);
- // TODO force error if fd == 0
- if(fdmax < fd) {
- fdmax = fd;
- }
- }
- #endif // ENABLE_ASYNC_DNS
- tv.tv_sec = _noWait ? 0 : 1;
- tv.tv_usec = 0;
- int retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
- if(retval > 0) {
- for(SocketEntries::iterator itr = socketEntries.begin();
- itr != socketEntries.end(); ++itr) {
- SocketEntry& entry = *itr;
- if(FD_ISSET(entry.socket->getSockfd(), &rfds) ||
- FD_ISSET(entry.socket->getSockfd(), &wfds)) {
- entry.command->setStatusActive();
- }
- }
- }
- #ifdef ENABLE_ASYNC_DNS
- for(AsyncNameResolverEntries::iterator itr = nameResolverEntries.begin();
- itr != nameResolverEntries.end(); ++itr) {
- AsyncNameResolverEntry& entry = *itr;
- entry.nameResolver->process(&rfds, &wfds);
- switch(entry.nameResolver->getStatus()) {
- case AsyncNameResolver::STATUS_SUCCESS:
- case AsyncNameResolver::STATUS_ERROR:
- entry.command->setStatusActive();
- break;
- default:
- break;
- }
- }
- #endif // ENABLE_ASYNC_DNS
- }
- void DownloadEngine::updateFdSet() {
- fdmax = 0;
- FD_ZERO(&rfdset);
- FD_ZERO(&wfdset);
- for(SocketEntries::iterator itr = socketEntries.begin();
- itr != socketEntries.end(); ++itr) {
- SocketEntry& entry = *itr;
- int fd = entry.socket->getSockfd();
- switch(entry.type) {
- case SocketEntry::TYPE_RD:
- FD_SET(fd, &rfdset);
- break;
- case SocketEntry::TYPE_WR:
- FD_SET(fd, &wfdset);
- break;
- }
- if(fdmax < fd) {
- fdmax = fd;
- }
- }
- }
- bool DownloadEngine::addSocket(const SocketEntry& entry) {
- SocketEntries::iterator itr =
- std::find(socketEntries.begin(), socketEntries.end(), entry);
- if(itr == socketEntries.end()) {
- socketEntries.push_back(entry);
- updateFdSet();
- return true;
- } else {
- return false;
- }
- }
- bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
- SocketEntries::iterator itr =
- std::find(socketEntries.begin(), socketEntries.end(), entry);
- if(itr == socketEntries.end()) {
- return false;
- } else {
- socketEntries.erase(itr);
- updateFdSet();
- return true;
- }
- }
- bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
- Command* command) {
- SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
- return addSocket(entry);
- }
- bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
- Command* command) {
- SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
- return deleteSocket(entry);
- }
- bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
- Command* command) {
- SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
- return addSocket(entry);
- }
- bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
- Command* command) {
- SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
- return deleteSocket(entry);
- }
- void DownloadEngine::calculateStatistics()
- {
- if(!_statCalc.isNull()) {
- _statCalc->calculateStat(_requestGroupMan, _fileAllocationMan, _checkIntegrityMan);
- }
- }
- void DownloadEngine::onEndOfRun()
- {
- _requestGroupMan->closeFile();
- _requestGroupMan->save();
- }
- void DownloadEngine::afterEachIteration()
- {
- if(globalHaltRequested == 1) {
- logger->notice(_("Shutdown sequence commencing... Press Ctrl-C again for emergency shutdown."));
- requestHalt();
- globalHaltRequested = 2;
- } else if(globalHaltRequested == 3) {
- logger->notice(_("Emergency shutdown sequence commencing..."));
- _requestGroupMan->forceHalt();
- globalHaltRequested = 4;
- }
- }
- void DownloadEngine::requestHalt()
- {
- _haltRequested = true;
- _requestGroupMan->halt();
- }
- void DownloadEngine::fillCommand()
- {
- std::deque<Command*> commands;
- _requestGroupMan->getInitialCommands(commands, this);
- addCommand(commands);
- }
- void DownloadEngine::setStatCalc(const StatCalcHandle& statCalc)
- {
- _statCalc = statCalc;
- }
- void DownloadEngine::addCommand(const Commands& commands)
- {
- this->commands.insert(this->commands.end(), commands.begin(), commands.end());
- }
- #ifdef ENABLE_ASYNC_DNS
- bool DownloadEngine::addNameResolverCheck
- (const SharedHandle<AsyncNameResolver>& resolver,
- Command* command)
- {
- AsyncNameResolverEntry entry(resolver, command);
- AsyncNameResolverEntries::iterator itr =
- std::find(nameResolverEntries.begin(), nameResolverEntries.end(), entry);
- if(itr == nameResolverEntries.end()) {
- nameResolverEntries.push_back(entry);
- return true;
- } else {
- return false;
- }
- }
- bool DownloadEngine::deleteNameResolverCheck
- (const SharedHandle<AsyncNameResolver>& resolver,
- Command* command)
- {
- AsyncNameResolverEntry entry(resolver, command);
- AsyncNameResolverEntries::iterator itr =
- std::find(nameResolverEntries.begin(), nameResolverEntries.end(), entry);
- if(itr == nameResolverEntries.end()) {
- return false;
- } else {
- nameResolverEntries.erase(itr);
- return true;
- }
- }
- #endif // ENABLE_ASYNC_DNS
- void DownloadEngine::setNoWait(bool b)
- {
- _noWait = b;
- }
- void DownloadEngine::addRoutineCommand(Command* command)
- {
- _routineCommands.push_back(command);
- }
- void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
- const SharedHandle<SocketCore>& sock,
- time_t timeout)
- {
- std::string addr = ipaddr+":"+Util::uitos(port);
- logger->info("Pool socket for %s", addr.c_str());
- SocketPoolEntry e(sock, timeout);
- std::multimap<std::string, SocketPoolEntry>::value_type p(addr, e);
- _socketPool.insert(p);
- }
- SharedHandle<SocketCore>
- DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port)
- {
- SharedHandle<SocketCore> s;
- std::string addr = ipaddr+":"+Util::uitos(port);
- std::multimap<std::string, SocketPoolEntry>::iterator first = _socketPool.find(addr);
-
- for(std::multimap<std::string, SocketPoolEntry>::iterator i = first;
- i != _socketPool.end() && (*i).first == addr; ++i) {
- const SocketPoolEntry& e = (*i).second;
- if(!e.isTimeout()) {
- logger->info("Reuse socket for %s", addr.c_str());
- s = e.getSocket();
- _socketPool.erase(first, ++i);
- break;
- }
- }
- return s;
- }
- SharedHandle<SocketCore>
- DownloadEngine::popPooledSocket
- (const std::deque<std::string>& ipaddrs, uint16_t port)
- {
- for(std::deque<std::string>::const_iterator i = ipaddrs.begin();
- i != ipaddrs.end(); ++i) {
- SharedHandle<SocketCore> s = popPooledSocket(*i, port);
- if(!s.isNull()) {
- return s;
- }
- }
- return SharedHandle<SocketCore>();
- }
- DownloadEngine::SocketPoolEntry::SocketPoolEntry
- (const SharedHandle<SocketCore>& socket,
- time_t timeout):
- _socket(socket),
- _timeout(timeout) {}
- DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {}
- bool DownloadEngine::SocketPoolEntry::isTimeout() const
- {
- return _registeredTime.elapsed(_timeout);
- }
- SharedHandle<SocketCore> DownloadEngine::SocketPoolEntry::getSocket() const
- {
- return _socket;
- }
- } // namespace aria2
|