Peer.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "Peer.h"
  36. #include <cstring>
  37. #include <cassert>
  38. #include "util.h"
  39. #include "a2functional.h"
  40. #include "PeerSessionResource.h"
  41. #include "BtMessageDispatcher.h"
  42. namespace aria2 {
  43. #define BAD_CONDITION_INTERVAL 10
  44. Peer::Peer(std::string ipaddr, uint16_t port, bool incoming):
  45. ipaddr(ipaddr),
  46. port(port),
  47. _badConditionStartTime(0),
  48. _seeder(false),
  49. _res(0),
  50. _incoming(incoming),
  51. _localPeer(false)
  52. {
  53. memset(_peerId, 0, PEER_ID_LENGTH);
  54. resetStatus();
  55. id = ipaddr;
  56. strappend(id, A2STR::COLON_C, util::uitos(port));
  57. }
  58. Peer::~Peer()
  59. {
  60. releaseSessionResource();
  61. }
  62. void Peer::usedBy(int32_t cuid)
  63. {
  64. _cuid = cuid;
  65. }
  66. void Peer::allocateSessionResource(size_t pieceLength, uint64_t totalLength)
  67. {
  68. delete _res;
  69. _res = new PeerSessionResource(pieceLength, totalLength);
  70. _res->getPeerStat().downloadStart();
  71. }
  72. void Peer::releaseSessionResource()
  73. {
  74. delete _res;
  75. _res = 0;
  76. }
  77. void Peer::setPeerId(const unsigned char* peerId)
  78. {
  79. memcpy(_peerId, peerId, PEER_ID_LENGTH);
  80. }
  81. void Peer::resetStatus() {
  82. _cuid = 0;
  83. }
  84. bool Peer::amChoking() const
  85. {
  86. assert(_res);
  87. return _res->amChoking();
  88. }
  89. void Peer::amChoking(bool b) const
  90. {
  91. assert(_res);
  92. _res->amChoking(b);
  93. }
  94. // localhost is interested in this peer
  95. bool Peer::amInterested() const
  96. {
  97. assert(_res);
  98. return _res->amInterested();
  99. }
  100. void Peer::amInterested(bool b) const
  101. {
  102. assert(_res);
  103. _res->amInterested(b);
  104. }
  105. // this peer is choking localhost
  106. bool Peer::peerChoking() const
  107. {
  108. assert(_res);
  109. return _res->peerChoking();
  110. }
  111. void Peer::peerChoking(bool b) const
  112. {
  113. assert(_res);
  114. _res->peerChoking(b);
  115. }
  116. // this peer is interested in localhost
  117. bool Peer::peerInterested() const
  118. {
  119. assert(_res);
  120. return _res->peerInterested();
  121. }
  122. void Peer::peerInterested(bool b)
  123. {
  124. assert(_res);
  125. _res->peerInterested(b);
  126. }
  127. // this peer should be choked
  128. bool Peer::chokingRequired() const
  129. {
  130. assert(_res);
  131. return _res->chokingRequired();
  132. }
  133. void Peer::chokingRequired(bool b)
  134. {
  135. assert(_res);
  136. _res->chokingRequired(b);
  137. }
  138. // this peer is eligible for unchoking optionally.
  139. bool Peer::optUnchoking() const
  140. {
  141. assert(_res);
  142. return _res->optUnchoking();
  143. }
  144. void Peer::optUnchoking(bool b)
  145. {
  146. assert(_res);
  147. _res->optUnchoking(b);
  148. }
  149. // this peer is snubbing.
  150. bool Peer::snubbing() const
  151. {
  152. assert(_res);
  153. return _res->snubbing();
  154. }
  155. void Peer::snubbing(bool b)
  156. {
  157. assert(_res);
  158. _res->snubbing(b);
  159. }
  160. void Peer::updateUploadLength(size_t bytes)
  161. {
  162. assert(_res);
  163. _res->updateUploadLength(bytes);
  164. }
  165. void Peer::updateDownloadLength(size_t bytes)
  166. {
  167. assert(_res);
  168. _res->updateDownloadLength(bytes);
  169. }
  170. void Peer::updateSeeder()
  171. {
  172. assert(_res);
  173. if(_res->hasAllPieces()) {
  174. _seeder = true;
  175. }
  176. }
  177. void Peer::updateBitfield(size_t index, int operation) {
  178. assert(_res);
  179. _res->updateBitfield(index, operation);
  180. updateSeeder();
  181. }
  182. unsigned int Peer::calculateUploadSpeed()
  183. {
  184. assert(_res);
  185. return _res->getPeerStat().calculateUploadSpeed();
  186. }
  187. unsigned int Peer::calculateUploadSpeed(const struct timeval& now)
  188. {
  189. assert(_res);
  190. return _res->getPeerStat().calculateUploadSpeed(now);
  191. }
  192. unsigned int Peer::calculateDownloadSpeed()
  193. {
  194. assert(_res);
  195. return _res->getPeerStat().calculateDownloadSpeed();
  196. }
  197. unsigned int Peer::calculateDownloadSpeed(const struct timeval& now)
  198. {
  199. assert(_res);
  200. return _res->getPeerStat().calculateDownloadSpeed(now);
  201. }
  202. uint64_t Peer::getSessionUploadLength() const
  203. {
  204. assert(_res);
  205. return _res->uploadLength();
  206. }
  207. uint64_t Peer::getSessionDownloadLength() const
  208. {
  209. assert(_res);
  210. return _res->downloadLength();
  211. }
  212. void Peer::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
  213. {
  214. assert(_res);
  215. _res->setBitfield(bitfield, bitfieldLength);
  216. updateSeeder();
  217. }
  218. const unsigned char* Peer::getBitfield() const
  219. {
  220. assert(_res);
  221. return _res->getBitfield();
  222. }
  223. size_t Peer::getBitfieldLength() const
  224. {
  225. assert(_res);
  226. return _res->getBitfieldLength();
  227. }
  228. bool Peer::shouldBeChoking() const {
  229. assert(_res);
  230. return _res->shouldBeChoking();
  231. }
  232. bool Peer::hasPiece(size_t index) const {
  233. assert(_res);
  234. return _res->hasPiece(index);
  235. }
  236. void Peer::setFastExtensionEnabled(bool enabled)
  237. {
  238. assert(_res);
  239. return _res->fastExtensionEnabled(enabled);
  240. }
  241. bool Peer::isFastExtensionEnabled() const
  242. {
  243. assert(_res);
  244. return _res->fastExtensionEnabled();
  245. }
  246. size_t Peer::countPeerAllowedIndexSet() const
  247. {
  248. assert(_res);
  249. return _res->peerAllowedIndexSet().size();
  250. }
  251. const std::vector<size_t>& Peer::getPeerAllowedIndexSet() const
  252. {
  253. assert(_res);
  254. return _res->peerAllowedIndexSet();
  255. }
  256. bool Peer::isInPeerAllowedIndexSet(size_t index) const
  257. {
  258. assert(_res);
  259. return _res->peerAllowedIndexSetContains(index);
  260. }
  261. void Peer::addPeerAllowedIndex(size_t index)
  262. {
  263. assert(_res);
  264. _res->addPeerAllowedIndex(index);
  265. }
  266. bool Peer::isInAmAllowedIndexSet(size_t index) const
  267. {
  268. assert(_res);
  269. return _res->amAllowedIndexSetContains(index);
  270. }
  271. void Peer::addAmAllowedIndex(size_t index)
  272. {
  273. assert(_res);
  274. _res->addAmAllowedIndex(index);
  275. }
  276. void Peer::setAllBitfield() {
  277. assert(_res);
  278. _res->markSeeder();
  279. _seeder = true;
  280. }
  281. void Peer::startBadCondition()
  282. {
  283. _badConditionStartTime.reset();
  284. }
  285. bool Peer::isGood() const
  286. {
  287. return _badConditionStartTime.elapsed(BAD_CONDITION_INTERVAL);
  288. }
  289. uint8_t Peer::getExtensionMessageID(const std::string& name) const
  290. {
  291. assert(_res);
  292. return _res->getExtensionMessageID(name);
  293. }
  294. std::string Peer::getExtensionName(uint8_t id) const
  295. {
  296. assert(_res);
  297. return _res->getExtensionName(id);
  298. }
  299. void Peer::setExtension(const std::string& name, uint8_t id)
  300. {
  301. assert(_res);
  302. _res->addExtension(name, id);
  303. }
  304. void Peer::setExtendedMessagingEnabled(bool enabled)
  305. {
  306. assert(_res);
  307. _res->extendedMessagingEnabled(enabled);
  308. }
  309. bool Peer::isExtendedMessagingEnabled() const
  310. {
  311. assert(_res);
  312. return _res->extendedMessagingEnabled();
  313. }
  314. void Peer::setDHTEnabled(bool enabled)
  315. {
  316. assert(_res);
  317. _res->dhtEnabled(enabled);
  318. }
  319. bool Peer::isDHTEnabled() const
  320. {
  321. assert(_res);
  322. return _res->dhtEnabled();
  323. }
  324. const Time& Peer::getLastDownloadUpdate() const
  325. {
  326. assert(_res);
  327. return _res->getLastDownloadUpdate();
  328. }
  329. const Time& Peer::getLastAmUnchoking() const
  330. {
  331. assert(_res);
  332. return _res->getLastAmUnchoking();
  333. }
  334. uint64_t Peer::getCompletedLength() const
  335. {
  336. assert(_res);
  337. return _res->getCompletedLength();
  338. }
  339. void Peer::setIncomingPeer(bool incoming)
  340. {
  341. _incoming = incoming;
  342. }
  343. void Peer::setFirstContactTime(const Time& time)
  344. {
  345. _firstContactTime = time;
  346. }
  347. void Peer::setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dpt)
  348. {
  349. assert(_res);
  350. _res->setBtMessageDispatcher(dpt);
  351. }
  352. size_t Peer::countOutstandingUpload() const
  353. {
  354. assert(_res);
  355. return _res->countOutstandingUpload();
  356. }
  357. } // namespace aria2