PeerSessionResource.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "PeerSessionResource.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "BitfieldMan.h"
  39. #include "A2STR.h"
  40. #include "BtMessageDispatcher.h"
  41. #include "wallclock.h"
  42. namespace aria2 {
  43. PeerSessionResource::PeerSessionResource(int32_t pieceLength, int64_t totalLength)
  44. :
  45. amChoking_(true),
  46. amInterested_(false),
  47. peerChoking_(true),
  48. peerInterested_(false),
  49. chokingRequired_(true),
  50. optUnchoking_(false),
  51. snubbing_(false),
  52. bitfieldMan_(new BitfieldMan(pieceLength, totalLength)),
  53. fastExtensionEnabled_(false),
  54. extendedMessagingEnabled_(false),
  55. dhtEnabled_(false),
  56. lastDownloadUpdate_(0),
  57. lastAmUnchoking_(0),
  58. dispatcher_(0)
  59. {}
  60. PeerSessionResource::~PeerSessionResource()
  61. {
  62. delete bitfieldMan_;
  63. }
  64. void PeerSessionResource::amChoking(bool b)
  65. {
  66. amChoking_ = b;
  67. if(!b) {
  68. lastAmUnchoking_ = global::wallclock();
  69. }
  70. }
  71. void PeerSessionResource::amInterested(bool b)
  72. {
  73. amInterested_ = b;
  74. }
  75. void PeerSessionResource::peerChoking(bool b)
  76. {
  77. peerChoking_ = b;
  78. }
  79. void PeerSessionResource::peerInterested(bool b)
  80. {
  81. peerInterested_ = b;
  82. }
  83. void PeerSessionResource::chokingRequired(bool b)
  84. {
  85. chokingRequired_ = b;
  86. }
  87. void PeerSessionResource::optUnchoking(bool b)
  88. {
  89. optUnchoking_ = b;
  90. }
  91. bool PeerSessionResource::shouldBeChoking() const
  92. {
  93. if(optUnchoking_) {
  94. return false;
  95. }
  96. return chokingRequired_;
  97. }
  98. void PeerSessionResource::snubbing(bool b)
  99. {
  100. snubbing_ = b;
  101. if(snubbing_) {
  102. chokingRequired(true);
  103. optUnchoking(false);
  104. }
  105. }
  106. bool PeerSessionResource::hasAllPieces() const
  107. {
  108. return bitfieldMan_->isAllBitSet();
  109. }
  110. void PeerSessionResource::updateBitfield(size_t index, int operation)
  111. {
  112. if(operation == 1) {
  113. bitfieldMan_->setBit(index);
  114. } else if(operation == 0) {
  115. bitfieldMan_->unsetBit(index);
  116. }
  117. }
  118. void PeerSessionResource::setBitfield
  119. (const unsigned char* bitfield, size_t bitfieldLength)
  120. {
  121. bitfieldMan_->setBitfield(bitfield, bitfieldLength);
  122. }
  123. const unsigned char* PeerSessionResource::getBitfield() const
  124. {
  125. return bitfieldMan_->getBitfield();
  126. }
  127. size_t PeerSessionResource::getBitfieldLength() const
  128. {
  129. return bitfieldMan_->getBitfieldLength();
  130. }
  131. bool PeerSessionResource::hasPiece(size_t index) const
  132. {
  133. return bitfieldMan_->isBitSet(index);
  134. }
  135. void PeerSessionResource::markSeeder()
  136. {
  137. bitfieldMan_->setAllBit();
  138. }
  139. void PeerSessionResource::fastExtensionEnabled(bool b)
  140. {
  141. fastExtensionEnabled_ = b;
  142. }
  143. const std::set<size_t>& PeerSessionResource::peerAllowedIndexSet() const
  144. {
  145. return peerAllowedIndexSet_;
  146. }
  147. void PeerSessionResource::addPeerAllowedIndex(size_t index)
  148. {
  149. peerAllowedIndexSet_.insert(index);
  150. }
  151. bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
  152. {
  153. return peerAllowedIndexSet_.count(index) == 1;
  154. }
  155. void PeerSessionResource::addAmAllowedIndex(size_t index)
  156. {
  157. amAllowedIndexSet_.insert(index);
  158. }
  159. bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
  160. {
  161. return amAllowedIndexSet_.count(index) == 1;
  162. }
  163. void PeerSessionResource::extendedMessagingEnabled(bool b)
  164. {
  165. extendedMessagingEnabled_ = b;
  166. }
  167. uint8_t
  168. PeerSessionResource::getExtensionMessageID(const std::string& name) const
  169. {
  170. Extensions::const_iterator itr = extensions_.find(name);
  171. if(itr == extensions_.end()) {
  172. return 0;
  173. } else {
  174. return (*itr).second;
  175. }
  176. }
  177. std::string PeerSessionResource::getExtensionName(uint8_t id) const
  178. {
  179. for(Extensions::const_iterator itr = extensions_.begin(),
  180. eoi = extensions_.end(); itr != eoi; ++itr) {
  181. const Extensions::value_type& p = *itr;
  182. if(p.second == id) {
  183. return p.first;
  184. }
  185. }
  186. return A2STR::NIL;
  187. }
  188. void PeerSessionResource::addExtension(const std::string& name, uint8_t id)
  189. {
  190. extensions_[name] = id;
  191. }
  192. void PeerSessionResource::dhtEnabled(bool b)
  193. {
  194. dhtEnabled_ = b;
  195. }
  196. int64_t PeerSessionResource::uploadLength() const
  197. {
  198. return peerStat_.getSessionUploadLength();
  199. }
  200. void PeerSessionResource::updateUploadLength(int32_t bytes)
  201. {
  202. peerStat_.updateUploadLength(bytes);
  203. }
  204. int64_t PeerSessionResource::downloadLength() const
  205. {
  206. return peerStat_.getSessionDownloadLength();
  207. }
  208. void PeerSessionResource::updateDownloadLength(int32_t bytes)
  209. {
  210. peerStat_.updateDownloadLength(bytes);
  211. lastDownloadUpdate_ = global::wallclock();
  212. }
  213. int64_t PeerSessionResource::getCompletedLength() const
  214. {
  215. return bitfieldMan_->getCompletedLength();
  216. }
  217. void PeerSessionResource::setBtMessageDispatcher(BtMessageDispatcher* dpt)
  218. {
  219. dispatcher_ = dpt;
  220. }
  221. size_t PeerSessionResource::countOutstandingUpload() const
  222. {
  223. assert(dispatcher_);
  224. return dispatcher_->countOutstandingUpload();
  225. }
  226. void PeerSessionResource::reconfigure(int32_t pieceLength, int64_t totalLenth)
  227. {
  228. delete bitfieldMan_;
  229. bitfieldMan_ = new BitfieldMan(pieceLength, totalLenth);
  230. }
  231. } // namespace aria2