Class SingleInstanceManager
java.lang.Object
gaiasky.util.SingleInstanceManager
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceListener interface for URLs received from other instances. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(SingleInstanceManager.UrlListener listener) Registers a listener to be notified when a URL is received from another instance.voiddispatchUrl(String url) Public entry point to dispatch a URL received locally in this process (e.g.static booleanAttempts to forward the given URL to an already-running Gaia Sky instance.static SingleInstanceManagerReturns the active manager instance, if a single-instance server is running in this process.voidStarts the single-instance server, if it is not running yet.voidStops the single-instance server, if it is running.
-
Constructor Details
-
SingleInstanceManager
public SingleInstanceManager()
-
-
Method Details
-
forwardToRunningInstance
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
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
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 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 asdispatch(String).- Parameters:
url- The URL.
-