Package anbxj

Class Channel_MultiThreadedServer

java.lang.Object
anbxj.Channel_MultiThreadedServer

public class Channel_MultiThreadedServer extends Object
A multi-threaded TCP server that accepts client connections on a specified channel and runs a protocol for each connected client in a separate thread.

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 Details

    • ee

      protected final Crypto_EncryptionEngine ee
      The cryptographic engine used for creating sessions.
    • DEFAULT_IDLE_TIMEOUT_MS

      public static final long DEFAULT_IDLE_TIMEOUT_MS
      Default 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 by channelSettings, accepts up to expectedConnections client connections, and for each connection runs the given protocolRunner in 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_VALUE to fit the socket API), giving the protocol agents extra time to start up. After the first connection, the timeout reverts to idleTimeoutMs. A non-positive value (≤ 0) disables the timeout entirely (the server runs until explicitly stopped).

      When expectedConnections is not Long.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 - a Consumer that receives a fresh AnB_Session for each connected client and executes the protocol steps
      idleTimeoutMs - 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 connections
      serializer - the message serializer for wire-format encoding; null selects raw Java object serialisation (the original unframed format)
      expectedConnections - maximum number of client connections to accept; use Long.MAX_VALUE for 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 to expectedConnections. The server will use the global default message serializer (or null for 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 - a Consumer that will be called for each connected client, receiving the newly created AnB_Session for 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 sessions
      expectedConnections - the maximum number of client connections to accept. Use Long.MAX_VALUE for 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 - a Consumer that will be called for each connected client, receiving the newly created AnB_Session for 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 principal
      channelSettings - the channel settings
      protocolRunner - a Consumer that runs the protocol for each client
      ee - 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 as Channel_MultiThreadedServer(String, Channel_Settings, Consumer, long, Crypto_EncryptionEngine), but additionally accepts a Channel_MessageSerializer that maps protocol steps to their protobuf message schemas. When this constructor is used, each accepted connection will be wrapped in a Channel_Protobuf rather 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 - a Consumer invoked for each connected client, receiving the newly created AnB_Session
      idleTimeoutMs - maximum idle time in milliseconds before the server shuts down automatically; a value ≤ 0 means run forever
      ee - the cryptographic engine used to create sessions
      serializer - the protocol-specific message serializer for Protobuf serialisation; must not be null
      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 to stop()).

      The server socket is created according to the channel settings. If SSL is required, an SSLServerSocket is 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 protocolRunner is invoked with a fresh AnB_Session created 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 running flag to false. The server loop will then exit and the thread pool will be shut down.