Peer.h 6.9 KB

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