diff --git a/ChangeLog b/ChangeLog index 89f70ede..20e9c627 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,72 @@ +2009-03-13 Tatsuhiro Tsujikawa + + Refactored BtMessage subclasses. BtCancelMessage, BtRequestMessage + and BtRejectMessage are derived from RangeBtMessage class. + BtAllowedFastMessage, BtHaveMessage and BtSuggestPieceMessage are + derived from IndexBtMessage class. BtChokeMessage, + BtHaveAllMessage, BtHaveNoneMessage, BtInterestedMessage, + BtNotInterestedMessage and BtUnchokeMessage are derived from + ZeroBtMessage class. + * src/AbstractBtMessage.cc + * src/AbstractBtMessage.h + * src/BtAllowedFastMessage.cc + * src/BtAllowedFastMessage.h + * src/BtAllowedFastMessageValidator.h: Removed + * src/BtBitfieldMessage.cc + * src/BtBitfieldMessage.h + * src/BtCancelMessage.cc + * src/BtCancelMessage.h + * src/BtCancelMessageValidator.h: Removed + * src/BtChokeMessage.cc + * src/BtChokeMessage.h + * src/BtExtendedMessage.cc + * src/BtExtendedMessage.h + * src/BtHandshakeMessage.cc + * src/BtHandshakeMessage.h + * src/BtHaveAllMessage.cc + * src/BtHaveAllMessage.h + * src/BtHaveMessage.cc + * src/BtHaveMessage.h + * src/BtHaveMessageValidator.h: Removed + * src/BtHaveNoneMessage.cc + * src/BtHaveNoneMessage.h + * src/BtInterestedMessage.cc + * src/BtInterestedMessage.h + * src/BtKeepAliveMessage.cc + * src/BtKeepAliveMessage.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/BtRejectMessageValidator.h: Removed + * src/BtRequestMessage.cc + * src/BtRequestMessage.h + * src/BtRequestMessageValidator.h: Removed + * src/BtSuggestPieceMessage.cc + * src/BtSuggestPieceMessage.h + * src/BtSuggestPieceMessageValidator.h: Removed + * src/BtUnchokeMessage.cc + * src/BtUnchokeMessage.h + * src/DefaultBtMessageFactory.cc + * src/IndexBtMessage.cc + * src/IndexBtMessage.h + * src/IndexBtMessageValidator.h + * src/Makefile.am + * src/PeerMessageUtil.cc + * src/PeerMessageUtil.h + * src/RangeBtMessage.cc + * src/RangeBtMessage.h + * src/RangeBtMessageValidator.h + * src/SimpleBtMessage.cc + * src/SimpleBtMessage.h + * src/ZeroBtMessage.cc + * src/ZeroBtMessage.h + * test/BtRequestMessageTest.cc + 2009-03-12 Tatsuhiro Tsujikawa Reverted usage_text.h to r1093 to avoid to lose translated diff --git a/src/AbstractBtMessage.cc b/src/AbstractBtMessage.cc index 455dbc7b..eb339ac5 100644 --- a/src/AbstractBtMessage.cc +++ b/src/AbstractBtMessage.cc @@ -43,12 +43,13 @@ namespace aria2 { -AbstractBtMessage::AbstractBtMessage(uint8_t id): +AbstractBtMessage::AbstractBtMessage(uint8_t id, const std::string& name): BtMessage(id), sendingInProgress(false), invalidate(false), uploading(false), cuid(0), + _name(name), logger(LogFactory::getInstance()) {} @@ -132,4 +133,9 @@ void AbstractBtMessage::setBtRequestFactory(const WeakHandle& this->requestFactory = factory; } +const std::string& AbstractBtMessage::getName() const +{ + return _name; +} + } // namespace aria2 diff --git a/src/AbstractBtMessage.h b/src/AbstractBtMessage.h index 3acb1c5b..5c1fffff 100644 --- a/src/AbstractBtMessage.h +++ b/src/AbstractBtMessage.h @@ -58,6 +58,8 @@ protected: bool uploading; int32_t cuid; + std::string _name; + SharedHandle btContext; SharedHandle pieceStorage; @@ -78,7 +80,7 @@ protected: Logger* logger; public: - AbstractBtMessage(uint8_t id); + AbstractBtMessage(uint8_t id, const std::string& name); virtual ~AbstractBtMessage(); @@ -145,6 +147,8 @@ public: void setBtMessageFactory(const WeakHandle& factory); void setBtRequestFactory(const WeakHandle& factory); + + const std::string& getName() const; }; typedef SharedHandle AbstractBtMessageHandle; diff --git a/src/BtAllowedFastMessage.cc b/src/BtAllowedFastMessage.cc index b9d557fe..a3e81350 100644 --- a/src/BtAllowedFastMessage.cc +++ b/src/BtAllowedFastMessage.cc @@ -33,28 +33,18 @@ */ /* copyright --> */ #include "BtAllowedFastMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" #include "DlAbortEx.h" -#include "message.h" #include "Peer.h" #include "StringFormat.h" namespace aria2 { -BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 5) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "allowed fast", dataLength, 5).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "allowed fast", ID).str()); - } - BtAllowedFastMessageHandle message(new BtAllowedFastMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - return message; +const std::string BtAllowedFastMessage::NAME("allowed fast"); + +SharedHandle BtAllowedFastMessage::create +(const unsigned char* data, size_t dataLength) +{ + return IndexBtMessage::create(data, dataLength); } void BtAllowedFastMessage::doReceivedAction() { @@ -63,34 +53,11 @@ void BtAllowedFastMessage::doReceivedAction() { (StringFormat("%s received while fast extension is disabled", toString().c_str()).str()); } - peer->addPeerAllowedIndex(index); -} - -const unsigned char* BtAllowedFastMessage::getMessage() { - if(!msg) { - /** - * len --- 5, 4bytes - * id --- 17, 1byte - * piece index --- index, 4bytes - * total: 9bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - } - return msg; -} - -size_t BtAllowedFastMessage::getMessageLength() { - return MESSAGE_LENGTH; + peer->addPeerAllowedIndex(getIndex()); } void BtAllowedFastMessage::onSendComplete() { - peer->addAmAllowedIndex(index); -} - -std::string BtAllowedFastMessage::toString() const { - return "allowed fast index="+Util::itos(index); + peer->addAmAllowedIndex(getIndex()); } } // namespace aria2 diff --git a/src/BtAllowedFastMessage.h b/src/BtAllowedFastMessage.h index 8d25d388..ab22f0a7 100644 --- a/src/BtAllowedFastMessage.h +++ b/src/BtAllowedFastMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_ALLOWED_FAST_MESSAGE_H_ #define _D_BT_ALLOWED_FAST_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "IndexBtMessage.h" namespace aria2 { @@ -43,39 +43,20 @@ class BtAllowedFastMessage; typedef SharedHandle BtAllowedFastMessageHandle; -class BtAllowedFastMessage : public SimpleBtMessage { -private: - size_t index; - unsigned char* msg; - - static const size_t MESSAGE_LENGTH = 9; +class BtAllowedFastMessage : public IndexBtMessage { public: BtAllowedFastMessage(size_t index = 0) - :SimpleBtMessage(ID), - index(index), - msg(0) {} - - virtual ~BtAllowedFastMessage() { - delete [] msg; - } + :IndexBtMessage(ID, NAME, index) {} static const uint8_t ID = 17; - void setIndex(size_t index) { - this->index = index; - } - size_t getIndex() const { return index; } + static const std::string NAME; - static BtAllowedFastMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - virtual void onSendComplete(); }; diff --git a/src/BtBitfieldMessage.cc b/src/BtBitfieldMessage.cc index 13541777..dc5ea786 100644 --- a/src/BtBitfieldMessage.cc +++ b/src/BtBitfieldMessage.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "BtBitfieldMessage.h" + +#include + #include "PeerMessageUtil.h" #include "Util.h" #include "DlAbortEx.h" @@ -40,10 +43,11 @@ #include "Peer.h" #include "StringFormat.h" #include "PieceStorage.h" -#include namespace aria2 { +const std::string BtBitfieldMessage::NAME("bitfield"); + void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) { if(this->bitfield == bitfield) { return; @@ -58,15 +62,8 @@ void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfie BtBitfieldMessageHandle BtBitfieldMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength <= 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "bitfield", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "bitfield", ID).str()); - } + PeerMessageUtil::assertPayloadLengthGreater(1,dataLength, NAME); + PeerMessageUtil::assertID(ID, data, NAME); BtBitfieldMessageHandle message(new BtBitfieldMessage()); message->setBitfield((unsigned char*)data+1, dataLength-1); return message; @@ -100,7 +97,7 @@ size_t BtBitfieldMessage::getMessageLength() { } std::string BtBitfieldMessage::toString() const { - return "bitfield "+Util::toHex(bitfield, bitfieldLength); + return NAME+" "+Util::toHex(bitfield, bitfieldLength); } } // namespace aria2 diff --git a/src/BtBitfieldMessage.h b/src/BtBitfieldMessage.h index 03fe02a8..2a05471e 100644 --- a/src/BtBitfieldMessage.h +++ b/src/BtBitfieldMessage.h @@ -57,13 +57,13 @@ private: msgLength = 0; } public: - BtBitfieldMessage():SimpleBtMessage(ID) + BtBitfieldMessage():SimpleBtMessage(ID, NAME) { init(); } BtBitfieldMessage(const unsigned char* bitfield, - size_t bitfieldLength):SimpleBtMessage(ID) + size_t bitfieldLength):SimpleBtMessage(ID, NAME) { init(); setBitfield(bitfield, bitfieldLength); @@ -76,6 +76,8 @@ public: static const uint8_t ID = 5; + static const std::string NAME; + void setBitfield(const unsigned char* bitfield, size_t bitfieldLength); const unsigned char* getBitfield() const { return bitfield; } diff --git a/src/BtCancelMessage.cc b/src/BtCancelMessage.cc index a87e904c..536f4b58 100644 --- a/src/BtCancelMessage.cc +++ b/src/BtCancelMessage.cc @@ -33,62 +33,21 @@ */ /* copyright --> */ #include "BtCancelMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" -#include "DlAbortEx.h" -#include "message.h" #include "BtMessageDispatcher.h" -#include "StringFormat.h" namespace aria2 { -BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 13) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "cancel", dataLength, 13).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "cancel", ID).str()); - } - BtCancelMessageHandle message(new BtCancelMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - message->setBegin(PeerMessageUtil::getIntParam(data, 5)); - message->setLength(PeerMessageUtil::getIntParam(data, 9)); - return message; +const std::string BtCancelMessage::NAME("cancel"); + +SharedHandle BtCancelMessage::create +(const unsigned char* data, size_t dataLength) +{ + return RangeBtMessage::create(data, dataLength); } -void BtCancelMessage::doReceivedAction() { - dispatcher->doCancelSendingPieceAction(index, begin, length); -} - -const unsigned char* BtCancelMessage::getMessage() { - if(!msg) { - /** - * len --- 13, 4bytes - * id --- 8, 1byte - * index --- index, 4bytes - * begin --- begin, 4bytes - * length -- length, 4bytes - * total: 17bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - PeerMessageUtil::setIntParam(&msg[9], begin); - PeerMessageUtil::setIntParam(&msg[13], length); - } - return msg; -} - -size_t BtCancelMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtCancelMessage::toString() const { - return "cancel index="+Util::uitos(index)+", begin="+Util::uitos(begin)+ - ", length="+Util::uitos(length); +void BtCancelMessage::doReceivedAction() +{ + dispatcher->doCancelSendingPieceAction(getIndex(), getBegin(), getLength()); } } // namespace aria2 diff --git a/src/BtCancelMessage.h b/src/BtCancelMessage.h index 8dd6f12c..e78cb892 100644 --- a/src/BtCancelMessage.h +++ b/src/BtCancelMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_CANCEL_MESSAGE_H_ #define _D_BT_CANCEL_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "RangeBtMessage.h" namespace aria2 { @@ -43,49 +43,18 @@ class BtCancelMessage; typedef SharedHandle BtCancelMessageHandle; -class BtCancelMessage : public SimpleBtMessage { -private: - size_t index; - uint32_t begin; - size_t length; - unsigned char* msg; - - static const size_t MESSAGE_LENGTH = 17; +class BtCancelMessage : public RangeBtMessage { public: BtCancelMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0) - :SimpleBtMessage(ID), - index(index), - begin(begin), - length(length), - msg(0) {} - - virtual ~BtCancelMessage() { - delete [] msg; - } + :RangeBtMessage(ID, NAME, index, begin, length) {} static const int8_t ID = 8; - size_t getIndex() const { return index; } - - void setIndex(size_t index) { this->index = index; } - - uint32_t getBegin() const { return begin; } - - void setBegin(uint32_t begin) { this->begin = begin; } - - size_t getLength() const { return length; } - - void setLength(size_t length) { this->length = length; } + static const std::string NAME; static BtCancelMessageHandle create(const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtChokeMessage.cc b/src/BtChokeMessage.cc index 6854386b..85cad16e 100644 --- a/src/BtChokeMessage.cc +++ b/src/BtChokeMessage.cc @@ -33,65 +33,36 @@ */ /* copyright --> */ #include "BtChokeMessage.h" -#include "PeerMessageUtil.h" -#include "DlAbortEx.h" -#include "message.h" #include "Peer.h" #include "BtMessageDispatcher.h" #include "BtRequestFactory.h" -#include "StringFormat.h" namespace aria2 { -BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "choke", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "choke", ID).str()); - } - BtChokeMessageHandle chokeMessage(new BtChokeMessage()); - return chokeMessage; +const std::string BtChokeMessage::NAME("choke"); + +SharedHandle BtChokeMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtChokeMessage::doReceivedAction() { +void BtChokeMessage::doReceivedAction() +{ peer->peerChoking(true); dispatcher->doChokedAction(); requestFactory->doChokedAction(); } -bool BtChokeMessage::sendPredicate() const { +bool BtChokeMessage::sendPredicate() const +{ return !peer->amChoking(); } -const unsigned char* BtChokeMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 0, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtChokeMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -void BtChokeMessage::onSendComplete() { +void BtChokeMessage::onSendComplete() +{ peer->amChoking(true); dispatcher->doChokingAction(); } -std::string BtChokeMessage::toString() const { - static const std::string CHOKE("choke"); - return CHOKE; -} - } // namespace aria2 diff --git a/src/BtChokeMessage.h b/src/BtChokeMessage.h index 442097fe..b3445e86 100644 --- a/src/BtChokeMessage.h +++ b/src/BtChokeMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_CHOKE_MESSAGE_H_ #define _D_BT_CHOKE_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -43,29 +43,18 @@ class BtChokeMessage; typedef SharedHandle BtChokeMessageHandle; -class BtChokeMessage : public SimpleBtMessage { -private: - unsigned char* msg; - - static const size_t MESSAGE_LENGTH = 5; +class BtChokeMessage : public ZeroBtMessage { public: - BtChokeMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtChokeMessage() { - delete [] msg; - } + BtChokeMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 0; + static const std::string NAME; + virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - - static BtChokeMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual bool sendPredicate() const; diff --git a/src/BtExtendedMessage.cc b/src/BtExtendedMessage.cc index ec626cf2..0256d9f3 100644 --- a/src/BtExtendedMessage.cc +++ b/src/BtExtendedMessage.cc @@ -47,9 +47,11 @@ namespace aria2 { +const std::string BtExtendedMessage::NAME("extended"); + BtExtendedMessage::BtExtendedMessage (const ExtensionMessageHandle& extensionMessage): - SimpleBtMessage(ID), + SimpleBtMessage(ID, NAME), _extensionMessage(extensionMessage), _msg(0), _msgLength(0) @@ -90,7 +92,7 @@ bool BtExtendedMessage::sendPredicate() const } std::string BtExtendedMessage::toString() const { - return "extended "+_extensionMessage->toString(); + return NAME+" "+_extensionMessage->toString(); } BtExtendedMessageHandle @@ -98,15 +100,8 @@ BtExtendedMessage::create(const SharedHandle& factory, const PeerHandle& peer, const unsigned char* data, size_t dataLength) { - if(dataLength < 2) { - throw DlAbortEx - (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID).str()); - } + PeerMessageUtil::assertPayloadLengthGreater(1, dataLength, NAME); + PeerMessageUtil::assertID(ID, data, NAME); assert(!factory.isNull()); ExtensionMessageHandle extmsg = factory->createMessage(data+1, dataLength-1); diff --git a/src/BtExtendedMessage.h b/src/BtExtendedMessage.h index c231c869..63912f92 100644 --- a/src/BtExtendedMessage.h +++ b/src/BtExtendedMessage.h @@ -60,6 +60,8 @@ public: static const uint8_t ID = 20; + static const std::string NAME; + static BtExtendedMessageHandle create (const SharedHandle& factory, const SharedHandle& peer, diff --git a/src/BtHandshakeMessage.cc b/src/BtHandshakeMessage.cc index 5fd5aff4..ead983b1 100644 --- a/src/BtHandshakeMessage.cc +++ b/src/BtHandshakeMessage.cc @@ -40,16 +40,18 @@ namespace aria2 { +const std::string BtHandshakeMessage::NAME("handshake"); + const unsigned char* BtHandshakeMessage::BT_PSTR = (const unsigned char*)"BitTorrent protocol"; -BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID) +BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID, NAME) { init(); } BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash, const unsigned char* peerId): - SimpleBtMessage(ID) + SimpleBtMessage(ID, NAME) { init(); memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH); @@ -100,7 +102,7 @@ size_t BtHandshakeMessage::getMessageLength() { } std::string BtHandshakeMessage::toString() const { - return "handshake peerId="+ + return NAME+" peerId="+ Util::urlencode(peerId, PEER_ID_LENGTH)+ ", reserved="+Util::toHex(reserved, RESERVED_LENGTH); } diff --git a/src/BtHandshakeMessage.h b/src/BtHandshakeMessage.h index ef36a3bb..c8ac4ca7 100644 --- a/src/BtHandshakeMessage.h +++ b/src/BtHandshakeMessage.h @@ -74,6 +74,8 @@ public: static const uint8_t ID = INT8_MAX; + static const std::string NAME; + virtual void doReceivedAction() {}; virtual const unsigned char* getMessage(); diff --git a/src/BtHaveAllMessage.cc b/src/BtHaveAllMessage.cc index efdc6f9b..10f6cc3d 100644 --- a/src/BtHaveAllMessage.cc +++ b/src/BtHaveAllMessage.cc @@ -34,59 +34,31 @@ /* copyright --> */ #include "BtHaveAllMessage.h" #include "DlAbortEx.h" -#include "PeerMessageUtil.h" -#include "message.h" #include "Peer.h" #include "StringFormat.h" #include "PieceStorage.h" namespace aria2 { -BtHaveAllMessageHandle BtHaveAllMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "have all", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have all", ID).str()); - } - BtHaveAllMessageHandle message(new BtHaveAllMessage()); - return message; +const std::string BtHaveAllMessage::NAME("have all"); + +SharedHandle BtHaveAllMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtHaveAllMessage::doReceivedAction() { +void BtHaveAllMessage::doReceivedAction() +{ if(!peer->isFastExtensionEnabled()) { throw DlAbortEx (StringFormat("%s received while fast extension is disabled", toString().c_str()).str()); } - pieceStorage->subtractPieceStats(peer->getBitfield(), peer->getBitfieldLength()); + pieceStorage->subtractPieceStats(peer->getBitfield(), + peer->getBitfieldLength()); peer->setAllBitfield(); pieceStorage->addPieceStats(peer->getBitfield(), peer->getBitfieldLength()); } -const unsigned char* BtHaveAllMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 14, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtHaveAllMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtHaveAllMessage::toString() const { - static const std::string HAVE_ALL("have all"); - return HAVE_ALL; -} - } // namespace aria2 diff --git a/src/BtHaveAllMessage.h b/src/BtHaveAllMessage.h index 4eb79c1f..537bf2de 100644 --- a/src/BtHaveAllMessage.h +++ b/src/BtHaveAllMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_HAVE_ALL_MESSAGE_H_ #define _D_BT_HAVE_ALL_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -43,29 +43,18 @@ class BtHaveAllMessage; typedef SharedHandle BtHaveAllMessageHandle; -class BtHaveAllMessage : public SimpleBtMessage { -private: - unsigned char* msg; - - static const size_t MESSAGE_LENGTH = 5; +class BtHaveAllMessage : public ZeroBtMessage { public: - BtHaveAllMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtHaveAllMessage() { - delete [] msg; - } + BtHaveAllMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 14; - static BtHaveAllMessageHandle create(const unsigned char* data, size_t dataLength); + static const std::string NAME; + + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtHaveMessage.cc b/src/BtHaveMessage.cc index 1afd48df..6641f6a0 100644 --- a/src/BtHaveMessage.cc +++ b/src/BtHaveMessage.cc @@ -33,57 +33,23 @@ */ /* copyright --> */ #include "BtHaveMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" -#include "DlAbortEx.h" -#include "message.h" #include "Peer.h" -#include "StringFormat.h" #include "PieceStorage.h" namespace aria2 { -BtHaveMessageHandle BtHaveMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 5) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "have", dataLength, 5).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have", ID).str()); - } - BtHaveMessageHandle message(new BtHaveMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - return message; +const std::string BtHaveMessage::NAME("have"); + +SharedHandle BtHaveMessage::create +(const unsigned char* data, size_t dataLength) +{ + return IndexBtMessage::create(data, dataLength); } -void BtHaveMessage::doReceivedAction() { - peer->updateBitfield(index, 1); - pieceStorage->addPieceStats(index); -} - -const unsigned char* BtHaveMessage::getMessage() { - if(!msg) { - /** - * len --- 5, 4bytes - * id --- 4, 1byte - * piece index --- index, 4bytes - * total: 9bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - } - return msg; -} - -size_t BtHaveMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtHaveMessage::toString() const { - return "have index="+Util::itos(index); +void BtHaveMessage::doReceivedAction() +{ + peer->updateBitfield(getIndex(), 1); + pieceStorage->addPieceStats(getIndex()); } } // namespace aria2 diff --git a/src/BtHaveMessage.h b/src/BtHaveMessage.h index 9ff9423f..51087f2b 100644 --- a/src/BtHaveMessage.h +++ b/src/BtHaveMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_HAVE_MESSAGE_H_ #define _D_BT_HAVE_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "IndexBtMessage.h" namespace aria2 { @@ -43,35 +43,18 @@ class BtHaveMessage; typedef SharedHandle BtHaveMessageHandle; -class BtHaveMessage : public SimpleBtMessage { -private: - size_t index; - unsigned char* msg; - static const size_t MESSAGE_LENGTH = 9; +class BtHaveMessage : public IndexBtMessage { public: - BtHaveMessage(size_t index = 0):SimpleBtMessage(ID), index(index), msg(0) {} - - virtual ~BtHaveMessage() { - delete [] msg; - } + BtHaveMessage(size_t index = 0):IndexBtMessage(ID, NAME, index) {} static const uint8_t ID = 4; - void setIndex(size_t index) { - this->index = index; - } + static const std::string NAME; - size_t getIndex() const { return index; } - - static BtHaveMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtHaveNoneMessage.cc b/src/BtHaveNoneMessage.cc index 3981a20a..1ff27e06 100644 --- a/src/BtHaveNoneMessage.cc +++ b/src/BtHaveNoneMessage.cc @@ -34,28 +34,21 @@ /* copyright --> */ #include "BtHaveNoneMessage.h" #include "DlAbortEx.h" -#include "PeerMessageUtil.h" -#include "message.h" #include "Peer.h" #include "StringFormat.h" namespace aria2 { -BtHaveNoneMessageHandle BtHaveNoneMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "have none", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have none", ID).str()); - } - BtHaveNoneMessageHandle message(new BtHaveNoneMessage()); - return message; +const std::string BtHaveNoneMessage::NAME("have none"); + +SharedHandle BtHaveNoneMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtHaveNoneMessage::doReceivedAction() { +void BtHaveNoneMessage::doReceivedAction() +{ if(!peer->isFastExtensionEnabled()) { throw DlAbortEx (StringFormat("%s received while fast extension is disabled", @@ -63,26 +56,4 @@ void BtHaveNoneMessage::doReceivedAction() { } } -const unsigned char* BtHaveNoneMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 15, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtHaveNoneMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtHaveNoneMessage::toString() const { - static const std::string HAVE_NONE("have none"); - return HAVE_NONE; -} - } // namespace aria2 diff --git a/src/BtHaveNoneMessage.h b/src/BtHaveNoneMessage.h index 4aaee64c..90a4619d 100644 --- a/src/BtHaveNoneMessage.h +++ b/src/BtHaveNoneMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_HAVE_NONE_MESSAGE_H_ #define _D_BT_HAVE_NONE_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -43,29 +43,17 @@ class BtHaveNoneMessage; typedef SharedHandle BtHaveNoneMessageHandle; -class BtHaveNoneMessage : public SimpleBtMessage { -private: - unsigned char* msg; - - static const size_t MESSAGE_LENGTH = 5; +class BtHaveNoneMessage : public ZeroBtMessage { public: - BtHaveNoneMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtHaveNoneMessage() { - delete [] msg; - } - + BtHaveNoneMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 15; - static BtHaveNoneMessageHandle create(const unsigned char* data, size_t dataLength); + static const std::string NAME; + + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtInterestedMessage.cc b/src/BtInterestedMessage.cc index 18ae56c2..38b23106 100644 --- a/src/BtInterestedMessage.cc +++ b/src/BtInterestedMessage.cc @@ -33,67 +33,36 @@ */ /* copyright --> */ #include "BtInterestedMessage.h" -#include "PeerMessageUtil.h" -#include "DlAbortEx.h" -#include "message.h" #include "Peer.h" -#include "BtContext.h" -#include "StringFormat.h" #include "PeerStorage.h" namespace aria2 { -BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "interested", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "interested", ID).str()); - } - BtInterestedMessageHandle message(new BtInterestedMessage()); - return message; +const std::string BtInterestedMessage::NAME("interested"); + +SharedHandle BtInterestedMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtInterestedMessage::doReceivedAction() { +void BtInterestedMessage::doReceivedAction() +{ peer->peerInterested(true); if(!peer->amChoking()) { _peerStorage->executeChoke(); } } -bool BtInterestedMessage::sendPredicate() const { +bool BtInterestedMessage::sendPredicate() const +{ return !peer->amInterested(); } -const unsigned char* BtInterestedMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 2, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtInterestedMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - void BtInterestedMessage::onSendComplete() { peer->amInterested(true); } -std::string BtInterestedMessage::toString() const { - static const std::string INTERESTED("interested"); - return INTERESTED; -} - void BtInterestedMessage::setPeerStorage (const SharedHandle& peerStorage) { diff --git a/src/BtInterestedMessage.h b/src/BtInterestedMessage.h index 0a3fe8ec..72bd2bf6 100644 --- a/src/BtInterestedMessage.h +++ b/src/BtInterestedMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_INTERESTED_MESSAGE_H_ #define _D_BT_INTERESTED_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -44,32 +44,21 @@ class BtInterestedMessage; typedef SharedHandle BtInterestedMessageHandle; -class BtInterestedMessage : public SimpleBtMessage { +class BtInterestedMessage : public ZeroBtMessage { private: - unsigned char* msg; - SharedHandle _peerStorage; - - static const size_t MESSAGE_LENGTH = 5; public: - BtInterestedMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtInterestedMessage() { - delete [] msg; - } + BtInterestedMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 2; - static BtInterestedMessageHandle create(const unsigned char* data, size_t dataLength); + static const std::string NAME; + + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - virtual bool sendPredicate() const; virtual void onSendComplete(); diff --git a/src/BtKeepAliveMessage.cc b/src/BtKeepAliveMessage.cc index 6998ba49..b59b9ebb 100644 --- a/src/BtKeepAliveMessage.cc +++ b/src/BtKeepAliveMessage.cc @@ -37,6 +37,8 @@ namespace aria2 { +const std::string BtKeepAliveMessage::NAME("keep alive"); + const unsigned char* BtKeepAliveMessage::getMessage() { if(!msg) { /** diff --git a/src/BtKeepAliveMessage.h b/src/BtKeepAliveMessage.h index f100c7cb..bec16187 100644 --- a/src/BtKeepAliveMessage.h +++ b/src/BtKeepAliveMessage.h @@ -49,7 +49,7 @@ private: static const size_t MESSAGE_LENGTH = 4; public: - BtKeepAliveMessage():SimpleBtMessage(ID), msg(0) {} + BtKeepAliveMessage():SimpleBtMessage(ID, NAME), msg(0) {} virtual ~BtKeepAliveMessage() { delete [] msg; @@ -57,6 +57,8 @@ public: static const uint8_t ID = 99; + static const std::string NAME; + virtual void doReceivedAction() {} virtual const unsigned char* getMessage(); @@ -64,7 +66,7 @@ public: virtual size_t getMessageLength(); virtual std::string toString() const { - return "keep alive"; + return NAME; } }; diff --git a/src/BtNotInterestedMessage.cc b/src/BtNotInterestedMessage.cc index 37829be8..a4adc7df 100644 --- a/src/BtNotInterestedMessage.cc +++ b/src/BtNotInterestedMessage.cc @@ -33,67 +33,36 @@ */ /* copyright --> */ #include "BtNotInterestedMessage.h" -#include "PeerMessageUtil.h" -#include "DlAbortEx.h" -#include "message.h" #include "Peer.h" -#include "BtContext.h" #include "PeerStorage.h" -#include "StringFormat.h" namespace aria2 { -BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "not interested", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "not interested", ID).str()); - } - BtNotInterestedMessageHandle message(new BtNotInterestedMessage()); - return message; +const std::string BtNotInterestedMessage::NAME("not interested"); + +SharedHandle BtNotInterestedMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtNotInterestedMessage::doReceivedAction() { +void BtNotInterestedMessage::doReceivedAction() +{ peer->peerInterested(false); if(!peer->amChoking()) { _peerStorage->executeChoke(); } } -bool BtNotInterestedMessage::sendPredicate() const { +bool BtNotInterestedMessage::sendPredicate() const +{ return peer->amInterested(); } -const unsigned char* BtNotInterestedMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 3, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtNotInterestedMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - void BtNotInterestedMessage::onSendComplete() { peer->amInterested(false); } -std::string BtNotInterestedMessage::toString() const { - static const std::string NOT_INTERESTED("not interested"); - return NOT_INTERESTED; -} - void BtNotInterestedMessage::setPeerStorage (const SharedHandle& peerStorage) { diff --git a/src/BtNotInterestedMessage.h b/src/BtNotInterestedMessage.h index 329dda1a..630b9fac 100644 --- a/src/BtNotInterestedMessage.h +++ b/src/BtNotInterestedMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_NOT_INTERESTED_MESSAGE_H_ #define _D_BT_NOT_INTERESTED_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -44,32 +44,21 @@ class BtNotInterestedMessage; typedef SharedHandle BtNotInterestedMessageHandle; -class BtNotInterestedMessage : public SimpleBtMessage { +class BtNotInterestedMessage : public ZeroBtMessage { private: - unsigned char* msg; - SharedHandle _peerStorage; - - static const size_t MESSAGE_LENGTH = 5; public: - BtNotInterestedMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtNotInterestedMessage() { - delete [] msg; - } + BtNotInterestedMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 3; - static BtNotInterestedMessageHandle create(const unsigned char* data, size_t dataLength); + static const std::string NAME; + + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - virtual bool sendPredicate() const; virtual void onSendComplete(); diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index bba0ca74..f20eb1e6 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -33,6 +33,11 @@ */ /* copyright --> */ #include "BtPieceMessage.h" + +#include +#include +#include + #include "PeerMessageUtil.h" #include "Util.h" #include "message.h" @@ -51,12 +56,11 @@ #include "BtRequestFactory.h" #include "PeerConnection.h" #include "StringFormat.h" -#include -#include -#include namespace aria2 { +const std::string BtPieceMessage::NAME("piece"); + void BtPieceMessage::setBlock(const unsigned char* block, size_t blockLength) { delete [] this->block; this->blockLength = blockLength; @@ -65,15 +69,8 @@ void BtPieceMessage::setBlock(const unsigned char* block, size_t blockLength) { } BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength <= 9) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "piece", dataLength, 9).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID).str()); - } + PeerMessageUtil::assertPayloadLengthGreater(9, dataLength, NAME); + PeerMessageUtil::assertID(ID, data, NAME); BtPieceMessageHandle message(new BtPieceMessage()); message->setIndex(PeerMessageUtil::getIntParam(data, 1)); message->setBegin(PeerMessageUtil::getIntParam(data, 5)); @@ -173,7 +170,7 @@ size_t BtPieceMessage::sendPieceData(off_t offset, size_t length) const { } std::string BtPieceMessage::toString() const { - return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+ + return NAME+" index="+Util::itos(index)+", begin="+Util::itos(begin)+ ", length="+Util::itos(blockLength); } diff --git a/src/BtPieceMessage.h b/src/BtPieceMessage.h index 3b4a83b5..a2435709 100644 --- a/src/BtPieceMessage.h +++ b/src/BtPieceMessage.h @@ -93,7 +93,7 @@ private: typedef SharedHandle BtCancelSendingPieceEventListenerHandle; public: BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0) - :AbstractBtMessage(ID), + :AbstractBtMessage(ID, NAME), index(index), begin(begin), blockLength(blockLength), @@ -118,6 +118,8 @@ public: static const uint8_t ID = 7; + static const std::string NAME; + size_t getIndex() const { return index; } void setIndex(size_t index) { this->index = index; } diff --git a/src/BtPortMessage.cc b/src/BtPortMessage.cc index f4d3c6b3..4367ee6a 100644 --- a/src/BtPortMessage.cc +++ b/src/BtPortMessage.cc @@ -48,8 +48,10 @@ namespace aria2 { +const std::string BtPortMessage::NAME("port"); + BtPortMessage::BtPortMessage(uint16_t port): - SimpleBtMessage(ID), _port(port), _msg(0) {} + SimpleBtMessage(ID, NAME), _port(port), _msg(0) {} BtPortMessage::~BtPortMessage() { @@ -58,15 +60,8 @@ BtPortMessage::~BtPortMessage() SharedHandle BtPortMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 3) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "port", dataLength, 3).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID).str()); - } + PeerMessageUtil::assertPayloadLengthEqual(3, dataLength, NAME); + PeerMessageUtil::assertID(ID, data, NAME); uint16_t port = PeerMessageUtil::getShortIntParam(data, 1); SharedHandle message(new BtPortMessage(port)); return message; @@ -114,7 +109,7 @@ size_t BtPortMessage::getMessageLength() { } std::string BtPortMessage::toString() const { - return "port port="+Util::uitos(_port); + return NAME+" port="+Util::uitos(_port); } void BtPortMessage::setLocalNode(const WeakHandle& localNode) diff --git a/src/BtPortMessage.h b/src/BtPortMessage.h index 47fabaeb..45c8e22d 100644 --- a/src/BtPortMessage.h +++ b/src/BtPortMessage.h @@ -64,6 +64,8 @@ public: static const uint8_t ID = 9; + static const std::string NAME; + uint16_t getPort() const { return _port; } static SharedHandle create(const unsigned char* data, size_t dataLength); diff --git a/src/BtRejectMessage.cc b/src/BtRejectMessage.cc index 51f796a9..fce49979 100644 --- a/src/BtRejectMessage.cc +++ b/src/BtRejectMessage.cc @@ -33,10 +33,7 @@ */ /* copyright --> */ #include "BtRejectMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" #include "DlAbortEx.h" -#include "message.h" #include "Peer.h" #include "RequestSlot.h" #include "BtMessageDispatcher.h" @@ -44,24 +41,16 @@ namespace aria2 { -BtRejectMessageHandle BtRejectMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 13) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "reject", dataLength, 13).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "reject", ID).str()); - } - BtRejectMessageHandle message(new BtRejectMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - message->setBegin(PeerMessageUtil::getIntParam(data, 5)); - message->setLength(PeerMessageUtil::getIntParam(data, 9)); - return message; +const std::string BtRejectMessage::NAME("reject"); + +SharedHandle BtRejectMessage::create +(const unsigned char* data, size_t dataLength) +{ + return RangeBtMessage::create(data, dataLength); } -void BtRejectMessage::doReceivedAction() { +void BtRejectMessage::doReceivedAction() +{ if(!peer->isFastExtensionEnabled()) { throw DlAbortEx (StringFormat("%s received while fast extension is disabled.", @@ -69,7 +58,8 @@ void BtRejectMessage::doReceivedAction() { } // TODO Current implementation does not close a connection even if // a request for this reject message has never sent. - RequestSlot slot = dispatcher->getOutstandingRequest(index, begin, length); + RequestSlot slot = + dispatcher->getOutstandingRequest(getIndex(), getBegin(), getLength()); if(RequestSlot::isNull(slot)) { //throw DlAbortEx("reject recieved, but it is not in the request slots."); } else { @@ -78,32 +68,4 @@ void BtRejectMessage::doReceivedAction() { } -const unsigned char* BtRejectMessage::getMessage() { - if(!msg) { - /** - * len --- 13, 4bytes - * id --- 16, 1byte - * index --- index, 4bytes - * begin --- begin, 4bytes - * length -- length, 4bytes - * total: 17bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - PeerMessageUtil::setIntParam(&msg[9], begin); - PeerMessageUtil::setIntParam(&msg[13], length); - } - return msg; -} - -size_t BtRejectMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtRejectMessage::toString() const { - return "reject index="+Util::uitos(index)+", begin="+Util::uitos(begin)+ - ", length="+Util::uitos(length); -} - } // namespace aria2 diff --git a/src/BtRejectMessage.h b/src/BtRejectMessage.h index 50b98ed0..222a18d1 100644 --- a/src/BtRejectMessage.h +++ b/src/BtRejectMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_REJECT_MESSAGE_H_ #define _D_BT_REJECT_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "RangeBtMessage.h" namespace aria2 { @@ -43,45 +43,19 @@ class BtRejectMessage; typedef SharedHandle BtRejectMessageHandle; -class BtRejectMessage : public SimpleBtMessage { -private: - size_t index; - uint32_t begin; - size_t length; - unsigned char* msg; - static const size_t MESSAGE_LENGTH = 17; +class BtRejectMessage : public RangeBtMessage { public: BtRejectMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0) - :SimpleBtMessage(ID), - index(index), - begin(begin), - length(length), - msg(0) {} - - virtual ~BtRejectMessage() { - delete [] msg; - } + :RangeBtMessage(ID, NAME, index, begin, length) {} static const uint8_t ID = 16; - size_t getIndex() const { return index; } - void setIndex(size_t index) { this->index = index; } + static const std::string NAME; - uint32_t getBegin() const { return begin; } - void setBegin(uint32_t begin) { this->begin = begin; } - - size_t getLength() const { return length; } - void setLength(size_t length) { this->length = length; } - - static BtRejectMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtRequestMessage.cc b/src/BtRequestMessage.cc index c6b188db..44b504a5 100644 --- a/src/BtRequestMessage.cc +++ b/src/BtRequestMessage.cc @@ -33,101 +33,70 @@ */ /* copyright --> */ #include "BtRequestMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" -#include "DlAbortEx.h" #include "BtAbortOutstandingRequestEvent.h" -#include "message.h" #include "Peer.h" #include "Piece.h" -#include "BtContext.h" #include "PieceStorage.h" #include "BtMessageDispatcher.h" #include "BtMessageFactory.h" -#include "StringFormat.h" namespace aria2 { -BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 13) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "request", dataLength, 13).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "request", ID).str()); - } - BtRequestMessageHandle message(new BtRequestMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - message->setBegin(PeerMessageUtil::getIntParam(data, 5)); - message->setLength(PeerMessageUtil::getIntParam(data, 9)); - return message; +const std::string BtRequestMessage::NAME("request"); + +SharedHandle BtRequestMessage::create +(const unsigned char* data, size_t dataLength) +{ + return RangeBtMessage::create(data, dataLength); } -void BtRequestMessage::doReceivedAction() { - if(pieceStorage->hasPiece(index) && +void BtRequestMessage::doReceivedAction() +{ + if(pieceStorage->hasPiece(getIndex()) && (!peer->amChoking() || - (peer->amChoking() && peer->isInAmAllowedIndexSet(index)))) { - BtMessageHandle msg = messageFactory->createPieceMessage(index, - begin, - length); + (peer->amChoking() && peer->isInAmAllowedIndexSet(getIndex())))) { + BtMessageHandle msg = messageFactory->createPieceMessage(getIndex(), + getBegin(), + getLength()); dispatcher->addMessageToQueue(msg); } else { if(peer->isFastExtensionEnabled()) { - BtMessageHandle msg = messageFactory->createRejectMessage(index, - begin, - length); + BtMessageHandle msg = messageFactory->createRejectMessage(getIndex(), + getBegin(), + getLength()); dispatcher->addMessageToQueue(msg); } } } -const unsigned char* BtRequestMessage::getMessage() { - if(!msg) { - /** - * len --- 13, 4bytes - * id --- 6, 1byte - * index --- index, 4bytes - * begin --- begin, 4bytes - * length --- length, 4bytes - * total: 17bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - PeerMessageUtil::setIntParam(&msg[9], begin); - PeerMessageUtil::setIntParam(&msg[13], length); - } - return msg; -} - -size_t BtRequestMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtRequestMessage::toString() const { - return "request index="+Util::uitos(index)+", begin="+Util::uitos(begin)+ - ", length="+Util::uitos(length); -} - -void BtRequestMessage::onQueued() { - RequestSlot requestSlot(index, begin, length, blockIndex, pieceStorage->getPiece(index)); +void BtRequestMessage::onQueued() +{ + RequestSlot requestSlot(getIndex(), getBegin(), getLength(), _blockIndex, + pieceStorage->getPiece(getIndex())); dispatcher->addOutstandingRequest(requestSlot); } -bool BtRequestMessage::BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) { - BtAbortOutstandingRequestEvent* intEvent = dynamic_cast(event.get()); +bool BtRequestMessage:: +BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) +{ + BtAbortOutstandingRequestEvent* intEvent = + dynamic_cast(event.get()); return intEvent != 0; } -void BtRequestMessage::BtAbortOutstandingRequestEventListener::handleEventInternal(const BtEventHandle& event) { +void BtRequestMessage:: +BtAbortOutstandingRequestEventListener::handleEventInternal +(const BtEventHandle& event) +{ message->handleAbortOutstandingRequestEvent(event); } -void BtRequestMessage::handleAbortOutstandingRequestEvent(const BtEventHandle& event) { - BtAbortOutstandingRequestEvent* intEvent = (BtAbortOutstandingRequestEvent*)event.get(); - if(index == intEvent->getPiece()->getIndex() && +void BtRequestMessage:: +handleAbortOutstandingRequestEvent(const BtEventHandle& event) +{ + BtAbortOutstandingRequestEvent* intEvent = + (BtAbortOutstandingRequestEvent*)event.get(); + if(getIndex() == intEvent->getPiece()->getIndex() && !invalidate && !sendingInProgress) { invalidate = true; diff --git a/src/BtRequestMessage.h b/src/BtRequestMessage.h index 97d66ca3..e7a8d10a 100644 --- a/src/BtRequestMessage.h +++ b/src/BtRequestMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_REQUEST_MESSAGE_H_ #define _D_BT_REQUEST_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "RangeBtMessage.h" #include "AbstractBtEventListener.h" namespace aria2 { @@ -44,75 +44,53 @@ class BtRequestMessage; typedef SharedHandle BtRequestMessageHandle; -class BtRequestMessage : public SimpleBtMessage { +class BtRequestMessage : public RangeBtMessage { private: - size_t index; - uint32_t begin; - uint32_t length; - size_t blockIndex; - unsigned char* msg; + size_t _blockIndex; - static const size_t MESSAGE_LENGTH = 17; - - class BtAbortOutstandingRequestEventListener : public AbstractBtEventListener { + class BtAbortOutstandingRequestEventListener:public AbstractBtEventListener { private: BtRequestMessage* message; public: - BtAbortOutstandingRequestEventListener(BtRequestMessage* message):message(message) {} + BtAbortOutstandingRequestEventListener(BtRequestMessage* message): + message(message) {} virtual bool canHandle(const SharedHandle& event); virtual void handleEventInternal(const SharedHandle& event); }; - typedef SharedHandle BtAbortOutstandingRequestEventListenerHandle; + typedef SharedHandle + BtAbortOutstandingRequestEventListenerHandle; public: BtRequestMessage(size_t index = 0, uint32_t begin = 0, uint32_t length = 0, size_t blockIndex = 0) - :SimpleBtMessage(ID), - index(index), - begin(begin), - length(length), - blockIndex(blockIndex), - msg(0) + :RangeBtMessage(ID, NAME, index, begin, length), + _blockIndex(blockIndex) { - SharedHandle listener(new BtAbortOutstandingRequestEventListener(this)); + SharedHandle listener + (new BtAbortOutstandingRequestEventListener(this)); addEventListener(listener); } - virtual ~BtRequestMessage() { - delete [] msg; - } - static const uint8_t ID = 6; - size_t getIndex() const { return index; } - void setIndex(size_t index) { this->index = index; } + static const std::string NAME; - uint32_t getBegin() const { return begin; } - void setBegin(uint32_t begin) { this->begin = begin; } + size_t getBlockIndex() const { return _blockIndex; } + void setBlockIndex(size_t blockIndex) { _blockIndex = blockIndex; } - uint32_t getLength() const { return length; } - void setLength(uint32_t length) { this->length = length; } - - size_t getBlockIndex() const { return blockIndex; } - void setBlockIndex(size_t blockIndex) { this->blockIndex = blockIndex; } - - static BtRequestMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - virtual void onQueued(); - virtual void handleAbortOutstandingRequestEvent(const SharedHandle& event); + virtual void handleAbortOutstandingRequestEvent + (const SharedHandle& event); }; } // namespace aria2 diff --git a/src/BtRequestMessageValidator.h b/src/BtRequestMessageValidator.h deleted file mode 100644 index f01b037a..00000000 --- a/src/BtRequestMessageValidator.h +++ /dev/null @@ -1,73 +0,0 @@ -/* */ -#ifndef _D_BT_REQUEST_MESSAGE_VALIDATOR_H_ -#define _D_BT_REQUEST_MESSAGE_VALIDATOR_H_ - -#include "BtMessageValidator.h" -#include "BtRequestMessage.h" -#include "PeerMessageUtil.h" - -namespace aria2 { - -class BtRequestMessageValidator : public BtMessageValidator { -private: - const BtRequestMessage* message; - size_t numPiece; - size_t pieceLength; -public: - BtRequestMessageValidator(const BtRequestMessage* message, - size_t numPiece, - size_t pieceLength): - message(message), - numPiece(numPiece), - pieceLength(pieceLength) {} - - virtual bool validate(Errors& error) { - // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); - PeerMessageUtil::checkBegin(message->getBegin(), pieceLength); - PeerMessageUtil::checkLength(message->getLength()); - PeerMessageUtil::checkRange(message->getBegin(), - message->getLength(), - pieceLength); - return true; - } -}; - -typedef SharedHandle BtRequestMessageValidatorHandle; - -} // namespace aria2 - -#endif // _D_BT_REQUEST_MESSAGE_VALIDATOR_H_ diff --git a/src/BtSuggestPieceMessage.cc b/src/BtSuggestPieceMessage.cc index e946504d..4e9cedb8 100644 --- a/src/BtSuggestPieceMessage.cc +++ b/src/BtSuggestPieceMessage.cc @@ -33,50 +33,15 @@ */ /* copyright --> */ #include "BtSuggestPieceMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" -#include "DlAbortEx.h" -#include "message.h" -#include "StringFormat.h" namespace aria2 { -BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 5) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "suggest piece", dataLength, 5).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "suggest piece", ID).str()); - } - BtSuggestPieceMessageHandle message(new BtSuggestPieceMessage()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); - return message; -} +const std::string BtSuggestPieceMessage::NAME("suggest piece"); -const unsigned char* BtSuggestPieceMessage::getMessage() { - if(!msg) { - /** - * len --- 5, 4bytes - * id --- 13, 1byte - * piece index --- index, 4bytes - * total: 9bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID); - PeerMessageUtil::setIntParam(&msg[5], index); - } - return msg; -} - -size_t BtSuggestPieceMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - -std::string BtSuggestPieceMessage::toString() const { - return "suggest piece index="+Util::itos(index); +SharedHandle BtSuggestPieceMessage::create +(const unsigned char* data, size_t dataLength) +{ + return IndexBtMessage::create(data, dataLength); } } // namespace aria2 diff --git a/src/BtSuggestPieceMessage.h b/src/BtSuggestPieceMessage.h index b12662ca..820be287 100644 --- a/src/BtSuggestPieceMessage.h +++ b/src/BtSuggestPieceMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_SUGGEST_PIECE_MESSAGE_H_ #define _D_BT_SUGGEST_PIECE_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "IndexBtMessage.h" namespace aria2 { @@ -43,37 +43,20 @@ class BtSuggestPieceMessage; typedef SharedHandle BtSuggestPieceMessageHandle; -class BtSuggestPieceMessage : public SimpleBtMessage { -private: - size_t index; - unsigned char* msg; - static const size_t MESSAGE_LENGTH = 9; +class BtSuggestPieceMessage : public IndexBtMessage { public: - BtSuggestPieceMessage():SimpleBtMessage(ID), index(0), msg(0) {} - - virtual ~BtSuggestPieceMessage() { - delete [] msg; - } + BtSuggestPieceMessage():IndexBtMessage(ID, NAME, 0) {} static const uint8_t ID = 13; - void setIndex(size_t index) { - this->index = index; - } + static const std::string NAME; - size_t getIndex() const { return index; } - - static BtSuggestPieceMessageHandle create(const unsigned char* data, size_t dataLength); + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction() { // TODO Current implementation ignores this message. } - - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; }; } // namespace aria2 diff --git a/src/BtUnchokeMessage.cc b/src/BtUnchokeMessage.cc index 92b73db2..65b419a0 100644 --- a/src/BtUnchokeMessage.cc +++ b/src/BtUnchokeMessage.cc @@ -33,60 +33,30 @@ */ /* copyright --> */ #include "BtUnchokeMessage.h" -#include "PeerMessageUtil.h" -#include "DlAbortEx.h" -#include "message.h" #include "Peer.h" -#include "StringFormat.h" namespace aria2 { -BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, size_t dataLength) { - if(dataLength != 1) { - throw DlAbortEx - (StringFormat(EX_INVALID_PAYLOAD_SIZE, "unchoke", dataLength, 1).str()); - } - uint8_t id = PeerMessageUtil::getId(data); - if(id != ID) { - throw DlAbortEx - (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "unchoke", ID).str()); - } - BtUnchokeMessageHandle message(new BtUnchokeMessage()); - return message; +const std::string BtUnchokeMessage::NAME("unchoke"); + +SharedHandle BtUnchokeMessage::create +(const unsigned char* data, size_t dataLength) +{ + return ZeroBtMessage::create(data, dataLength); } -void BtUnchokeMessage::doReceivedAction() { +void BtUnchokeMessage::doReceivedAction() +{ peer->peerChoking(false); } -bool BtUnchokeMessage::sendPredicate() const { +bool BtUnchokeMessage::sendPredicate() const +{ return peer->amChoking(); } -const unsigned char* BtUnchokeMessage::getMessage() { - if(!msg) { - /** - * len --- 1, 4bytes - * id --- 1, 1byte - * total: 5bytes - */ - msg = new unsigned char[MESSAGE_LENGTH]; - PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID); - } - return msg; -} - -size_t BtUnchokeMessage::getMessageLength() { - return MESSAGE_LENGTH; -} - void BtUnchokeMessage::onSendComplete() { peer->amChoking(false); } -std::string BtUnchokeMessage::toString() const { - static const std::string UNCHOKE("unchoke"); - return UNCHOKE; -} - } // namespace aria2 diff --git a/src/BtUnchokeMessage.h b/src/BtUnchokeMessage.h index d318ba15..0691017f 100644 --- a/src/BtUnchokeMessage.h +++ b/src/BtUnchokeMessage.h @@ -35,7 +35,7 @@ #ifndef _D_BT_UNCHOKE_MESSAGE_H_ #define _D_BT_UNCHOKE_MESSAGE_H_ -#include "SimpleBtMessage.h" +#include "ZeroBtMessage.h" namespace aria2 { @@ -43,29 +43,22 @@ class BtUnchokeMessage; typedef SharedHandle BtUnchokeMessageHandle; -class BtUnchokeMessage : public SimpleBtMessage { +class BtUnchokeMessage : public ZeroBtMessage { private: unsigned char* msg; static const size_t MESSAGE_LENGTH = 5; public: - BtUnchokeMessage():SimpleBtMessage(ID), msg(0) {} - - virtual ~BtUnchokeMessage() { - delete [] msg; - } + BtUnchokeMessage():ZeroBtMessage(ID, NAME) {} static const uint8_t ID = 1; - static BtUnchokeMessageHandle create(const unsigned char* data, size_t dataLength); + static const std::string NAME; + + static SharedHandle create + (const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); - virtual const unsigned char* getMessage(); - - virtual size_t getMessageLength(); - - virtual std::string toString() const; - virtual bool sendPredicate() const; virtual void onSendComplete(); diff --git a/src/DefaultBtMessageFactory.cc b/src/DefaultBtMessageFactory.cc index e480ecad..55a4a97c 100644 --- a/src/DefaultBtMessageFactory.cc +++ b/src/DefaultBtMessageFactory.cc @@ -41,24 +41,20 @@ #include "BtInterestedMessage.h" #include "BtNotInterestedMessage.h" #include "BtHaveMessage.h" -#include "BtHaveMessageValidator.h" #include "BtBitfieldMessage.h" #include "BtBitfieldMessageValidator.h" +#include "RangeBtMessageValidator.h" +#include "IndexBtMessageValidator.h" #include "BtRequestMessage.h" -#include "BtRequestMessageValidator.h" #include "BtCancelMessage.h" -#include "BtCancelMessageValidator.h" #include "BtPieceMessage.h" #include "BtPieceMessageValidator.h" #include "BtPortMessage.h" #include "BtHaveAllMessage.h" #include "BtHaveNoneMessage.h" #include "BtRejectMessage.h" -#include "BtRejectMessageValidator.h" #include "BtSuggestPieceMessage.h" -#include "BtSuggestPieceMessageValidator.h" #include "BtAllowedFastMessage.h" -#include "BtAllowedFastMessageValidator.h" #include "BtHandshakeMessage.h" #include "BtHandshakeMessageValidator.h" #include "BtExtendedMessage.h" @@ -115,8 +111,8 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL msg = BtHaveMessage::create(data, dataLength); { SharedHandle v - (new BtHaveMessageValidator((BtHaveMessage*)msg.get(), - btContext->getNumPieces())); + (new IndexBtMessageValidator((BtHaveMessage*)msg.get(), + btContext->getNumPieces())); msg->setBtMessageValidator(v); } break; @@ -131,20 +127,22 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL break; case BtRequestMessage::ID: { BtRequestMessageHandle temp = BtRequestMessage::create(data, dataLength); - BtMessageValidatorHandle validator - (new BtRequestMessageValidator(temp.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(temp->getIndex()))); + SharedHandle validator + (new RangeBtMessageValidator + (temp.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtCancelMessage::ID: { BtCancelMessageHandle temp = BtCancelMessage::create(data, dataLength); - BtMessageValidatorHandle validator - (new BtCancelMessageValidator(temp.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(temp->getIndex()))); + SharedHandle validator + (new RangeBtMessageValidator + (temp.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; @@ -167,28 +165,27 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL break; case BtRejectMessage::ID: { BtRejectMessageHandle temp = BtRejectMessage::create(data, dataLength); - BtMessageValidatorHandle validator - (new BtRejectMessageValidator(temp.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(temp->getIndex()))); + SharedHandle validator + (new RangeBtMessageValidator + (temp.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtSuggestPieceMessage::ID: { BtSuggestPieceMessageHandle temp = BtSuggestPieceMessage::create(data, dataLength); - BtMessageValidatorHandle validator - (new BtSuggestPieceMessageValidator(temp.get(), - btContext->getNumPieces())); + SharedHandle validator + (new IndexBtMessageValidator(temp.get(), btContext->getNumPieces())); temp->setBtMessageValidator(validator); msg = temp; break; } case BtAllowedFastMessage::ID: { BtAllowedFastMessageHandle temp = BtAllowedFastMessage::create(data, dataLength); - BtMessageValidatorHandle validator - (new BtAllowedFastMessageValidator(temp.get(), - btContext->getNumPieces())); + SharedHandle validator + (new IndexBtMessageValidator(temp.get(), btContext->getNumPieces())); temp->setBtMessageValidator(validator); msg = temp; break; @@ -264,10 +261,11 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, size_t b blockIndex*piece->getBlockLength(), piece->getBlockLength(blockIndex), blockIndex)); - BtMessageValidatorHandle validator - (new BtRequestMessageValidator(msg.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(msg->getIndex()))); + SharedHandle validator + (new RangeBtMessageValidator + (msg.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(msg->getIndex()))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -277,10 +275,11 @@ BtMessageHandle DefaultBtMessageFactory::createCancelMessage(size_t index, uint32_t begin, size_t length) { BtCancelMessageHandle msg(new BtCancelMessage(index, begin, length)); - BtMessageValidatorHandle validator - (new BtCancelMessageValidator(msg.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(index))); + SharedHandle validator + (new RangeBtMessageValidator + (msg.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -304,8 +303,7 @@ DefaultBtMessageFactory::createHaveMessage(size_t index) { BtHaveMessageHandle msg(new BtHaveMessage(index)); SharedHandle v - (new BtHaveMessageValidator(msg.get(), - btContext->getNumPieces())); + (new IndexBtMessageValidator(msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(v); setCommonProperty(msg); return msg; @@ -385,10 +383,11 @@ BtMessageHandle DefaultBtMessageFactory::createRejectMessage(size_t index, uint32_t begin, size_t length) { BtRejectMessageHandle msg(new BtRejectMessage(index, begin, length)); - BtMessageValidatorHandle validator - (new BtRejectMessageValidator(msg.get(), - btContext->getNumPieces(), - _pieceStorage->getPieceLength(index))); + SharedHandle validator + (new RangeBtMessageValidator + (msg.get(), + btContext->getNumPieces(), + _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -398,9 +397,8 @@ BtMessageHandle DefaultBtMessageFactory::createAllowedFastMessage(size_t index) { BtAllowedFastMessageHandle msg(new BtAllowedFastMessage(index)); - BtMessageValidatorHandle validator - (new BtAllowedFastMessageValidator(msg.get(), - btContext->getNumPieces())); + SharedHandle validator + (new IndexBtMessageValidator(msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; diff --git a/src/BtAllowedFastMessageValidator.h b/src/IndexBtMessage.cc similarity index 67% rename from src/BtAllowedFastMessageValidator.h rename to src/IndexBtMessage.cc index a298ae1f..3c4fce4e 100644 --- a/src/BtAllowedFastMessageValidator.h +++ b/src/IndexBtMessage.cc @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2009 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 @@ -32,34 +32,35 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_ -#define _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_ - -#include "BtMessageValidator.h" -#include "BtAllowedFastMessage.h" -#include "PeerMessageUtil.h" +#include "IndexBtMessage.h" +#include "Util.h" namespace aria2 { -class BtAllowedFastMessageValidator : public BtMessageValidator { -private: - const BtAllowedFastMessage* message; - size_t numPiece; -public: - BtAllowedFastMessageValidator(const BtAllowedFastMessage* message, - size_t numPiece): - message(message), - numPiece(numPiece) {} - - virtual bool validate(Errors& error) { - // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); - return true; +const unsigned char* IndexBtMessage::getMessage() +{ + if(!_msg) { + /** + * len --- 5, 4bytes + * id --- ?, 1byte + * piece index --- index, 4bytes + * total: 9bytes + */ + _msg = new unsigned char[MESSAGE_LENGTH]; + PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 5, getId()); + PeerMessageUtil::setIntParam(&_msg[5], _index); } -}; + return _msg; +} -typedef SharedHandle BtAllowedFastMessageValidatorHandle; +size_t IndexBtMessage::getMessageLength() +{ + return MESSAGE_LENGTH; +} + +std::string IndexBtMessage::toString() const +{ + return getName()+" index="+Util::itos(_index); +} } // namespace aria2 - -#endif // _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_ diff --git a/src/BtRejectMessageValidator.h b/src/IndexBtMessage.h similarity index 60% rename from src/BtRejectMessageValidator.h rename to src/IndexBtMessage.h index a0d5071c..3230baf2 100644 --- a/src/BtRejectMessageValidator.h +++ b/src/IndexBtMessage.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2009 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 @@ -32,42 +32,52 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_BT_REJECT_MESSAGE_VALIDATOR_H_ -#define _D_BT_REJECT_MESSAGE_VALIDATOR_H_ +#ifndef _D_INDEX_BT_MESSAGE_H_ +#define _D_INDEX_BT_MESSAGE_H_ -#include "BtMessageValidator.h" -#include "BtRejectMessage.h" +#include "SimpleBtMessage.h" #include "PeerMessageUtil.h" namespace aria2 { -class BtRejectMessageValidator : public BtMessageValidator { +class IndexBtMessage : public SimpleBtMessage { private: - const BtRejectMessage* message; - size_t numPiece; - size_t pieceLength; -public: - BtRejectMessageValidator(const BtRejectMessage* message, - size_t numPiece, - size_t pieceLength): - message(message), - numPiece(numPiece), - pieceLength(pieceLength) {} + size_t _index; + unsigned char* _msg; - virtual bool validate(Errors& error) { - // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); - PeerMessageUtil::checkBegin(message->getBegin(), pieceLength); - PeerMessageUtil::checkLength(message->getLength()); - PeerMessageUtil::checkRange(message->getBegin(), - message->getLength(), - pieceLength); - return true; + static const size_t MESSAGE_LENGTH = 9; +protected: + template + static SharedHandle create(const unsigned char* data, size_t dataLength) + { + PeerMessageUtil::assertPayloadLengthEqual(5, dataLength, T::NAME); + PeerMessageUtil::assertID(T::ID, data, T::NAME); + SharedHandle message(new T()); + message->setIndex(PeerMessageUtil::getIntParam(data, 1)); + return message; } -}; +public: + IndexBtMessage(uint8_t id, const std::string& name, size_t index) + :SimpleBtMessage(id, name), + _index(index), + _msg(0) {} -typedef SharedHandle BtRejectMessageValidatorHandle; + virtual ~IndexBtMessage() + { + delete [] _msg; + } + + void setIndex(size_t index) { _index = index; } + + size_t getIndex() const { return _index; } + + virtual const unsigned char* getMessage(); + + virtual size_t getMessageLength(); + + virtual std::string toString() const; +}; } // namespace aria2 -#endif // _D_BT_REJECT_MESSAGE_VALIDATOR_H_ +#endif // _D_INDEX_BT_MESSAGE_H_ diff --git a/src/BtHaveMessageValidator.h b/src/IndexBtMessageValidator.h similarity index 76% rename from src/BtHaveMessageValidator.h rename to src/IndexBtMessageValidator.h index 1fed369c..17bf4fd3 100644 --- a/src/BtHaveMessageValidator.h +++ b/src/IndexBtMessageValidator.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2009 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 @@ -32,28 +32,29 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_BT_HAVE_MESSAGE_VALIDATOR_H_ -#define _D_BT_HAVE_MESSAGE_VALIDATOR_H_ +#ifndef _D_INDEX_BT_VALIDATOR_H_ +#define _D_INDEX_BT_VALIDATOR_H_ #include "BtMessageValidator.h" -#include "BtHaveMessage.h" +#include "IndexBtMessage.h" #include "PeerMessageUtil.h" namespace aria2 { -class BtHaveMessageValidator : public BtMessageValidator { +class IndexBtMessageValidator : public BtMessageValidator { private: - const BtHaveMessage* message; - size_t numPiece; + const IndexBtMessage* _message; + size_t _numPiece; public: - BtHaveMessageValidator(const BtHaveMessage* message, - size_t numPiece): - message(message), - numPiece(numPiece) {} + IndexBtMessageValidator(const IndexBtMessage* message, + size_t numPiece): + _message(message), + _numPiece(numPiece) {} - virtual bool validate(Errors& errors) { + virtual bool validate(Errors& errors) + { // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); + PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece); return true; } @@ -61,4 +62,4 @@ public: } // namespace aria2 -#endif // _D_BT_HAVE_MESSAGE_VALIDATOR_H_ +#endif // _D_INDEX_BT_MESSAGE_VALIDATOR_H_ diff --git a/src/Makefile.am b/src/Makefile.am index 479eb636..231cc9f8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -296,14 +296,8 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\ BtUnchokeMessage.cc BtUnchokeMessage.h\ BtHandshakeMessage.cc BtHandshakeMessage.h\ BtMessageValidator.h\ - BtAllowedFastMessageValidator.h\ BtBitfieldMessageValidator.h\ - BtCancelMessageValidator.h\ - BtHaveMessageValidator.h\ BtPieceMessageValidator.h\ - BtRejectMessageValidator.h\ - BtRequestMessageValidator.h\ - BtSuggestPieceMessageValidator.h\ BtHandshakeMessageValidator.h\ BtMessageFactory.h\ DefaultBtMessageFactory.cc DefaultBtMessageFactory.h\ @@ -414,7 +408,11 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\ LibsslDHKeyExchange.h\ BtConstants.h\ BtLeecherStateChoke.cc BtLeecherStateChoke.h\ - BtSeederStateChoke.cc BtSeederStateChoke.h + BtSeederStateChoke.cc BtSeederStateChoke.h\ + RangeBtMessage.cc RangeBtMessage.h\ + IndexBtMessage.cc IndexBtMessage.h\ + ZeroBtMessage.cc ZeroBtMessage.h\ + RangeBtMessageValidator.h endif # ENABLE_BITTORRENT if ENABLE_METALINK diff --git a/src/Makefile.in b/src/Makefile.in index 543bfe44..cd0607ab 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -100,14 +100,8 @@ bin_PROGRAMS = aria2c$(EXEEXT) @ENABLE_BITTORRENT_TRUE@ BtUnchokeMessage.cc BtUnchokeMessage.h\ @ENABLE_BITTORRENT_TRUE@ BtHandshakeMessage.cc BtHandshakeMessage.h\ @ENABLE_BITTORRENT_TRUE@ BtMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtAllowedFastMessageValidator.h\ @ENABLE_BITTORRENT_TRUE@ BtBitfieldMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtCancelMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtHaveMessageValidator.h\ @ENABLE_BITTORRENT_TRUE@ BtPieceMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtRejectMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtRequestMessageValidator.h\ -@ENABLE_BITTORRENT_TRUE@ BtSuggestPieceMessageValidator.h\ @ENABLE_BITTORRENT_TRUE@ BtHandshakeMessageValidator.h\ @ENABLE_BITTORRENT_TRUE@ BtMessageFactory.h\ @ENABLE_BITTORRENT_TRUE@ DefaultBtMessageFactory.cc DefaultBtMessageFactory.h\ @@ -218,7 +212,11 @@ bin_PROGRAMS = aria2c$(EXEEXT) @ENABLE_BITTORRENT_TRUE@ LibsslDHKeyExchange.h\ @ENABLE_BITTORRENT_TRUE@ BtConstants.h\ @ENABLE_BITTORRENT_TRUE@ BtLeecherStateChoke.cc BtLeecherStateChoke.h\ -@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.cc BtSeederStateChoke.h +@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.cc BtSeederStateChoke.h\ +@ENABLE_BITTORRENT_TRUE@ RangeBtMessage.cc RangeBtMessage.h\ +@ENABLE_BITTORRENT_TRUE@ IndexBtMessage.cc IndexBtMessage.h\ +@ENABLE_BITTORRENT_TRUE@ ZeroBtMessage.cc ZeroBtMessage.h\ +@ENABLE_BITTORRENT_TRUE@ RangeBtMessageValidator.h @ENABLE_METALINK_TRUE@am__append_10 = Metalinker.cc Metalinker.h\ @ENABLE_METALINK_TRUE@ MetalinkEntry.cc MetalinkEntry.h\ @@ -464,10 +462,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ BtSuggestPieceMessage.cc BtSuggestPieceMessage.h \ BtUnchokeMessage.cc BtUnchokeMessage.h BtHandshakeMessage.cc \ BtHandshakeMessage.h BtMessageValidator.h \ - BtAllowedFastMessageValidator.h BtBitfieldMessageValidator.h \ - BtCancelMessageValidator.h BtHaveMessageValidator.h \ - BtPieceMessageValidator.h BtRejectMessageValidator.h \ - BtRequestMessageValidator.h BtSuggestPieceMessageValidator.h \ + BtBitfieldMessageValidator.h BtPieceMessageValidator.h \ BtHandshakeMessageValidator.h BtMessageFactory.h \ DefaultBtMessageFactory.cc DefaultBtMessageFactory.h \ BtMessageDispatcher.h DefaultBtMessageDispatcher.cc \ @@ -544,9 +539,11 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ LibsslARC4Decryptor.h LibsslARC4Encryptor.h \ LibsslDHKeyExchange.h BtConstants.h BtLeecherStateChoke.cc \ BtLeecherStateChoke.h BtSeederStateChoke.cc \ - BtSeederStateChoke.h Metalinker.cc Metalinker.h \ - MetalinkEntry.cc MetalinkEntry.h MetalinkResource.cc \ - MetalinkResource.h MetalinkProcessor.h \ + BtSeederStateChoke.h RangeBtMessage.cc RangeBtMessage.h \ + IndexBtMessage.cc IndexBtMessage.h ZeroBtMessage.cc \ + ZeroBtMessage.h RangeBtMessageValidator.h Metalinker.cc \ + Metalinker.h MetalinkEntry.cc MetalinkEntry.h \ + MetalinkResource.cc MetalinkResource.h MetalinkProcessor.h \ MetalinkProcessorFactory.cc MetalinkProcessorFactory.h \ MetalinkParserController.cc MetalinkParserController.h \ MetalinkParserStateMachine.cc MetalinkParserStateMachine.h \ @@ -698,7 +695,10 @@ am__objects_2 = @ENABLE_BITTORRENT_TRUE@ ReceiverMSEHandshakeCommand.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ MSEHandshake.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ BtLeecherStateChoke.$(OBJEXT) \ -@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.$(OBJEXT) +@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ RangeBtMessage.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ IndexBtMessage.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ ZeroBtMessage.$(OBJEXT) @ENABLE_METALINK_TRUE@am__objects_10 = Metalinker.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ MetalinkEntry.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ MetalinkResource.$(OBJEXT) \ @@ -1427,6 +1427,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpServerResponseCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpSkipResponseCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InOrderURISelector.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IndexBtMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitialMetalinkParserState.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommandFactory.Po@am__quote@ @@ -1484,6 +1485,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PiecesMetalinkParserState.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Platform.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ProtocolDetector.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RangeBtMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RarestPieceSelector.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RealtimeCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ReceiverMSEHandshakeCommand.Po@am__quote@ @@ -1532,6 +1534,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/VerificationMetalinkParserState.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/VersionMetalinkParserState.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/XML2SAXMetalinkProcessor.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ZeroBtMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asctime_r.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bencode.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download_helper.Po@am__quote@ diff --git a/src/PeerMessageUtil.cc b/src/PeerMessageUtil.cc index 325d387f..9ceeceb4 100644 --- a/src/PeerMessageUtil.cc +++ b/src/PeerMessageUtil.cc @@ -41,6 +41,8 @@ #include "a2netcompat.h" #include "StringFormat.h" #include "BtConstants.h" +#include "message.h" +#include "StringFormat.h" namespace aria2 { @@ -180,4 +182,35 @@ PeerMessageUtil::unpackcompact(const unsigned char* compact) return std::pair(host, port); } + +void PeerMessageUtil::assertPayloadLengthGreater +(size_t threshold, size_t actual, const std::string& msgName) +{ + if(actual <= threshold) { + throw DlAbortEx + (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str()); + } +} + +void PeerMessageUtil::assertPayloadLengthEqual +(size_t expected, size_t actual, const std::string& msgName) +{ + if(expected != actual) { + throw DlAbortEx + (StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(), + actual, expected).str()); + } +} + +void PeerMessageUtil::assertID +(uint8_t expected, const unsigned char* data, const std::string& msgName) +{ + uint8_t id = getId(data); + if(expected != id) { + throw DlAbortEx + (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(), + expected).str()); + } +} + } // namespace aria2 diff --git a/src/PeerMessageUtil.h b/src/PeerMessageUtil.h index b3de793c..1df121b0 100644 --- a/src/PeerMessageUtil.h +++ b/src/PeerMessageUtil.h @@ -79,6 +79,18 @@ public: static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port); static std::pair unpackcompact(const unsigned char* compact); + + // Throws exception if threshold >= actual + static void assertPayloadLengthGreater(size_t threshold, size_t actual, + const std::string& msgName); + + // Throws exception if expected != actual + static void assertPayloadLengthEqual(size_t expected, size_t actual, + const std::string& msgName); + + // Throws exception if expected is not equal to id from data. + static void assertID(uint8_t expected, const unsigned char* data, + const std::string& msgName); }; } // namespace aria2 diff --git a/src/RangeBtMessage.cc b/src/RangeBtMessage.cc new file mode 100644 index 00000000..b0dd9773 --- /dev/null +++ b/src/RangeBtMessage.cc @@ -0,0 +1,85 @@ +/* */ +#include "RangeBtMessage.h" +#include "Util.h" + +namespace aria2 { + +RangeBtMessage::RangeBtMessage(uint8_t id, + const std::string& name, + size_t index, uint32_t begin, size_t length) + :SimpleBtMessage(id, name), + _index(index), + _begin(begin), + _length(length), + _msg(0) {} + +RangeBtMessage::~RangeBtMessage() +{ + delete [] _msg; +} + +const unsigned char* RangeBtMessage::getMessage() +{ + if(!_msg) { + /** + * len --- 13, 4bytes + * id --- ?, 1byte + * index --- index, 4bytes + * begin --- begin, 4bytes + * length -- length, 4bytes + * total: 17bytes + */ + _msg = new unsigned char[MESSAGE_LENGTH]; + PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 13, getId()); + PeerMessageUtil::setIntParam(&_msg[5], _index); + PeerMessageUtil::setIntParam(&_msg[9], _begin); + PeerMessageUtil::setIntParam(&_msg[13], _length); + } + return _msg; +} + +size_t RangeBtMessage::getMessageLength() +{ + return MESSAGE_LENGTH; +} + +std::string RangeBtMessage::toString() const +{ + return getName()+" index="+Util::uitos(_index)+", begin="+Util::uitos(_begin)+ + ", length="+Util::uitos(_length); +} + +} // namespace aria2 diff --git a/src/RangeBtMessage.h b/src/RangeBtMessage.h new file mode 100644 index 00000000..a0b12df4 --- /dev/null +++ b/src/RangeBtMessage.h @@ -0,0 +1,91 @@ +/* */ +#ifndef _D_RANGE_BT_MESSAGE_H_ +#define _D_RANGE_BT_MESSAGE_H_ + +#include "SimpleBtMessage.h" +#include "PeerMessageUtil.h" + +namespace aria2 { + +class RangeBtMessage : public SimpleBtMessage { +private: + size_t _index; + uint32_t _begin; + size_t _length; + unsigned char* _msg; + + static const size_t MESSAGE_LENGTH = 17; +protected: + template + static SharedHandle create + (const unsigned char* data, size_t dataLength) + { + PeerMessageUtil::assertPayloadLengthEqual(13, dataLength, T::NAME); + PeerMessageUtil::assertID(T::ID, data, T::NAME); + SharedHandle message(new T()); + message->setIndex(PeerMessageUtil::getIntParam(data, 1)); + message->setBegin(PeerMessageUtil::getIntParam(data, 5)); + message->setLength(PeerMessageUtil::getIntParam(data, 9)); + return message; + } +public: + RangeBtMessage(uint8_t id, const std::string& name, + size_t index, uint32_t begin, size_t length); + + virtual ~RangeBtMessage(); + + size_t getIndex() const { return _index; } + + void setIndex(size_t index) { _index = index; } + + uint32_t getBegin() const { return _begin; } + + void setBegin(uint32_t begin) { _begin = begin; } + + size_t getLength() const { return _length; } + + void setLength(size_t length) { _length = length; } + + virtual const unsigned char* getMessage(); + + virtual size_t getMessageLength(); + + virtual std::string toString() const; +}; + +} // namespace aria2 + +#endif // _D_RANGE_BT_MESSAGE_H_ diff --git a/src/BtSuggestPieceMessageValidator.h b/src/RangeBtMessageValidator.h similarity index 66% rename from src/BtSuggestPieceMessageValidator.h rename to src/RangeBtMessageValidator.h index dbfeea87..a6d6cdfa 100644 --- a/src/BtSuggestPieceMessageValidator.h +++ b/src/RangeBtMessageValidator.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2009 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 @@ -32,35 +32,41 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_ -#define _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_ +#ifndef _D_RANGE_BT_MESSAGE_VALIDATOR_H_ +#define _D_RANGE_BT_MESSAGE_VALIDATOR_H_ #include "BtMessageValidator.h" -#include "BtSuggestPieceMessage.h" +#include "RangeBtMessage.h" #include "PeerMessageUtil.h" namespace aria2 { -class BtSuggestPieceMessageValidator : public BtMessageValidator { +class RangeBtMessageValidator : public BtMessageValidator { private: - const BtSuggestPieceMessage* message; - size_t numPiece; + const RangeBtMessage* _message; + size_t _numPiece; + size_t _pieceLength; public: - BtSuggestPieceMessageValidator(const BtSuggestPieceMessage* message, - size_t numPiece): - message(message), - numPiece(numPiece) {} + RangeBtMessageValidator(const RangeBtMessage* message, + size_t numPiece, + size_t pieceLength): + _message(message), + _numPiece(numPiece), + _pieceLength(pieceLength) {} - virtual bool validate(Errors& errors) { + virtual bool validate(Errors& errors) + { // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); + PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece); + PeerMessageUtil::checkBegin(_message->getBegin(), _pieceLength); + PeerMessageUtil::checkLength(_message->getLength()); + PeerMessageUtil::checkRange(_message->getBegin(), + _message->getLength(), + _pieceLength); return true; } - }; -typedef SharedHandle BtSuggestPieceMessageValidatorHandle; - } // namespace aria2 -#endif // _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_ +#endif // _D_RANGE_BT_MESSAGE_VALIDATOR_H_ diff --git a/src/SimpleBtMessage.cc b/src/SimpleBtMessage.cc index d333adef..b90ad04c 100644 --- a/src/SimpleBtMessage.cc +++ b/src/SimpleBtMessage.cc @@ -40,7 +40,8 @@ namespace aria2 { -SimpleBtMessage::SimpleBtMessage(uint8_t id):AbstractBtMessage(id) {} +SimpleBtMessage::SimpleBtMessage(uint8_t id, const std::string& name): + AbstractBtMessage(id, name) {} SimpleBtMessage::~SimpleBtMessage() {} diff --git a/src/SimpleBtMessage.h b/src/SimpleBtMessage.h index 3387b810..c5647b93 100644 --- a/src/SimpleBtMessage.h +++ b/src/SimpleBtMessage.h @@ -41,7 +41,7 @@ namespace aria2 { class SimpleBtMessage : public AbstractBtMessage { public: - SimpleBtMessage(uint8_t id); + SimpleBtMessage(uint8_t id, const std::string& name); virtual ~SimpleBtMessage(); diff --git a/src/ZeroBtMessage.cc b/src/ZeroBtMessage.cc new file mode 100644 index 00000000..153731ae --- /dev/null +++ b/src/ZeroBtMessage.cc @@ -0,0 +1,63 @@ +/* */ +#include "ZeroBtMessage.h" + +namespace aria2 { + +const unsigned char* ZeroBtMessage::getMessage() +{ + if(!_msg) { + /** + * len --- 1, 4bytes + * id --- ?, 1byte + * total: 5bytes + */ + _msg = new unsigned char[MESSAGE_LENGTH]; + PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 1, getId()); + } + return _msg; +} + +size_t ZeroBtMessage::getMessageLength() +{ + return MESSAGE_LENGTH; +} + +std::string ZeroBtMessage::toString() const +{ + return getName(); +} + +} // namespace aria2 diff --git a/src/BtCancelMessageValidator.h b/src/ZeroBtMessage.h similarity index 62% rename from src/BtCancelMessageValidator.h rename to src/ZeroBtMessage.h index 5f75f352..ad2bfc3e 100644 --- a/src/BtCancelMessageValidator.h +++ b/src/ZeroBtMessage.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2009 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 @@ -32,41 +32,45 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_BT_CANCEL_MESSAGE_VALIDATOR_H_ -#define _D_BT_CANCEL_MESSAGE_VALIDATOR_H_ +#ifndef _D_ZERO_BT_MESSAGE_H_ +#define _D_ZERO_BT_MESSAGE_H_ -#include "BtMessageValidator.h" -#include "BtCancelMessage.h" +#include "SimpleBtMessage.h" #include "PeerMessageUtil.h" namespace aria2 { -class BtCancelMessageValidator : public BtMessageValidator { +class ZeroBtMessage : public SimpleBtMessage { private: - const BtCancelMessage* message; - size_t numPiece; - size_t pieceLength; -public: - BtCancelMessageValidator(const BtCancelMessage* message, - size_t numPiece, - size_t pieceLength): - message(message), - numPiece(numPiece), - pieceLength(pieceLength) {} + unsigned char* _msg; - virtual bool validate(Errors& error) { - // TODO - PeerMessageUtil::checkIndex(message->getIndex(), numPiece); - PeerMessageUtil::checkBegin(message->getBegin(), pieceLength); - PeerMessageUtil::checkLength(message->getLength()); - PeerMessageUtil::checkRange(message->getBegin(), message->getLength(), - pieceLength); - return true; + static const size_t MESSAGE_LENGTH = 5; +protected: + template + static SharedHandle create(const unsigned char* data, size_t dataLength) + { + PeerMessageUtil::assertPayloadLengthEqual(1, dataLength, T::NAME); + PeerMessageUtil::assertID(T::ID, data, T::NAME); + SharedHandle message(new T()); + return message; } -}; -typedef SharedHandle BtCancelMessageValidatorHandle; +public: + ZeroBtMessage(uint8_t id, const std::string& name): + SimpleBtMessage(id, name), _msg(0) {} + + virtual ~ZeroBtMessage() + { + delete [] _msg; + } + + virtual const unsigned char* getMessage(); + + virtual size_t getMessageLength(); + + virtual std::string toString() const; +}; } // namespace aria2 -#endif // _D_BT_CANCEL_MESSAGE_VALIDATOR_H_ +#endif // _D_ZERO_BT_MESSAGE_H_ diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index 2ab4c667..9711dff2 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -14,7 +14,7 @@ #include "Peer.h" #include "FileEntry.h" #include "BtHandshakeMessage.h" -#include "BtRequestMessageValidator.h" +#include "RangeBtMessageValidator.h" namespace aria2 { @@ -144,7 +144,7 @@ void BtRequestMessageTest::testCreate() { CPPUNIT_ASSERT_EQUAL((uint8_t)6, pm->getId()); CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex()); CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin()); - CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength()); + CPPUNIT_ASSERT_EQUAL((size_t)1024, pm->getLength()); // case: payload size is wrong try { @@ -289,7 +289,7 @@ void BtRequestMessageTest::testValidate() { BtRequestMessage msg(0, 0, 16*1024); msg.setBtMessageValidator (SharedHandle - (new BtRequestMessageValidator(&msg, 1024, 256*1024))); + (new RangeBtMessageValidator(&msg, 1024, 256*1024))); std::deque errors; msg.validate(errors); @@ -299,7 +299,7 @@ void BtRequestMessageTest::testValidate_lengthTooLong() { BtRequestMessage msg(0, 0, 16*1024+1); msg.setBtMessageValidator (SharedHandle - (new BtRequestMessageValidator(&msg, 1024, 256*1024))); + (new RangeBtMessageValidator(&msg, 1024, 256*1024))); std::deque errors; try { msg.validate(errors);