Class SingleInstanceManager

java.lang.Object
gaiasky.util.SingleInstanceManager

public class SingleInstanceManager extends Object
Implements single-instance handling for Gaia Sky using a TCP loopback server socket. This works on Linux, macOS and Windows.

When a second instance of Gaia Sky is started with a gaiasky:// URL, it connects to the loopback port of the already-running instance, forwards the URL, and exits. The running instance receives the URL and processes it (typically by downloading and hot-loading the dataset).

A simple handshake token is used so that the port is not confused with other programs that may happen to occupy it.

The server side is stateful and is owned by the GaiaSky instance, like the other managers. The client side is a one-shot static utility (forwardToRunningInstance(String)), as it runs before any Gaia Sky instance exists.

  • Constructor Details

    • SingleInstanceManager

      public SingleInstanceManager()
  • Method Details

    • forwardToRunningInstance

      public static boolean forwardToRunningInstance(String url)
      Attempts to forward the given URL to an already-running Gaia Sky instance. This is a one-shot static utility, as it is called before any Gaia Sky instance exists.
      Parameters:
      url - The URL to forward.
      Returns:
      True if the URL was successfully forwarded to a running instance, false if no running instance was found.
    • startServer

      public void startServer()
      Starts the single-instance server, if it is not running yet. The server runs on a daemon thread and accepts connections from new instances. If the port is already taken (by another Gaia Sky instance or by another program), the server does not start — this is not an error, as the forwarding attempt has usually already failed in that case.
    • stopServer

      public void stopServer()
      Stops the single-instance server, if it is running.
    • getActiveInstance

      public static SingleInstanceManager getActiveInstance()
      Returns the active manager instance, if a single-instance server is running in this process. Used by the macOS open-URL handler to decide whether to forward the URL to the running instance (another process) or to store it as pending in this one.
      Returns:
      The active SingleInstanceManager, or null if the server has not been started in this process.
    • addListener

      public void addListener(SingleInstanceManager.UrlListener listener)
      Registers a listener to be notified when a URL is received from another instance. Any URLs received before this call are delivered immediately.
      Parameters:
      listener - The listener.
    • dispatchUrl

      public void dispatchUrl(String url)
      Public entry point to dispatch a URL received locally in this process (e.g. through the macOS open-URL handler when this instance is the running one). Same behavior as dispatch(String).
      Parameters:
      url - The URL.