Forráskód Böngészése

2009-04-21 Tatsuhiro Tsujikawa <[email protected]>

	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
Tatsuhiro Tsujikawa 16 éve
szülő
commit
afdd61f9d1

+ 26 - 0
ChangeLog

@@ -1,3 +1,29 @@
+2009-04-21  Tatsuhiro Tsujikawa  <[email protected]>
+
+	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  <[email protected]>
 
 	Code cleanup

+ 0 - 59
src/AbstractBtEventListener.h

@@ -1,59 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL.  If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so.  If you
- * do not wish to do so, delete this exception statement from your
- * version.  If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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<BtEvent>& event) = 0;
-
-  virtual void handleEventInternal(const SharedHandle<BtEvent>& event) = 0;
-
-  virtual void handleEvent(const SharedHandle<BtEvent>& event) {
-    if(canHandle(event)) {
-      handleEventInternal(event);
-    }
-  }
-};
-
-} // namespace aria2
-
-#endif // _D_ABSTRACT_BT_EVENT_LISTENER_H_

+ 0 - 15
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<std::string>& errors)
   }
 }
 
-void AbstractBtMessage::handleEvent(const SharedHandle<BtEvent>& event)
-{
-  for(std::deque<SharedHandle<BtEventListener> >::iterator itr = listeners.begin();
-      itr != listeners.end(); ++itr) {
-    (*itr)->handleEvent(event);
-  }
-}
-
-void
-AbstractBtMessage::addEventListener(const SharedHandle<BtEventListener>& listener)
-{
-  listeners.push_back(listener);
-}
-
 void
 AbstractBtMessage::setBtMessageValidator(const SharedHandle<BtMessageValidator>& validator) {
   this->validator = validator;

+ 6 - 5
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<BtMessageValidator> validator;
 
-  std::deque<SharedHandle<BtEventListener> > 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<BtEvent>& event);
+  virtual void onAbortOutstandingRequestEvent
+  (const BtAbortOutstandingRequestEvent& event) {}
+
+  virtual void onCancelSendingPieceEvent
+  (const BtCancelSendingPieceEvent& event) {}
 
-  void addEventListener(const SharedHandle<BtEventListener>& listener);
+  virtual void onChokingEvent(const BtChokingEvent& event) {}
 
   void setBtMessageValidator(const SharedHandle<BtMessageValidator>& validator);
 

+ 7 - 14
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> piece;
 public:
-  BtAbortOutstandingRequestEvent(const SharedHandle<Piece>& piece):piece(piece) {}
-
-  SharedHandle<Piece> getPiece() const {
-    return piece;
-  }
+  BtAbortOutstandingRequestEvent(const SharedHandle<Piece>& piece):
+    piece(piece) {}
 
-  void setPiece(const SharedHandle<Piece>& piece) {
-    this->piece = piece;
-  }
+  SharedHandle<Piece> getPiece() const { return piece; }
 };
 
-typedef SharedHandle<BtAbortOutstandingRequestEvent> BtAbortOutstandingRequestEventHandle;
-
 } // namespace aria2
 
 #endif // _D_BT_ABORT_OUTSTANDING_REQUEST_EVENT_H_

+ 5 - 27
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<BtCancelSendingPieceEvent> BtCancelSendingPieceEventHandle;
-
 } // namespace aria2
 
 #endif // _D_BT_CANCEL_SENDING_PIECE_EVENT_H_

+ 0 - 50
src/BtChokedEvent.h

@@ -1,50 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL.  If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so.  If you
- * do not wish to do so, delete this exception statement from your
- * version.  If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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<BtChokedEvent> BtChokedEventHandle;
-
-} // namespace aria2
-
-#endif // _D_BT_CHOKED_EVENT_H_

+ 2 - 5
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<BtChokingEvent> BtChokingEventHandle;
+class BtChokingEvent {};
 
 } // namespace aria2
 

+ 0 - 51
src/BtEvent.h

@@ -1,51 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL.  If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so.  If you
- * do not wish to do so, delete this exception statement from your
- * version.  If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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<BtEvent> BtEventHandle;
-
-} // namespace aria2
-
-#endif // _D_BT_EVENT_H_

