Peer.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Peer.cpp
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @author Gav Wood <i@gavwood.com>
  17. * @date 2014
  18. */
  19. #include "Peer.h"
  20. using namespace std;
  21. using namespace dev;
  22. using namespace dev::p2p;
  23. namespace dev
  24. {
  25. namespace p2p
  26. {
  27. bool Peer::shouldReconnect() const
  28. {
  29. return id && endpoint && chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds());
  30. }
  31. unsigned Peer::fallbackSeconds() const
  32. {
  33. if (peerType == PeerType::Required)
  34. return 5;
  35. switch (m_lastDisconnect)
  36. {
  37. case BadProtocol:
  38. return 30 * (m_failedAttempts + 1);
  39. case UselessPeer:
  40. case TooManyPeers:
  41. return 25 * (m_failedAttempts + 1);
  42. case ClientQuit:
  43. return 15 * (m_failedAttempts + 1);
  44. case NoDisconnect:
  45. default:
  46. if (m_failedAttempts < 5)
  47. return m_failedAttempts ? m_failedAttempts * 5 : 5;
  48. else if (m_failedAttempts < 15)
  49. return 25 + (m_failedAttempts - 5) * 10;
  50. else
  51. return 25 + 100 + (m_failedAttempts - 15) * 20;
  52. }
  53. }
  54. bool Peer::operator<(Peer const& _p) const
  55. {
  56. if (isOffline() != _p.isOffline())
  57. return isOffline();
  58. else if (isOffline())
  59. if (m_lastAttempted == _p.m_lastAttempted)
  60. return m_failedAttempts < _p.m_failedAttempts;
  61. else
  62. return m_lastAttempted < _p.m_lastAttempted;
  63. else
  64. if (m_score == _p.m_score)
  65. if (m_rating == _p.m_rating)
  66. if (m_failedAttempts == _p.m_failedAttempts)
  67. return id < _p.id;
  68. else
  69. return m_failedAttempts < _p.m_failedAttempts;
  70. else
  71. return m_rating < _p.m_rating;
  72. else
  73. return m_score < _p.m_score;
  74. }
  75. }
  76. }