DefaultBtMessageDispatcher.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_DEFAULT_BT_MESSAGE_DISPATCHER_H_
  36. #define _D_DEFAULT_BT_MESSAGE_DISPATCHER_H_
  37. #include "BtMessageDispatcher.h"
  38. #include "BtContext.h"
  39. #include "PeerStorage.h"
  40. #include "PieceStorage.h"
  41. #include "RequestSlot.h"
  42. #include "BtMessage.h"
  43. #include "Peer.h"
  44. #include "Option.h"
  45. #include "LogFactory.h"
  46. #include "Logger.h"
  47. #include "BtRegistry.h"
  48. class DefaultBtMessageDispatcher : public BtMessageDispatcher {
  49. private:
  50. int32_t cuid;
  51. BtMessages messageQueue;
  52. RequestSlots requestSlots;
  53. BtContextHandle btContext;
  54. PeerStorageHandle peerStorage;
  55. PieceStorageHandle pieceStorage;
  56. PeerHandle peer;
  57. const Option* option;
  58. const Logger* logger;
  59. public:
  60. DefaultBtMessageDispatcher():
  61. cuid(0),
  62. btContext(0),
  63. peerStorage(0),
  64. pieceStorage(0),
  65. peer(0),
  66. option(0),
  67. logger(LogFactory::getInstance())
  68. {
  69. logger->debug("DefaultBtMessageDispatcher::instantiated");
  70. }
  71. virtual ~DefaultBtMessageDispatcher()
  72. {
  73. logger->debug("DefaultBtMessageDispatcher::deleted");
  74. }
  75. virtual void addMessageToQueue(const BtMessageHandle& btMessage);
  76. virtual void addMessageToQueue(const BtMessages& btMessages);
  77. virtual void sendMessages();
  78. virtual void doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t blockLength);
  79. virtual void doCancelSendingPieceAction(const PieceHandle& piece);
  80. virtual void doAbortOutstandingRequestAction(const PieceHandle& piece);
  81. virtual void doChokedAction();
  82. virtual void doChokingAction();
  83. virtual void checkRequestSlotAndDoNecessaryThing();
  84. virtual bool isSendingInProgress();
  85. virtual uint32_t countMessageInQueue() {
  86. return messageQueue.size();
  87. }
  88. virtual uint32_t countOutstandingRequest();
  89. virtual bool isOutstandingRequest(uint32_t index, uint32_t blockIndex);
  90. virtual RequestSlot getOutstandingRequest(uint32_t index, uint32_t begin, uint32_t blockLength) {
  91. for(RequestSlots::iterator itr = requestSlots.begin();
  92. itr != requestSlots.end(); itr++) {
  93. if(itr->getIndex() == index &&
  94. itr->getBegin() == begin &&
  95. itr->getLength() == blockLength) {
  96. return *itr;
  97. }
  98. }
  99. return RequestSlot::nullSlot;
  100. }
  101. virtual void removeOutstandingRequest(const RequestSlot& slot) {
  102. RequestSlots temp;
  103. remove_copy(requestSlots.begin(), requestSlots.end(), back_inserter(temp), slot);
  104. requestSlots = temp;
  105. }
  106. virtual void addOutstandingRequest(const RequestSlot& requestSlot) {
  107. if(!isOutstandingRequest(requestSlot.getIndex(), requestSlot.getBlockIndex())) {
  108. requestSlots.push_back(requestSlot);
  109. }
  110. }
  111. BtMessages& getMessageQueue() {
  112. return messageQueue;
  113. }
  114. void setOption(const Option* option) {
  115. this->option = option;
  116. }
  117. const Option* getOption() const {
  118. return option;
  119. }
  120. RequestSlots& getRequestSlots() {
  121. return requestSlots;
  122. }
  123. void setPeer(const PeerHandle& peer) {
  124. this->peer = peer;
  125. }
  126. void setBtContext(const BtContextHandle& btContext) {
  127. this->btContext = btContext;
  128. this->pieceStorage = PIECE_STORAGE(btContext);
  129. this->peerStorage = PEER_STORAGE(btContext);
  130. }
  131. void setCuid(int32_t cuid) {
  132. this->cuid = cuid;
  133. }
  134. };
  135. typedef SharedHandle<DefaultBtMessageDispatcher> DefaultBtMessageDispatcherHandle;
  136. #endif // _D_DEFAULT_BT_MESSAGE_DISPATCHER_H_