From 696dc036acd30ed1ecaafce6aca2bbb3f52e4de4 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Sat, 26 Nov 2022 16:44:09 +0100 Subject: [PATCH] More fixes for proxies --- .../main/java/io/xpipe/beacon/BeaconClient.java | 4 ++-- .../beacon/exchange/NamedFunctionExchange.java | 6 +++--- .../java/io/xpipe/core/util/JacksonMapper.java | 7 +++++++ .../io/xpipe/core/util/JacksonizedValue.java | 8 ++++---- .../io/xpipe/core/util/XPipeInstallation.java | 2 +- .../java/io/xpipe/extension/NamedFunction.java | 16 ++++++++++------ .../main/java/io/xpipe/extension/XPipeProxy.java | 11 +++++++++++ .../xpipe/extension/XPipeServiceProviders.java | 1 + 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java index 4cf23811..a3bb86d4 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java @@ -11,7 +11,7 @@ import com.fasterxml.jackson.databind.node.TextNode; import io.xpipe.beacon.exchange.MessageExchanges; import io.xpipe.beacon.exchange.data.ClientErrorMessage; import io.xpipe.beacon.exchange.data.ServerErrorMessage; -import io.xpipe.core.process.ProcessControl; +import io.xpipe.core.process.CommandProcessControl; import io.xpipe.core.util.JacksonMapper; import lombok.Builder; import lombok.EqualsAndHashCode; @@ -104,7 +104,7 @@ public class BeaconClient implements AutoCloseable { return client; } - public static BeaconClient connectGateway(ProcessControl control, GatewayClientInformation information) throws Exception { + public static BeaconClient connectGateway(CommandProcessControl control, GatewayClientInformation information) throws Exception { var client = new BeaconClient(() -> {}, control.getStdout(), control.getStdin()); client.sendObject(JacksonMapper.newMapper().valueToTree(information)); return client; diff --git a/beacon/src/main/java/io/xpipe/beacon/exchange/NamedFunctionExchange.java b/beacon/src/main/java/io/xpipe/beacon/exchange/NamedFunctionExchange.java index e7fe60ff..bbfbdd33 100644 --- a/beacon/src/main/java/io/xpipe/beacon/exchange/NamedFunctionExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/exchange/NamedFunctionExchange.java @@ -1,5 +1,6 @@ package io.xpipe.beacon.exchange; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import io.xpipe.beacon.RequestMessage; import io.xpipe.beacon.ResponseMessage; import lombok.Builder; @@ -7,8 +8,6 @@ import lombok.NonNull; import lombok.Value; import lombok.extern.jackson.Jacksonized; -import java.util.List; - public class NamedFunctionExchange implements MessageExchange { @Override @@ -23,7 +22,8 @@ public class NamedFunctionExchange implements MessageExchange { @NonNull String id; - @NonNull List arguments; + @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property="type") + @NonNull Object[] arguments; } @Jacksonized diff --git a/core/src/main/java/io/xpipe/core/util/JacksonMapper.java b/core/src/main/java/io/xpipe/core/util/JacksonMapper.java index c6771ca0..f886a3d1 100644 --- a/core/src/main/java/io/xpipe/core/util/JacksonMapper.java +++ b/core/src/main/java/io/xpipe/core/util/JacksonMapper.java @@ -74,6 +74,13 @@ public class JacksonMapper { return INSTANCE.copy(); } + public static ObjectMapper getDefault() { + if (!JacksonMapper.isInit()) { + return DEFAULT; + } + return INSTANCE; + } + public static boolean isInit() { return init; } diff --git a/core/src/main/java/io/xpipe/core/util/JacksonizedValue.java b/core/src/main/java/io/xpipe/core/util/JacksonizedValue.java index cedf6159..594f331c 100644 --- a/core/src/main/java/io/xpipe/core/util/JacksonizedValue.java +++ b/core/src/main/java/io/xpipe/core/util/JacksonizedValue.java @@ -12,7 +12,7 @@ public class JacksonizedValue { @SneakyThrows public final String toString() { - var tree = JacksonMapper.newMapper().valueToTree(this); + var tree = JacksonMapper.getDefault().valueToTree(this); return tree.toPrettyString(); } @@ -25,14 +25,14 @@ public class JacksonizedValue { return false; } - var tree = JacksonMapper.newMapper().valueToTree(this); - var otherTree = JacksonMapper.newMapper().valueToTree(o); + var tree = JacksonMapper.getDefault().valueToTree(this); + var otherTree = JacksonMapper.getDefault().valueToTree(o); return tree.equals(otherTree); } @Override public final int hashCode() { - var tree = JacksonMapper.newMapper().valueToTree(this); + var tree = JacksonMapper.getDefault().valueToTree(this); return tree.hashCode(); } } diff --git a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java index ed7b58ae..d7559b24 100644 --- a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java +++ b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java @@ -33,7 +33,7 @@ public class XPipeInstallation { public static Optional getInstallationExecutable(ShellProcessControl p) throws Exception { var installation = getDefaultInstallationBasePath(p); - var executable = FileNames.join(installation, getDaemonExecutableInInstallationDirectory(p.getOsType())); + var executable = getDaemonExecutableInInstallationDirectory(p.getOsType()); var file = FileNames.join(installation, executable); try (CommandProcessControl c = p.command(p.getShellType().createFileExistsCommand(file)).start()) { diff --git a/extension/src/main/java/io/xpipe/extension/NamedFunction.java b/extension/src/main/java/io/xpipe/extension/NamedFunction.java index 776d0ead..f49f5dea 100644 --- a/extension/src/main/java/io/xpipe/extension/NamedFunction.java +++ b/extension/src/main/java/io/xpipe/extension/NamedFunction.java @@ -1,6 +1,6 @@ package io.xpipe.extension; -import io.xpipe.api.connector.XPipeConnection; +import io.xpipe.beacon.exchange.NamedFunctionExchange; import io.xpipe.extension.event.ErrorEvent; import lombok.Getter; import lombok.SneakyThrows; @@ -35,17 +35,21 @@ public class NamedFunction { return get(id).callLocal(args); } + @SneakyThrows public static T callRemote(String id, Object... args) { - XPipeConnection.execute(con -> { - con.sendRequest(null); - }); - return get(id).callLocal(args); + var proxy = XPipeProxy.getProxy(args[0]); + var client = XPipeProxy.connect(proxy); + client.sendRequest( + NamedFunctionExchange.Request.builder().id(id).arguments(args).build()); + NamedFunctionExchange.Response response = client.receiveResponse(); + return (T) response.getReturnValue(); } @SneakyThrows public static T call(Class clazz, Object... args) { var base = args[0]; - if (base instanceof Proxyable) { + var proxy = XPipeProxy.getProxy(base); + if (proxy != null) { return callRemote(clazz.getDeclaredConstructor().newInstance().getId(), args); } else { return callLocal(clazz.getDeclaredConstructor().newInstance().getId(), args); diff --git a/extension/src/main/java/io/xpipe/extension/XPipeProxy.java b/extension/src/main/java/io/xpipe/extension/XPipeProxy.java index 41080aaf..c35a8427 100644 --- a/extension/src/main/java/io/xpipe/extension/XPipeProxy.java +++ b/extension/src/main/java/io/xpipe/extension/XPipeProxy.java @@ -3,6 +3,7 @@ package io.xpipe.extension; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import io.xpipe.api.connector.XPipeConnection; +import io.xpipe.beacon.BeaconClient; import io.xpipe.beacon.exchange.ProxyReadConnectionExchange; import io.xpipe.core.impl.InputStreamStore; import io.xpipe.core.process.ShellProcessControl; @@ -20,6 +21,16 @@ import java.util.function.Function; public class XPipeProxy { + public static BeaconClient connect(ShellStore proxy) throws Exception { + var control = proxy.create().start(); + var command = control.command("xpipe beacon").start(); + return BeaconClient.connectGateway( + command, + BeaconClient.GatewayClientInformation.builder() + .version(XPipeDaemon.getInstance().getVersion()) + .build()); + } + @SneakyThrows private static DataSource downstreamTransform(DataSource input, ShellStore proxy) { var proxyNode = JacksonMapper.newMapper().valueToTree(proxy); diff --git a/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java b/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java index a7fdbbee..22e341aa 100644 --- a/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java +++ b/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java @@ -35,6 +35,7 @@ public class XPipeServiceProviders { SupportedApplicationProviders.loadAll(layer); PrefsProviders.init(layer); + NamedFunction.init(layer); TrackEvent.info("Finished loading extension providers"); } }