+ 0 - 58
src/BtEventListener.h

@@ -1,58 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL.  If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so.  If you
- * do not wish to do so, delete this exception statement from your
- * version.  If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#ifndef _D_BT_EVENT_LISTENER_H_
-#define _D_BT_EVENT_LISTENER_H_
-
-#include "common.h"
-#include "SharedHandle.h"
-#include <deque>
-
-namespace aria2 {
-
-class BtEvent;
-
-class BtEventListener {
-public:
-  virtual ~BtEventListener() {}
-
-  virtual void handleEvent(const SharedHandle<BtEvent>& event) = 0;
-};
-
-typedef SharedHandle<BtEventListener> BtEventListenerHandle;
-typedef std::deque<BtEventListenerHandle> BtEventListeners;
-
-} // namespace aria2
-
-#endif // _D_BT_EVENT_LISTENER_H_

+ 13 - 2
src/BtMessage.h

@@ -36,10 +36,15 @@
 #define _D_BT_MESSAGE_H_
 
 #include "common.h"
-#include "SharedHandle.h"
+
 #include <string>
 #include <deque>
 
+#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<std::string>& errors) = 0;
 
-  virtual void handleEvent(const SharedHandle<BtEvent>& 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;
 

+ 8 - 26
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<BtChokingEvent*>(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<BtCancelSendingPieceEvent*>(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()) {

+ 3 - 38
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>& 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>& btEvent);
-
-    virtual void handleEventInternal(const SharedHandle<BtEvent>& btEvent);
-  };
-
-  typedef SharedHandle<BtChokingEventListener> BtChokingEventListenerHandle;
-
-  class BtCancelSendingPieceEventListener : public AbstractBtEventListener {
-  private:
-    BtPieceMessage* message;
-  public:
-    BtCancelSendingPieceEventListener(BtPieceMessage* message):message(message) {}
-
-    virtual bool canHandle(const SharedHandle<BtEvent>& btEvent);
-
-    virtual void handleEventInternal(const SharedHandle<BtEvent>& btEvent);
-  };
-
-  typedef SharedHandle<BtCancelSendingPieceEventListener> 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<BtEventListener> listener(new BtChokingEventListener(this));
-      addEventListener(listener);
-    }
-    {
-      SharedHandle<BtEventListener> listener(new BtCancelSendingPieceEventListener(this));
-      addEventListener(listener);
-    }
   }
 
   virtual ~BtPieceMessage() {
@@ -148,9 +112,10 @@ public:
 
   virtual std::string toString() const;
 
-  void handleChokingEvent(const SharedHandle<BtEvent>& event);
+  virtual void onChokingEvent(const BtChokingEvent& event);
   
-  void handleCancelSendingPieceEvent(const SharedHandle<BtEvent>& event);
+  virtual void onCancelSendingPieceEvent
+  (const BtCancelSendingPieceEvent& event);
 };
 
 } // namespace aria2

+ 4 - 23
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<BtAbortOutstandingRequestEvent*>(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;
   }
 }

+ 3 - 23
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<BtEvent>& event);
-
-    virtual void handleEventInternal(const SharedHandle<BtEvent>& event);
-  };
-
-  typedef SharedHandle<BtAbortOutstandingRequestEventListener>
-  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<BtEventListener> 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<BtEvent>& event);
+  virtual void onAbortOutstandingRequestEvent
+  (const BtAbortOutstandingRequestEvent& event);
 };
 
 } // namespace aria2

