PeerSessionResource.cc 7.3 KB

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