From 40f9de1dc3ae477b8a40f152ce71c51b9cb38510 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Fri, 4 Feb 2022 01:04:08 +0100 Subject: [PATCH] Rework beacon exchanges --- .../io/xpipe/api/impl/DataSourceImpl.java | 2 +- .../java/io/xpipe/beacon/BeaconClient.java | 10 +- .../java/io/xpipe/beacon/BeaconHandler.java | 10 +- .../java/io/xpipe/beacon/BeaconServer.java | 2 +- .../xpipe/beacon/exchange/InfoExchange.java | 9 +- .../beacon/exchange/ReadExecuteExchange.java | 4 +- .../xpipe/beacon/exchange/SelectExchange.java | 6 +- .../beacon/exchange/WriteExecuteExchange.java | 4 +- .../exchange/WritePreparationExchange.java | 4 +- .../xpipe/beacon/message/ResponseMessage.java | 5 + .../io/xpipe/core/source/DataSourceId.java | 11 +- .../core/source/DataSourceReference.java | 167 ++++++++++++++++++ .../io/xpipe/core/util/CoreJacksonModule.java | 29 +++ 13 files changed, 229 insertions(+), 34 deletions(-) create mode 100644 core/src/main/java/io/xpipe/core/source/DataSourceReference.java diff --git a/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java b/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java index 8b965eb8..c55dd047 100644 --- a/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java +++ b/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java @@ -23,7 +23,7 @@ public abstract class DataSourceImpl implements DataSource { new XPipeApiConnector() { @Override protected void handle(BeaconClient sc) throws ClientException, ServerException, ConnectorException { - var req = InfoExchange.Request.builder().id(ds).build(); + var req = InfoExchange.Request.builder().ref(ds).build(); InfoExchange.Response res = performSimpleExchange(sc, req); } diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java index f29eef39..b9b1f953 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java @@ -44,11 +44,13 @@ public class BeaconClient implements AutoCloseable { void accept(T var1) throws E; } - public static Optional tryConnect() { - if (BeaconConfig.debugEnabled()) { - System.out.println("Attempting connection to server at port " + BeaconConfig.getUsedPort()); - } + @FunctionalInterface + public interface FailableRunnable { + void run() throws E; + } + + public static Optional tryConnect() { try { return Optional.of(new BeaconClient()); } catch (IOException ex) { diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconHandler.java b/beacon/src/main/java/io/xpipe/beacon/BeaconHandler.java index e7819d06..83b41367 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconHandler.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconHandler.java @@ -1,22 +1,16 @@ package io.xpipe.beacon; -import io.xpipe.beacon.message.ResponseMessage; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public interface BeaconHandler { + void postResponse(BeaconClient.FailableRunnable r); + void prepareBody() throws IOException; InputStream startBodyRead() throws IOException; - public void sendResponse(T obj) throws Exception; - - public void sendClientErrorResponse(String message) throws Exception; - - public void sendServerErrorResponse(Throwable ex) throws Exception; - OutputStream getOutputStream() throws Exception; } diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java b/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java index cdf35d87..4d91c72f 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java @@ -25,7 +25,7 @@ public class BeaconServer { public static boolean tryStart() throws Exception { var custom = BeaconConfig.getCustomExecCommand(); if (custom != null) { - new ProcessBuilder("cmd", "/c", "CALL", custom).inheritIO().start(); + Runtime.getRuntime().exec(custom); return true; } diff --git a/beacon/src/main/java/io/xpipe/beacon/exchange/InfoExchange.java b/beacon/src/main/java/io/xpipe/beacon/exchange/InfoExchange.java index 6a6a3509..94a9ec68 100644 --- a/beacon/src/main/java/io/xpipe/beacon/exchange/InfoExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/exchange/InfoExchange.java @@ -3,10 +3,11 @@ package io.xpipe.beacon.exchange; import io.xpipe.beacon.message.RequestMessage; import io.xpipe.beacon.message.ResponseMessage; import io.xpipe.core.source.DataSourceConfigInstance; -import io.xpipe.core.source.DataSourceId; import io.xpipe.core.source.DataSourceInfo; +import io.xpipe.core.source.DataSourceReference; import io.xpipe.core.store.DataStore; import lombok.Builder; +import lombok.NonNull; import lombok.Value; import lombok.extern.jackson.Jacksonized; @@ -31,15 +32,19 @@ public class InfoExchange implements MessageExchange { + + @Override + public void serialize(DataSourceReference value, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeString(value.toRefString()); + } + } + + public static class DataSourceReferenceDeserializer extends JsonDeserializer { + + @Override + public DataSourceReference deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return DataSourceReference.parse(p.getValueAsString()); + } } public static class CharsetSerializer extends JsonSerializer { @@ -77,4 +102,8 @@ public class CoreJacksonModule extends SimpleModule { @JsonSerialize(as = Throwable.class) public abstract static class ThrowableTypeMixIn { } + + @JsonSerialize(as = DataSourceReference.class) + public abstract static class DataSourceReferenceTypeMixIn { + } }