Class NodeServer
- All Implemented Interfaces:
NodeServerLifecycle
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 Summary
ConstructorsConstructorDescriptionNodeServer(int port, AllowlistManager allowlistManager, PermissionManager permissionManager, PeerManager peerManager, MessageCodec codec, Blockchain blockchain, BlockchainEventBus eventBus) Constructs aNodeServerwith all required dependencies. -
Method Summary
Modifier and TypeMethodDescriptionintgetPort()Returns the port this server is (or will be) listening on.booleanReturnstrueif the server accept loop is running.voidsetGossipProtocol(GossipProtocol gossipProtocol) Wires the gossip protocol after construction (avoids circular dependency at construction time between NodeServer and GossipProtocol).voidstart()Binds the server socket and starts the accept loop on a daemon thread.voidstop()Stops the server by closing the server socket and shutting down the handler pool.
-
Constructor Details
-
NodeServer
public NodeServer(int port, AllowlistManager allowlistManager, PermissionManager permissionManager, PeerManager peerManager, MessageCodec codec, Blockchain blockchain, BlockchainEventBus eventBus) Constructs aNodeServerwith 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 nullIllegalArgumentException- 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:
startin interfaceNodeServerLifecycle- Throws:
IllegalStateException- if the server is already runningRuntimeException- 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
serverSocketcauses the blockingaccept()call inacceptLoop()to throw aSocketException, which terminates the accept loop cleanly.- Specified by:
stopin interfaceNodeServerLifecycle
-
setGossipProtocol
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()Returnstrueif the server accept loop is running.- Specified by:
isRunningin interfaceNodeServerLifecycle- Returns:
trueif running
-