2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Made protected member variable private. Added accessor funcs.
	* src/AbstractBtMessage.cc
	* src/AbstractBtMessage.h
	* src/BtAllowedFastMessage.cc
	* src/BtAllowedFastMessage.h
	* src/BtBitfieldMessage.cc
	* src/BtBitfieldMessage.h
	* src/BtCancelMessage.cc
	* src/BtCancelMessage.h
	* src/BtChokeMessage.cc
	* src/BtChokeMessage.h
	* src/BtExtendedMessage.cc
	* src/BtHaveAllMessage.cc
	* src/BtHaveAllMessage.h
	* src/BtHaveMessage.cc
	* src/BtHaveMessage.h
	* src/BtHaveNoneMessage.cc
	* src/BtHaveNoneMessage.h
	* src/BtInterestedMessage.cc
	* src/BtInterestedMessage.h
	* src/BtNotInterestedMessage.cc
	* src/BtNotInterestedMessage.h
	* src/BtPieceMessage.cc
	* src/BtPieceMessage.h
	* src/BtPortMessage.cc
	* src/BtPortMessage.h
	* src/BtRejectMessage.cc
	* src/BtRejectMessage.h
	* src/BtRequestMessage.cc
	* src/BtRequestMessage.h
	* src/BtUnchokeMessage.cc
	* src/BtUnchokeMessage.h
	* src/SimpleBtMessage.cc
	* src/SimpleBtMessage.h
pull/1/head
Tatsuhiro Tsujikawa 2010-06-11 11:48:22 +00:00
parent a68b29a31a
commit 812563bb6d
34 changed files with 440 additions and 304 deletions

View File

@ -1,3 +1,40 @@
2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Made protected member variable private. Added accessor funcs.
* src/AbstractBtMessage.cc
* src/AbstractBtMessage.h
* src/BtAllowedFastMessage.cc
* src/BtAllowedFastMessage.h
* src/BtBitfieldMessage.cc
* src/BtBitfieldMessage.h
* src/BtCancelMessage.cc
* src/BtCancelMessage.h
* src/BtChokeMessage.cc
* src/BtChokeMessage.h
* src/BtExtendedMessage.cc
* src/BtHaveAllMessage.cc
* src/BtHaveAllMessage.h
* src/BtHaveMessage.cc
* src/BtHaveMessage.h
* src/BtHaveNoneMessage.cc
* src/BtHaveNoneMessage.h
* src/BtInterestedMessage.cc
* src/BtInterestedMessage.h
* src/BtNotInterestedMessage.cc
* src/BtNotInterestedMessage.h
* src/BtPieceMessage.cc
* src/BtPieceMessage.h
* src/BtPortMessage.cc
* src/BtPortMessage.h
* src/BtRejectMessage.cc
* src/BtRejectMessage.h
* src/BtRequestMessage.cc
* src/BtRequestMessage.h
* src/BtUnchokeMessage.cc
* src/BtUnchokeMessage.h
* src/SimpleBtMessage.cc
* src/SimpleBtMessage.h
2010-06-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2010-06-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Made protected member variable private. Added accessor funcs. Made protected member variable private. Added accessor funcs.

View File

@ -43,57 +43,59 @@ namespace aria2 {
AbstractBtMessage::AbstractBtMessage(uint8_t id, const std::string& name): AbstractBtMessage::AbstractBtMessage(uint8_t id, const std::string& name):
BtMessage(id), BtMessage(id),
sendingInProgress(false), _sendingInProgress(false),
invalidate(false), _invalidate(false),
uploading(false), _uploading(false),
cuid(0), _cuid(0),
_name(name), _name(name),
_metadataGetMode(false), _metadataGetMode(false),
logger(LogFactory::getInstance()) _logger(LogFactory::getInstance())
{} {}
AbstractBtMessage::~AbstractBtMessage() {} AbstractBtMessage::~AbstractBtMessage() {}
void AbstractBtMessage::setPeer(const SharedHandle<Peer>& peer) void AbstractBtMessage::setPeer(const SharedHandle<Peer>& peer)
{ {
this->peer = peer; _peer = peer;
} }
void AbstractBtMessage::validate() void AbstractBtMessage::validate()
{ {
if(validator.get()) { if(!_validator.isNull()) {
validator->validate(); _validator->validate();
} }
} }
void void
AbstractBtMessage::setBtMessageValidator(const SharedHandle<BtMessageValidator>& validator) { AbstractBtMessage::setBtMessageValidator(const SharedHandle<BtMessageValidator>& validator) {
this->validator = validator; _validator = validator;
} }
void AbstractBtMessage::setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage) void AbstractBtMessage::setPieceStorage
(const SharedHandle<PieceStorage>& pieceStorage)
{ {
this->pieceStorage = pieceStorage; _pieceStorage = pieceStorage;
} }
void AbstractBtMessage::setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher) void AbstractBtMessage::setBtMessageDispatcher
(const WeakHandle<BtMessageDispatcher>& dispatcher)
{ {
this->dispatcher = dispatcher; _dispatcher = dispatcher;
} }
void AbstractBtMessage::setPeerConnection(const WeakHandle<PeerConnection>& peerConnection) void AbstractBtMessage::setPeerConnection(const WeakHandle<PeerConnection>& peerConnection)
{ {
this->peerConnection = peerConnection; _peerConnection = peerConnection;
} }
void AbstractBtMessage::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory) void AbstractBtMessage::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
{ {
this->messageFactory = factory; _messageFactory = factory;
} }
void AbstractBtMessage::setBtRequestFactory(const WeakHandle<BtRequestFactory>& factory) void AbstractBtMessage::setBtRequestFactory(const WeakHandle<BtRequestFactory>& factory)
{ {
this->requestFactory = factory; _requestFactory = factory;
} }
} // namespace aria2 } // namespace aria2

View File

