Interface P2PService
- All Superinterfaces:
org.hyperledger.besu.plugin.services.BesuService
public interface P2PService
extends org.hyperledger.besu.plugin.services.BesuService
Service giving plugins access to the peer-to-peer network, letting them query peers and
connections, subscribe to connect, disconnect and message events, send messages to specific peers
and control the P2P network lifecycle.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceConsumer of network connection events.static interfaceConsumer of network disconnection events.static interfaceConsumer of network message events. -
Method Summary
Modifier and TypeMethodDescriptionvoidDisables P2P discovery.voiddisconnect(org.apache.tuweni.bytes.Bytes peerId) Disconnect from a specific peer.voidEnables P2P discovery.Collection<? extends Peer> Get all maintained connection peers.Collection<? extends PeerConnection> Get all connected peer connections.intGet the number of connected peers.booleanReturns whether the P2P network is enabled.voidsend(String protocol, org.apache.tuweni.bytes.Bytes peerId, org.hyperledger.besu.datatypes.p2p.MessageData messageData) Send a message to a specific peer.voidsubscribeConnect(P2PService.ConnectionListener networkSubscriber) Subscribe to connection events.voidsubscribeDisconnect(P2PService.DisconnectionListener networkSubscriber) Subscribe to disconnection events.voidsubscribeMessage(Capability capability, P2PService.MessageListener networkSubscriber) Subscribe to messages on a specific capability.
-
Method Details
-
enableDiscovery
void enableDiscovery()Enables P2P discovery. -
disableDiscovery
void disableDiscovery()Disables P2P discovery. -
isP2pEnabled
boolean isP2pEnabled()Returns whether the P2P network is enabled. When disabled (e.g. started with--p2p-enabled=false), peer-count checks should be skipped because no peers will ever connect.- Returns:
trueif the P2P network is enabled,falseotherwise
-
getPeerCount
int getPeerCount()Get the number of connected peers.- Returns:
- the number of connected peers
-
getPeerConnections
Collection<? extends PeerConnection> getPeerConnections()Get all connected peer connections.- Returns:
- list of connected peer connections
-
getMaintainedConnectionPeers
Collection<? extends Peer> getMaintainedConnectionPeers()Get all maintained connection peers.- Returns:
- list of maintained connection peers
-
subscribeConnect
Subscribe to connection events.- Parameters:
networkSubscriber- the subscriber to receive connection events
-
subscribeDisconnect
Subscribe to disconnection events.- Parameters:
networkSubscriber- the subscriber to receive disconnection events
-
subscribeMessage
Subscribe to messages on a specific capability.- Parameters:
capability- the capability to subscribe tonetworkSubscriber- the subscriber to receive messages for the specified capability
-
send
void send(String protocol, org.apache.tuweni.bytes.Bytes peerId, org.hyperledger.besu.datatypes.p2p.MessageData messageData) throws PeerConnection.PeerNotConnected Send a message to a specific peer.- Parameters:
protocol- the protocol to usepeerId- the peer id to send the message tomessageData- the message data to send- Throws:
PeerConnection.PeerNotConnected- if the peer is not connected
-
disconnect
void disconnect(org.apache.tuweni.bytes.Bytes peerId) Disconnect from a specific peer.- Parameters:
peerId- the peer id to disconnect from
-