Peer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_PEER_H_
  36. #define _D_PEER_H_
  37. #include "common.h"
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include <algorithm>
  42. #include "SharedHandle.h"
  43. #include "TimerA2.h"
  44. #include "BtConstants.h"
  45. #include "PeerStat.h"
  46. #include "a2functional.h"
  47. #include "Command.h"
  48. namespace aria2 {
  49. class PeerSessionResource;
  50. class BtMessageDispatcher;
  51. class Peer {
  52. private:
  53. std::string ipaddr_;
  54. // TCP port of the other end of communication. If incoming_ is
  55. // true, then this port is not a port the peer is listening to and
  56. // we cannot connect to it.
  57. uint16_t port_;
  58. std::string id_;
  59. cuid_t cuid_;
  60. unsigned char peerId_[PEER_ID_LENGTH];
  61. Timer firstContactTime_;
  62. Timer badConditionStartTime_;
  63. bool seeder_;
  64. PeerSessionResource* res_;
  65. // If true, port is assumed not to be a listening port.
  66. bool incoming_;
  67. // If true, this peer is from local network.
  68. bool localPeer_;
  69. // If true, this peer is disconnected gracefully.
  70. bool disconnectedGracefully_;
  71. // Before calling updateSeeder(), make sure that
  72. // allocateSessionResource() is called and res_ is created.
  73. // Otherwise assertion fails.
  74. void updateSeeder();
  75. public:
  76. Peer(std::string ipaddr, uint16_t port, bool incoming = false);
  77. ~Peer();
  78. bool operator==(const Peer& p)
  79. {
  80. return id_ == p.id_;
  81. }
  82. bool operator!=(const Peer& p)
  83. {
  84. return !(*this == p);
  85. }
  86. void resetStatus();
  87. const std::string& getIPAddress() const
  88. {
  89. return ipaddr_;
  90. }
  91. uint16_t getPort() const
  92. {
  93. return port_;
  94. }
  95. void setPort(uint16_t port)
  96. {
  97. port_ = port;
  98. }
  99. void usedBy(cuid_t cuid);
  100. cuid_t usedBy() const
  101. {
  102. return cuid_;
  103. }
  104. bool unused() const
  105. {
  106. return cuid_ == 0;
  107. }
  108. // Returns true iff res_ != 0.
  109. bool isActive() const
  110. {
  111. return res_ != 0;
  112. }
  113. void setPeerId(const unsigned char* peerId);
  114. const unsigned char* getPeerId() const
  115. {
  116. return peerId_;
  117. }
  118. bool isSeeder() const
  119. {
  120. return seeder_;
  121. }
  122. const std::string& getID() const
  123. {
  124. return id_;
  125. }
  126. void startBadCondition();
  127. bool isGood() const;
  128. void allocateSessionResource(size_t pieceLength, uint64_t totalLength);
  129. void reconfigureSessionResource(size_t pieceLength, uint64_t totalLength);
  130. void releaseSessionResource();
  131. const Timer& getFirstContactTime() const
  132. {
  133. return firstContactTime_;
  134. }
  135. void setFirstContactTime(const Timer& time);
  136. const Timer& getBadConditionStartTime() const
  137. {
  138. return badConditionStartTime_;
  139. }
  140. // Before calling following member functions, make sure that
  141. // allocateSessionResource() is called and res_ is created.
  142. // Otherwise assertion fails.
  143. // localhost is choking this peer
  144. bool amChoking() const;
  145. void amChoking(bool b) const;
  146. // localhost is interested in this peer
  147. bool amInterested() const;
  148. void amInterested(bool b) const;
  149. // this peer is choking localhost
  150. bool peerChoking() const;
  151. void peerChoking(bool b) const;
  152. // this peer is interested in localhost
  153. bool peerInterested() const;
  154. void peerInterested(bool b);
  155. // this peer should be choked
  156. bool chokingRequired() const;
  157. void chokingRequired(bool b);
  158. // this peer is eligible for unchoking optionally.
  159. bool optUnchoking() const;
  160. void optUnchoking(bool b);
  161. // this peer is snubbing.
  162. bool snubbing() const;
  163. void snubbing(bool b);
  164. void updateUploadLength(size_t bytes);
  165. void updateDownloadLength(size_t bytes);
  166. /**
  167. * Returns the transfer rate from localhost to remote host.
  168. */
  169. unsigned int calculateUploadSpeed();
  170. /**
  171. * Returns the transfer rate from remote host to localhost.
  172. */
  173. unsigned int calculateDownloadSpeed();
  174. /**
  175. * Returns the number of bytes uploaded to the remote host.
  176. */
  177. uint64_t getSessionUploadLength() const;
  178. /**
  179. * Returns the number of bytes downloaded from the remote host.
  180. */
  181. uint64_t getSessionDownloadLength() const;
  182. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  183. const unsigned char* getBitfield() const;
  184. size_t getBitfieldLength() const;
  185. void setAllBitfield();
  186. /**
  187. * operation = 1: set index-th bit to 1
  188. * operation = 0: set index-th bit to 0
  189. */
  190. void updateBitfield(size_t index, int operation);
  191. void setFastExtensionEnabled(bool enabled);
  192. bool isFastExtensionEnabled() const;
  193. void addPeerAllowedIndex(size_t index);
  194. bool isInPeerAllowedIndexSet(size_t index) const;
  195. size_t countPeerAllowedIndexSet() const;
  196. const std::vector<size_t>& getPeerAllowedIndexSet() const;
  197. void addAmAllowedIndex(size_t index);
  198. bool isInAmAllowedIndexSet(size_t index) const;
  199. void setExtendedMessagingEnabled(bool enabled);
  200. bool isExtendedMessagingEnabled() const;
  201. void setDHTEnabled(bool enabled);
  202. bool isDHTEnabled() const;
  203. bool shouldBeChoking() const;
  204. bool hasPiece(size_t index) const;
  205. uint8_t getExtensionMessageID(const std::string& name) const;
  206. std::string getExtensionName(uint8_t id) const;
  207. void setExtension(const std::string& name, uint8_t id);
  208. const Timer& getLastDownloadUpdate() const;
  209. const Timer& getLastAmUnchoking() const;
  210. uint64_t getCompletedLength() const;
  211. bool isIncomingPeer() const
  212. {
  213. return incoming_;
  214. }
  215. void setIncomingPeer(bool incoming);
  216. bool isLocalPeer() const
  217. {
  218. return localPeer_;
  219. }
  220. void setLocalPeer(bool flag)
  221. {
  222. localPeer_ = flag;
  223. }
  224. bool isDisconnectedGracefully() const
  225. {
  226. return disconnectedGracefully_;
  227. }
  228. void setDisconnectedGracefully(bool f)
  229. {
  230. disconnectedGracefully_ = f;
  231. }
  232. void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dpt);
  233. size_t countOutstandingUpload() const;
  234. };
  235. template<typename InputIterator>
  236. size_t countSeeder(InputIterator first, InputIterator last)
  237. {
  238. return std::count_if(first, last, mem_fun_sh(&Peer::isSeeder));
  239. }
  240. } // namespace aria2
  241. #endif // _D_PEER_H_