@ -50,71 +50,106 @@ class BtMessageValidator;
class Logger; class Logger;
class AbstractBtMessage : public BtMessage { class AbstractBtMessage : public BtMessage {
protected: private:
bool sendingInProgress; bool _sendingInProgress;
bool invalidate; bool _invalidate;
bool uploading; bool _uploading;
cuid_t cuid; cuid_t _cuid;
std::string _name; std::string _name;
SharedHandle<PieceStorage> pieceStorage; SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<Peer> peer; SharedHandle<Peer> _peer;
WeakHandle<BtMessageDispatcher> dispatcher; WeakHandle<BtMessageDispatcher> _dispatcher;
WeakHandle<BtMessageFactory> messageFactory; WeakHandle<BtMessageFactory> _messageFactory;
WeakHandle<BtRequestFactory> requestFactory; WeakHandle<BtRequestFactory> _requestFactory;
WeakHandle<PeerConnection> peerConnection; WeakHandle<PeerConnection> _peerConnection;
SharedHandle<BtMessageValidator> validator; SharedHandle<BtMessageValidator> _validator;
bool _metadataGetMode; bool _metadataGetMode;
Logger* logger; Logger* _logger;
protected:
Logger* getLogger() const
{
return _logger;
}
const SharedHandle<PieceStorage>& getPieceStorage() const
{
return _pieceStorage;
}
const WeakHandle<PeerConnection>& getPeerConnection() const
{
return _peerConnection;
}
const WeakHandle<BtMessageDispatcher>& getBtMessageDispatcher() const
{
return _dispatcher;
}
const WeakHandle<BtRequestFactory>& getBtRequestFactory() const
{
return _requestFactory;
}
const WeakHandle<BtMessageFactory>& getBtMessageFactory() const
{
return _messageFactory;
}
bool isMetadataGetMode() const
{
return _metadataGetMode;
}
public: public:
AbstractBtMessage(uint8_t id, const std::string& name); AbstractBtMessage(uint8_t id, const std::string& name);
virtual ~AbstractBtMessage(); virtual ~AbstractBtMessage();
virtual bool isSendingInProgress() { virtual bool isSendingInProgress() {
return sendingInProgress; return _sendingInProgress;
} }
void setSendingInProgress(bool sendingInProgress) { void setSendingInProgress(bool sendingInProgress) {
this->sendingInProgress = sendingInProgress; _sendingInProgress = sendingInProgress;
} }
virtual bool isInvalidate() { virtual bool isInvalidate() {
return invalidate; return _invalidate;
} }
void setInvalidate(bool invalidate) { void setInvalidate(bool invalidate) {
this->invalidate = invalidate; _invalidate = invalidate;
} }
virtual bool isUploading() { virtual bool isUploading() {
return uploading; return _uploading;
} }
void setUploading(bool uploading) { void setUploading(bool uploading) {
this->uploading = uploading; _uploading = uploading;
} }
cuid_t getCuid() const { cuid_t getCuid() const {
return cuid; return _cuid;
} }
void setCuid(cuid_t cuid) { void setCuid(cuid_t cuid) {
this->cuid = cuid; _cuid = cuid;
} }
const SharedHandle<Peer>& getPeer() const const SharedHandle<Peer>& getPeer() const
{ {
return peer; return _peer;
} }
void setPeer(const SharedHandle<Peer>& peer); void setPeer(const SharedHandle<Peer>& peer);
@ -137,7 +172,8 @@ public:
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage); void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher); void setBtMessageDispatcher
(const WeakHandle<BtMessageDispatcher>& dispatcher);
void setPeerConnection(const WeakHandle<PeerConnection>& peerConnection); void setPeerConnection(const WeakHandle<PeerConnection>& peerConnection);

View File

@ -41,6 +41,9 @@ namespace aria2 {
const std::string BtAllowedFastMessage::NAME("allowed fast"); const std::string BtAllowedFastMessage::NAME("allowed fast");
BtAllowedFastMessage::BtAllowedFastMessage(size_t index):
IndexBtMessage(ID, NAME, index) {}
SharedHandle<BtAllowedFastMessage> BtAllowedFastMessage::create SharedHandle<BtAllowedFastMessage> BtAllowedFastMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -48,19 +51,19 @@ SharedHandle<BtAllowedFastMessage> BtAllowedFastMessage::create
} }
void BtAllowedFastMessage::doReceivedAction() { void BtAllowedFastMessage::doReceivedAction() {
if(!peer->isFastExtensionEnabled()) { if(!getPeer()->isFastExtensionEnabled()) {
throw DL_ABORT_EX throw DL_ABORT_EX
(StringFormat("%s received while fast extension is disabled", (StringFormat("%s received while fast extension is disabled",
toString().c_str()).str()); toString().c_str()).str());
} }
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->addPeerAllowedIndex(getIndex()); getPeer()->addPeerAllowedIndex(getIndex());
} }
void BtAllowedFastMessage::onSendComplete() { void BtAllowedFastMessage::onSendComplete() {
peer->addAmAllowedIndex(getIndex()); getPeer()->addAmAllowedIndex(getIndex());
} }
} // namespace aria2 } // namespace aria2

View File

@ -45,8 +45,7 @@ typedef SharedHandle<BtAllowedFastMessage> BtAllowedFastMessageHandle;
class BtAllowedFastMessage : public IndexBtMessage { class BtAllowedFastMessage : public IndexBtMessage {
public: public:
BtAllowedFastMessage(size_t index = 0) BtAllowedFastMessage(size_t index = 0);
:IndexBtMessage(ID, NAME, index) {}
static const uint8_t ID = 17; static const uint8_t ID = 17;

View File

@ -49,15 +49,34 @@ namespace aria2 {
const std::string BtBitfieldMessage::NAME("bitfield"); const std::string BtBitfieldMessage::NAME("bitfield");
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) { BtBitfieldMessage::BtBitfieldMessage():SimpleBtMessage(ID, NAME),
if(this->bitfield == bitfield) { _bitfield(0),
_bitfieldLength(0)
{}
BtBitfieldMessage::BtBitfieldMessage
(const unsigned char* bitfield, size_t bitfieldLength):
SimpleBtMessage(ID, NAME),
_bitfield(0),
_bitfieldLength(0)
{
setBitfield(bitfield, bitfieldLength);
}
BtBitfieldMessage::~BtBitfieldMessage()
{
delete [] _bitfield;
}
void BtBitfieldMessage::setBitfield
(const unsigned char* bitfield, size_t bitfieldLength) {
if(_bitfield == bitfield) {
return; return;
} }
delete [] this->bitfield; delete [] _bitfield;
_bitfieldLength = bitfieldLength;
this->bitfieldLength = bitfieldLength; _bitfield = new unsigned char[_bitfieldLength];
this->bitfield = new unsigned char[this->bitfieldLength]; memcpy(_bitfield, bitfield, _bitfieldLength);
memcpy(this->bitfield, bitfield, this->bitfieldLength);
} }
BtBitfieldMessageHandle BtBitfieldMessageHandle
@ -71,12 +90,13 @@ BtBitfieldMessage::create(const unsigned char* data, size_t dataLength)
} }
void BtBitfieldMessage::doReceivedAction() { void BtBitfieldMessage::doReceivedAction() {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
pieceStorage->updatePieceStats(bitfield, bitfieldLength, peer->getBitfield()); getPieceStorage()->updatePieceStats(_bitfield, _bitfieldLength,
peer->setBitfield(bitfield, bitfieldLength); getPeer()->getBitfield());
if(peer->isSeeder() && pieceStorage->downloadFinished()) { getPeer()->setBitfield(_bitfield, _bitfieldLength);
if(getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER); throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
} }
} }
@ -88,19 +108,19 @@ unsigned char* BtBitfieldMessage::createMessage() {
* bitfield --- bitfield, len bytes * bitfield --- bitfield, len bytes
* total: 5+len bytes * total: 5+len bytes
*/ */
const size_t msgLength = 5+bitfieldLength; const size_t msgLength = 5+_bitfieldLength;
unsigned char* msg = new unsigned char[msgLength]; unsigned char* msg = new unsigned char[msgLength];
bittorrent::createPeerMessageString(msg, msgLength, 1+bitfieldLength, ID); bittorrent::createPeerMessageString(msg, msgLength, 1+_bitfieldLength, ID);
memcpy(msg+5, bitfield, bitfieldLength); memcpy(msg+5, _bitfield, _bitfieldLength);
return msg; return msg;
} }
size_t BtBitfieldMessage::getMessageLength() { size_t BtBitfieldMessage::getMessageLength() {
return 5+bitfieldLength; return 5+_bitfieldLength;
} }
std::string BtBitfieldMessage::toString() const { std::string BtBitfieldMessage::toString() const {
return strconcat(NAME, " ", util::toHex(bitfield, bitfieldLength)); return strconcat(NAME, " ", util::toHex(_bitfield, _bitfieldLength));
} }
} // namespace aria2 } // namespace aria2

