Class NodeClient

java.lang.Object
com.privatechain.network.rpc.NodeClient

public final class NodeClient extends Object
Manages outbound TCP connections from this node to remote peers.

NodeClient is the counterpart to NodeServer: while the server handles inbound messages, the client is responsible for sending messages to remote nodes. It is used by BlockBroadcaster, GossipProtocol, and SyncManager to push data to specific peers.

Connection model

This implementation uses short-lived, per-message TCP connections. A connection is opened, one message is written and (optionally) one response is read, then the socket is closed. This simplifies resource management and avoids connection state drift. A pooled persistent-connection model can be introduced in a future milestone per ADR-001 (T-064).

Usage


 NodeClient client = new NodeClient("local-node-id", peerManager, codec);

 // Fire-and-forget (no response expected)
 client.send(peer, blockMessage);

 // Request-response
 Optional<NetworkMessage> status = client.sendAndReceive(peer, getStatusMessage);
 

Thread safety

All methods are stateless and safe for concurrent use from multiple threads.

Since:
1.0.0
See Also:
  • Constructor Details

    • NodeClient

      public NodeClient(String localNodeId, PeerManager peerManager, MessageCodec codec)
      Constructs a NodeClient.
      Parameters:
      localNodeId - this node's stable logical identifier (non-null, non-blank)
      peerManager - the peer lifecycle manager (non-null)
      codec - the message codec (non-null)
      Throws:
      NullPointerException - if any parameter is null
      IllegalArgumentException - if localNodeId is blank
  • Method Details

    • send

      public boolean send(Peer peer, MessageCodec.NetworkMessage message)
      Sends a message to the given peer without waiting for a response.

      The connection is opened, the message is written as a UTF-8 JSON line, and the socket is closed. On failure the peer is not automatically removed from the peer manager — higher-level components (e.g., the heartbeat pruner in PeerManager) are responsible for lifecycle decisions.

      Parameters:
      peer - the target peer (non-null)
      message - the message to send (non-null)
      Returns:
      true if the message was sent successfully; false on I/O error
      Throws:
      NullPointerException - if peer or message is null
    • sendAndReceive

      public Optional<MessageCodec.NetworkMessage> sendAndReceive(Peer peer, MessageCodec.NetworkMessage message)
      Sends a message to the given peer and blocks until a response is received.

      The connection is opened, the request is written, a response line is read and decoded, and the socket is closed. Returns Optional.empty() if the peer does not respond within the read timeout or if any I/O error occurs.

      Parameters:
      peer - the target peer (non-null)
      message - the request message to send (non-null)
      Returns:
      the decoded response, or empty on timeout/error
      Throws:
      NullPointerException - if peer or message is null