Peer.cc 7.9 KB

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