2008-02-09 17:14:40 +00:00
|
|
|
/* <!-- 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-02-09 17:14:40 +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.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "PeerSessionResource.h"
|
2008-11-03 07:16:25 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
#include "BitfieldMan.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2008-11-03 07:16:25 +00:00
|
|
|
#include "BtMessageDispatcher.h"
|
2008-02-09 17:14:40 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
PeerSessionResource::PeerSessionResource(size_t pieceLength, uint64_t totalLength):
|
2008-02-09 17:14:40 +00:00
|
|
|
_amChoking(true),
|
|
|
|
_amInterested(false),
|
|
|
|
_peerChoking(true),
|
|
|
|
_peerInterested(false),
|
|
|
|
_chokingRequired(true),
|
|
|
|
_optUnchoking(false),
|
|
|
|
_snubbing(false),
|
2010-02-11 08:28:41 +00:00
|
|
|
_bitfieldMan(new BitfieldMan(pieceLength, totalLength)),
|
2008-02-09 17:14:40 +00:00
|
|
|
_fastExtensionEnabled(false),
|
|
|
|
_extendedMessagingEnabled(false),
|
|
|
|
_dhtEnabled(false),
|
2008-04-13 01:25:36 +00:00
|
|
|
_lastDownloadUpdate(0),
|
|
|
|
_lastAmUnchoking(0)
|
2008-02-09 17:14:40 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
PeerSessionResource::~PeerSessionResource()
|
|
|
|
{
|
|
|
|
delete _bitfieldMan;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::amChoking(bool b)
|
|
|
|
{
|
|
|
|
_amChoking = b;
|
2008-04-13 01:25:36 +00:00
|
|
|
if(!b) {
|
|
|
|
_lastAmUnchoking.reset();
|
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::amInterested(bool b)
|
|
|
|
{
|
|
|
|
_amInterested = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::peerChoking(bool b)
|
|
|
|
{
|
|
|
|
_peerChoking = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::peerInterested(bool b)
|
|
|
|
{
|
|
|
|
_peerInterested = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::chokingRequired(bool b)
|
|
|
|
{
|
|
|
|
_chokingRequired = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::optUnchoking(bool b)
|
|
|
|
{
|
|
|
|
_optUnchoking = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerSessionResource::shouldBeChoking() const
|
|
|
|
{
|
|
|
|
if(_optUnchoking) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _chokingRequired;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::snubbing(bool b)
|
|
|
|
{
|
|
|
|
_snubbing = b;
|
2008-04-13 01:25:36 +00:00
|
|
|
if(_snubbing) {
|
|
|
|
chokingRequired(true);
|
|
|
|
optUnchoking(false);
|
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerSessionResource::hasAllPieces() const
|
|
|
|
{
|
|
|
|
return _bitfieldMan->isAllBitSet();
|
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateBitfield(size_t index, int operation)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
if(operation == 1) {
|
|
|
|
_bitfieldMan->setBit(index);
|
|
|
|
} else if(operation == 0) {
|
|
|
|
_bitfieldMan->unsetBit(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
|
|
|
|
{
|
|
|
|
_bitfieldMan->setBitfield(bitfield, bitfieldLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* PeerSessionResource::getBitfield() const
|
|
|
|
{
|
|
|
|
return _bitfieldMan->getBitfield();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PeerSessionResource::getBitfieldLength() const
|
|
|
|
{
|
|
|
|
return _bitfieldMan->getBitfieldLength();
|
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::hasPiece(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
return _bitfieldMan->isBitSet(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::markSeeder()
|
|
|
|
{
|
|
|
|
_bitfieldMan->setAllBit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::fastExtensionEnabled(bool b)
|
|
|
|
{
|
|
|
|
_fastExtensionEnabled = b;
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<size_t>& PeerSessionResource::peerAllowedIndexSet() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
return _peerAllowedIndexSet;
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
static void updateIndexSet(std::vector<size_t>& c, size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<size_t>::iterator i = std::lower_bound(c.begin(), c.end(), index);
|
2008-05-16 14:51:14 +00:00
|
|
|
if(i == c.end() || (*i) != index) {
|
|
|
|
c.insert(i, index);
|
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::addPeerAllowedIndex(size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-05-16 14:51:14 +00:00
|
|
|
updateIndexSet(_peerAllowedIndexSet, index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-05-16 14:51:14 +00:00
|
|
|
return std::binary_search(_peerAllowedIndexSet.begin(),
|
2010-01-05 16:01:46 +00:00
|
|
|
_peerAllowedIndexSet.end(),
|
|
|
|
index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::addAmAllowedIndex(size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-05-16 14:51:14 +00:00
|
|
|
updateIndexSet(_amAllowedIndexSet, index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-05-16 14:51:14 +00:00
|
|
|
return std::binary_search(_amAllowedIndexSet.begin(),
|
2010-01-05 16:01:46 +00:00
|
|
|
_amAllowedIndexSet.end(),
|
|
|
|
index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::extendedMessagingEnabled(bool b)
|
|
|
|
{
|
|
|
|
_extendedMessagingEnabled = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
PeerSessionResource::getExtensionMessageID(const std::string& name) const
|
|
|
|
{
|
|
|
|
Extensions::const_iterator itr = _extensions.find(name);
|
|
|
|
if(itr == _extensions.end()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return (*itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PeerSessionResource::getExtensionName(uint8_t id) const
|
|
|
|
{
|
2010-02-28 16:04:52 +00:00
|
|
|
for(Extensions::const_iterator itr = _extensions.begin(),
|
|
|
|
eoi = _extensions.end(); itr != eoi; ++itr) {
|
2008-02-09 17:14:40 +00:00
|
|
|
const Extensions::value_type& p = *itr;
|
|
|
|
if(p.second == id) {
|
|
|
|
return p.first;
|
|
|
|
}
|
|
|
|
}
|
2008-05-13 14:15:23 +00:00
|
|
|
return A2STR::NIL;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::addExtension(const std::string& name, uint8_t id)
|
|
|
|
{
|
|
|
|
_extensions[name] = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::dhtEnabled(bool b)
|
|
|
|
{
|
|
|
|
_dhtEnabled = b;
|
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
uint64_t PeerSessionResource::uploadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-09-19 13:57:18 +00:00
|
|
|
return _peerStat.getSessionUploadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateUploadLength(size_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
_peerStat.updateUploadLength(bytes);
|
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
uint64_t PeerSessionResource::downloadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2008-09-19 13:57:18 +00:00
|
|
|
return _peerStat.getSessionDownloadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateDownloadLength(size_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
_peerStat.updateDownloadLength(bytes);
|
2008-04-13 01:25:36 +00:00
|
|
|
|
|
|
|
_lastDownloadUpdate.reset();
|
|
|
|
}
|
|
|
|
|
2008-05-17 05:12:14 +00:00
|
|
|
uint64_t PeerSessionResource::getCompletedLength() const
|
|
|
|
{
|
|
|
|
return _bitfieldMan->getCompletedLength();
|
|
|
|
}
|
|
|
|
|
2008-11-03 07:16:25 +00:00
|
|
|
void PeerSessionResource::setBtMessageDispatcher
|
|
|
|
(const WeakHandle<BtMessageDispatcher>& dpt)
|
|
|
|
{
|
|
|
|
_dispatcher = dpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PeerSessionResource::countOutstandingUpload() const
|
|
|
|
{
|
|
|
|
assert(!_dispatcher.isNull());
|
|
|
|
return _dispatcher->countOutstandingUpload();
|
|
|
|
}
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
} // namespace aria2
|