Files
rippled/include/xrpl/overlay/PeerSet.h

69 lines
1.8 KiB
C++

#ifndef XRPL_APP_PEERS_PEERSET_H_INCLUDED
#define XRPL_APP_PEERS_PEERSET_H_INCLUDED
#include <xrpld/app/main/Application.h>
#include <xrpld/overlay/detail/ProtocolMessage.h>
#include <xrpl/overlay/Peer.h>
namespace xrpl {
/** Supports data retrieval by managing a set of peers.
When desired data (such as a ledger or a transaction set)
is missing locally it can be obtained by querying connected
peers. This class manages common aspects of the retrieval.
Callers maintain the set by adding and removing peers depending
on whether the peers have useful information.
The data is represented by its hash.
*/
class PeerSet
{
public:
virtual ~PeerSet() = default;
/**
* Try add more peers
* @param limit number of peers to add
* @param hasItem callback that helps to select peers
* @param onPeerAdded callback called when a peer is added
*/
virtual void
addPeers(
std::size_t limit,
std::function<bool(std::shared_ptr<Peer> const&)> hasItem,
std::function<void(std::shared_ptr<Peer> const&)> onPeerAdded) = 0;
/** send a message */
template <typename MessageType>
void
sendRequest(MessageType const& message, std::shared_ptr<Peer> const& peer)
{
this->sendRequest(message, protocolMessageType(message), peer);
}
virtual void
sendRequest(
::google::protobuf::Message const& message,
protocol::MessageType type,
std::shared_ptr<Peer> const& peer) = 0;
/** get the set of ids of previously added peers */
virtual std::set<Peer::id_t> const&
getPeerIds() const = 0;
};
class PeerSetBuilder
{
public:
virtual ~PeerSetBuilder() = default;
virtual std::unique_ptr<PeerSet>
build() = 0;
};
} // namespace xrpl
#endif