View File

@ -45,29 +45,14 @@ typedef SharedHandle<BtBitfieldMessage> BtBitfieldMessageHandle;
class BtBitfieldMessage : public SimpleBtMessage { class BtBitfieldMessage : public SimpleBtMessage {
private: private:
unsigned char* bitfield; unsigned char* _bitfield;
size_t bitfieldLength; size_t _bitfieldLength;
void init() {
bitfield = 0;
bitfieldLength = 0;
}
public: public:
BtBitfieldMessage():SimpleBtMessage(ID, NAME) BtBitfieldMessage();
{
init();
}
BtBitfieldMessage(const unsigned char* bitfield, BtBitfieldMessage(const unsigned char* bitfield, size_t bitfieldLength);
size_t bitfieldLength):SimpleBtMessage(ID, NAME)
{
init();
setBitfield(bitfield, bitfieldLength);
}
virtual ~BtBitfieldMessage() { virtual ~BtBitfieldMessage();
delete [] bitfield;
}
static const uint8_t ID = 5; static const uint8_t ID = 5;
@ -75,11 +60,12 @@ public:
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength); void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
const unsigned char* getBitfield() const { return bitfield; } const unsigned char* getBitfield() const { return _bitfield; }
size_t getBitfieldLength() const { return bitfieldLength; } size_t getBitfieldLength() const { return _bitfieldLength; }
static BtBitfieldMessageHandle create(const unsigned char* data, size_t dataLength); static BtBitfieldMessageHandle create
(const unsigned char* data, size_t dataLength);
virtual void doReceivedAction(); virtual void doReceivedAction();

View File

@ -39,6 +39,10 @@ namespace aria2 {
const std::string BtCancelMessage::NAME("cancel"); const std::string BtCancelMessage::NAME("cancel");
BtCancelMessage::BtCancelMessage
(size_t index, uint32_t begin, size_t length)
:RangeBtMessage(ID, NAME, index, begin, length) {}
SharedHandle<BtCancelMessage> BtCancelMessage::create SharedHandle<BtCancelMessage> BtCancelMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -47,10 +51,11 @@ SharedHandle<BtCancelMessage> BtCancelMessage::create
void BtCancelMessage::doReceivedAction() void BtCancelMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
dispatcher->doCancelSendingPieceAction(getIndex(), getBegin(), getLength()); getBtMessageDispatcher()->doCancelSendingPieceAction
(getIndex(), getBegin(), getLength());
} }
} // namespace aria2 } // namespace aria2

View File

@ -45,14 +45,14 @@ typedef SharedHandle<BtCancelMessage> BtCancelMessageHandle;
class BtCancelMessage : public RangeBtMessage { class BtCancelMessage : public RangeBtMessage {
public: public:
BtCancelMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0) BtCancelMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0);
:RangeBtMessage(ID, NAME, index, begin, length) {}
static const int8_t ID = 8; static const int8_t ID = 8;
static const std::string NAME; static const std::string NAME;
static BtCancelMessageHandle create(const unsigned char* data, size_t dataLength); static BtCancelMessageHandle create
(const unsigned char* data, size_t dataLength);
virtual void doReceivedAction(); virtual void doReceivedAction();
}; };

View File

@ -41,6 +41,8 @@ namespace aria2 {
const std::string BtChokeMessage::NAME("choke"); const std::string BtChokeMessage::NAME("choke");
BtChokeMessage::BtChokeMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtChokeMessage> BtChokeMessage::create SharedHandle<BtChokeMessage> BtChokeMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -49,23 +51,23 @@ SharedHandle<BtChokeMessage> BtChokeMessage::create
void BtChokeMessage::doReceivedAction() void BtChokeMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->peerChoking(true); getPeer()->peerChoking(true);
dispatcher->doChokedAction(); getBtMessageDispatcher()->doChokedAction();
requestFactory->doChokedAction(); getBtRequestFactory()->doChokedAction();
} }
bool BtChokeMessage::sendPredicate() const bool BtChokeMessage::sendPredicate() const
{ {
return !peer->amChoking(); return !getPeer()->amChoking();
} }
void BtChokeMessage::onSendComplete() void BtChokeMessage::onSendComplete()
{ {
peer->amChoking(true); getPeer()->amChoking(true);
dispatcher->doChokingAction(); getBtMessageDispatcher()->doChokingAction();
} }
} // namespace aria2 } // namespace aria2

View File

@ -45,7 +45,7 @@ typedef SharedHandle<BtChokeMessage> BtChokeMessageHandle;
class BtChokeMessage : public ZeroBtMessage { class BtChokeMessage : public ZeroBtMessage {
public: public:
BtChokeMessage():ZeroBtMessage(ID, NAME) {} BtChokeMessage();
static const uint8_t ID = 0; static const uint8_t ID = 0;

View File

@ -85,7 +85,7 @@ size_t BtExtendedMessage::getMessageLength() {
bool BtExtendedMessage::sendPredicate() const bool BtExtendedMessage::sendPredicate() const
{ {
return peer->isExtendedMessagingEnabled(); return getPeer()->isExtendedMessagingEnabled();
} }
std::string BtExtendedMessage::toString() const { std::string BtExtendedMessage::toString() const {

View File

@ -43,6 +43,8 @@ namespace aria2 {
const std::string BtHaveAllMessage::NAME("have all"); const std::string BtHaveAllMessage::NAME("have all");
BtHaveAllMessage::BtHaveAllMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtHaveAllMessage> BtHaveAllMessage::create SharedHandle<BtHaveAllMessage> BtHaveAllMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -51,19 +53,20 @@ SharedHandle<BtHaveAllMessage> BtHaveAllMessage::create
void BtHaveAllMessage::doReceivedAction() void BtHaveAllMessage::doReceivedAction()
{ {
if(!peer->isFastExtensionEnabled()) { if(!getPeer()->isFastExtensionEnabled()) {
throw DL_ABORT_EX throw DL_ABORT_EX
(StringFormat("%s received while fast extension is disabled", (StringFormat("%s received while fast extension is disabled",
toString().c_str()).str()); toString().c_str()).str());
} }
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
pieceStorage->subtractPieceStats(peer->getBitfield(), getPieceStorage()->subtractPieceStats(getPeer()->getBitfield(),
peer->getBitfieldLength()); getPeer()->getBitfieldLength());
peer->setAllBitfield(); getPeer()->setAllBitfield();
pieceStorage->addPieceStats(peer->getBitfield(), peer->getBitfieldLength()); getPieceStorage()->addPieceStats
if(peer->isSeeder() && pieceStorage->downloadFinished()) { (getPeer()->getBitfield(), getPeer()->getBitfieldLength());
if(getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER); throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
} }
} }

View File

@ -45,7 +45,7 @@ typedef SharedHandle<BtHaveAllMessage> BtHaveAllMessageHandle;
class BtHaveAllMessage : public ZeroBtMessage { class BtHaveAllMessage : public ZeroBtMessage {
public: public:
BtHaveAllMessage():ZeroBtMessage(ID, NAME) {} BtHaveAllMessage();
static const uint8_t ID = 14; static const uint8_t ID = 14;

View File

@ -42,6 +42,8 @@ namespace aria2 {
const std::string BtHaveMessage::NAME("have"); const std::string BtHaveMessage::NAME("have");
BtHaveMessage::BtHaveMessage(size_t index):IndexBtMessage(ID, NAME, index) {}
SharedHandle<BtHaveMessage> BtHaveMessage::create SharedHandle<BtHaveMessage> BtHaveMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -50,12 +52,12 @@ SharedHandle<BtHaveMessage> BtHaveMessage::create
void BtHaveMessage::doReceivedAction() void BtHaveMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->updateBitfield(getIndex(), 1); getPeer()->updateBitfield(getIndex(), 1);
pieceStorage->addPieceStats(getIndex()); getPieceStorage()->addPieceStats(getIndex());
if(peer->isSeeder() && pieceStorage->downloadFinished()) { if(getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER); throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
} }
} }

View File

@ -45,7 +45,7 @@ typedef SharedHandle<BtHaveMessage> BtHaveMessageHandle;
class BtHaveMessage : public IndexBtMessage { class BtHaveMessage : public IndexBtMessage {
public: public:
BtHaveMessage(size_t index = 0):IndexBtMessage(ID, NAME, index) {} BtHaveMessage(size_t index = 0);
static const uint8_t ID = 4; static const uint8_t ID = 4;

View File

@ -41,6 +41,8 @@ namespace aria2 {
const std::string BtHaveNoneMessage::NAME("have none"); const std::string BtHaveNoneMessage::NAME("have none");
BtHaveNoneMessage::BtHaveNoneMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtHaveNoneMessage> BtHaveNoneMessage::create SharedHandle<BtHaveNoneMessage> BtHaveNoneMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -49,7 +51,7 @@ SharedHandle<BtHaveNoneMessage> BtHaveNoneMessage::create
void BtHaveNoneMessage::doReceivedAction() void BtHaveNoneMessage::doReceivedAction()
{ {
if(!peer->isFastExtensionEnabled()) { if(!getPeer()->isFastExtensionEnabled()) {
throw DL_ABORT_EX throw DL_ABORT_EX
(StringFormat("%s received while fast extension is disabled", (StringFormat("%s received while fast extension is disabled",
toString().c_str()).str()); toString().c_str()).str());

View File

@ -45,7 +45,8 @@ typedef SharedHandle<BtHaveNoneMessage> BtHaveNoneMessageHandle;
class BtHaveNoneMessage : public ZeroBtMessage { class BtHaveNoneMessage : public ZeroBtMessage {
public: public:
BtHaveNoneMessage():ZeroBtMessage(ID, NAME) {} BtHaveNoneMessage();
static const uint8_t ID = 15; static const uint8_t ID = 15;
static const std::string NAME; static const std::string NAME;

View File

@ -40,6 +40,8 @@ namespace aria2 {
const std::string BtInterestedMessage::NAME("interested"); const std::string BtInterestedMessage::NAME("interested");
BtInterestedMessage::BtInterestedMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtInterestedMessage> BtInterestedMessage::create SharedHandle<BtInterestedMessage> BtInterestedMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -48,22 +50,22 @@ SharedHandle<BtInterestedMessage> BtInterestedMessage::create
void BtInterestedMessage::doReceivedAction() void BtInterestedMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->peerInterested(true); getPeer()->peerInterested(true);
if(!peer->amChoking()) { if(!getPeer()->amChoking()) {
_peerStorage->executeChoke(); _peerStorage->executeChoke();
} }
} }
bool BtInterestedMessage::sendPredicate() const bool BtInterestedMessage::sendPredicate() const
{ {
return !peer->amInterested(); return !getPeer()->amInterested();
} }
void BtInterestedMessage::onSendComplete() { void BtInterestedMessage::onSendComplete() {
peer->amInterested(true); getPeer()->amInterested(true);
} }
void BtInterestedMessage::setPeerStorage void BtInterestedMessage::setPeerStorage

View File

@ -48,7 +48,7 @@ class BtInterestedMessage : public ZeroBtMessage {
private: private:
SharedHandle<PeerStorage> _peerStorage; SharedHandle<PeerStorage> _peerStorage;
public: public:
BtInterestedMessage():ZeroBtMessage(ID, NAME) {} BtInterestedMessage();
static const uint8_t ID = 2; static const uint8_t ID = 2;

View File

@ -40,6 +40,8 @@ namespace aria2 {
const std::string BtNotInterestedMessage::NAME("not interested"); const std::string BtNotInterestedMessage::NAME("not interested");
BtNotInterestedMessage::BtNotInterestedMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtNotInterestedMessage> BtNotInterestedMessage::create SharedHandle<BtNotInterestedMessage> BtNotInterestedMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -48,22 +50,22 @@ SharedHandle<BtNotInterestedMessage> BtNotInterestedMessage::create
void BtNotInterestedMessage::doReceivedAction() void BtNotInterestedMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->peerInterested(false); getPeer()->peerInterested(false);
if(!peer->amChoking()) { if(!getPeer()->amChoking()) {
_peerStorage->executeChoke(); _peerStorage->executeChoke();
} }
} }
bool BtNotInterestedMessage::sendPredicate() const bool BtNotInterestedMessage::sendPredicate() const
{ {
return peer->amInterested(); return getPeer()->amInterested();
} }
void BtNotInterestedMessage::onSendComplete() { void BtNotInterestedMessage::onSendComplete() {
peer->amInterested(false); getPeer()->amInterested(false);
} }
void BtNotInterestedMessage::setPeerStorage void BtNotInterestedMessage::setPeerStorage

View File

@ -48,7 +48,7 @@ class BtNotInterestedMessage : public ZeroBtMessage {
private: private:
SharedHandle<PeerStorage> _peerStorage; SharedHandle<PeerStorage> _peerStorage;
public: public:
BtNotInterestedMessage():ZeroBtMessage(ID, NAME) {} BtNotInterestedMessage();
static const uint8_t ID = 3; static const uint8_t ID = 3;

View File

@ -59,14 +59,33 @@ namespace aria2 {
const std::string BtPieceMessage::NAME("piece"); const std::string BtPieceMessage::NAME("piece");
BtPieceMessage::BtPieceMessage
(size_t index, uint32_t begin, size_t blockLength):
AbstractBtMessage(ID, NAME),
_index(index),
_begin(begin),
_blockLength(blockLength),
_block(0),
_rawData(0)
{
setUploading(true);
}
BtPieceMessage::~BtPieceMessage()
{
delete [] _rawData;
}
void BtPieceMessage::setRawMessage(unsigned char* data) void BtPieceMessage::setRawMessage(unsigned char* data)
{ {
delete [] _rawData; delete [] _rawData;
_rawData = data; _rawData = data;
this->block = data+9; _block = data+9;
} }
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, size_t dataLength) { BtPieceMessageHandle BtPieceMessage::create
(const unsigned char* data, size_t dataLength)
{
bittorrent::assertPayloadLengthGreater(9, dataLength, NAME); bittorrent::assertPayloadLengthGreater(9, dataLength, NAME);
bittorrent::assertID(ID, data, NAME); bittorrent::assertID(ID, data, NAME);
BtPieceMessageHandle message(new BtPieceMessage()); BtPieceMessageHandle message(new BtPieceMessage());
@ -76,32 +95,34 @@ BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, size_t da
return message; return message;
} }
void BtPieceMessage::doReceivedAction() { void BtPieceMessage::doReceivedAction()
if(_metadataGetMode) { {
if(isMetadataGetMode()) {
return; return;
} }
RequestSlot slot = dispatcher->getOutstandingRequest(index, RequestSlot slot = getBtMessageDispatcher()->getOutstandingRequest
begin, (_index, _begin, _blockLength);
blockLength); getPeer()->updateDownloadLength(_blockLength);
peer->updateDownloadLength(blockLength);
if(!RequestSlot::isNull(slot)) { if(!RequestSlot::isNull(slot)) {
peer->snubbing(false); getPeer()->snubbing(false);
SharedHandle<Piece> piece = pieceStorage->getPiece(index); SharedHandle<Piece> piece = getPieceStorage()->getPiece(_index);
off_t offset = (off_t)index*_downloadContext->getPieceLength()+begin; off_t offset = (off_t)_index*_downloadContext->getPieceLength()+_begin;
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug(MSG_PIECE_RECEIVED, getLogger()->debug(MSG_PIECE_RECEIVED,
util::itos(cuid).c_str(), index, begin, blockLength, offset, util::itos(getCuid()).c_str(),
slot.getBlockIndex()); _index, _begin, _blockLength, offset,
slot.getBlockIndex());
} }
pieceStorage->getDiskAdaptor()->writeData(block, blockLength, offset); getPieceStorage()->getDiskAdaptor()->writeData
(_block, _blockLength, offset);
piece->completeBlock(slot.getBlockIndex()); piece->completeBlock(slot.getBlockIndex());
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug(MSG_PIECE_BITFIELD, util::itos(cuid).c_str(), getLogger()->debug(MSG_PIECE_BITFIELD, util::itos(getCuid()).c_str(),
util::toHex(piece->getBitfield(), util::toHex(piece->getBitfield(),
piece->getBitfieldLength()).c_str()); piece->getBitfieldLength()).c_str());
} }
piece->updateHash(begin, block, blockLength); piece->updateHash(_begin, _block, _blockLength);
dispatcher->removeOutstandingRequest(slot); getBtMessageDispatcher()->removeOutstandingRequest(slot);
if(piece->pieceComplete()) { if(piece->pieceComplete()) {
if(checkPieceHash(piece)) { if(checkPieceHash(piece)) {
onNewPiece(piece); onNewPiece(piece);
@ -110,9 +131,9 @@ void BtPieceMessage::doReceivedAction() {
} }
} }
} else { } else {
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug("CUID#%s - RequestSlot not found, index=%d, begin=%d", getLogger()->debug("CUID#%s - RequestSlot not found, index=%d, begin=%d",
util::itos(cuid).c_str(), index, begin); util::itos(getCuid()).c_str(), _index, _begin);
} }
} }
} }
@ -130,156 +151,169 @@ unsigned char* BtPieceMessage::createMessageHeader()
*/ */
unsigned char* msgHeader = new unsigned char[MESSAGE_HEADER_LENGTH]; unsigned char* msgHeader = new unsigned char[MESSAGE_HEADER_LENGTH];
bittorrent::createPeerMessageString(msgHeader, MESSAGE_HEADER_LENGTH, bittorrent::createPeerMessageString(msgHeader, MESSAGE_HEADER_LENGTH,
9+blockLength, ID); 9+_blockLength, ID);
bittorrent::setIntParam(&msgHeader[5], index); bittorrent::setIntParam(&msgHeader[5], _index);
bittorrent::setIntParam(&msgHeader[9], begin); bittorrent::setIntParam(&msgHeader[9], _begin);
return msgHeader; return msgHeader;
} }
size_t BtPieceMessage::getMessageHeaderLength() { size_t BtPieceMessage::getMessageHeaderLength()
{
return MESSAGE_HEADER_LENGTH; return MESSAGE_HEADER_LENGTH;
} }
void BtPieceMessage::send() { void BtPieceMessage::send()
if(invalidate) { {
if(isInvalidate()) {
return; return;
} }
size_t writtenLength; size_t writtenLength;
if(!sendingInProgress) { if(!isSendingInProgress()) {
if(logger->info()) { if(getLogger()->info()) {
logger->info(MSG_SEND_PEER_MESSAGE, getLogger()->info(MSG_SEND_PEER_MESSAGE,
util::itos(cuid).c_str(), peer->ipaddr.c_str(), peer->port, util::itos(getCuid()).c_str(),
toString().c_str()); getPeer()->ipaddr.c_str(), getPeer()->port,
toString().c_str());
} }
unsigned char* msgHdr = createMessageHeader(); unsigned char* msgHdr = createMessageHeader();
size_t msgHdrLen = getMessageHeaderLength(); size_t msgHdrLen = getMessageHeaderLength();
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug("msglength = %lu bytes", getLogger()->debug("msglength = %lu bytes",
static_cast<unsigned long>(msgHdrLen+blockLength)); static_cast<unsigned long>(msgHdrLen+_blockLength));
} }
peerConnection->pushBytes(msgHdr, msgHdrLen); getPeerConnection()->pushBytes(msgHdr, msgHdrLen);
peerConnection->sendPendingData(); getPeerConnection()->sendPendingData();
off_t pieceDataOffset = off_t pieceDataOffset =
(off_t)index*_downloadContext->getPieceLength()+begin; (off_t)_index*_downloadContext->getPieceLength()+_begin;
writtenLength = sendPieceData(pieceDataOffset, blockLength); writtenLength = sendPieceData(pieceDataOffset, _blockLength);
} else { } else {
writtenLength = peerConnection->sendPendingData(); writtenLength = getPeerConnection()->sendPendingData();
} }
peer->updateUploadLength(writtenLength); getPeer()->updateUploadLength(writtenLength);
sendingInProgress = !peerConnection->sendBufferIsEmpty(); setSendingInProgress(!getPeerConnection()->sendBufferIsEmpty());
} }
size_t BtPieceMessage::sendPieceData(off_t offset, size_t length) const { size_t BtPieceMessage::sendPieceData(off_t offset, size_t length) const
{
assert(length <= 16*1024); assert(length <= 16*1024);
unsigned char* buf = new unsigned char[length]; unsigned char* buf = new unsigned char[length];
ssize_t r; ssize_t r;
try { try {
r = pieceStorage->getDiskAdaptor()->readData(buf, length, offset); r = getPieceStorage()->getDiskAdaptor()->readData(buf, length, offset);
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
delete [] buf; delete [] buf;
throw; throw;
} }
if(r == static_cast<ssize_t>(length)) { if(r == static_cast<ssize_t>(length)) {
peerConnection->pushBytes(buf, length); getPeerConnection()->pushBytes(buf, length);
return peerConnection->sendPendingData(); return getPeerConnection()->sendPendingData();
} else { } else {
throw DL_ABORT_EX(EX_DATA_READ); throw DL_ABORT_EX(EX_DATA_READ);
} }
} }
std::string BtPieceMessage::toString() const { std::string BtPieceMessage::toString() const
return strconcat(NAME, " index=", util::itos(index), ", begin=", {
util::itos(begin), ", length=", util::itos(blockLength)); return strconcat(NAME, " index=", util::itos(_index), ", begin=",
util::itos(_begin), ", length=", util::itos(_blockLength));
} }
bool BtPieceMessage::checkPieceHash(const SharedHandle<Piece>& piece) { bool BtPieceMessage::checkPieceHash(const SharedHandle<Piece>& piece)
{
if(piece->isHashCalculated()) { if(piece->isHashCalculated()) {
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug("Hash is available!! index=%lu", getLogger()->debug("Hash is available!! index=%lu",
static_cast<unsigned long>(piece->getIndex())); static_cast<unsigned long>(piece->getIndex()));
} }
return return
piece->getHashString()==_downloadContext->getPieceHash(piece->getIndex()); piece->getHashString()==_downloadContext->getPieceHash(piece->getIndex());
} else { } else {
off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength(); off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength();
return MessageDigestHelper::staticSHA1Digest(pieceStorage->getDiskAdaptor(), offset, piece->getLength()) return MessageDigestHelper::staticSHA1Digest
(getPieceStorage()->getDiskAdaptor(), offset, piece->getLength())
== _downloadContext->getPieceHash(piece->getIndex()); == _downloadContext->getPieceHash(piece->getIndex());
} }
} }
void BtPieceMessage::onNewPiece(const SharedHandle<Piece>& piece) { void BtPieceMessage::onNewPiece(const SharedHandle<Piece>& piece)
if(logger->info()) { {
logger->info(MSG_GOT_NEW_PIECE, util::itos(cuid).c_str(),piece->getIndex()); if(getLogger()->info()) {
getLogger()->info(MSG_GOT_NEW_PIECE,
util::itos(getCuid()).c_str(), piece->getIndex());
} }
pieceStorage->completePiece(piece); getPieceStorage()->completePiece(piece);
pieceStorage->advertisePiece(cuid, piece->getIndex()); getPieceStorage()->advertisePiece(getCuid(), piece->getIndex());
} }
void BtPieceMessage::onWrongPiece(const SharedHandle<Piece>& piece) { void BtPieceMessage::onWrongPiece(const SharedHandle<Piece>& piece)
if(logger->info()) { {
logger->info(MSG_GOT_WRONG_PIECE, if(getLogger()->info()) {
util::itos(cuid).c_str(), piece->getIndex()); getLogger()->info(MSG_GOT_WRONG_PIECE,
util::itos(getCuid()).c_str(), piece->getIndex());
} }
erasePieceOnDisk(piece); erasePieceOnDisk(piece);
piece->clearAllBlock(); piece->clearAllBlock();
piece->destroyHashContext(); piece->destroyHashContext();
requestFactory->removeTargetPiece(piece); getBtRequestFactory()->removeTargetPiece(piece);
} }
void BtPieceMessage::erasePieceOnDisk(const SharedHandle<Piece>& piece) { void BtPieceMessage::erasePieceOnDisk(const SharedHandle<Piece>& piece)
{
size_t BUFSIZE = 4096; size_t BUFSIZE = 4096;
unsigned char buf[BUFSIZE]; unsigned char buf[BUFSIZE];
memset(buf, 0, BUFSIZE); memset(buf, 0, BUFSIZE);
off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength(); off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength();
div_t res = div(piece->getLength(), BUFSIZE); div_t res = div(piece->getLength(), BUFSIZE);
for(int i = 0; i < res.quot; ++i) { for(int i = 0; i < res.quot; ++i) {
pieceStorage->getDiskAdaptor()->writeData(buf, BUFSIZE, offset); getPieceStorage()->getDiskAdaptor()->writeData(buf, BUFSIZE, offset);
offset += BUFSIZE; offset += BUFSIZE;
} }
if(res.rem > 0) { if(res.rem > 0) {
pieceStorage->getDiskAdaptor()->writeData(buf, res.rem, offset); getPieceStorage()->getDiskAdaptor()->writeData(buf, res.rem, offset);
} }
} }
void BtPieceMessage::onChokingEvent(const BtChokingEvent& event) void BtPieceMessage::onChokingEvent(const BtChokingEvent& event)
{ {
if(!invalidate && if(!isInvalidate() &&
!sendingInProgress && !isSendingInProgress() &&
!peer->isInAmAllowedIndexSet(index)) { !getPeer()->isInAmAllowedIndexSet(_index)) {
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug(MSG_REJECT_PIECE_CHOKED, getLogger()->debug(MSG_REJECT_PIECE_CHOKED,
util::itos(cuid).c_str(), index, begin, blockLength); util::itos(getCuid()).c_str(),
_index, _begin, _blockLength);
} }
if(peer->isFastExtensionEnabled()) { if(getPeer()->isFastExtensionEnabled()) {
BtMessageHandle rej = messageFactory->createRejectMessage(index, BtMessageHandle rej =
begin, getBtMessageFactory()->createRejectMessage
blockLength); (_index, _begin, _blockLength);
dispatcher->addMessageToQueue(rej); getBtMessageDispatcher()->addMessageToQueue(rej);
} }
invalidate = true; setInvalidate(true);
} }
} }
void BtPieceMessage::onCancelSendingPieceEvent void BtPieceMessage::onCancelSendingPieceEvent
(const BtCancelSendingPieceEvent& event) (const BtCancelSendingPieceEvent& event)
{ {
if(!invalidate && if(!isInvalidate() &&
!sendingInProgress && !isSendingInProgress() &&
index == event.getIndex() && _index == event.getIndex() &&
begin == event.getBegin() && _begin == event.getBegin() &&
blockLength == event.getLength()) { _blockLength == event.getLength()) {
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug(MSG_REJECT_PIECE_CANCEL, getLogger()->debug(MSG_REJECT_PIECE_CANCEL,
util::itos(cuid).c_str(), index, begin, blockLength); util::itos(getCuid()).c_str(),
_index, _begin, _blockLength);
} }
if(peer->isFastExtensionEnabled()) { if(getPeer()->isFastExtensionEnabled()) {
BtMessageHandle rej = messageFactory->createRejectMessage(index, BtMessageHandle rej =
begin, getBtMessageFactory()->createRejectMessage
blockLength); (_index, _begin, _blockLength);
dispatcher->addMessageToQueue(rej); getBtMessageDispatcher()->addMessageToQueue(rej);
} }
invalidate = true; setInvalidate(true);
} }
} }

