Peer.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "Peer.h"
  23. Peer* Peer::nullPeer = new Peer("", 0, 0, 0);
  24. void Peer::updateBitfield(int index, int operation) {
  25. if(operation == 1) {
  26. bitfield->setBit(index);
  27. } else if(operation == 0) {
  28. bitfield->unsetBit(index);
  29. }
  30. }
  31. #define THRESHOLD 1024*1024*2
  32. bool Peer::shouldBeChoking() const {
  33. if(optUnchoking) {
  34. return false;
  35. }
  36. return chokingRequired;
  37. }
  38. bool Peer::hasPiece(int index) const {
  39. return bitfield->isBitSet(index);
  40. }
  41. bool Peer::isSeeder() const {
  42. return bitfield->isAllBitSet();
  43. }
  44. void Peer::resetStatus() {
  45. tryCount = 0;
  46. cuid = 0;
  47. amChoking = true;
  48. amInterested = false;
  49. peerChoking = true;
  50. peerInterested = false;
  51. resetDeltaUpload();
  52. resetDeltaDownload();
  53. chokingRequired = true;
  54. optUnchoking = false;
  55. fastExtensionEnabled = false;
  56. latency = DEFAULT_LATENCY;
  57. fastSet.clear();
  58. }
  59. bool Peer::isInFastSet(int index) const {
  60. return find(fastSet.begin(), fastSet.end(), index) != fastSet.end();
  61. }
  62. void Peer::addFastSetIndex(int index) {
  63. if(!isInFastSet(index)) {
  64. fastSet.push_back(index);
  65. }
  66. }
  67. void Peer::setAllBitfield() {
  68. bitfield->setAllBit();
  69. }
  70. void Peer::updateLatency(int latency) {
  71. this->latency = (this->latency*20+latency*80)/200;
  72. }