Peer.cc 8.1 KB

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