Peer.h 6.4 KB

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