Peer.h 5.9 KB

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