View File

@ -47,10 +47,10 @@ typedef SharedHandle<BtPieceMessage> BtPieceMessageHandle;
class BtPieceMessage : public AbstractBtMessage { class BtPieceMessage : public AbstractBtMessage {
private: private:
size_t index; size_t _index;
uint32_t begin; uint32_t _begin;
uint32_t blockLength; uint32_t _blockLength;
unsigned char* block; unsigned char* _block;
unsigned char* _rawData; unsigned char* _rawData;
SharedHandle<DownloadContext> _downloadContext; SharedHandle<DownloadContext> _downloadContext;
@ -66,47 +66,37 @@ private:
size_t sendPieceData(off_t offset, size_t length) const; size_t sendPieceData(off_t offset, size_t length) const;
public: public:
BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0) BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0);
:AbstractBtMessage(ID, NAME),
index(index),
begin(begin),
blockLength(blockLength),
block(0),
_rawData(0)
{
uploading = true;
}
virtual ~BtPieceMessage() { virtual ~BtPieceMessage();
delete [] _rawData;
}
static const uint8_t ID = 7; static const uint8_t ID = 7;
static const std::string NAME; static const std::string NAME;
size_t getIndex() const { return index; } size_t getIndex() const { return _index; }
void setIndex(size_t index) { this->index = index; } void setIndex(size_t index) { _index = index; }
uint32_t getBegin() const { return begin; } uint32_t getBegin() const { return _begin; }
void setBegin(uint32_t begin) { this->begin = begin; } void setBegin(uint32_t begin) { _begin = begin; }
const unsigned char* getBlock() const { return block; } const unsigned char* getBlock() const { return _block; }
size_t getBlockLength() const { return blockLength; } size_t getBlockLength() const { return _blockLength; }
// Stores raw message data. After this function call, this object // Stores raw message data. After this function call, this object
// has ownership of data. Caller must not be free or alter data. // has ownership of data. Caller must not be free or alter data.
// Member block is pointed to block starting position in data. // Member block is pointed to block starting position in data.
void setRawMessage(unsigned char* data); void setRawMessage(unsigned char* data);
void setBlockLength(size_t blockLength) { this->blockLength = blockLength; } void setBlockLength(size_t blockLength) { _blockLength = blockLength; }
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext); void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
static BtPieceMessageHandle create(const unsigned char* data, size_t dataLength); static BtPieceMessageHandle create
(const unsigned char* data, size_t dataLength);
virtual void doReceivedAction(); virtual void doReceivedAction();

