mirror of
https://github.com/xpipe-io/xpipe.git
synced 2026-04-25 00:52:31 -04:00
Improve beacon connection initialization and timeout
This commit is contained in:
@@ -48,12 +48,17 @@ public class BeaconClient implements AutoCloseable {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
public static BeaconClient connect(ClientInformation information) throws Exception {
|
||||
public static BeaconClient establishConnection(ClientInformation information) throws Exception {
|
||||
var socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), BeaconConfig.getUsedPort()), 5000);
|
||||
socket.setSoTimeout(5000);
|
||||
var client = new BeaconClient(socket, socket.getInputStream(), socket.getOutputStream());
|
||||
client.sendObject(JacksonMapper.getDefault().valueToTree(information));
|
||||
var res = client.receiveObject();
|
||||
if (!res.isTextual() || !"ACK".equals(res.asText())) {
|
||||
throw new BeaconException("Daemon responded with invalid acknowledgement");
|
||||
}
|
||||
socket.setSoTimeout(0);
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -102,9 +107,9 @@ public class BeaconClient implements AutoCloseable {
|
||||
};
|
||||
}
|
||||
|
||||
public static Optional<BeaconClient> tryConnect(ClientInformation information) {
|
||||
public static Optional<BeaconClient> tryEstablishConnection(ClientInformation information) {
|
||||
try {
|
||||
return Optional.of(connect(information));
|
||||
return Optional.of(establishConnection(information));
|
||||
} catch (Exception ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -190,6 +195,10 @@ public class BeaconClient implements AutoCloseable {
|
||||
}
|
||||
|
||||
public <T extends ResponseMessage> T receiveResponse() throws ConnectorException, ClientException, ServerException {
|
||||
return parseResponse(receiveObject());
|
||||
}
|
||||
|
||||
private JsonNode receiveObject() throws ConnectorException, ClientException, ServerException {
|
||||
JsonNode node;
|
||||
try (InputStream blockIn = BeaconFormat.readBlocks(in)) {
|
||||
node = JacksonMapper.getDefault().readTree(blockIn);
|
||||
@@ -216,7 +225,7 @@ public class BeaconClient implements AutoCloseable {
|
||||
throw ce.get().throwException();
|
||||
}
|
||||
|
||||
return parseResponse(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
private Optional<ClientErrorMessage> parseClientError(JsonNode node) throws ConnectorException {
|
||||
@@ -305,18 +314,6 @@ public class BeaconClient implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("reachableCheck")
|
||||
@Value
|
||||
@Builder
|
||||
@Jacksonized
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public static class ReachableCheckInformation extends ClientInformation {
|
||||
|
||||
@Override
|
||||
public String toDisplayString() {
|
||||
return "Reachable check";
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("daemon")
|
||||
@Value
|
||||
|
||||
@@ -10,7 +10,7 @@ public class BeaconDaemonController {
|
||||
private static boolean alreadyStarted;
|
||||
|
||||
public static void start(XPipeDaemonMode mode) throws Exception {
|
||||
if (BeaconServer.isRunning()) {
|
||||
if (BeaconServer.isReachable()) {
|
||||
alreadyStarted = true;
|
||||
return;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class BeaconDaemonController {
|
||||
}
|
||||
|
||||
waitForStartup(process, custom);
|
||||
if (!BeaconServer.isRunning()) {
|
||||
if (!BeaconServer.isReachable()) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
@@ -35,11 +35,11 @@ public class BeaconDaemonController {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!BeaconServer.isRunning()) {
|
||||
if (!BeaconServer.isReachable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var client = BeaconClient.connect(BeaconClient.ApiClientInformation.builder()
|
||||
var client = BeaconClient.establishConnection(BeaconClient.ApiClientInformation.builder()
|
||||
.version("?")
|
||||
.language("Java API Test")
|
||||
.build());
|
||||
@@ -65,7 +65,7 @@ public class BeaconDaemonController {
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
|
||||
var s = BeaconClient.tryConnect(BeaconClient.ApiClientInformation.builder()
|
||||
var s = BeaconClient.tryEstablishConnection(BeaconClient.ApiClientInformation.builder()
|
||||
.version("?")
|
||||
.language("Java")
|
||||
.build());
|
||||
@@ -84,7 +84,7 @@ public class BeaconDaemonController {
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
|
||||
var r = BeaconServer.isRunning();
|
||||
var r = BeaconServer.isReachable();
|
||||
if (!r) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ public class BeaconJacksonModule extends SimpleModule {
|
||||
context.registerSubtypes(
|
||||
new NamedType(BeaconClient.ApiClientInformation.class),
|
||||
new NamedType(BeaconClient.CliClientInformation.class),
|
||||
new NamedType(BeaconClient.DaemonInformation.class),
|
||||
new NamedType(BeaconClient.ReachableCheckInformation.class));
|
||||
new NamedType(BeaconClient.DaemonInformation.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import io.xpipe.core.util.XPipeInstallation;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -15,9 +18,9 @@ import java.util.List;
|
||||
*/
|
||||
public class BeaconServer {
|
||||
|
||||
public static boolean isRunning() {
|
||||
try (var ignored = BeaconClient.connect(
|
||||
BeaconClient.ReachableCheckInformation.builder().build())) {
|
||||
public static boolean isReachable() {
|
||||
try (var socket = new Socket()) {
|
||||
socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), BeaconConfig.getUsedPort()), 5000);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user