Peer.cc 9.2 KB

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