Peer.cc 8.3 KB

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