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

View file

@ -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<Object> arguments;
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property="type")
@NonNull Object[] arguments;
}
@Jacksonized

View file

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

View file

@ -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();
}
}

View file

@ -33,7 +33,7 @@ public class XPipeInstallation {
public static Optional<String> 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()) {

View file

@ -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> 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> T call(Class<? extends NamedFunction> 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);

View file

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

View file

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