Add startup connection timeout

This commit is contained in:
crschnick 2023-11-30 17:12:16 +00:00
parent 67b2a7957f
commit ba843e8b13
2 changed files with 22 additions and 23 deletions

View file

@ -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<Integer> {
final List<String> 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,26 +67,19 @@ public class LauncherCommand implements Callable<Integer> {
cmd.execute(args);
}
private static TrackEvent.TrackEventBuilder event(String msg) {
return TrackEvent.builder().category("launcher").type("debug").message(msg);
}
private void checkStart() {
try {
if (BeaconServer.isRunning()) {
try (var con = new LauncherConnection()) {
con.constructSocket();
con.performSimpleExchange(
FocusExchange.Request.builder().mode(getEffectiveMode()).build());
con.performSimpleExchange(FocusExchange.Request.builder().mode(getEffectiveMode()).build());
if (!inputs.isEmpty()) {
con.performSimpleExchange(
OpenExchange.Request.builder().arguments(inputs).build());
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());
con.performSimpleExchange(OpenExchange.Request.builder().arguments(List.of(e.getURI().toString())).build());
});
ThreadHelper.sleep(1000);
}
@ -93,6 +87,10 @@ public class LauncherCommand implements Callable<Integer> {
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
// there might be another instance running, for example

View file

@ -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;