Interface ServerManager

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
PerClientServerManager, SharedServerManager

public interface ServerManager extends Closeable
Manages the lifecycle of a PipesServer process and client connections.

Implementations handle starting, monitoring, and restarting the server process. In per-client mode (default), each PipesClient has its own ServerManager. In shared mode, multiple PipesClients share a single ServerManager.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    connect(int socketTimeoutMs)
    Establishes a connection to the server and returns a connected Socket.
    void
    Ensures the server is running, starting or restarting it if necessary.
    int
    Returns the port number the server is listening on.
    Returns the path to the temporary directory used by the server.
    default int
    Handles a crash by checking the process exit code and marking for restart.
    default void
    incrementFilesProcessed(long maxFilesPerProcess)
    Increments the count of files processed and marks for restart if limit reached.
    boolean
    Checks if the server process is currently running.
    default void
    Marks the server for restart due to a fatal error (OOM, timeout, etc.).
    default boolean
    Checks if the server has been marked for restart.
    void
    Shuts down the server process and cleans up resources.

    Methods inherited from interface java.io.Closeable

    close
  • Method Details

    • getPort

      int getPort()
      Returns the port number the server is listening on. May return -1 if the server hasn't been started yet.
      Returns:
      the server port, or -1 if not started
    • ensureRunning

      Ensures the server is running, starting or restarting it if necessary.

      In shared mode, this method is synchronized to prevent multiple clients from attempting to restart the server simultaneously.

      Throws:
      IOException - if the server cannot be started
      InterruptedException - if interrupted while waiting for server startup
      TimeoutException - if the server doesn't start within the configured timeout
      ServerInitializationException - if the server fails to initialize
    • connect

      Socket connect(int socketTimeoutMs) throws IOException, ServerInitializationException
      Establishes a connection to the server and returns a connected Socket.

      The behavior differs by implementation:

      • Per-client mode: Accepts incoming connection from the dedicated server
      • Shared mode: Connects out to the shared server

      This method should be called after ensureRunning().

      Parameters:
      socketTimeoutMs - the socket timeout in milliseconds
      Returns:
      a connected Socket ready for communication
      Throws:
      IOException - if connection fails
      ServerInitializationException - if the server died before connecting
    • shutdown

      void shutdown() throws InterruptedException
      Shuts down the server process and cleans up resources. After calling this method, ensureRunning() can be called to restart.
      Throws:
      InterruptedException - if interrupted while waiting for shutdown
    • isRunning

      boolean isRunning()
      Checks if the server process is currently running.
      Returns:
      true if the server process is running
    • getTempDirectory

      Path getTempDirectory()
      Returns the path to the temporary directory used by the server. May return null if the server hasn't been started yet.
      Returns:
      the temp directory path, or null if not started
    • markServerForRestart

      default void markServerForRestart()
      Marks the server for restart due to a fatal error (OOM, timeout, etc.).

      This is called by clients when they receive a fatal error status from the server. It signals that the server process is stopping, even if isRunning() might still return true briefly. The next call to ensureRunning() will wait for the process to fully exit and then restart.

      In per-client mode, this is typically a no-op since the client owns the server. In shared mode, this is important for coordinating restarts among multiple clients.

    • incrementFilesProcessed

      default void incrementFilesProcessed(long maxFilesPerProcess)
      Increments the count of files processed and marks for restart if limit reached.

      This tracks progress toward the maxFilesProcessedPerProcess limit. When the limit is reached, needsRestart() will return true and the next call to ensureRunning() will restart the server.

      Parameters:
      maxFilesPerProcess - the maximum files before restart (0 means unlimited)
    • needsRestart

      default boolean needsRestart()
      Checks if the server has been marked for restart.

      This allows clients to detect that a restart is pending before attempting to use an existing connection that might be stale.

      Returns:
      true if the server has been marked for restart
    • handleCrashAndGetExitCode

      default int handleCrashAndGetExitCode()
      Handles a crash by checking the process exit code and marking for restart.

      In per-client mode, waits briefly for the process to exit and checks the exit code to determine if this was an OOM or TIMEOUT. In shared mode, just marks for restart (exit code checking is not reliable since multiple clients share the process).

      Returns:
      the exit code if available, or -1 if the process is still running or unavailable