Class Channel_MultiThreadedServer
The server supports both plain and SSL/TLS channels, and can be configured with an idle timeout that automatically shuts down the server when no client connects within the given period.
For each accepted client, a new AnB_Session is created using the server's
cryptographic configuration. The provided protocolRunner consumer is then
invoked with that session, allowing the protocol steps to be executed.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault idle timeout in milliseconds (5 seconds).protected final Crypto_EncryptionEngineThe cryptographic engine used for creating sessions. -
Constructor Summary
ConstructorsConstructorDescriptionChannel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee) Constructs a new multi-threaded server with the specified parameters.Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, long expectedConnections) Constructs a new multi-threaded server that limits the number of accepted client connections toexpectedConnections.Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, Channel_MessageSerializer serializer) Constructs a new multi-threaded server that uses Protocol Buffers serialisation for every accepted client session.Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, Channel_MessageSerializer serializer, long expectedConnections) Creates a multi-threaded TCP server that listens on the port specified bychannelSettings, accepts up toexpectedConnectionsclient connections, and for each connection runs the givenprotocolRunnerin a separate thread from an internal thread pool.Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, Crypto_EncryptionEngine ee) Constructs a new multi-threaded server with the default idle timeout (5 seconds). -
Method Summary
-
Field Details
-
ee
The cryptographic engine used for creating sessions. -
DEFAULT_IDLE_TIMEOUT_MS
public static final long DEFAULT_IDLE_TIMEOUT_MSDefault idle timeout in milliseconds (5 seconds).- See Also:
-
-
Constructor Details
-
Channel_MultiThreadedServer
public Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, Channel_MessageSerializer serializer, long expectedConnections) Creates a multi-threaded TCP server that listens on the port specified bychannelSettings, accepts up toexpectedConnectionsclient connections, and for each connection runs the givenprotocolRunnerin a separate thread from an internal thread pool.An idle timeout can be configured: if no client connects within the calculated timeout, the server shuts down automatically. The timeout for the very first connection is multiplied by 3 (capped at
Integer.MAX_VALUEto fit the socket API), giving the protocol agents extra time to start up. After the first connection, the timeout reverts toidleTimeoutMs. A non-positive value (≤ 0) disables the timeout entirely (the server runs until explicitly stopped).When
expectedConnectionsis notLong.MAX_VALUE, the listening socket is closed as soon as that many connections have been accepted; no further connections will be served.This is the most general constructor - all other convenience constructors eventually delegate to it.
- Parameters:
serverAlias- the alias of the server principal (e.g."bob")channelSettings- the channel settings (port, SSL mode, cipher suites, etc.)protocolRunner- aConsumerthat receives a freshAnB_Sessionfor each connected client and executes the protocol stepsidleTimeoutMs- maximum idle time in milliseconds before the server shuts down automatically; a value ≤ 0 means "no timeout"ee- the shared cryptographic engine used to create sessions for incoming connectionsserializer- the message serializer for wire-format encoding;nullselects raw Java object serialisation (the original unframed format)expectedConnections- maximum number of client connections to accept; useLong.MAX_VALUEfor unlimited
-
Channel_MultiThreadedServer
public Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, long expectedConnections) Constructs a new multi-threaded server that limits the number of accepted client connections toexpectedConnections. The server will use the global default message serializer (ornullfor plain Java serialisation).- Parameters:
serverAlias- the alias of the server principal (e.g., "bob")channelSettings- the channel settings defining the port, SSL mode, etc.protocolRunner- aConsumerthat will be called for each connected client, receiving the newly createdAnB_Sessionfor that client.idleTimeoutMs- maximum idle time in milliseconds before the server shuts down automatically. Zero or negative value means no timeout (run forever).ee- the cryptographic engine used for creating sessionsexpectedConnections- the maximum number of client connections to accept. UseLong.MAX_VALUEfor unlimited.
-
Channel_MultiThreadedServer
public Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee) Constructs a new multi-threaded server with the specified parameters.- Parameters:
serverAlias- the alias of the server principal (e.g., "bob")channelSettings- the channel settings defining the port, SSL mode, etc.protocolRunner- aConsumerthat will be called for each connected client, receiving the newly createdAnB_Sessionfor that client.idleTimeoutMs- maximum idle time in milliseconds before the server shuts down automatically. Zero or negative value means no timeout (run forever).ee- the cryptographic engine
-
Channel_MultiThreadedServer
public Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, Crypto_EncryptionEngine ee) Constructs a new multi-threaded server with the default idle timeout (5 seconds).- Parameters:
serverAlias- the alias of the server principalchannelSettings- the channel settingsprotocolRunner- aConsumerthat runs the protocol for each clientee- the cryptographic engine
-
Channel_MultiThreadedServer
public Channel_MultiThreadedServer(String serverAlias, Channel_Settings channelSettings, Consumer<AnB_Session> protocolRunner, long idleTimeoutMs, Crypto_EncryptionEngine ee, Channel_MessageSerializer serializer) Constructs a new multi-threaded server that uses Protocol Buffers serialisation for every accepted client session. The server is created with the same TCP/TLS configuration asChannel_MultiThreadedServer(String, Channel_Settings, Consumer, long, Crypto_EncryptionEngine), but additionally accepts aChannel_MessageSerializerthat maps protocol steps to their protobuf message schemas. When this constructor is used, each accepted connection will be wrapped in aChannel_Protobufrather than a standard Java-serialisation channel.- Parameters:
serverAlias- the alias of the server principal (e.g., "bob")channelSettings- the channel settings defining port, SSL mode, etc.protocolRunner- aConsumerinvoked for each connected client, receiving the newly createdAnB_SessionidleTimeoutMs- maximum idle time in milliseconds before the server shuts down automatically; a value ≤ 0 means run foreveree- the cryptographic engine used to create sessionsserializer- the protocol-specific message serializer for Protobuf serialisation; must not benull- See Also:
-
-
Method Details
-
start
public void start()Starts the server. This method blocks until the server shuts down (either due to an idle timeout or an explicit call tostop()).The server socket is created according to the channel settings. If SSL is required, an
SSLServerSocketis created and configured with client authentication and cipher suites. Accepted sockets are passed directly to the client handler.Each accepted client connection is handled in a separate thread from an internal thread pool. The
protocolRunneris invoked with a freshAnB_Sessioncreated for that client.- Throws:
IllegalStateException- if the server socket cannot be created (e.g., port already in use).
-
stop
public void stop()Signals the server to stop accepting new connections and to shut down gracefully after all currently running client handlers have finished.This method does not wait for the shutdown to complete; it merely sets the
runningflag tofalse. The server loop will then exit and the thread pool will be shut down.
-