Rework beacon to support more cli features

This commit is contained in:
Christopher Schnick 2022-02-05 00:51:16 +01:00
parent 40f9de1dc3
commit a9ee9c1bdc
11 changed files with 147 additions and 19 deletions

View file

@ -33,7 +33,8 @@ public interface DataSource {
* @return a reference to the data source that can be used to access the underlying data source * @return a reference to the data source that can be used to access the underlying data source
*/ */
static DataSource get(DataSourceId id) { static DataSource get(DataSourceId id) {
return DataSourceImpl.get(id); return null;
//return DataSourceImpl.get(id);
} }
static DataSource wrap(InputStream in, String type, Map<String, String> configOptions) { static DataSource wrap(InputStream in, String type, Map<String, String> configOptions) {

View file

@ -11,6 +11,7 @@ import io.xpipe.beacon.exchange.StoreResourceExchange;
import io.xpipe.beacon.exchange.StoreStreamExchange; import io.xpipe.beacon.exchange.StoreStreamExchange;
import io.xpipe.core.source.DataSourceConfig; import io.xpipe.core.source.DataSourceConfig;
import io.xpipe.core.source.DataSourceId; import io.xpipe.core.source.DataSourceId;
import io.xpipe.core.source.DataSourceReference;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@ -18,7 +19,7 @@ import java.util.Map;
public abstract class DataSourceImpl implements DataSource { public abstract class DataSourceImpl implements DataSource {
public static DataSource get(DataSourceId ds) { public static DataSource get(DataSourceReference ds) {
final DataSource[] source = new DataSource[1]; final DataSource[] source = new DataSource[1];
new XPipeApiConnector() { new XPipeApiConnector() {
@Override @Override

View file

@ -1,10 +1,10 @@
package io.xpipe.api.test; package io.xpipe.api.test;
import io.xpipe.beacon.BeaconClient;
import io.xpipe.beacon.BeaconServer;
import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
public class XPipeConfig implements BeforeAllCallback, ExtensionContext.Store.CloseableResource { public class XPipeConfig implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean started = false; private static boolean started = false;
@ -13,15 +13,17 @@ public class XPipeConfig implements BeforeAllCallback, ExtensionContext.Store.Cl
public void beforeAll(ExtensionContext context) throws Exception { public void beforeAll(ExtensionContext context) throws Exception {
if (!started) { if (!started) {
started = true; started = true;
// Your "before all tests" startup logic goes here if (BeaconServer.tryStart()) {
// The following line registers a callback hook when the root test context is shut down throw new AssertionError();
context.getRoot().getStore(GLOBAL).put("any unique name", this); }
//BeaconServer.start();
} }
} }
@Override @Override
public void close() { public void close() throws Exception {
// Your "after all tests" logic goes here var client = new BeaconClient();
if (BeaconServer.tryStop(client)) {
throw new AssertionError();
}
} }
} }

View file

@ -1,5 +1,7 @@
package io.xpipe.beacon; package io.xpipe.beacon;
import io.xpipe.beacon.exchange.StopExchange;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.ServerSocket; import java.net.ServerSocket;
@ -42,6 +44,11 @@ public class BeaconServer {
return false; return false;
} }
public static boolean tryStop(BeaconClient client) throws Exception {
StopExchange.Response res = client.simpleExchange(StopExchange.Request.builder().build());
return res.isSuccess();
}
private static Optional<Path> getPortableLauncherExecutable() { private static Optional<Path> getPortableLauncherExecutable() {
var env = System.getenv("XPIPE_HOME"); var env = System.getenv("XPIPE_HOME");
Path file = null; Path file = null;
@ -49,9 +56,9 @@ public class BeaconServer {
// Prepare for invalid XPIPE_HOME path value // Prepare for invalid XPIPE_HOME path value
try { try {
if (System.getProperty("os.name").startsWith("Windows")) { if (System.getProperty("os.name").startsWith("Windows")) {
file = Path.of(env, "xpipe-launcher.exe"); file = Path.of(env, "xpipe_launcher.exe");
} else { } else {
file = Path.of(env, "xpipe-launcher"); file = Path.of(env, "xpipe_launcher");
} }
return Files.exists(file) ? Optional.of(file) : Optional.empty(); return Files.exists(file) ? Optional.of(file) : Optional.empty();
} catch (Exception ex) { } catch (Exception ex) {
@ -68,9 +75,9 @@ public class BeaconServer {
try { try {
Path file = null; Path file = null;
if (System.getProperty("os.name").startsWith("Windows")) { if (System.getProperty("os.name").startsWith("Windows")) {
file = Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe", "xpipe-launcher.exe"); file = Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe", "xpipe_launcher.exe");
} else { } else {
file = Path.of("/opt/xpipe/xpipe-launcher"); file = Path.of("/opt/xpipe/xpipe_launcher");
} }
return Files.exists(file) ? Optional.of(file) : Optional.empty(); return Files.exists(file) ? Optional.of(file) : Optional.empty();
} catch (Exception ex) { } catch (Exception ex) {

View file

@ -0,0 +1,43 @@
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.DataSourceReference;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
public class EditExecuteExchange implements MessageExchange<EditExecuteExchange.Request, EditExecuteExchange.Response> {
@Override
public String getId() {
return "editExecute";
}
@Override
public Class<EditExecuteExchange.Request> getRequestClass() {
return EditExecuteExchange.Request.class;
}
@Override
public Class<EditExecuteExchange.Response> getResponseClass() {
return EditExecuteExchange.Response.class;
}
@Jacksonized
@Builder
@Value
public static class Request implements RequestMessage {
@NonNull DataSourceReference ref;
@NonNull
DataSourceConfigInstance config;
}
@Jacksonized
@Builder
@Value
public static class Response implements ResponseMessage {
}
}

View file

@ -0,0 +1,43 @@
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.DataSourceReference;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
public class EditPreparationExchange implements MessageExchange<EditPreparationExchange.Request, EditPreparationExchange.Response> {
@Override
public String getId() {
return "editPreparation";
}
@Override
public Class<EditPreparationExchange.Request> getRequestClass() {
return EditPreparationExchange.Request.class;
}
@Override
public Class<EditPreparationExchange.Response> getResponseClass() {
return EditPreparationExchange.Response.class;
}
@Jacksonized
@Builder
@Value
public static class Request implements RequestMessage {
@NonNull
DataSourceReference ref;
}
@Jacksonized
@Builder
@Value
public static class Response implements ResponseMessage {
DataSourceConfigInstance config;
}
}

View file

@ -33,5 +33,7 @@ module io.xpipe.beacon {
DialogExchange, DialogExchange,
InfoExchange, InfoExchange,
PreStoreExchange, PreStoreExchange,
EditPreparationExchange,
EditExecuteExchange,
VersionExchange; VersionExchange;
} }

View file

@ -8,6 +8,7 @@ plugins {
apply from: "$rootDir/deps/java.gradle" apply from: "$rootDir/deps/java.gradle"
apply from: "$rootDir/deps/jackson.gradle" apply from: "$rootDir/deps/jackson.gradle"
apply from: "$rootDir/deps/lombok.gradle" apply from: "$rootDir/deps/lombok.gradle"
apply from: "$rootDir/deps/junit.gradle"
apply from: 'publish.gradle' apply from: 'publish.gradle'
apply from: "$rootDir/deps/publish-base.gradle" apply from: "$rootDir/deps/publish-base.gradle"

View file

@ -21,7 +21,7 @@ public interface DataSourceReference {
return new Id(DataSourceId.fromString(s)); return new Id(DataSourceId.fromString(s));
} }
return new Name(s); return new Name(s.trim());
} }
enum Type { enum Type {

View file

@ -10,9 +10,6 @@ public class DataSourceIdTest {
@Test @Test
public void testCreateInvalidParameters() { public void testCreateInvalidParameters() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
DataSourceId.create(null, "abc");
});
Assertions.assertThrows(IllegalArgumentException.class, () -> { Assertions.assertThrows(IllegalArgumentException.class, () -> {
DataSourceId.create("a:bc", "abc"); DataSourceId.create("a:bc", "abc");
}); });
@ -46,7 +43,7 @@ public class DataSourceIdTest {
} }
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = {"abc", "abc:", "ab::c", ":abc", " :ab", "::::", "", " "}) @ValueSource(strings = {"abc", "abc:", "ab::c", "::abc", " ab", "::::", "", " "})
public void testFromStringInvalidParameters(String arg) { public void testFromStringInvalidParameters(String arg) {
Assertions.assertThrows(IllegalArgumentException.class, () -> { Assertions.assertThrows(IllegalArgumentException.class, () -> {
DataSourceId.fromString(arg); DataSourceId.fromString(arg);

View file

@ -0,0 +1,31 @@
package io.xpipe.core.test;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.core.source.DataSourceReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class DataSourceReferenceTest {
@Test
public void parseValidParameters() {
Assertions.assertEquals(DataSourceReference.parse(" ").getType(), DataSourceReference.Type.EMPTY);
Assertions.assertEquals(DataSourceReference.parse(null).getType(), DataSourceReference.Type.EMPTY);
Assertions.assertEquals(DataSourceReference.parse("abc").getType(), DataSourceReference.Type.NAME);
Assertions.assertEquals(DataSourceReference.parse(" abc_ d e").getName(), "abc_ d e");
Assertions.assertEquals(DataSourceReference.parse("ab:c").getId(), DataSourceId.fromString(" AB: C "));
Assertions.assertEquals(DataSourceReference.parse(" ab:c ").getId(), DataSourceId.fromString("ab:c "));
}
@ParameterizedTest
@ValueSource(strings = {"abc:", "ab::c", "::abc"})
public void parseInvalidParameters(String arg) {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
DataSourceReference.parse(arg);
});
}
}