+ 11 - 26
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<BtEvent> _event;
-public:
-  HandleEvent(const SharedHandle<BtEvent>& event):_event(event) {}
-
-  void operator()(const SharedHandle<BtMessage>& 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 {

+ 0 - 4
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\

+ 10 - 16
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 \

+ 25 - 0
src/a2functional.h

@@ -88,6 +88,31 @@ mem_fun_sh(ReturnType (ClassType::*f)() const)
   return const_mem_fun_sh_t<ReturnType, ClassType>(f);
 };
 
+// mem_fun1_t for SharedHandle
+template<typename ReturnType, typename ClassType, typename ArgType>
+class mem_fun1_sh_t:public std::binary_function<SharedHandle<ClassType>,
+						ArgType,
+						ReturnType>
+{
+private:
+  ReturnType (ClassType::*f)(ArgType);
+
+public:
+  mem_fun1_sh_t(ReturnType (ClassType::*f)(ArgType)):f(f) {}
+
+  ReturnType operator()(const SharedHandle<ClassType>& x, ArgType a) const
+  {
+    return (x.get()->*f)(a);
+  }
+};
+
+template<typename ReturnType, typename ClassType, typename ArgType>
+mem_fun1_sh_t<ReturnType, ClassType, ArgType>
+mem_fun_sh(ReturnType (ClassType::*f)(ArgType))
+{
+  return mem_fun1_sh_t<ReturnType, ClassType, ArgType>(f);
+};
+
 template<class BinaryOp, class UnaryOp>
 class adopt2nd_t:public std::binary_function<typename BinaryOp::first_argument_type,
 					     typename UnaryOp::argument_type,

+ 12 - 19
test/BtPieceMessageTest.cc

@@ -156,7 +156,7 @@ void BtPieceMessageTest::testChokingEvent() {
   CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
   CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
 
-  msg->handleEvent(SharedHandle<BtEvent>(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<BtEvent>(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<BtEvent>(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<BtEvent>(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<BtEvent>(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<BtEvent>(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<BtEvent>(new BtCancelSendingPieceEvent(0, 1024, 16*1024)));
+  msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(0, 1024, 16*1024));
 
   CPPUNIT_ASSERT(!msg->isInvalidate());
   
-  msg->handleEvent
-    (SharedHandle<BtEvent>(new BtCancelSendingPieceEvent(1, 0, 16*1024)));
+  msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 0, 16*1024));
 
   CPPUNIT_ASSERT(!msg->isInvalidate());
 
-  msg->handleEvent
-    (SharedHandle<BtEvent>(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<BtEvent>(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<BtEvent>(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<BtEvent>(new BtCancelSendingPieceEvent(1, 1024, 16*1024)));
+  msg->onCancelSendingPieceEvent(BtCancelSendingPieceEvent(1, 1024, 16*1024));
 
   CPPUNIT_ASSERT(!msg->isInvalidate());
 }

+ 4 - 17
test/BtRequestMessageTest.cc

@@ -236,47 +236,34 @@ void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionD
 
 void BtRequestMessageTest::testHandleAbortRequestEvent() {
   SharedHandle<Piece> piece(new Piece(1, 16*1024));
-  SharedHandle<BtAbortOutstandingRequestEvent> 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> piece(new Piece(2, 16*1024));
-  SharedHandle<BtAbortOutstandingRequestEvent> 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> piece(new Piece(1, 16*1024));
-  SharedHandle<BtAbortOutstandingRequestEvent> 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> piece(new Piece(1, 16*1024));
-  SharedHandle<BtAbortOutstandingRequestEvent> 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());
 }
 

+ 4 - 6
test/DefaultBtMessageDispatcherTest.cc

@@ -99,12 +99,10 @@ public:
       return sendCalled;
     }
 
-    virtual void handleEvent(const SharedHandle<BtEvent>& event) {
-      BtCancelSendingPieceEvent* e =
-	dynamic_cast<BtCancelSendingPieceEvent*>(event.get());
-      if(e) {
-	doCancelActionCalled = true;
-      }
+    virtual void onCancelSendingPieceEvent
+    (const BtCancelSendingPieceEvent& event)
+    {
+      doCancelActionCalled = true;
     }
 
     bool isDoCancelActionCalled() const {

+ 7 - 1
test/MockBtMessage.h

@@ -84,7 +84,13 @@ public:
     return false;
   }
 
-  virtual void handleEvent(const SharedHandle<BtEvent>& event) {}
+  virtual void onAbortOutstandingRequestEvent
+  (const BtAbortOutstandingRequestEvent& event) {}
+
+  virtual void onCancelSendingPieceEvent
+  (const BtCancelSendingPieceEvent& event) {}
+
+  virtual void onChokingEvent(const BtChokingEvent& event) {}
 
   virtual void onQueued() {}