DefaultPeerStorage.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_PEER_STORAGE_H
  36. #define D_DEFAULT_PEER_STORAGE_H
  37. #include "PeerStorage.h"
  38. #include <string>
  39. #include <map>
  40. #include "TimerA2.h"
  41. namespace aria2 {
  42. class BtRuntime;
  43. class BtSeederStateChoke;
  44. class BtLeecherStateChoke;
  45. class PieceStorage;
  46. class DefaultPeerStorage : public PeerStorage {
  47. private:
  48. std::shared_ptr<BtRuntime> btRuntime_;
  49. std::shared_ptr<PieceStorage> pieceStorage_;
  50. size_t maxPeerListSize_;
  51. // This contains ip address and port pair and is used to ensure that
  52. // no duplicate peers are stored.
  53. std::set<std::pair<std::string, uint16_t>> uniqPeers_;
  54. // Unused (not connected) peers, sorted by last added.
  55. std::deque<std::shared_ptr<Peer>> unusedPeers_;
  56. // The set of used peers. Some of them are not connected yet. To
  57. // know it is connected or not, call Peer::isActive().
  58. PeerSet usedPeers_;
  59. std::deque<std::shared_ptr<Peer>> droppedPeers_;
  60. std::unique_ptr<BtSeederStateChoke> seederStateChoke_;
  61. std::unique_ptr<BtLeecherStateChoke> leecherStateChoke_;
  62. Timer lastTransferStatMapUpdated_;
  63. std::map<std::string, Timer> badPeers_;
  64. Timer lastBadPeerCleaned_;
  65. bool isPeerAlreadyAdded(const std::shared_ptr<Peer>& peer);
  66. void addUniqPeer(const std::shared_ptr<Peer>& peer);
  67. void addDroppedPeer(const std::shared_ptr<Peer>& peer);
  68. public:
  69. DefaultPeerStorage();
  70. virtual ~DefaultPeerStorage();
  71. // TODO We need addAndCheckoutPeer for incoming peers
  72. virtual bool addPeer(const std::shared_ptr<Peer>& peer) CXX11_OVERRIDE;
  73. virtual size_t countAllPeer() const CXX11_OVERRIDE;
  74. std::shared_ptr<Peer> getPeer(const std::string& ipaddr, uint16_t port) const;
  75. virtual void
  76. addPeer(const std::vector<std::shared_ptr<Peer>>& peers) CXX11_OVERRIDE;
  77. const std::deque<std::shared_ptr<Peer>>& getUnusedPeers();
  78. virtual const PeerSet& getUsedPeers() CXX11_OVERRIDE;
  79. virtual const std::deque<std::shared_ptr<Peer>>&
  80. getDroppedPeers() CXX11_OVERRIDE;
  81. virtual bool isPeerAvailable() CXX11_OVERRIDE;
  82. virtual bool isBadPeer(const std::string& ipaddr) CXX11_OVERRIDE;
  83. virtual void addBadPeer(const std::string& ipaddr) CXX11_OVERRIDE;
  84. virtual std::shared_ptr<Peer> checkoutPeer(cuid_t cuid) CXX11_OVERRIDE;
  85. virtual void returnPeer(const std::shared_ptr<Peer>& peer) CXX11_OVERRIDE;
  86. virtual bool chokeRoundIntervalElapsed() CXX11_OVERRIDE;
  87. virtual void executeChoke() CXX11_OVERRIDE;
  88. void deleteUnusedPeer(size_t delSize);
  89. void onErasingPeer(const std::shared_ptr<Peer>& peer);
  90. void onReturningPeer(const std::shared_ptr<Peer>& peer);
  91. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  92. void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
  93. void setMaxPeerListSize(size_t maxPeerListSize)
  94. {
  95. maxPeerListSize_ = maxPeerListSize;
  96. }
  97. };
  98. } // namespace aria2
  99. #endif // D_DEFAULT_PEER_STORAGE_H