123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- /* <!-- 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 "TrackerWatcherCommand.h"
- #include <sstream>
- #include "DownloadEngine.h"
- #include "BtAnnounce.h"
- #include "BtRuntime.h"
- #include "PieceStorage.h"
- #include "PeerStorage.h"
- #include "Peer.h"
- #include "prefs.h"
- #include "message.h"
- #include "ByteArrayDiskWriterFactory.h"
- #include "RecoverableException.h"
- #include "PeerInitiateConnectionCommand.h"
- #include "DiskAdaptor.h"
- #include "FileEntry.h"
- #include "RequestGroup.h"
- #include "Option.h"
- #include "DlAbortEx.h"
- #include "Logger.h"
- #include "LogFactory.h"
- #include "A2STR.h"
- #include "SocketCore.h"
- #include "Request.h"
- #include "AnnounceTier.h"
- #include "DownloadContext.h"
- #include "bittorrent_helper.h"
- #include "a2functional.h"
- #include "util.h"
- #include "fmt.h"
- #include "UDPTrackerRequest.h"
- #include "UDPTrackerClient.h"
- #include "BtRegistry.h"
- #include "NameResolveCommand.h"
- namespace aria2 {
- HTTPAnnRequest::HTTPAnnRequest(const std::shared_ptr<RequestGroup>& rg)
- : rg_(rg)
- {}
- HTTPAnnRequest::~HTTPAnnRequest()
- {}
- bool HTTPAnnRequest::stopped() const
- {
- return rg_->getNumCommand() == 0;
- }
- bool HTTPAnnRequest::success() const
- {
- return rg_->downloadFinished();
- }
- void HTTPAnnRequest::stop(DownloadEngine* e)
- {
- rg_->setForceHaltRequested(true);
- }
- bool HTTPAnnRequest::issue(DownloadEngine* e)
- {
- try {
- std::vector<Command*>* commands = new std::vector<Command*>();
- auto_delete_container<std::vector<Command*> > commandsDel(commands);
- rg_->createInitialCommand(*commands, e);
- e->addCommand(*commands);
- e->setNoWait(true);
- commands->clear();
- A2_LOG_DEBUG("added tracker request command");
- return true;
- } catch(RecoverableException& ex) {
- A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
- return false;
- }
- }
- bool HTTPAnnRequest::processResponse
- (const std::shared_ptr<BtAnnounce>& btAnnounce)
- {
- try {
- std::stringstream strm;
- unsigned char data[2048];
- rg_->getPieceStorage()->getDiskAdaptor()->openFile();
- while(1) {
- ssize_t dataLength = rg_->getPieceStorage()->
- getDiskAdaptor()->readData(data, sizeof(data), strm.tellp());
- if(dataLength == 0) {
- break;
- }
- strm.write(reinterpret_cast<const char*>(data), dataLength);
- }
- std::string res = strm.str();
- btAnnounce->processAnnounceResponse
- (reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
- return true;
- } catch(RecoverableException& e) {
- A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
- return false;
- }
- }
- UDPAnnRequest::UDPAnnRequest(const std::shared_ptr<UDPTrackerRequest>& req)
- : req_(req)
- {}
- UDPAnnRequest::~UDPAnnRequest()
- {}
- bool UDPAnnRequest::stopped() const
- {
- return !req_ || req_->state == UDPT_STA_COMPLETE;
- }
- bool UDPAnnRequest::success() const
- {
- return req_ && req_->state == UDPT_STA_COMPLETE &&
- req_->error == UDPT_ERR_SUCCESS;
- }
- void UDPAnnRequest::stop(DownloadEngine* e)
- {
- if(req_) {
- req_.reset();
- }
- }
- bool UDPAnnRequest::issue(DownloadEngine* e)
- {
- if(req_) {
- NameResolveCommand* command = new NameResolveCommand
- (e->newCUID(), e, req_);
- e->addCommand(command);
- e->setNoWait(true);
- return true;
- } else {
- return false;
- }
- }
- bool UDPAnnRequest::processResponse
- (const std::shared_ptr<BtAnnounce>& btAnnounce)
- {
- if(req_) {
- btAnnounce->processUDPTrackerResponse(req_);
- return true;
- } else {
- return false;
- }
- }
- TrackerWatcherCommand::TrackerWatcherCommand
- (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e)
- : Command(cuid),
- requestGroup_(requestGroup),
- e_(e),
- udpTrackerClient_(e_->getBtRegistry()->getUDPTrackerClient())
- {
- requestGroup_->increaseNumCommand();
- if(udpTrackerClient_) {
- udpTrackerClient_->increaseWatchers();
- }
- }
- TrackerWatcherCommand::~TrackerWatcherCommand()
- {
- requestGroup_->decreaseNumCommand();
- if(udpTrackerClient_) {
- udpTrackerClient_->decreaseWatchers();
- }
- }
- bool TrackerWatcherCommand::execute() {
- if(requestGroup_->isForceHaltRequested()) {
- if(!trackerRequest_) {
- return true;
- } else if(trackerRequest_->stopped() ||
- trackerRequest_->success()) {
- return true;
- } else {
- trackerRequest_->stop(e_);
- e_->setRefreshInterval(0);
- e_->addCommand(this);
- return false;
- }
- }
- if(btAnnounce_->noMoreAnnounce()) {
- A2_LOG_DEBUG("no more announce");
- return true;
- }
- if(!trackerRequest_) {
- trackerRequest_ = createAnnounce(e_);
- if(trackerRequest_) {
- trackerRequest_->issue(e_);
- }
- } else if(trackerRequest_->stopped()) {
- // We really want to make sure that tracker request has finished
- // by checking getNumCommand() == 0. Because we reset
- // trackerRequestGroup_, if it is still used in other Command, we
- // will get Segmentation fault.
- if(trackerRequest_->success()) {
- if(trackerRequest_->processResponse(btAnnounce_)) {
- btAnnounce_->announceSuccess();
- btAnnounce_->resetAnnounce();
- addConnection();
- } else {
- btAnnounce_->announceFailure();
- if(btAnnounce_->isAllAnnounceFailed()) {
- btAnnounce_->resetAnnounce();
- }
- }
- trackerRequest_.reset();
- } else {
- // handle errors here
- btAnnounce_->announceFailure(); // inside it, trackers = 0.
- trackerRequest_.reset();
- if(btAnnounce_->isAllAnnounceFailed()) {
- btAnnounce_->resetAnnounce();
- }
- }
- }
- e_->addCommand(this);
- return false;
- }
- void TrackerWatcherCommand::addConnection()
- {
- while(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
- if(!peerStorage_->isPeerAvailable()) {
- break;
- }
- cuid_t ncuid = e_->newCUID();
- std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid);
- // sanity check
- if(!peer) {
- break;
- }
- PeerInitiateConnectionCommand* command;
- command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer,
- e_, btRuntime_);
- command->setPeerStorage(peerStorage_);
- command->setPieceStorage(pieceStorage_);
- e_->addCommand(command);
- A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Adding new command CUID#%" PRId64 "",
- getCuid(), peer->usedBy()));
- }
- }
- std::shared_ptr<AnnRequest>
- TrackerWatcherCommand::createAnnounce(DownloadEngine* e)
- {
- std::shared_ptr<AnnRequest> treq;
- while(!btAnnounce_->isAllAnnounceFailed() &&
- btAnnounce_->isAnnounceReady()) {
- std::string uri = btAnnounce_->getAnnounceUrl();
- uri_split_result res;
- memset(&res, 0, sizeof(res));
- if(uri_split(&res, uri.c_str()) == 0) {
- // Without UDP tracker support, send it to normal tracker flow
- // and make it fail.
- if(udpTrackerClient_ &&
- uri::getFieldString(res, USR_SCHEME, uri.c_str()) == "udp") {
- uint16_t localPort;
- localPort = e->getBtRegistry()->getUdpPort();
- treq = createUDPAnnRequest
- (uri::getFieldString(res, USR_HOST, uri.c_str()), res.port,
- localPort);
- } else {
- treq = createHTTPAnnRequest(btAnnounce_->getAnnounceUrl());
- }
- btAnnounce_->announceStart(); // inside it, trackers++.
- break;
- } else {
- btAnnounce_->announceFailure();
- }
- }
- if(btAnnounce_->isAllAnnounceFailed()) {
- btAnnounce_->resetAnnounce();
- }
- return treq;
- }
- std::shared_ptr<AnnRequest>
- TrackerWatcherCommand::createUDPAnnRequest(const std::string& host,
- uint16_t port,
- uint16_t localPort)
- {
- std::shared_ptr<UDPTrackerRequest> req =
- btAnnounce_->createUDPTrackerRequest(host, port, localPort);
- return std::shared_ptr<AnnRequest>(new UDPAnnRequest(req));
- }
- namespace {
- bool backupTrackerIsAvailable
- (const std::shared_ptr<DownloadContext>& context)
- {
- auto torrentAttrs = bittorrent::getTorrentAttrs(context);
- if(torrentAttrs->announceList.size() >= 2) {
- return true;
- }
- if(torrentAttrs->announceList.empty()) {
- return false;
- }
- if(torrentAttrs->announceList[0].size() >= 2) {
- return true;
- } else {
- return false;
- }
- }
- } // namespace
- std::shared_ptr<AnnRequest>
- TrackerWatcherCommand::createHTTPAnnRequest(const std::string& uri)
- {
- std::vector<std::string> uris;
- uris.push_back(uri);
- std::shared_ptr<Option> option = util::copy(getOption());
- std::shared_ptr<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
- if(backupTrackerIsAvailable(requestGroup_->getDownloadContext())) {
- A2_LOG_DEBUG("This is multi-tracker announce.");
- } else {
- A2_LOG_DEBUG("This is single-tracker announce.");
- }
- rg->setNumConcurrentCommand(1);
- // If backup tracker is available, try 2 times for each tracker
- // and if they all fails, then try next one.
- option->put(PREF_MAX_TRIES, "2");
- // TODO When dry-run mode becomes available in BitTorrent, set
- // PREF_DRY_RUN=false too.
- option->put(PREF_USE_HEAD, A2_V_FALSE);
- // Setting tracker timeouts
- rg->setTimeout(option->getAsInt(PREF_BT_TRACKER_TIMEOUT));
- option->put(PREF_CONNECT_TIMEOUT,
- option->get(PREF_BT_TRACKER_CONNECT_TIMEOUT));
- option->put(PREF_REUSE_URI, A2_V_FALSE);
- option->put(PREF_SELECT_LEAST_USED_HOST, A2_V_FALSE);
- std::shared_ptr<DownloadContext> dctx
- (new DownloadContext(option->getAsInt(PREF_PIECE_LENGTH),
- 0,
- "[tracker.announce]"));
- dctx->getFileEntries().front()->setUris(uris);
- rg->setDownloadContext(dctx);
- std::shared_ptr<DiskWriterFactory> dwf(new ByteArrayDiskWriterFactory());
- rg->setDiskWriterFactory(dwf);
- rg->setFileAllocationEnabled(false);
- rg->setPreLocalFileCheckEnabled(false);
- // Clearing pre- and post handler is not needed because the
- // RequestGroup is not handled by RequestGroupMan.
- rg->clearPreDownloadHandler();
- rg->clearPostDownloadHandler();
- dctx->setAcceptMetalink(false);
- A2_LOG_INFO(fmt("Creating tracker request group GID#%s",
- GroupId::toHex(rg->getGID()).c_str()));
- return std::shared_ptr<AnnRequest>(new HTTPAnnRequest(rg));
- }
- void TrackerWatcherCommand::setBtRuntime
- (const std::shared_ptr<BtRuntime>& btRuntime)
- {
- btRuntime_ = btRuntime;
- }
- void TrackerWatcherCommand::setPeerStorage
- (const std::shared_ptr<PeerStorage>& peerStorage)
- {
- peerStorage_ = peerStorage;
- }
- void TrackerWatcherCommand::setPieceStorage
- (const std::shared_ptr<PieceStorage>& pieceStorage)
- {
- pieceStorage_ = pieceStorage;
- }
- void TrackerWatcherCommand::setBtAnnounce
- (const std::shared_ptr<BtAnnounce>& btAnnounce)
- {
- btAnnounce_ = btAnnounce;
- }
- const std::shared_ptr<Option>& TrackerWatcherCommand::getOption() const
- {
- return requestGroup_->getOption();
- }
- } // namespace aria2
|