Introduce API for daemon modes

This commit is contained in:
Christopher Schnick 2023-01-09 06:19:02 +01:00
parent 2f54a79407
commit b647c24821
9 changed files with 69 additions and 12 deletions

View file

@ -6,6 +6,7 @@ import io.xpipe.beacon.BeaconException;
import io.xpipe.beacon.BeaconServer;
import io.xpipe.beacon.exchange.cli.DialogExchange;
import io.xpipe.core.dialog.DialogReference;
import io.xpipe.core.util.XPipeDaemonMode;
import io.xpipe.core.util.XPipeInstallation;
import java.util.Optional;
@ -133,7 +134,7 @@ public final class XPipeApiConnection extends BeaconConnection {
private void start() throws Exception {
var installation = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
BeaconServer.start(installation);
BeaconServer.start(installation, XPipeDaemonMode.BACKGROUND);
}
@FunctionalInterface

View file

@ -1,5 +1,6 @@
package io.xpipe.beacon;
import io.xpipe.core.util.XPipeDaemonMode;
import io.xpipe.core.util.XPipeInstallation;
import java.io.IOException;
@ -8,7 +9,7 @@ public class BeaconDaemonController {
private static boolean alreadyStarted;
public static void start() throws Exception {
public static void start(XPipeDaemonMode mode) throws Exception {
if (BeaconServer.isRunning()) {
alreadyStarted = true;
return;
@ -20,7 +21,7 @@ public class BeaconDaemonController {
custom = true;
} else {
var defaultBase = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
process = BeaconServer.start(defaultBase);
process = BeaconServer.start(defaultBase, mode);
}
waitForStartup(process, custom);

View file

@ -4,6 +4,7 @@ import io.xpipe.beacon.exchange.StopExchange;
import io.xpipe.core.impl.FileNames;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellTypes;
import io.xpipe.core.util.XPipeDaemonMode;
import io.xpipe.core.util.XPipeInstallation;
import java.io.BufferedReader;
@ -39,10 +40,10 @@ public class BeaconServer {
return null;
}
public static Process start(String installationBase) throws Exception {
public static Process start(String installationBase, XPipeDaemonMode mode) throws Exception {
String command;
if (!BeaconConfig.launchDaemonInDebugMode()) {
command = XPipeInstallation.createExternalAsyncLaunchCommand(installationBase, BeaconConfig.getDaemonArguments());
command = XPipeInstallation.createExternalAsyncLaunchCommand(installationBase, mode, BeaconConfig.getDaemonArguments());
} else {
command = XPipeInstallation.createExternalLaunchCommand(
getDaemonDebugExecutable(installationBase), BeaconConfig.getDaemonArguments());

View file

@ -2,7 +2,9 @@ package io.xpipe.beacon.exchange;
import io.xpipe.beacon.RequestMessage;
import io.xpipe.beacon.ResponseMessage;
import io.xpipe.core.util.XPipeDaemonMode;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
@ -17,6 +19,8 @@ public class FocusExchange implements MessageExchange {
@Builder
@Value
public static class Request implements RequestMessage {
@NonNull
XPipeDaemonMode mode;
}
@Jacksonized

View file

@ -3,6 +3,7 @@ package io.xpipe.beacon.exchange.cli;
import io.xpipe.beacon.RequestMessage;
import io.xpipe.beacon.ResponseMessage;
import io.xpipe.beacon.exchange.MessageExchange;
import io.xpipe.core.util.XPipeDaemonMode;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
@ -20,12 +21,14 @@ public class ModeExchange implements MessageExchange {
@Value
public static class Request implements RequestMessage {
@NonNull
String modeId;
XPipeDaemonMode mode;
}
@Jacksonized
@Builder
@Value
public static class Response implements ResponseMessage {
@NonNull
XPipeDaemonMode usedMode;
}
}

View file

@ -4,13 +4,17 @@ import java.util.ServiceLoader;
public abstract class SecretProvider {
private static final SecretProvider INSTANCE = ServiceLoader.load(ModuleLayer.boot(), SecretProvider.class).findFirst().orElseThrow();
private static SecretProvider INSTANCE;
public abstract byte[] encrypt(byte[] c);
public abstract byte[] decrypt(byte[] c);
public static SecretProvider get() {
if (INSTANCE == null) {
INSTANCE = ServiceLoader.load(ModuleLayer.boot(), SecretProvider.class).findFirst().orElseThrow();
}
return INSTANCE;
}
}

View file

@ -0,0 +1,42 @@
package io.xpipe.core.util;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public enum XPipeDaemonMode {
@JsonProperty("background")
BACKGROUND("background", List.of("base", "background")),
@JsonProperty("tray")
TRAY("tray", List.of("tray", "taskbar")),
@JsonProperty("gui")
GUI("gui", List.of("gui", "desktop", "interface"));
public static XPipeDaemonMode get(String name) {
return Arrays.stream(XPipeDaemonMode.values())
.filter(xPipeDaemonMode ->
xPipeDaemonMode.getNameAlternatives().contains(name.toLowerCase(Locale.ROOT)))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("Unknown mode: " + name + ". Possible values: "
+ Arrays.stream(values())
.map(XPipeDaemonMode::getDisplayName)
.collect(Collectors.joining(", "))));
}
@Getter
private final String displayName;
@Getter
private final List<String> nameAlternatives;
XPipeDaemonMode(String displayName, List<String> nameAlternatives) {
this.displayName = displayName;
this.nameAlternatives = nameAlternatives;
}
}

View file

@ -11,15 +11,15 @@ import java.util.List;
public class XPipeInstallation {
public static String createExternalAsyncLaunchCommand(String installationBase, String arguments) {
public static String createExternalAsyncLaunchCommand(String installationBase, XPipeDaemonMode mode, String arguments) {
var suffix = (arguments != null ? " " + arguments : "");
if (OsType.getLocal().equals(OsType.LINUX)) {
return "nohup \"" + installationBase + "/app/bin/xpiped\" --external" + suffix + " & disown";
return "nohup \"" + installationBase + "/app/bin/xpiped\" --mode " + mode.getDisplayName() + suffix + " & disown";
} else if (OsType.getLocal().equals(OsType.MAC)) {
return "open \"" + installationBase + "\" --args --external" + suffix;
return "open \"" + installationBase + "\" --args --mode " + mode.getDisplayName() + suffix;
}
return "\"" + FileNames.join(installationBase, XPipeInstallation.getDaemonExecutablePath(OsType.getLocal())) + "\" --external" + suffix;
return "\"" + FileNames.join(installationBase, XPipeInstallation.getDaemonExecutablePath(OsType.getLocal())) + "\" --mode " + mode.getDisplayName() + suffix;
}
public static String createExternalLaunchCommand(String command, String arguments) {

View file

@ -4,6 +4,7 @@ import io.xpipe.api.DataSource;
import io.xpipe.beacon.BeaconDaemonController;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.util.JacksonMapper;
import io.xpipe.core.util.XPipeDaemonMode;
import io.xpipe.core.util.XPipeSession;
import io.xpipe.extension.XPipeServiceProviders;
import org.junit.jupiter.api.AfterAll;
@ -30,7 +31,7 @@ public class DaemonExtensionTest extends ExtensionTest {
JacksonMapper.initModularized(ModuleLayer.boot());
XPipeServiceProviders.load(ModuleLayer.boot());
XPipeSession.init(UUID.randomUUID());
BeaconDaemonController.start();
BeaconDaemonController.start(XPipeDaemonMode.TRAY);
}
@AfterAll