Peer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. public:
  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. private:
  59. std::string id;
  60. cuid_t _cuid;
  61. unsigned char _peerId[PEER_ID_LENGTH];
  62. Timer _firstContactTime;
  63. Timer _badConditionStartTime;
  64. bool _seeder;
  65. PeerSessionResource* _res;
  66. // If true, port is assumed not to be a listening port.
  67. bool _incoming;
  68. // If true, this peer is from local network.
  69. bool _localPeer;
  70. // Before calling updateSeeder(), make sure that
  71. // allocateSessionResource() is called and _res is created.
  72. // Otherwise assertion fails.
  73. void updateSeeder();
  74. public:
  75. Peer(std::string ipaddr, uint16_t port, bool incoming = false);
  76. ~Peer();
  77. bool operator==(const Peer& p)
  78. {
  79. return id == p.id;
  80. }
  81. bool operator!=(const Peer& p)
  82. {
  83. return !(*this == p);
  84. }
  85. void resetStatus();
  86. void usedBy(cuid_t cuid);
  87. cuid_t usedBy() const
  88. {
  89. return _cuid;
  90. }
  91. bool unused() const
  92. {
  93. return _cuid == 0;
  94. }
  95. // Returns true iff _res != 0.
  96. bool isActive() const
  97. {
  98. return _res != 0;
  99. }
  100. void setPeerId(const unsigned char* peerId);
  101. const unsigned char* getPeerId() const
  102. {
  103. return _peerId;
  104. }
  105. bool isSeeder() const
  106. {
  107. return _seeder;
  108. }
  109. const std::string& getID() const
  110. {
  111. return id;
  112. }
  113. void startBadCondition();
  114. bool isGood() const;
  115. void allocateSessionResource(size_t pieceLength, uint64_t totalLength);
  116. void releaseSessionResource();
  117. const Timer& getFirstContactTime() const
  118. {
  119. return _firstContactTime;
  120. }
  121. void setFirstContactTime(const Timer& time);
  122. const Timer& getBadConditionStartTime() const
  123. {
  124. return _badConditionStartTime;
  125. }
  126. // Before calling following member functions, make sure that
  127. // allocateSessionResource() is called and _res is created.
  128. // Otherwise assertion fails.
  129. // localhost is choking this peer
  130. bool amChoking() const;
  131. void amChoking(bool b) const;
  132. // localhost is interested in this peer
  133. bool amInterested() const;
  134. void amInterested(bool b) const;
  135. // this peer is choking localhost
  136. bool peerChoking() const;
  137. void peerChoking(bool b) const;
  138. // this peer is interested in localhost
  139. bool peerInterested() const;
  140. void peerInterested(bool b);
  141. // this peer should be choked
  142. bool chokingRequired() const;
  143. void chokingRequired(bool b);
  144. // this peer is eligible for unchoking optionally.
  145. bool optUnchoking() const;
  146. void optUnchoking(bool b);
  147. // this peer is snubbing.
  148. bool snubbing() const;
  149. void snubbing(bool b);
  150. void updateUploadLength(size_t bytes);
  151. void updateDownloadLength(size_t bytes);
  152. /**
  153. * Returns the transfer rate from localhost to remote host.
  154. */
  155. unsigned int calculateUploadSpeed();
  156. /**
  157. * Returns the transfer rate from remote host to localhost.
  158. */
  159. unsigned int calculateDownloadSpeed();
  160. /**
  161. * Returns the number of bytes uploaded to the remote host.
  162. */
  163. uint64_t getSessionUploadLength() const;
  164. /**
  165. * Returns the number of bytes downloaded from the remote host.
  166. */
  167. uint64_t getSessionDownloadLength() const;
  168. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  169. const unsigned char* getBitfield() const;
  170. size_t getBitfieldLength() const;
  171. void setAllBitfield();
  172. /**
  173. * operation = 1: set index-th bit to 1
  174. * operation = 0: set index-th bit to 0
  175. */
  176. void updateBitfield(size_t index, int operation);
  177. void setFastExtensionEnabled(bool enabled);
  178. bool isFastExtensionEnabled() const;
  179. void addPeerAllowedIndex(size_t index);
  180. bool isInPeerAllowedIndexSet(size_t index) const;
  181. size_t countPeerAllowedIndexSet() const;
  182. const std::vector<size_t>& getPeerAllowedIndexSet() const;
  183. void addAmAllowedIndex(size_t index);
  184. bool isInAmAllowedIndexSet(size_t index) const;
  185. void setExtendedMessagingEnabled(bool enabled);
  186. bool isExtendedMessagingEnabled() const;
  187. void setDHTEnabled(bool enabled);
  188. bool isDHTEnabled() const;
  189. bool shouldBeChoking() const;
  190. bool hasPiece(size_t index) const;
  191. uint8_t getExtensionMessageID(const std::string& name) const;
  192. std::string getExtensionName(uint8_t id) const;
  193. void setExtension(const std::string& name, uint8_t id);
  194. const Timer& getLastDownloadUpdate() const;
  195. const Timer& getLastAmUnchoking() const;
  196. uint64_t getCompletedLength() const;
  197. bool isIncomingPeer() const
  198. {
  199. return _incoming;
  200. }
  201. void setIncomingPeer(bool incoming);
  202. bool isLocalPeer() const
  203. {
  204. return _localPeer;
  205. }
  206. void setLocalPeer(bool flag)
  207. {
  208. _localPeer = flag;
  209. }
  210. void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dpt);
  211. size_t countOutstandingUpload() const;
  212. };
  213. template<typename InputIterator>
  214. size_t countSeeder(InputIterator first, InputIterator last)
  215. {
  216. return std::count_if(first, last, mem_fun_sh(&Peer::isSeeder));
  217. }
  218. } // namespace aria2
  219. #endif // _D_PEER_H_