Class NodeServer

java.lang.Object
com.privatechain.network.rpc.NodeServer
All Implemented Interfaces:
NodeServerLifecycle

public final class NodeServer extends Object implements NodeServerLifecycle
TCP server that accepts inbound peer connections and dispatches received messages.

NodeServer is the network entry point for all inbound P2P traffic. Every raw message passes through a strict access-control pipeline before any deserialization occurs, satisfying NFR-SEC-04:

 Inbound TCP message
        │
        ▼
 AllowlistManager.isAllowed(senderId) ──[DENY]──► drop + WARN log
        │ [ALLOW]
        ▼
 PermissionManager.hasRole(senderId, required) ──[DENIED]──► error response
        │ [AUTHORIZED]
        ▼
 MessageCodec.decode(bytes) ──► dispatch to handler
 

Transport

This implementation uses Java's built-in ServerSocket rather than Netty, keeping blockchain-network dependency-free beyond the JDK for the current milestone. Each accepted connection is handled on a dedicated thread from a cached thread pool. A full Netty migration can follow via ADR-001 (T-064).

Lifecycle


 NodeServer server = new NodeServer(8545, allowlist, permMgr, peerMgr, codec, ...);
 server.start();
 // ... node is running ...
 server.stop();
 

Thread safety

The server accept loop runs on a dedicated daemon thread. Message handlers run on the connection thread pool. The running flag is volatile to ensure visibility across threads during shutdown.

Since:
1.0.0
See Also:
  • Constructor Details

    • NodeServer

      public NodeServer(int port, AllowlistManager allowlistManager, PermissionManager permissionManager, PeerManager peerManager, MessageCodec codec, Blockchain blockchain, BlockchainEventBus eventBus)
      Constructs a NodeServer with all required dependencies.
      Parameters:
      port - TCP port to listen on (1024–65535)
      allowlistManager - allowlist gate for inbound messages (non-null)
      permissionManager - RBAC gate for operation authorization (non-null)
      peerManager - peer lifecycle manager (non-null)
      codec - message codec for encoding/decoding (non-null)
      blockchain - the chain manager for block ingestion (non-null)
      eventBus - event bus for publishing peer events (non-null)
      Throws:
      NullPointerException - if any required dependency is null
      IllegalArgumentException - if port is out of range
  • Method Details

    • start

      public void start()
      Binds the server socket and starts the accept loop on a daemon thread.
      Specified by:
      start in interface NodeServerLifecycle
      Throws:
      IllegalStateException - if the server is already running
      RuntimeException - if the server socket cannot be bound to the port
    • stop

      public void stop()
      Stops the server by closing the server socket and shutting down the handler pool.

      Closing serverSocket causes the blocking accept() call in acceptLoop() to throw a SocketException, which terminates the accept loop cleanly.

      Specified by:
      stop in interface NodeServerLifecycle
    • setGossipProtocol

      public void setGossipProtocol(GossipProtocol gossipProtocol)
      Wires the gossip protocol after construction (avoids circular dependency at construction time between NodeServer and GossipProtocol).
      Parameters:
      gossipProtocol - the gossip layer to use (non-null)
      Throws:
      NullPointerException - if gossipProtocol is null
    • getPort

      public int getPort()
      Returns the port this server is (or will be) listening on.
      Returns:
      TCP port
    • isRunning

      public boolean isRunning()
      Returns true if the server accept loop is running.
      Specified by:
      isRunning in interface NodeServerLifecycle
      Returns:
      true if running