Interface ServerManager
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
PerClientServerManager,SharedServerManager
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 TypeMethodDescriptionconnect(int socketTimeoutMs) Establishes a connection to the server and returns a connected Socket.voidEnsures the server is running, starting or restarting it if necessary.intgetPort()Returns the port number the server is listening on.Returns the path to the temporary directory used by the server.default intHandles a crash by checking the process exit code and marking for restart.default voidincrementFilesProcessed(long maxFilesPerProcess) Increments the count of files processed and marks for restart if limit reached.booleanChecks if the server process is currently running.default voidMarks the server for restart due to a fatal error (OOM, timeout, etc.).default booleanChecks if the server has been marked for restart.voidshutdown()Shuts down the server process and cleans up resources.
-
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
void ensureRunning() throws IOException, InterruptedException, TimeoutException, ServerInitializationExceptionEnsures 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 startedInterruptedException- if interrupted while waiting for server startupTimeoutException- if the server doesn't start within the configured timeoutServerInitializationException- if the server fails to initialize
-
connect
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 failsServerInitializationException- if the server died before connecting
-
shutdown
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 toensureRunning()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 toensureRunning()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
-