Peer.cc 8.5 KB

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