Peer.cc 8.6 KB

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