More fixes for proxies

This commit is contained in:
Christopher Schnick 2022-11-26 16:44:09 +01:00
parent 097d23f306
commit 696dc036ac
8 changed files with 39 additions and 16 deletions

View file

@ -11,7 +11,7 @@ import com.fasterxml.jackson.databind.node.TextNode;
import io.xpipe.beacon.exchange.MessageExchanges; import io.xpipe.beacon.exchange.MessageExchanges;
import io.xpipe.beacon.exchange.data.ClientErrorMessage; import io.xpipe.beacon.exchange.data.ClientErrorMessage;
import io.xpipe.beacon.exchange.data.ServerErrorMessage; 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 io.xpipe.core.util.JacksonMapper;
import lombok.Builder; import lombok.Builder;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -104,7 +104,7 @@ public class BeaconClient implements AutoCloseable {
return client; 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()); var client = new BeaconClient(() -> {}, control.getStdout(), control.getStdin());
client.sendObject(JacksonMapper.newMapper().valueToTree(information)); client.sendObject(JacksonMapper.newMapper().valueToTree(information));
return client; return client;

View file

@ -1,5 +1,6 @@
package io.xpipe.beacon.exchange; package io.xpipe.beacon.exchange;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.xpipe.beacon.RequestMessage; import io.xpipe.beacon.RequestMessage;
import io.xpipe.beacon.ResponseMessage; import io.xpipe.beacon.ResponseMessage;
import lombok.Builder; import lombok.Builder;
@ -7,8 +8,6 @@ import lombok.NonNull;
import lombok.Value; import lombok.Value;
import lombok.extern.jackson.Jacksonized; import lombok.extern.jackson.Jacksonized;
import java.util.List;
public class NamedFunctionExchange implements MessageExchange { public class NamedFunctionExchange implements MessageExchange {
@Override @Override
@ -23,7 +22,8 @@ public class NamedFunctionExchange implements MessageExchange {
@NonNull @NonNull
String id; String id;
@NonNull List<Object> arguments; @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property="type")
@NonNull Object[] arguments;
} }
@Jacksonized @Jacksonized

View file

@ -74,6 +74,13 @@ public class JacksonMapper {
return INSTANCE.copy(); return INSTANCE.copy();
} }
public static ObjectMapper getDefault() {
if (!JacksonMapper.isInit()) {
return DEFAULT;
}
return INSTANCE;
}
public static boolean isInit() { public static boolean isInit() {
return init; return init;
} }

View file

@ -12,7 +12,7 @@ public class JacksonizedValue {
@SneakyThrows @SneakyThrows
public final String toString() { public final String toString() {
var tree = JacksonMapper.newMapper().valueToTree(this); var tree = JacksonMapper.getDefault().valueToTree(this);
return tree.toPrettyString(); return tree.toPrettyString();
} }
@ -25,14 +25,14 @@ public class JacksonizedValue {
return false; return false;
} }
var tree = JacksonMapper.newMapper().valueToTree(this); var tree = JacksonMapper.getDefault().valueToTree(this);
var otherTree = JacksonMapper.newMapper().valueToTree(o); var otherTree = JacksonMapper.getDefault().valueToTree(o);
return tree.equals(otherTree); return tree.equals(otherTree);
} }
@Override @Override
public final int hashCode() { public final int hashCode() {
var tree = JacksonMapper.newMapper().valueToTree(this); var tree = JacksonMapper.getDefault().valueToTree(this);
return tree.hashCode(); return tree.hashCode();
} }
} }

View file

@ -33,7 +33,7 @@ public class XPipeInstallation {
public static Optional<String> getInstallationExecutable(ShellProcessControl p) throws Exception { public static Optional<String> getInstallationExecutable(ShellProcessControl p) throws Exception {
var installation = getDefaultInstallationBasePath(p); var installation = getDefaultInstallationBasePath(p);
var executable = FileNames.join(installation, getDaemonExecutableInInstallationDirectory(p.getOsType())); var executable = getDaemonExecutableInInstallationDirectory(p.getOsType());
var file = FileNames.join(installation, executable); var file = FileNames.join(installation, executable);
try (CommandProcessControl c = try (CommandProcessControl c =
p.command(p.getShellType().createFileExistsCommand(file)).start()) { p.command(p.getShellType().createFileExistsCommand(file)).start()) {

View file

@ -1,6 +1,6 @@
package io.xpipe.extension; package io.xpipe.extension;
import io.xpipe.api.connector.XPipeConnection; import io.xpipe.beacon.exchange.NamedFunctionExchange;
import io.xpipe.extension.event.ErrorEvent; import io.xpipe.extension.event.ErrorEvent;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -35,17 +35,21 @@ public class NamedFunction {
return get(id).callLocal(args); return get(id).callLocal(args);
} }
@SneakyThrows
public static <T> T callRemote(String id, Object... args) { public static <T> T callRemote(String id, Object... args) {
XPipeConnection.execute(con -> { var proxy = XPipeProxy.getProxy(args[0]);
con.sendRequest(null); var client = XPipeProxy.connect(proxy);
}); client.sendRequest(
return get(id).callLocal(args); NamedFunctionExchange.Request.builder().id(id).arguments(args).build());
NamedFunctionExchange.Response response = client.receiveResponse();
return (T) response.getReturnValue();
} }
@SneakyThrows @SneakyThrows
public static <T> T call(Class<? extends NamedFunction> clazz, Object... args) { public static <T> T call(Class<? extends NamedFunction> clazz, Object... args) {
var base = args[0]; var base = args[0];
if (base instanceof Proxyable) { var proxy = XPipeProxy.getProxy(base);
if (proxy != null) {
return callRemote(clazz.getDeclaredConstructor().newInstance().getId(), args); return callRemote(clazz.getDeclaredConstructor().newInstance().getId(), args);
} else { } else {
return callLocal(clazz.getDeclaredConstructor().newInstance().getId(), args); return callLocal(clazz.getDeclaredConstructor().newInstance().getId(), args);

View file

@ -3,6 +3,7 @@ package io.xpipe.extension;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.xpipe.api.connector.XPipeConnection; import io.xpipe.api.connector.XPipeConnection;
import io.xpipe.beacon.BeaconClient;
import io.xpipe.beacon.exchange.ProxyReadConnectionExchange; import io.xpipe.beacon.exchange.ProxyReadConnectionExchange;
import io.xpipe.core.impl.InputStreamStore; import io.xpipe.core.impl.InputStreamStore;
import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellProcessControl;
@ -20,6 +21,16 @@ import java.util.function.Function;
public class XPipeProxy { 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 @SneakyThrows
private static DataSource<?> downstreamTransform(DataSource<?> input, ShellStore proxy) { private static DataSource<?> downstreamTransform(DataSource<?> input, ShellStore proxy) {
var proxyNode = JacksonMapper.newMapper().valueToTree(proxy); var proxyNode = JacksonMapper.newMapper().valueToTree(proxy);

View file

@ -35,6 +35,7 @@ public class XPipeServiceProviders {
SupportedApplicationProviders.loadAll(layer); SupportedApplicationProviders.loadAll(layer);
PrefsProviders.init(layer); PrefsProviders.init(layer);
NamedFunction.init(layer);
TrackEvent.info("Finished loading extension providers"); TrackEvent.info("Finished loading extension providers");
} }
} }