PeerSessionResource.cc 7.3 KB

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