diff --git a/ChangeLog b/ChangeLog index 3f567038..4bea9d6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2009-04-21 Tatsuhiro Tsujikawa + + Rewritten event dispatch in BtMessage. Removed BtEventListener, + BtEvent. + * src/AbstractBtEventListener.h: Removed. + * src/AbstractBtMessage.cc + * src/AbstractBtMessage.h + * src/BtAbortOutstandingRequestEvent.h + * src/BtCancelSendingPieceEvent.h + * src/BtChokedEvent.h: Removed. + * src/BtChokingEvent.h + * src/BtEvent.h: Removed + * src/BtEventListener.h: Removed. + * src/BtMessage.h + * src/BtPieceMessage.cc + * src/BtPieceMessage.h + * src/BtRequestMessage.cc + * src/BtRequestMessage.h + * src/DefaultBtMessageDispatcher.cc + * src/Makefile.am + * src/a2functional.h + * test/BtPieceMessageTest.cc + * test/BtRequestMessageTest.cc + * test/DefaultBtMessageDispatcherTest.cc + * test/MockBtMessage.h + 2009-04-17 Tatsuhiro Tsujikawa Code cleanup diff --git a/src/AbstractBtEventListener.h b/src/AbstractBtEventListener.h deleted file mode 100644 index 64a6427e..00000000 --- a/src/AbstractBtEventListener.h +++ /dev/null @@ -1,59 +0,0 @@ -/* */ -#ifndef _D_ABSTRACT_BT_EVENT_LISTENER_H_ -#define _D_ABSTRACT_BT_EVENT_LISTENER_H_ - -#include "BtEventListener.h" - -namespace aria2 { - -class AbstractBtEventListener : public BtEventListener { -public: - virtual ~AbstractBtEventListener() {} - - virtual bool canHandle(const SharedHandle& event) = 0; - - virtual void handleEventInternal(const SharedHandle& event) = 0; - - virtual void handleEvent(const SharedHandle& event) { - if(canHandle(event)) { - handleEventInternal(event); - } - } -}; - -} // namespace aria2 - -#endif // _D_ABSTRACT_BT_EVENT_LISTENER_H_ diff --git a/src/AbstractBtMessage.cc b/src/AbstractBtMessage.cc index eb339ac5..cc43ec48 100644 --- a/src/AbstractBtMessage.cc +++ b/src/AbstractBtMessage.cc @@ -36,7 +36,6 @@ #include "Peer.h" #include "BtContext.h" #include "PieceStorage.h" -#include "BtEventListener.h" #include "BtMessageValidator.h" #include "LogFactory.h" #include "Logger.h" @@ -74,20 +73,6 @@ bool AbstractBtMessage::validate(std::deque& errors) } } -void AbstractBtMessage::handleEvent(const SharedHandle& event) -{ - for(std::deque >::iterator itr = listeners.begin(); - itr != listeners.end(); ++itr) { - (*itr)->handleEvent(event); - } -} - -void -AbstractBtMessage::addEventListener(const SharedHandle& listener) -{ - listeners.push_back(listener); -} - void AbstractBtMessage::setBtMessageValidator(const SharedHandle& validator) { this->validator = validator; diff --git a/src/AbstractBtMessage.h b/src/AbstractBtMessage.h index 5c1fffff..b3d00d6c 100644 --- a/src/AbstractBtMessage.h +++ b/src/AbstractBtMessage.h @@ -48,7 +48,6 @@ class BtMessageFactory; class BtRequestFactory; class PeerConnection; class BtMessageValidator; -class BtEventListener; class Logger; class AbstractBtMessage : public BtMessage { @@ -76,8 +75,6 @@ protected: SharedHandle validator; - std::deque > listeners; - Logger* logger; public: AbstractBtMessage(uint8_t id, const std::string& name); @@ -126,9 +123,13 @@ public: virtual void onQueued() {} - virtual void handleEvent(const SharedHandle& event); + virtual void onAbortOutstandingRequestEvent + (const BtAbortOutstandingRequestEvent& event) {} - void addEventListener(const SharedHandle& listener); + virtual void onCancelSendingPieceEvent + (const BtCancelSendingPieceEvent& event) {} + + virtual void onChokingEvent(const BtChokingEvent& event) {} void setBtMessageValidator(const SharedHandle& validator); diff --git a/src/BtAbortOutstandingRequestEvent.h b/src/BtAbortOutstandingRequestEvent.h index 53e077ba..a8b6fdb3 100644 --- a/src/BtAbortOutstandingRequestEvent.h +++ b/src/BtAbortOutstandingRequestEvent.h @@ -35,29 +35,22 @@ #ifndef _D_BT_ABORT_OUTSTANDING_REQUEST_EVENT_H_ #define _D_BT_ABORT_OUTSTANDING_REQUEST_EVENT_H_ -#include "BtEvent.h" +#include "common.h" +#include "SharedHandle.h" +#include "Piece.h" namespace aria2 { -class Piece; - -class BtAbortOutstandingRequestEvent : public BtEvent { +class BtAbortOutstandingRequestEvent { private: SharedHandle piece; public: - BtAbortOutstandingRequestEvent(const SharedHandle& piece):piece(piece) {} + BtAbortOutstandingRequestEvent(const SharedHandle& piece): + piece(piece) {} - SharedHandle getPiece() const { - return piece; - } - - void setPiece(const SharedHandle& piece) { - this->piece = piece; - } + SharedHandle getPiece() const { return piece; } }; -typedef SharedHandle BtAbortOutstandingRequestEventHandle; - } // namespace aria2 #endif // _D_BT_ABORT_OUTSTANDING_REQUEST_EVENT_H_ diff --git a/src/BtCancelSendingPieceEvent.h b/src/BtCancelSendingPieceEvent.h index 6574dd24..0dabef74 100644 --- a/src/BtCancelSendingPieceEvent.h +++ b/src/BtCancelSendingPieceEvent.h @@ -35,11 +35,11 @@ #ifndef _D_BT_CANCEL_SENDING_PIECE_EVENT_H_ #define _D_BT_CANCEL_SENDING_PIECE_EVENT_H_ -#include "BtEvent.h" +#include "common.h" namespace aria2 { -class BtCancelSendingPieceEvent : public BtEvent { +class BtCancelSendingPieceEvent { private: size_t index; uint32_t begin; @@ -48,35 +48,13 @@ public: BtCancelSendingPieceEvent(size_t index, uint32_t begin, size_t length): index(index), begin(begin), length(length) {} - virtual ~BtCancelSendingPieceEvent() {} + size_t getIndex() const { return index; } - void setIndex(size_t index) { - this->index = index; - } + uint32_t getBegin() const { return begin; } - size_t getIndex() const { - return index; - } - - void setBegin(uint32_t begin) { - this->begin = begin; - } - - uint32_t getBegin() const { - return begin; - } - - void setLength(size_t length) { - this->length = length; - } - - size_t getLength() const { - return length; - } + size_t getLength() const { return length; } }; -typedef SharedHandle BtCancelSendingPieceEventHandle; - } // namespace aria2 #endif // _D_BT_CANCEL_SENDING_PIECE_EVENT_H_ diff --git a/src/BtChokedEvent.h b/src/BtChokedEvent.h deleted file mode 100644 index 0d8fd914..00000000 --- a/src/BtChokedEvent.h +++ /dev/null @@ -1,50 +0,0 @@ -/* */ -#ifndef _D_BT_CHOKED_EVENT_H_ -#define _D_BT_CHOKED_EVENT_H_ - -#include "BtEvent.h" -#include "SharedHandle.h" - -namespace aria2 { - -class BtChokedEvent : public BtEvent { -}; - -typedef SharedHandle BtChokedEventHandle; - -} // namespace aria2 - -#endif // _D_BT_CHOKED_EVENT_H_ diff --git a/src/BtChokingEvent.h b/src/BtChokingEvent.h index 8d8abd97..22709e60 100644 --- a/src/BtChokingEvent.h +++ b/src/BtChokingEvent.h @@ -35,14 +35,11 @@ #ifndef _D_BT_CHOKING_EVENT_H_ #define _D_BT_CHOKING_EVENT_H_ -#include "BtEvent.h" +#include "common.h" namespace aria2 { -class BtChokingEvent : public BtEvent { -}; - -typedef SharedHandle BtChokingEventHandle; +class BtChokingEvent {}; } // namespace aria2 diff --git a/src/BtEvent.h b/src/BtEvent.h deleted file mode 100644 index c40d175f..00000000 --- a/src/BtEvent.h +++ /dev/null @@ -1,51 +0,0 @@ -/* */ -#ifndef _D_BT_EVENT_H_ -#define _D_BT_EVENT_H_ - -#include "common.h" -#include "SharedHandle.h" - -namespace aria2 { -class BtEvent { -public: - virtual ~BtEvent() {} -}; - -typedef SharedHandle BtEventHandle; - -} // namespace aria2 - -#endif // _D_BT_EVENT_H_ diff --git a/src/BtEventListener.h b/src/BtEventListener.h deleted file mode 100644 index 25e60d2b..00000000 --- a/src/BtEventListener.h +++ /dev/null @@ -1,58 +0,0 @@ -/* */ -#ifndef _D_BT_EVENT_LISTENER_H_ -#define _D_BT_EVENT_LISTENER_H_ - -#include "common.h" -#include "SharedHandle.h" -#include - -namespace aria2 { - -class BtEvent; - -class BtEventListener { -public: - virtual ~BtEventListener() {} - - virtual void handleEvent(const SharedHandle& event) = 0; -}; - -typedef SharedHandle BtEventListenerHandle; -typedef std::deque BtEventListeners; - -} // namespace aria2 - -#endif // _D_BT_EVENT_LISTENER_H_ diff --git a/src/BtMessage.h b/src/BtMessage.h index 6a10cf7f..f1b5a581 100644 --- a/src/BtMessage.h +++ b/src/BtMessage.h @@ -36,10 +36,15 @@ #define _D_BT_MESSAGE_H_ #include "common.h" -#include "SharedHandle.h" + #include #include +#include "SharedHandle.h" +#include "BtAbortOutstandingRequestEvent.h" +#include "BtCancelSendingPieceEvent.h" +#include "BtChokingEvent.h" + namespace aria2 { class BtEvent; @@ -66,7 +71,13 @@ public: virtual bool validate(std::deque& errors) = 0; - virtual void handleEvent(const SharedHandle& event) = 0; + virtual void onAbortOutstandingRequestEvent + (const BtAbortOutstandingRequestEvent& event) = 0; + + virtual void onCancelSendingPieceEvent + (const BtCancelSendingPieceEvent& event) = 0; + + virtual void onChokingEvent(const BtChokingEvent& event) = 0; virtual void onQueued() = 0; diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index f20eb1e6..2b9ee7ee 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -42,8 +42,6 @@ #include "Util.h" #include "message.h" #include "DlAbortEx.h" -#include "BtChokingEvent.h" -#include "BtCancelSendingPieceEvent.h" #include "MessageDigestHelper.h" #include "DiskAdaptor.h" #include "Logger.h" @@ -216,16 +214,8 @@ void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) { } } -bool BtPieceMessage::BtChokingEventListener::canHandle(const BtEventHandle& event) { - BtChokingEvent* intEvent = dynamic_cast(event.get()); - return intEvent != 0; -} - -void BtPieceMessage::BtChokingEventListener::handleEventInternal(const BtEventHandle& event) { - message->handleChokingEvent(event); -} - -void BtPieceMessage::handleChokingEvent(const BtEventHandle& event) { +void BtPieceMessage::onChokingEvent(const BtChokingEvent& event) +{ if(!invalidate && !sendingInProgress && !peer->isInAmAllowedIndexSet(index)) { @@ -245,22 +235,14 @@ void BtPieceMessage::handleChokingEvent(const BtEventHandle& event) { } } -bool BtPieceMessage::BtCancelSendingPieceEventListener::canHandle(const BtEventHandle& event) { - BtCancelSendingPieceEvent* intEvent = dynamic_cast(event.get()); - return intEvent != 0; -} - -void BtPieceMessage::BtCancelSendingPieceEventListener::handleEventInternal(const BtEventHandle& event) { - message->handleCancelSendingPieceEvent(event); -} - -void BtPieceMessage::handleCancelSendingPieceEvent(const BtEventHandle& event) { - BtCancelSendingPieceEvent* intEvent = (BtCancelSendingPieceEvent*)(event.get()); +void BtPieceMessage::onCancelSendingPieceEvent +(const BtCancelSendingPieceEvent& event) +{ if(!invalidate && !sendingInProgress && - index == intEvent->getIndex() && - begin == intEvent->getBegin() && - blockLength == intEvent->getLength()) { + index == event.getIndex() && + begin == event.getBegin() && + blockLength == event.getLength()) { logger->debug(MSG_REJECT_PIECE_CANCEL, cuid, index, begin, blockLength); if(peer->isFastExtensionEnabled()) { diff --git a/src/BtPieceMessage.h b/src/BtPieceMessage.h index a2435709..9b543035 100644 --- a/src/BtPieceMessage.h +++ b/src/BtPieceMessage.h @@ -36,11 +36,9 @@ #define _D_BT_PIECE_MESSAGE_H_ #include "AbstractBtMessage.h" -#include "AbstractBtEventListener.h" namespace aria2 { -class BtEvent; class Piece; class BtPieceMessage; @@ -65,32 +63,6 @@ private: void erasePieceOnDisk(const SharedHandle& piece); size_t sendPieceData(off_t offset, size_t length) const; - - class BtChokingEventListener : public AbstractBtEventListener { - private: - BtPieceMessage* message; - public: - BtChokingEventListener(BtPieceMessage* message):message(message) {} - - virtual bool canHandle(const SharedHandle& btEvent); - - virtual void handleEventInternal(const SharedHandle& btEvent); - }; - - typedef SharedHandle BtChokingEventListenerHandle; - - class BtCancelSendingPieceEventListener : public AbstractBtEventListener { - private: - BtPieceMessage* message; - public: - BtCancelSendingPieceEventListener(BtPieceMessage* message):message(message) {} - - virtual bool canHandle(const SharedHandle& btEvent); - - virtual void handleEventInternal(const SharedHandle& btEvent); - }; - - typedef SharedHandle BtCancelSendingPieceEventListenerHandle; public: BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0) :AbstractBtMessage(ID, NAME), @@ -101,14 +73,6 @@ public: msgHeader(0) { uploading = true; - { - SharedHandle listener(new BtChokingEventListener(this)); - addEventListener(listener); - } - { - SharedHandle listener(new BtCancelSendingPieceEventListener(this)); - addEventListener(listener); - } } virtual ~BtPieceMessage() { @@ -148,9 +112,10 @@ public: virtual std::string toString() const; - void handleChokingEvent(const SharedHandle& event); + virtual void onChokingEvent(const BtChokingEvent& event); - void handleCancelSendingPieceEvent(const SharedHandle& event); + virtual void onCancelSendingPieceEvent + (const BtCancelSendingPieceEvent& event); }; } // namespace aria2 diff --git a/src/BtRequestMessage.cc b/src/BtRequestMessage.cc index 44b504a5..12af9702 100644 --- a/src/BtRequestMessage.cc +++ b/src/BtRequestMessage.cc @@ -33,7 +33,6 @@ */ /* copyright --> */ #include "BtRequestMessage.h" -#include "BtAbortOutstandingRequestEvent.h" #include "Peer.h" #include "Piece.h" #include "PieceStorage.h" @@ -76,29 +75,11 @@ void BtRequestMessage::onQueued() dispatcher->addOutstandingRequest(requestSlot); } -bool BtRequestMessage:: -BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) +void BtRequestMessage::onAbortOutstandingRequestEvent +(const BtAbortOutstandingRequestEvent& event) { - BtAbortOutstandingRequestEvent* intEvent = - dynamic_cast(event.get()); - return intEvent != 0; -} - -void BtRequestMessage:: -BtAbortOutstandingRequestEventListener::handleEventInternal -(const BtEventHandle& event) -{ - message->handleAbortOutstandingRequestEvent(event); -} - -void BtRequestMessage:: -handleAbortOutstandingRequestEvent(const BtEventHandle& event) -{ - BtAbortOutstandingRequestEvent* intEvent = - (BtAbortOutstandingRequestEvent*)event.get(); - if(getIndex() == intEvent->getPiece()->getIndex() && - !invalidate && - !sendingInProgress) { + if(getIndex() == event.getPiece()->getIndex() && + !invalidate && !sendingInProgress) { invalidate = true; } } diff --git a/src/BtRequestMessage.h b/src/BtRequestMessage.h index e7a8d10a..3dad0e9b 100644 --- a/src/BtRequestMessage.h +++ b/src/BtRequestMessage.h @@ -36,7 +36,6 @@ #define _D_BT_REQUEST_MESSAGE_H_ #include "RangeBtMessage.h" -#include "AbstractBtEventListener.h" namespace aria2 { @@ -48,32 +47,13 @@ class BtRequestMessage : public RangeBtMessage { private: size_t _blockIndex; - class BtAbortOutstandingRequestEventListener:public AbstractBtEventListener { - private: - BtRequestMessage* message; - public: - BtAbortOutstandingRequestEventListener(BtRequestMessage* message): - message(message) {} - - virtual bool canHandle(const SharedHandle& event); - - virtual void handleEventInternal(const SharedHandle& event); - }; - - typedef SharedHandle - BtAbortOutstandingRequestEventListenerHandle; public: BtRequestMessage(size_t index = 0, uint32_t begin = 0, uint32_t length = 0, size_t blockIndex = 0) :RangeBtMessage(ID, NAME, index, begin, length), - _blockIndex(blockIndex) - { - SharedHandle listener - (new BtAbortOutstandingRequestEventListener(this)); - addEventListener(listener); - } + _blockIndex(blockIndex) {} static const uint8_t ID = 6; @@ -89,8 +69,8 @@ public: virtual void onQueued(); - virtual void handleAbortOutstandingRequestEvent - (const SharedHandle& event); + virtual void onAbortOutstandingRequestEvent + (const BtAbortOutstandingRequestEvent& event); }; } // namespace aria2 diff --git a/src/DefaultBtMessageDispatcher.cc b/src/DefaultBtMessageDispatcher.cc index dbda96c2..4fee16db 100644 --- a/src/DefaultBtMessageDispatcher.cc +++ b/src/DefaultBtMessageDispatcher.cc @@ -39,7 +39,6 @@ #include "prefs.h" #include "BtAbortOutstandingRequestEvent.h" #include "BtCancelSendingPieceEvent.h" -#include "BtChokedEvent.h" #include "BtChokingEvent.h" #include "BtMessageFactory.h" #include "message.h" @@ -114,26 +113,15 @@ void DefaultBtMessageDispatcher::sendMessages() { } } -class HandleEvent { -private: - SharedHandle _event; -public: - HandleEvent(const SharedHandle& event):_event(event) {} - - void operator()(const SharedHandle& msg) const - { - msg->handleEvent(_event); - } -}; - // Cancel sending piece message to peer. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(size_t index, uint32_t begin, size_t length) { - BtCancelSendingPieceEventHandle event - (new BtCancelSendingPieceEvent(index, begin, length)); + BtCancelSendingPieceEvent event(index, begin, length); BtMessages tempQueue = messageQueue; - std::for_each(tempQueue.begin(), tempQueue.end(), HandleEvent(event)); + std::for_each(tempQueue.begin(), tempQueue.end(), + std::bind2nd(mem_fun_sh(&BtMessage::onCancelSendingPieceEvent), + event)); } // Cancel sending piece message to peer. @@ -177,11 +165,12 @@ void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(const PieceHand std::for_each(first, last, AbortOutstandingRequest(piece, cuid)); requestSlots.erase(first, last); - BtAbortOutstandingRequestEventHandle event - (new BtAbortOutstandingRequestEvent(piece)); + BtAbortOutstandingRequestEvent event(piece); BtMessages tempQueue = messageQueue; - std::for_each(tempQueue.begin(), tempQueue.end(), HandleEvent(event)); + std::for_each(tempQueue.begin(), tempQueue.end(), + std::bind2nd(mem_fun_sh(&BtMessage::onAbortOutstandingRequestEvent), + event)); } class ProcessChokedRequestSlot { @@ -236,20 +225,16 @@ void DefaultBtMessageDispatcher::doChokedAction() requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(), FindChokedRequestSlot(peer)), requestSlots.end()); - - BtChokedEventHandle event(new BtChokedEvent()); - - BtMessages tempQueue = messageQueue; - std::for_each(tempQueue.begin(), tempQueue.end(), HandleEvent(event)); } // localhost dispatched choke message to the peer. void DefaultBtMessageDispatcher::doChokingAction() { - BtChokingEventHandle event(new BtChokingEvent()); + BtChokingEvent event; BtMessages tempQueue = messageQueue; - std::for_each(tempQueue.begin(), tempQueue.end(), HandleEvent(event)); + std::for_each(tempQueue.begin(), tempQueue.end(), + std::bind2nd(mem_fun_sh(&BtMessage::onChokingEvent), event)); } class ProcessStaleRequestSlot { diff --git a/src/Makefile.am b/src/Makefile.am index 1ce87d27..7d74565a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -312,12 +312,8 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\ DefaultBtMessageReceiver.cc DefaultBtMessageReceiver.h\ BtRequestFactory.h\ DefaultBtRequestFactory.cc DefaultBtRequestFactory.h\ - BtEvent.h\ - BtEventListener.h\ - AbstractBtEventListener.h\ BtCancelSendingPieceEvent.h\ BtAbortOutstandingRequestEvent.h\ - BtChokedEvent.h\ BtChokingEvent.h\ BtInteractive.h\ DefaultBtInteractive.cc DefaultBtInteractive.h\ diff --git a/src/Makefile.in b/src/Makefile.in index 35b9bef3..46be5ff7 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -112,12 +112,8 @@ bin_PROGRAMS = aria2c$(EXEEXT) @ENABLE_BITTORRENT_TRUE@ DefaultBtMessageReceiver.cc DefaultBtMessageReceiver.h\ @ENABLE_BITTORRENT_TRUE@ BtRequestFactory.h\ @ENABLE_BITTORRENT_TRUE@ DefaultBtRequestFactory.cc DefaultBtRequestFactory.h\ -@ENABLE_BITTORRENT_TRUE@ BtEvent.h\ -@ENABLE_BITTORRENT_TRUE@ BtEventListener.h\ -@ENABLE_BITTORRENT_TRUE@ AbstractBtEventListener.h\ @ENABLE_BITTORRENT_TRUE@ BtCancelSendingPieceEvent.h\ @ENABLE_BITTORRENT_TRUE@ BtAbortOutstandingRequestEvent.h\ -@ENABLE_BITTORRENT_TRUE@ BtChokedEvent.h\ @ENABLE_BITTORRENT_TRUE@ BtChokingEvent.h\ @ENABLE_BITTORRENT_TRUE@ BtInteractive.h\ @ENABLE_BITTORRENT_TRUE@ DefaultBtInteractive.cc DefaultBtInteractive.h\ @@ -473,18 +469,16 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ DefaultBtMessageDispatcher.h BtMessageReceiver.h \ DefaultBtMessageReceiver.cc DefaultBtMessageReceiver.h \ BtRequestFactory.h DefaultBtRequestFactory.cc \ - DefaultBtRequestFactory.h BtEvent.h BtEventListener.h \ - AbstractBtEventListener.h BtCancelSendingPieceEvent.h \ - BtAbortOutstandingRequestEvent.h BtChokedEvent.h \ - BtChokingEvent.h BtInteractive.h DefaultBtInteractive.cc \ - DefaultBtInteractive.h ActivePeerConnectionCommand.cc \ - ActivePeerConnectionCommand.h BtDependency.cc BtDependency.h \ - PeerReceiveHandshakeCommand.cc PeerReceiveHandshakeCommand.h \ - BtSetup.cc BtSetup.h BtFileAllocationEntry.cc \ - BtFileAllocationEntry.h BtPostDownloadHandler.cc \ - BtPostDownloadHandler.h BtCheckIntegrityEntry.cc \ - BtCheckIntegrityEntry.h BtExtendedMessage.cc \ - BtExtendedMessage.h ExtensionMessage.h \ + DefaultBtRequestFactory.h BtCancelSendingPieceEvent.h \ + BtAbortOutstandingRequestEvent.h BtChokingEvent.h \ + BtInteractive.h DefaultBtInteractive.cc DefaultBtInteractive.h \ + ActivePeerConnectionCommand.cc ActivePeerConnectionCommand.h \ + BtDependency.cc BtDependency.h PeerReceiveHandshakeCommand.cc \ + PeerReceiveHandshakeCommand.h BtSetup.cc BtSetup.h \ + BtFileAllocationEntry.cc BtFileAllocationEntry.h \ + BtPostDownloadHandler.cc BtPostDownloadHandler.h \ + BtCheckIntegrityEntry.cc BtCheckIntegrityEntry.h \ + BtExtendedMessage.cc BtExtendedMessage.h ExtensionMessage.h \ ExtensionMessageFactory.h DefaultExtensionMessageFactory.cc \ DefaultExtensionMessageFactory.h HandshakeExtensionMessage.cc \ HandshakeExtensionMessage.h UTPexExtensionMessage.cc \ diff --git a/src/a2functional.h b/src/a2functional.h index 447027d2..fb8c4745 100644 --- a/src/a2functional.h +++ b/src/a2functional.h @@ -88,6 +88,31 @@ mem_fun_sh(ReturnType (ClassType::*f)() const) return const_mem_fun_sh_t(f); }; +// mem_fun1_t for SharedHandle +template +class mem_fun1_sh_t:public std::binary_function, + ArgType, + ReturnType> +{ +private: + ReturnType (ClassType::*f)(ArgType); + +public: + mem_fun1_sh_t(ReturnType (ClassType::*f)(ArgType)):f(f) {} + + ReturnType operator()(const SharedHandle& x, ArgType a) const + { + return (x.get()->*f)(a); + } +}; + +template +mem_fun1_sh_t +mem_fun_sh(ReturnType (ClassType::*f)(ArgType)) +{ + return mem_fun1_sh_t(f); +}; + template class adopt2nd_t:public std::binary_functionisInAmAllowedIndexSet(1)); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent(SharedHandle(new BtChokingEvent())); + msg->onChokingEvent(BtChokingEvent()); CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size()); @@ -170,7 +170,7 @@ void BtPieceMessageTest::testChokingEvent_allowedFastEnabled() { CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1)); CPPUNIT_ASSERT(peer->isFastExtensionEnabled()); - msg->handleEvent(SharedHandle(new BtChokingEvent())); + msg->onChokingEvent(BtChokingEvent()); CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size()); @@ -190,7 +190,7 @@ void BtPieceMessageTest::testChokingEvent_inAmAllowedIndexSet() { CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1)); CPPUNIT_ASSERT(peer->isFastExtensionEnabled()); - msg->handleEvent(SharedHandle(new BtChokingEvent())); + msg->onChokingEvent(BtChokingEvent()); CPPUNIT_ASSERT(!msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size()); @@ -203,7 +203,7 @@ void BtPieceMessageTest::testChokingEvent_invalidate() { CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1)); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent(SharedHandle(new BtChokingEvent())); + msg->onChokingEvent(BtChokingEvent()); CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size()); @@ -216,7 +216,7 @@ void BtPieceMessageTest::testChokingEvent_sendingInProgress() { CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1)); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent(SharedHandle(new BtChokingEvent())); + msg->onChokingEvent(BtChokingEvent()); CPPUNIT_ASSERT(!msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size()); @@ -227,8 +227,7 @@ void BtPieceMessageTest::testCancelSendingPieceEvent() { CPPUNIT_ASSERT(!msg->isSendingInProgress()); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 1024, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 16*1024)); CPPUNIT_ASSERT(msg->isInvalidate()); } @@ -238,18 +237,15 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_noMatch() { CPPUNIT_ASSERT(!msg->isSendingInProgress()); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(0, 1024, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(0, 1024, 16*1024)); CPPUNIT_ASSERT(!msg->isInvalidate()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 0, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 0, 16*1024)); CPPUNIT_ASSERT(!msg->isInvalidate()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 1024, 0))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 0)); CPPUNIT_ASSERT(!msg->isInvalidate()); } @@ -260,8 +256,7 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_allowedFastEnabled() { CPPUNIT_ASSERT(!msg->isSendingInProgress()); CPPUNIT_ASSERT(peer->isFastExtensionEnabled()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 1024, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 16*1024)); CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size()); @@ -279,8 +274,7 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_invalidate() { CPPUNIT_ASSERT(!msg->isSendingInProgress()); CPPUNIT_ASSERT(peer->isFastExtensionEnabled()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 1024, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 16*1024)); CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size()); @@ -292,8 +286,7 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_sendingInProgress() { CPPUNIT_ASSERT(msg->isSendingInProgress()); CPPUNIT_ASSERT(!peer->isFastExtensionEnabled()); - msg->handleEvent - (SharedHandle(new BtCancelSendingPieceEvent(1, 1024, 16*1024))); + msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 16*1024)); CPPUNIT_ASSERT(!msg->isInvalidate()); } diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index 9711dff2..28c9a628 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -236,47 +236,34 @@ void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionD void BtRequestMessageTest::testHandleAbortRequestEvent() { SharedHandle piece(new Piece(1, 16*1024)); - SharedHandle event - (new BtAbortOutstandingRequestEvent(piece)); - CPPUNIT_ASSERT(!msg->isInvalidate()); - msg->handleEvent(event); - + msg->onAbortOutstandingRequestEvent(BtAbortOutstandingRequestEvent(piece)); CPPUNIT_ASSERT(msg->isInvalidate()); } void BtRequestMessageTest::testHandleAbortRequestEvent_indexNoMatch() { SharedHandle piece(new Piece(2, 16*1024)); - SharedHandle event - (new BtAbortOutstandingRequestEvent(piece)); - CPPUNIT_ASSERT(!msg->isInvalidate()); CPPUNIT_ASSERT(!msg->isSendingInProgress()); - msg->handleEvent(event); + msg->onAbortOutstandingRequestEvent(BtAbortOutstandingRequestEvent(piece)); CPPUNIT_ASSERT(!msg->isInvalidate()); } void BtRequestMessageTest::testHandleAbortRequestEvent_alreadyInvalidated() { SharedHandle piece(new Piece(1, 16*1024)); - SharedHandle event - (new BtAbortOutstandingRequestEvent(piece)); msg->setInvalidate(true); - CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT(!msg->isSendingInProgress()); - msg->handleEvent(event); + msg->onAbortOutstandingRequestEvent(BtAbortOutstandingRequestEvent(piece)); CPPUNIT_ASSERT(msg->isInvalidate()); } void BtRequestMessageTest::testHandleAbortRequestEvent_sendingInProgress() { SharedHandle piece(new Piece(1, 16*1024)); - SharedHandle event - (new BtAbortOutstandingRequestEvent(piece)); msg->setSendingInProgress(true); - CPPUNIT_ASSERT(!msg->isInvalidate()); CPPUNIT_ASSERT(msg->isSendingInProgress()); - msg->handleEvent(event); + msg->onAbortOutstandingRequestEvent(BtAbortOutstandingRequestEvent(piece)); CPPUNIT_ASSERT(!msg->isInvalidate()); } diff --git a/test/DefaultBtMessageDispatcherTest.cc b/test/DefaultBtMessageDispatcherTest.cc index 33a62a99..7927fb45 100644 --- a/test/DefaultBtMessageDispatcherTest.cc +++ b/test/DefaultBtMessageDispatcherTest.cc @@ -99,12 +99,10 @@ public: return sendCalled; } - virtual void handleEvent(const SharedHandle& event) { - BtCancelSendingPieceEvent* e = - dynamic_cast(event.get()); - if(e) { - doCancelActionCalled = true; - } + virtual void onCancelSendingPieceEvent + (const BtCancelSendingPieceEvent& event) + { + doCancelActionCalled = true; } bool isDoCancelActionCalled() const { diff --git a/test/MockBtMessage.h b/test/MockBtMessage.h index d4010b62..c0c30353 100644 --- a/test/MockBtMessage.h +++ b/test/MockBtMessage.h @@ -84,7 +84,13 @@ public: return false; } - virtual void handleEvent(const SharedHandle& event) {} + virtual void onAbortOutstandingRequestEvent + (const BtAbortOutstandingRequestEvent& event) {} + + virtual void onCancelSendingPieceEvent + (const BtCancelSendingPieceEvent& event) {} + + virtual void onChokingEvent(const BtChokingEvent& event) {} virtual void onQueued() {}