View File

@ -68,15 +68,15 @@ void BtPortMessage::doReceivedAction()
{ {
if(!_taskFactory.isNull() && !_taskQueue.isNull()) { if(!_taskFactory.isNull() && !_taskQueue.isNull()) {
if(_port == 0) { if(_port == 0) {
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug("Ignored port 0."); getLogger()->debug("Ignored port 0.");
} }
return; return;
} }
// node id is random at this point. When ping reply received, new DHTNode // node id is random at this point. When ping reply received, new DHTNode
// instance created with proper node ID and is added to a routing table. // instance created with proper node ID and is added to a routing table.
SharedHandle<DHTNode> node(new DHTNode()); SharedHandle<DHTNode> node(new DHTNode());
node->setIPAddress(peer->ipaddr); node->setIPAddress(getPeer()->ipaddr);
node->setPort(_port); node->setPort(_port);
{ {
SharedHandle<DHTTask> task = _taskFactory->createPingTask(node); SharedHandle<DHTTask> task = _taskFactory->createPingTask(node);
@ -84,11 +84,13 @@ void BtPortMessage::doReceivedAction()
} }
if(_routingTable->countBucket() == 1) { if(_routingTable->countBucket() == 1) {
// initiate bootstrap // initiate bootstrap
logger->info("Dispatch node_lookup since too few buckets."); getLogger()->info("Dispatch node_lookup since too few buckets.");
_taskQueue->addImmediateTask(_taskFactory->createNodeLookupTask(_localNode->getID())); _taskQueue->addImmediateTask
(_taskFactory->createNodeLookupTask(_localNode->getID()));
} }
} else { } else {
logger->info("DHT port message received while localhost didn't declare support it."); getLogger()->info
("DHT port message received while localhost didn't declare support it.");
} }
} }

