2010-02-20 14:23:25 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 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 "LpdReceiveMessageCommand.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "LpdMessageReceiver.h"
|
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "PeerStorage.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "BtRegistry.h"
|
|
|
|
#include "Logger.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.h"
|
2010-02-20 14:23:25 +00:00
|
|
|
#include "LpdMessage.h"
|
|
|
|
#include "bittorrent_helper.h"
|
2010-06-20 12:09:24 +00:00
|
|
|
#include "util.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2010-02-20 14:23:25 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
LpdReceiveMessageCommand::LpdReceiveMessageCommand
|
2010-11-20 08:21:36 +00:00
|
|
|
(cuid_t cuid,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<LpdMessageReceiver>& receiver,
|
2010-11-20 08:21:36 +00:00
|
|
|
DownloadEngine* e)
|
|
|
|
: Command(cuid),
|
|
|
|
receiver_(receiver),
|
|
|
|
e_(e)
|
2010-02-20 14:23:25 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->addSocketForReadCheck(receiver_->getSocket(), this);
|
2010-02-20 14:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LpdReceiveMessageCommand::~LpdReceiveMessageCommand()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->deleteSocketForReadCheck(receiver_->getSocket(), this);
|
2010-02-20 14:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LpdReceiveMessageCommand::execute()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
|
2010-02-20 14:23:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for(size_t i = 0; i < 20; ++i) {
|
2013-07-03 15:56:29 +00:00
|
|
|
auto m = receiver_->receiveMessage();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!m) {
|
2010-02-20 14:23:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-03 15:56:29 +00:00
|
|
|
auto& reg = e_->getBtRegistry();
|
|
|
|
auto& dctx = reg->getDownloadContext(m->infoHash);
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!dctx) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Download Context is null for infohash=%s.",
|
2011-10-17 15:47:14 +00:00
|
|
|
util::toHex(m->infoHash).c_str()));
|
2010-02-20 14:23:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-06-18 14:47:09 +00:00
|
|
|
if(bittorrent::getTorrentAttrs(dctx)->privateTorrent) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG("Ignore LPD message because the torrent is private.");
|
2010-06-18 14:47:09 +00:00
|
|
|
continue;
|
2010-02-20 14:23:25 +00:00
|
|
|
}
|
|
|
|
RequestGroup* group = dctx->getOwnerRequestGroup();
|
|
|
|
assert(group);
|
2013-07-06 10:02:39 +00:00
|
|
|
auto btobj = reg->get(group->getGID());
|
2011-11-01 14:13:13 +00:00
|
|
|
assert(btobj);
|
2013-07-03 15:56:29 +00:00
|
|
|
auto& peerStorage = btobj->peerStorage;
|
2010-11-12 12:48:48 +00:00
|
|
|
assert(peerStorage);
|
2013-07-03 15:56:29 +00:00
|
|
|
auto& peer = m->peer;
|
2010-02-20 14:23:25 +00:00
|
|
|
if(peerStorage->addPeer(peer)) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("LPD peer %s:%u local=%d added.",
|
|
|
|
peer->getIPAddress().c_str(), peer->getPort(),
|
|
|
|
peer->isLocalPeer()?1:0));
|
2010-02-20 14:23:25 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("LPD peer %s:%u local=%d not added.",
|
|
|
|
peer->getIPAddress().c_str(), peer->getPort(),
|
|
|
|
peer->isLocalPeer()?1:0));
|
2010-02-20 14:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 12:55:52 +00:00
|
|
|
e_->addCommand(std::unique_ptr<Command>(this));
|
2010-02-20 14:23:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|