Peer.cc 8.0 KB

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