View File

@ -65,7 +65,8 @@ public:
uint16_t getPort() const { return _port; } uint16_t getPort() const { return _port; }
static SharedHandle<BtPortMessage> create(const unsigned char* data, size_t dataLength); static SharedHandle<BtPortMessage> create
(const unsigned char* data, size_t dataLength);
virtual void doReceivedAction(); virtual void doReceivedAction();

View File

@ -43,6 +43,10 @@ namespace aria2 {
const std::string BtRejectMessage::NAME("reject"); const std::string BtRejectMessage::NAME("reject");
BtRejectMessage::BtRejectMessage
(size_t index, uint32_t begin, size_t length):
RangeBtMessage(ID, NAME, index, begin, length) {}
SharedHandle<BtRejectMessage> BtRejectMessage::create SharedHandle<BtRejectMessage> BtRejectMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -51,22 +55,23 @@ SharedHandle<BtRejectMessage> BtRejectMessage::create
void BtRejectMessage::doReceivedAction() void BtRejectMessage::doReceivedAction()
{ {
if(!peer->isFastExtensionEnabled()) { if(!getPeer()->isFastExtensionEnabled()) {
throw DL_ABORT_EX throw DL_ABORT_EX
(StringFormat("%s received while fast extension is disabled.", (StringFormat("%s received while fast extension is disabled.",
toString().c_str()).str()); toString().c_str()).str());
} }
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
// TODO Current implementation does not close a connection even if // TODO Current implementation does not close a connection even if
// a request for this reject message has never sent. // a request for this reject message has never sent.
RequestSlot slot = RequestSlot slot =
dispatcher->getOutstandingRequest(getIndex(), getBegin(), getLength()); getBtMessageDispatcher()->getOutstandingRequest
(getIndex(), getBegin(), getLength());
if(RequestSlot::isNull(slot)) { if(RequestSlot::isNull(slot)) {
//throw DL_ABORT_EX("reject received, but it is not in the request slots."); //throw DL_ABORT_EX("reject received, but it is not in the request slots.");
} else { } else {
dispatcher->removeOutstandingRequest(slot); getBtMessageDispatcher()->removeOutstandingRequest(slot);
} }
} }

View File

@ -45,8 +45,7 @@ typedef SharedHandle<BtRejectMessage> BtRejectMessageHandle;
class BtRejectMessage : public RangeBtMessage { class BtRejectMessage : public RangeBtMessage {
public: public:
BtRejectMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0) BtRejectMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0);
:RangeBtMessage(ID, NAME, index, begin, length) {}
static const uint8_t ID = 16; static const uint8_t ID = 16;

View File

@ -43,6 +43,11 @@ namespace aria2 {
const std::string BtRequestMessage::NAME("request"); const std::string BtRequestMessage::NAME("request");
BtRequestMessage::BtRequestMessage
(size_t index, uint32_t begin, uint32_t length, size_t blockIndex):
RangeBtMessage(ID, NAME, index, begin, length),
_blockIndex(blockIndex) {}
SharedHandle<BtRequestMessage> BtRequestMessage::create SharedHandle<BtRequestMessage> BtRequestMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -51,22 +56,23 @@ SharedHandle<BtRequestMessage> BtRequestMessage::create
void BtRequestMessage::doReceivedAction() void BtRequestMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
if(pieceStorage->hasPiece(getIndex()) && if(getPieceStorage()->hasPiece(getIndex()) &&
(!peer->amChoking() || (!getPeer()->amChoking() ||
(peer->amChoking() && peer->isInAmAllowedIndexSet(getIndex())))) { (getPeer()->amChoking() &&
BtMessageHandle msg = messageFactory->createPieceMessage(getIndex(), getPeer()->isInAmAllowedIndexSet(getIndex())))) {
getBegin(), BtMessageHandle msg =
getLength()); getBtMessageFactory()->createPieceMessage
dispatcher->addMessageToQueue(msg); (getIndex(), getBegin(), getLength());
getBtMessageDispatcher()->addMessageToQueue(msg);
} else { } else {
if(peer->isFastExtensionEnabled()) { if(getPeer()->isFastExtensionEnabled()) {
BtMessageHandle msg = messageFactory->createRejectMessage(getIndex(), BtMessageHandle msg =
getBegin(), getBtMessageFactory()->createRejectMessage
getLength()); (getIndex(), getBegin(), getLength());
dispatcher->addMessageToQueue(msg); getBtMessageDispatcher()->addMessageToQueue(msg);
} }
} }
} }
@ -74,16 +80,16 @@ void BtRequestMessage::doReceivedAction()
void BtRequestMessage::onQueued() void BtRequestMessage::onQueued()
{ {
RequestSlot requestSlot(getIndex(), getBegin(), getLength(), _blockIndex, RequestSlot requestSlot(getIndex(), getBegin(), getLength(), _blockIndex,
pieceStorage->getPiece(getIndex())); getPieceStorage()->getPiece(getIndex()));
dispatcher->addOutstandingRequest(requestSlot); getBtMessageDispatcher()->addOutstandingRequest(requestSlot);
} }
void BtRequestMessage::onAbortOutstandingRequestEvent void BtRequestMessage::onAbortOutstandingRequestEvent
(const BtAbortOutstandingRequestEvent& event) (const BtAbortOutstandingRequestEvent& event)
{ {
if(getIndex() == event.getPiece()->getIndex() && if(getIndex() == event.getPiece()->getIndex() &&
!invalidate && !sendingInProgress) { !isInvalidate() && !isSendingInProgress()) {
invalidate = true; setInvalidate(true);
} }
} }

