Peer.cc 8.8 KB

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