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