Peer.cc 8.1 KB

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