View File

@ -46,14 +46,11 @@ typedef SharedHandle<BtRequestMessage> BtRequestMessageHandle;
class BtRequestMessage : public RangeBtMessage { class BtRequestMessage : public RangeBtMessage {
private: private:
size_t _blockIndex; size_t _blockIndex;
public: public:
BtRequestMessage(size_t index = 0, BtRequestMessage(size_t index = 0,
uint32_t begin = 0, uint32_t begin = 0,
uint32_t length = 0, uint32_t length = 0,
size_t blockIndex = 0) size_t blockIndex = 0);
:RangeBtMessage(ID, NAME, index, begin, length),
_blockIndex(blockIndex) {}
static const uint8_t ID = 6; static const uint8_t ID = 6;

View File

@ -39,6 +39,8 @@ namespace aria2 {
const std::string BtUnchokeMessage::NAME("unchoke"); const std::string BtUnchokeMessage::NAME("unchoke");
BtUnchokeMessage::BtUnchokeMessage():ZeroBtMessage(ID, NAME) {}
SharedHandle<BtUnchokeMessage> BtUnchokeMessage::create SharedHandle<BtUnchokeMessage> BtUnchokeMessage::create
(const unsigned char* data, size_t dataLength) (const unsigned char* data, size_t dataLength)
{ {
@ -47,19 +49,19 @@ SharedHandle<BtUnchokeMessage> BtUnchokeMessage::create
void BtUnchokeMessage::doReceivedAction() void BtUnchokeMessage::doReceivedAction()
{ {
if(_metadataGetMode) { if(isMetadataGetMode()) {
return; return;
} }
peer->peerChoking(false); getPeer()->peerChoking(false);
} }
bool BtUnchokeMessage::sendPredicate() const bool BtUnchokeMessage::sendPredicate() const
{ {
return peer->amChoking(); return getPeer()->amChoking();
} }
void BtUnchokeMessage::onSendComplete() { void BtUnchokeMessage::onSendComplete() {
peer->amChoking(false); getPeer()->amChoking(false);
} }
} // namespace aria2 } // namespace aria2

View File

@ -47,7 +47,7 @@ class BtUnchokeMessage : public ZeroBtMessage {
private: private:
static const size_t MESSAGE_LENGTH = 5; static const size_t MESSAGE_LENGTH = 5;
public: public:
BtUnchokeMessage():ZeroBtMessage(ID, NAME) {} BtUnchokeMessage();
static const uint8_t ID = 1; static const uint8_t ID = 1;

View File

@ -44,32 +44,32 @@ namespace aria2 {
SimpleBtMessage::SimpleBtMessage(uint8_t id, const std::string& name): SimpleBtMessage::SimpleBtMessage(uint8_t id, const std::string& name):
AbstractBtMessage(id, name) {} AbstractBtMessage(id, name) {}
SimpleBtMessage::~SimpleBtMessage() {}
void SimpleBtMessage::send() { void SimpleBtMessage::send() {
if(invalidate) { if(isInvalidate()) {
return; return;
} }
if(!sendPredicate() && !sendingInProgress) { if(!sendPredicate() && !isSendingInProgress()) {
return; return;
} }
if(!sendingInProgress) { if(!isSendingInProgress()) {
if(logger->info()) { if(getLogger()->info()) {
logger->info(MSG_SEND_PEER_MESSAGE, getLogger()->info(MSG_SEND_PEER_MESSAGE,
util::itos(cuid).c_str(), util::itos(getCuid()).c_str(),
peer->ipaddr.c_str(), peer->port, toString().c_str()); getPeer()->ipaddr.c_str(),
getPeer()->port,
toString().c_str());
} }
unsigned char* msg = createMessage(); unsigned char* msg = createMessage();
size_t msgLength = getMessageLength(); size_t msgLength = getMessageLength();
if(logger->debug()) { if(getLogger()->debug()) {
logger->debug("msglength = %lu bytes", getLogger()->debug("msglength = %lu bytes",
static_cast<unsigned long>(msgLength)); static_cast<unsigned long>(msgLength));
} }
peerConnection->pushBytes(msg, msgLength); getPeerConnection()->pushBytes(msg, msgLength);
} }
peerConnection->sendPendingData(); getPeerConnection()->sendPendingData();
sendingInProgress = !peerConnection->sendBufferIsEmpty(); setSendingInProgress(!getPeerConnection()->sendBufferIsEmpty());
if(!sendingInProgress) { if(!isSendingInProgress()) {
onSendComplete(); onSendComplete();
} }
} }

View File

@ -43,8 +43,6 @@ class SimpleBtMessage : public AbstractBtMessage {
public: public:
SimpleBtMessage(uint8_t id, const std::string& name); SimpleBtMessage(uint8_t id, const std::string& name);
virtual ~SimpleBtMessage();
virtual void send(); virtual void send();
virtual unsigned char* createMessage() = 0; virtual unsigned char* createMessage() = 0;