Peer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @author Gav Wood <i@gavwood.com>
  17. * @date 2014
  18. */
  19. #pragma once
  20. #include "Common.h"
  21. namespace dev
  22. {
  23. namespace p2p
  24. {
  25. /**
  26. * @brief Representation of connectivity state and all other pertinent Peer metadata.
  27. * A Peer represents connectivity between two nodes, which in this case, are the host
  28. * and remote nodes.
  29. *
  30. * State information necessary for loading network topology is maintained by NodeTable.
  31. *
  32. * @todo Implement 'bool required'
  33. * @todo reputation: Move score, rating to capability-specific map (&& remove friend class)
  34. * @todo reputation: implement via origin-tagged events
  35. * @todo Populate metadata upon construction; save when destroyed.
  36. * @todo Metadata for peers needs to be handled via a storage backend.
  37. * Specifically, peers can be utilized in a variety of
  38. * many-to-many relationships while also needing to modify shared instances of
  39. * those peers. Modifying these properties via a storage backend alleviates
  40. * Host of the responsibility. (&& remove save/restoreNetwork)
  41. * @todo reimplement recording of historical session information on per-transport basis
  42. * @todo move attributes into protected
  43. */
  44. class Peer: public Node
  45. {
  46. friend class Session; /// Allows Session to update score and rating.
  47. friend class Host; /// For Host: saveNetwork(), restoreNetwork()
  48. friend class RLPXHandshake;
  49. public:
  50. /// Construct Peer from Node.
  51. Peer(Node const& _node): Node(_node) {}
  52. bool isOffline() const { return !m_session.lock(); }
  53. virtual bool operator<(Peer const& _p) const;
  54. /// WIP: Returns current peer rating.
  55. int rating() const { return m_rating; }
  56. /// Return true if connection attempt should be made to this peer or false if
  57. bool shouldReconnect() const;
  58. /// Number of times connection has been attempted to peer.
  59. int failedAttempts() const { return m_failedAttempts; }
  60. /// Reason peer was previously disconnected.
  61. DisconnectReason lastDisconnect() const { return m_lastDisconnect; }
  62. /// Peer session is noted as useful.
  63. void noteSessionGood() { m_failedAttempts = 0; }
  64. protected:
  65. /// Returns number of seconds to wait until attempting connection, based on attempted connection history.
  66. unsigned fallbackSeconds() const;
  67. int m_score = 0; ///< All time cumulative.
  68. int m_rating = 0; ///< Trending.
  69. /// Network Availability
  70. std::chrono::system_clock::time_point m_lastConnected;
  71. std::chrono::system_clock::time_point m_lastAttempted;
  72. unsigned m_failedAttempts = 0;
  73. DisconnectReason m_lastDisconnect = NoDisconnect; ///< Reason for disconnect that happened last.
  74. /// Used by isOffline() and (todo) for peer to emit session information.
  75. std::weak_ptr<Session> m_session;
  76. };
  77. using Peers = std::vector<Peer>;
  78. }
  79. }