Class PeerManager
- All Implemented Interfaces:
PeerManagerLifecycle
Responsibilities (FR-NET-01):
- Maintaining the set of connected peer node IDs via
CopyOnWriteArraySet - Persisting peer metadata to the
PeerStoreon connect/disconnect - Running a periodic heartbeat to prune unresponsive peers
- Publishing
BlockchainEvent.PeerConnectedEventandBlockchainEvent.PeerDisconnectedEventto the sharedBlockchainEventBus(T-066, Milestone 8)
Milestone 8 wiring (T-066)
Every successful connect(Peer) publishes a
BlockchainEvent.PeerConnectedEvent and every
disconnect(String, String) publishes a
BlockchainEvent.PeerDisconnectedEvent asynchronously.
Design note
This class coordinates lifecycle — actual TCP I/O is delegated to
NodeServer and NodeClient in the transport layer.
- Since:
- 1.0.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHow often the heartbeat task checks peer liveness (seconds).static final intSilence duration before a peer is considered unresponsive (seconds). -
Constructor Summary
ConstructorsConstructorDescriptionPeerManager(PeerStore peerStore, BlockchainEventBus eventBus, int maxPeers) Constructs aPeerManagerwith all required dependencies. -
Method Summary
Modifier and TypeMethodDescriptionbooleanRegisters a peer as connected and publishes aBlockchainEvent.PeerConnectedEvent(T-066).voiddisconnect(String nodeId, String reason) Marks a peer as disconnected and publishes aBlockchainEvent.PeerDisconnectedEvent(T-066).voiddisconnectAll(String reason) Disconnects all currently connected peers with the given reason.intReturns the number of currently connected peers.Returns a snapshot list of all currently connectedPeerinstances.Returns the backingPeerStorefor direct peer-level operations.booleanisConnected(String nodeId) Returnstrueif the peer with the given node ID is currently connected.voidrecordHeartbeat(String nodeId) Records a successful heartbeat for the given peer, refreshing itslastSeentimestamp in thePeerStore.voidstart()Starts the peer manager and schedules the periodic heartbeat/pruning task.voidstop()Stops the peer manager, disconnects all peers, and shuts down the heartbeat scheduler.toString()Returns a human-readable summary of the peer manager state.
-
Field Details
-
HEARTBEAT_INTERVAL_SECONDS
public static final int HEARTBEAT_INTERVAL_SECONDSHow often the heartbeat task checks peer liveness (seconds).- See Also:
-
PEER_TIMEOUT_SECONDS
public static final int PEER_TIMEOUT_SECONDSSilence duration before a peer is considered unresponsive (seconds).- See Also:
-
-
Constructor Details
-
PeerManager
Constructs aPeerManagerwith all required dependencies.- Parameters:
peerStore- the backing peer store (non-null)eventBus- event bus for publishing peer lifecycle events (non-null)maxPeers- maximum simultaneous peer connections (≥ 1)- Throws:
NullPointerException- if peerStore or eventBus is nullIllegalArgumentException- if maxPeers < 1
-
-
Method Details
-
start
public void start()Starts the peer manager and schedules the periodic heartbeat/pruning task.This method is idempotent; calling it while already running is a no-op.
- Specified by:
startin interfacePeerManagerLifecycle
-
stop
public void stop()Stops the peer manager, disconnects all peers, and shuts down the heartbeat scheduler.- Specified by:
stopin interfacePeerManagerLifecycle
-
getConnectedPeerCount
public int getConnectedPeerCount()Returns the number of currently connected peers.- Specified by:
getConnectedPeerCountin interfacePeerManagerLifecycle- Returns:
- connected peer count (≥ 0)
-
connect
Registers a peer as connected and publishes aBlockchainEvent.PeerConnectedEvent(T-066).If the peer is already connected, or if the max peer count is reached, this method returns
falseand takes no action. On success the peer is persisted to thePeerStorewith a freshlastSeentimestamp viaPeerStore.put(Peer).- Parameters:
peer- the peer that has established a connection (non-null)- Returns:
trueif the peer was newly added;falseotherwise- Throws:
NullPointerException- if peer is null
-
disconnect
Marks a peer as disconnected and publishes aBlockchainEvent.PeerDisconnectedEvent(T-066).The peer remains in the
PeerStorefor potential reconnection. Only its entry in the connected-nodes set is removed.- Parameters:
nodeId- the node ID of the peer to disconnect (non-null, non-blank)reason- human-readable reason; may benull- Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
disconnectAll
Disconnects all currently connected peers with the given reason.- Parameters:
reason- human-readable reason; may benull
-
recordHeartbeat
Records a successful heartbeat for the given peer, refreshing itslastSeentimestamp in thePeerStore.Called by the transport layer whenever a ping/pong or any valid inbound message is received from the peer.
- Parameters:
nodeId- the node ID of the peer to refresh (non-null, non-blank)- Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
getConnectedPeers
Returns a snapshot list of all currently connectedPeerinstances.- Returns:
- unmodifiable list of connected peers; never null
-
isConnected
Returnstrueif the peer with the given node ID is currently connected.- Parameters:
nodeId- the node ID to check (non-null)- Returns:
trueif connected- Throws:
NullPointerException- if nodeId is null
-
getPeerStore
Returns the backingPeerStorefor direct peer-level operations.- Returns:
- non-null
PeerStore
-
toString
Returns a human-readable summary of the peer manager state.
-