PeerSessionResource.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "BitfieldManFactory.h"
  39. #include "BitfieldMan.h"
  40. #include "A2STR.h"
  41. #include "BtMessageDispatcher.h"
  42. namespace aria2 {
  43. PeerSessionResource::PeerSessionResource(size_t pieceLength, uint64_t totalLength):
  44. _amChoking(true),
  45. _amInterested(false),
  46. _peerChoking(true),
  47. _peerInterested(false),
  48. _chokingRequired(true),
  49. _optUnchoking(false),
  50. _snubbing(false),
  51. _bitfieldMan(BitfieldManFactory::getFactoryInstance()->createBitfieldMan(pieceLength, totalLength)),
  52. _fastExtensionEnabled(false),
  53. _extendedMessagingEnabled(false),
  54. _dhtEnabled(false),
  55. _latency(DEFAULT_LATENCY),
  56. _lastDownloadUpdate(0),
  57. _lastAmUnchoking(0)
  58. {}
  59. PeerSessionResource::~PeerSessionResource()
  60. {
  61. delete _bitfieldMan;
  62. }
  63. void PeerSessionResource::amChoking(bool b)
  64. {
  65. _amChoking = b;
  66. if(!b) {
  67. _lastAmUnchoking.reset();
  68. }
  69. }
  70. void PeerSessionResource::amInterested(bool b)
  71. {
  72. _amInterested = b;
  73. }
  74. void PeerSessionResource::peerChoking(bool b)
  75. {
  76. _peerChoking = b;
  77. }
  78. void PeerSessionResource::peerInterested(bool b)
  79. {
  80. _peerInterested = b;
  81. }
  82. void PeerSessionResource::chokingRequired(bool b)
  83. {
  84. _chokingRequired = b;
  85. }
  86. void PeerSessionResource::optUnchoking(bool b)
  87. {
  88. _optUnchoking = b;
  89. }
  90. bool PeerSessionResource::shouldBeChoking() const
  91. {
  92. if(_optUnchoking) {
  93. return false;
  94. }
  95. return _chokingRequired;
  96. }
  97. void PeerSessionResource::snubbing(bool b)
  98. {
  99. _snubbing = b;
  100. if(_snubbing) {
  101. chokingRequired(true);
  102. optUnchoking(false);
  103. }
  104. }
  105. bool PeerSessionResource::hasAllPieces() const
  106. {
  107. return _bitfieldMan->isAllBitSet();
  108. }
  109. void PeerSessionResource::updateBitfield(size_t index, int operation)
  110. {
  111. if(operation == 1) {
  112. _bitfieldMan->setBit(index);
  113. } else if(operation == 0) {
  114. _bitfieldMan->unsetBit(index);
  115. }
  116. }
  117. void PeerSessionResource::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
  118. {
  119. _bitfieldMan->setBitfield(bitfield, bitfieldLength);
  120. }
  121. const unsigned char* PeerSessionResource::getBitfield() const
  122. {
  123. return _bitfieldMan->getBitfield();
  124. }
  125. size_t PeerSessionResource::getBitfieldLength() const
  126. {
  127. return _bitfieldMan->getBitfieldLength();
  128. }
  129. bool PeerSessionResource::hasPiece(size_t index) const
  130. {
  131. return _bitfieldMan->isBitSet(index);
  132. }
  133. void PeerSessionResource::markSeeder()
  134. {
  135. _bitfieldMan->setAllBit();
  136. }
  137. void PeerSessionResource::fastExtensionEnabled(bool b)
  138. {
  139. _fastExtensionEnabled = b;
  140. }
  141. const std::deque<size_t>& PeerSessionResource::peerAllowedIndexSet() const
  142. {
  143. return _peerAllowedIndexSet;
  144. }
  145. static void updateIndexSet(std::deque<size_t>& c, size_t index)
  146. {
  147. std::deque<size_t>::iterator i = std::lower_bound(c.begin(), c.end(), index);
  148. if(i == c.end() || (*i) != index) {
  149. c.insert(i, index);
  150. }
  151. }
  152. void PeerSessionResource::addPeerAllowedIndex(size_t index)
  153. {
  154. updateIndexSet(_peerAllowedIndexSet, index);
  155. }
  156. bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
  157. {
  158. return std::binary_search(_peerAllowedIndexSet.begin(),
  159. _peerAllowedIndexSet.end(),
  160. index);
  161. }
  162. void PeerSessionResource::addAmAllowedIndex(size_t index)
  163. {
  164. updateIndexSet(_amAllowedIndexSet, index);
  165. }
  166. bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
  167. {
  168. return std::binary_search(_amAllowedIndexSet.begin(),
  169. _amAllowedIndexSet.end(),
  170. index);
  171. }
  172. void PeerSessionResource::extendedMessagingEnabled(bool b)
  173. {
  174. _extendedMessagingEnabled = b;
  175. }
  176. uint8_t
  177. PeerSessionResource::getExtensionMessageID(const std::string& name) const
  178. {
  179. Extensions::const_iterator itr = _extensions.find(name);
  180. if(itr == _extensions.end()) {
  181. return 0;
  182. } else {
  183. return (*itr).second;
  184. }
  185. }
  186. std::string PeerSessionResource::getExtensionName(uint8_t id) const
  187. {
  188. for(Extensions::const_iterator itr = _extensions.begin();
  189. itr != _extensions.end(); ++itr) {
  190. const Extensions::value_type& p = *itr;
  191. if(p.second == id) {
  192. return p.first;
  193. }
  194. }
  195. return A2STR::NIL;
  196. }
  197. void PeerSessionResource::addExtension(const std::string& name, uint8_t id)
  198. {
  199. _extensions[name] = id;
  200. }
  201. void PeerSessionResource::dhtEnabled(bool b)
  202. {
  203. _dhtEnabled = b;
  204. }
  205. void PeerSessionResource::updateLatency(unsigned int latency)
  206. {
  207. _latency = static_cast<unsigned int>(_latency*0.2+latency*0.8);
  208. }
  209. uint64_t PeerSessionResource::uploadLength() const
  210. {
  211. return _peerStat.getSessionUploadLength();
  212. }
  213. void PeerSessionResource::updateUploadLength(size_t bytes)
  214. {
  215. _peerStat.updateUploadLength(bytes);
  216. }
  217. uint64_t PeerSessionResource::downloadLength() const
  218. {
  219. return _peerStat.getSessionDownloadLength();
  220. }
  221. void PeerSessionResource::updateDownloadLength(size_t bytes)
  222. {
  223. _peerStat.updateDownloadLength(bytes);
  224. _lastDownloadUpdate.reset();
  225. }
  226. uint64_t PeerSessionResource::getCompletedLength() const
  227. {
  228. return _bitfieldMan->getCompletedLength();
  229. }
  230. void PeerSessionResource::setBtMessageDispatcher
  231. (const WeakHandle<BtMessageDispatcher>& dpt)
  232. {
  233. _dispatcher = dpt;
  234. }
  235. size_t PeerSessionResource::countOutstandingUpload() const
  236. {
  237. assert(!_dispatcher.isNull());
  238. return _dispatcher->countOutstandingUpload();
  239. }
  240. } // namespace aria2