From ba843e8b13c2dda7f5f59ab2c46ef70c7151f085 Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 30 Nov 2023 17:12:16 +0000 Subject: [PATCH] Add startup connection timeout --- .../xpipe/app/launcher/LauncherCommand.java | 44 +++++++++---------- .../java/io/xpipe/beacon/BeaconClient.java | 1 + 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java b/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java index 85ebd0834..c1f96758f 100644 --- a/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java +++ b/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java @@ -8,6 +8,7 @@ import io.xpipe.app.issue.LogErrorHandler; import io.xpipe.app.issue.TrackEvent; import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.util.ThreadHelper; +import io.xpipe.beacon.BeaconException; import io.xpipe.beacon.BeaconServer; import io.xpipe.beacon.exchange.FocusExchange; import io.xpipe.beacon.exchange.OpenExchange; @@ -40,7 +41,7 @@ public class LauncherCommand implements Callable { final List inputs = List.of(); public static void runLauncher(String[] args) { - event("Launcher received commands: " + Arrays.asList(args)); + TrackEvent.builder().category("launcher").type("debug").message("Launcher received commands: " + Arrays.asList(args)).handle(); var cmd = new CommandLine(new LauncherCommand()); cmd.setExecutionExceptionHandler((ex, commandLine, parseResult) -> { @@ -66,32 +67,29 @@ public class LauncherCommand implements Callable { cmd.execute(args); } - private static TrackEvent.TrackEventBuilder event(String msg) { - return TrackEvent.builder().category("launcher").type("debug").message(msg); - } - private void checkStart() { - if (BeaconServer.isRunning()) { - try (var con = new LauncherConnection()) { - con.constructSocket(); - con.performSimpleExchange( - FocusExchange.Request.builder().mode(getEffectiveMode()).build()); - if (!inputs.isEmpty()) { - con.performSimpleExchange( - OpenExchange.Request.builder().arguments(inputs).build()); - } + try { + if (BeaconServer.isRunning()) { + try (var con = new LauncherConnection()) { + con.constructSocket(); + con.performSimpleExchange(FocusExchange.Request.builder().mode(getEffectiveMode()).build()); + if (!inputs.isEmpty()) { + con.performSimpleExchange(OpenExchange.Request.builder().arguments(inputs).build()); + } - if (OsType.getLocal().equals(OsType.MACOS)) { - Desktop.getDesktop().setOpenURIHandler(e -> { - con.performSimpleExchange(OpenExchange.Request.builder() - .arguments(List.of(e.getURI().toString())) - .build()); - }); - ThreadHelper.sleep(1000); + if (OsType.getLocal().equals(OsType.MACOS)) { + Desktop.getDesktop().setOpenURIHandler(e -> { + con.performSimpleExchange(OpenExchange.Request.builder().arguments(List.of(e.getURI().toString())).build()); + }); + ThreadHelper.sleep(1000); + } } + TrackEvent.info("Another instance is already running on this port. Quitting ..."); + OperationMode.halt(1); } - TrackEvent.info("Another instance is already running on this port. Quitting ..."); - OperationMode.halt(1); + } catch (Exception ex) { + ErrorEvent.fromThrowable(ex).description("Unable to connect to existing running daemon instance as it did not respond." + + " Either try to kill the process xpiped manually or use the command xpipe daemon stop --force if the CLI is in your path.").handle(); } // Even in case we are unable to reach another beacon server diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java index 0adc872a8..71cc68329 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java @@ -46,6 +46,7 @@ public class BeaconClient implements AutoCloseable { public static BeaconClient connect(ClientInformation information) throws Exception { var socket = new Socket(InetAddress.getLoopbackAddress(), BeaconConfig.getUsedPort()); + socket.setSoTimeout(5000); var client = new BeaconClient(socket, socket.getInputStream(), socket.getOutputStream()); client.sendObject(JacksonMapper.getDefault().valueToTree(information)); return client;