subtly
10 years ago
4 changed files with 152 additions and 79 deletions
@ -0,0 +1,57 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Peer.cpp
|
|||
* @author Alex Leverington <nessence@gmail.com> |
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "Peer.h" |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::p2p; |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
namespace p2p |
|||
{ |
|||
|
|||
bool Peer::operator<(Peer const& _p) const |
|||
{ |
|||
if (isOffline() != _p.isOffline()) |
|||
return isOffline(); |
|||
else if (isOffline()) |
|||
if (m_lastAttempted == _p.m_lastAttempted) |
|||
return m_failedAttempts < _p.m_failedAttempts; |
|||
else |
|||
return m_lastAttempted < _p.m_lastAttempted; |
|||
else |
|||
if (m_score == _p.m_score) |
|||
if (m_rating == _p.m_rating) |
|||
if (m_failedAttempts == _p.m_failedAttempts) |
|||
return id < _p.id; |
|||
else |
|||
return m_failedAttempts < _p.m_failedAttempts; |
|||
else |
|||
return m_rating < _p.m_rating; |
|||
else |
|||
return m_score < _p.m_score; |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,82 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Peer.h
|
|||
* @author Alex Leverington <nessence@gmail.com> |
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Common.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
namespace p2p |
|||
{ |
|||
|
|||
/**
|
|||
* @brief Representation of connectivity state and all other pertinent Peer metadata. |
|||
* A Peer represents connectivity between two nodes, which in this case, are the host |
|||
* and remote nodes. |
|||
* |
|||
* State information necessary for loading network topology is maintained by NodeTable. |
|||
* |
|||
* @todo Implement 'bool required' |
|||
* @todo reputation: Move score, rating to capability-specific map (&& remove friend class) |
|||
* @todo reputation: implement via origin-tagged events |
|||
* @todo Populate metadata upon construction; save when destroyed. |
|||
* @todo Metadata for peers needs to be handled via a storage backend. |
|||
* Specifically, peers can be utilized in a variety of |
|||
* many-to-many relationships while also needing to modify shared instances of |
|||
* those peers. Modifying these properties via a storage backend alleviates |
|||
* Host of the responsibility. (&& remove save/restoreNetwork) |
|||
* @todo reimplement recording of historical session information on per-transport basis |
|||
* @todo rebuild nodetable when localNetworking is enabled/disabled |
|||
* @todo move attributes into protected |
|||
*/ |
|||
class Peer: public Node |
|||
{ |
|||
friend class Session; /// Allows Session to update score and rating.
|
|||
friend class Host; /// For Host: saveNetwork(), restoreNetwork()
|
|||
|
|||
public: |
|||
bool isOffline() const { return !m_session.lock(); } |
|||
|
|||
bi::tcp::endpoint const& peerEndpoint() const { return endpoint.tcp; } |
|||
|
|||
virtual bool operator<(Peer const& _p) const; |
|||
|
|||
protected: |
|||
int m_score = 0; ///< All time cumulative.
|
|||
int m_rating = 0; ///< Trending.
|
|||
|
|||
/// Network Availability
|
|||
|
|||
std::chrono::system_clock::time_point m_lastConnected; |
|||
std::chrono::system_clock::time_point m_lastAttempted; |
|||
unsigned m_failedAttempts = 0; |
|||
DisconnectReason m_lastDisconnect = NoDisconnect; ///< Reason for disconnect that happened last.
|
|||
|
|||
/// Used by isOffline() and (todo) for peer to emit session information.
|
|||
std::weak_ptr<Session> m_session; |
|||
}; |
|||
using Peers = std::vector<Peer>; |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue