mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-09-02 02:31:21 +12:00
Introduce API for daemon modes
This commit is contained in:
parent
2f54a79407
commit
b647c24821
9 changed files with 69 additions and 12 deletions
|
@ -6,6 +6,7 @@ import io.xpipe.beacon.BeaconException;
|
||||||
import io.xpipe.beacon.BeaconServer;
|
import io.xpipe.beacon.BeaconServer;
|
||||||
import io.xpipe.beacon.exchange.cli.DialogExchange;
|
import io.xpipe.beacon.exchange.cli.DialogExchange;
|
||||||
import io.xpipe.core.dialog.DialogReference;
|
import io.xpipe.core.dialog.DialogReference;
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import io.xpipe.core.util.XPipeInstallation;
|
import io.xpipe.core.util.XPipeInstallation;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -133,7 +134,7 @@ public final class XPipeApiConnection extends BeaconConnection {
|
||||||
|
|
||||||
private void start() throws Exception {
|
private void start() throws Exception {
|
||||||
var installation = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
|
var installation = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
|
||||||
BeaconServer.start(installation);
|
BeaconServer.start(installation, XPipeDaemonMode.BACKGROUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package io.xpipe.beacon;
|
package io.xpipe.beacon;
|
||||||
|
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import io.xpipe.core.util.XPipeInstallation;
|
import io.xpipe.core.util.XPipeInstallation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -8,7 +9,7 @@ public class BeaconDaemonController {
|
||||||
|
|
||||||
private static boolean alreadyStarted;
|
private static boolean alreadyStarted;
|
||||||
|
|
||||||
public static void start() throws Exception {
|
public static void start(XPipeDaemonMode mode) throws Exception {
|
||||||
if (BeaconServer.isRunning()) {
|
if (BeaconServer.isRunning()) {
|
||||||
alreadyStarted = true;
|
alreadyStarted = true;
|
||||||
return;
|
return;
|
||||||
|
@ -20,7 +21,7 @@ public class BeaconDaemonController {
|
||||||
custom = true;
|
custom = true;
|
||||||
} else {
|
} else {
|
||||||
var defaultBase = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
|
var defaultBase = XPipeInstallation.getLocalDefaultInstallationBasePath(true);
|
||||||
process = BeaconServer.start(defaultBase);
|
process = BeaconServer.start(defaultBase, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
waitForStartup(process, custom);
|
waitForStartup(process, custom);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import io.xpipe.beacon.exchange.StopExchange;
|
||||||
import io.xpipe.core.impl.FileNames;
|
import io.xpipe.core.impl.FileNames;
|
||||||
import io.xpipe.core.process.OsType;
|
import io.xpipe.core.process.OsType;
|
||||||
import io.xpipe.core.process.ShellTypes;
|
import io.xpipe.core.process.ShellTypes;
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import io.xpipe.core.util.XPipeInstallation;
|
import io.xpipe.core.util.XPipeInstallation;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -39,10 +40,10 @@ public class BeaconServer {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Process start(String installationBase) throws Exception {
|
public static Process start(String installationBase, XPipeDaemonMode mode) throws Exception {
|
||||||
String command;
|
String command;
|
||||||
if (!BeaconConfig.launchDaemonInDebugMode()) {
|
if (!BeaconConfig.launchDaemonInDebugMode()) {
|
||||||
command = XPipeInstallation.createExternalAsyncLaunchCommand(installationBase, BeaconConfig.getDaemonArguments());
|
command = XPipeInstallation.createExternalAsyncLaunchCommand(installationBase, mode, BeaconConfig.getDaemonArguments());
|
||||||
} else {
|
} else {
|
||||||
command = XPipeInstallation.createExternalLaunchCommand(
|
command = XPipeInstallation.createExternalLaunchCommand(
|
||||||
getDaemonDebugExecutable(installationBase), BeaconConfig.getDaemonArguments());
|
getDaemonDebugExecutable(installationBase), BeaconConfig.getDaemonArguments());
|
||||||
|
|
|
@ -2,7 +2,9 @@ package io.xpipe.beacon.exchange;
|
||||||
|
|
||||||
import io.xpipe.beacon.RequestMessage;
|
import io.xpipe.beacon.RequestMessage;
|
||||||
import io.xpipe.beacon.ResponseMessage;
|
import io.xpipe.beacon.ResponseMessage;
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
|
import lombok.NonNull;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
import lombok.extern.jackson.Jacksonized;
|
import lombok.extern.jackson.Jacksonized;
|
||||||
|
|
||||||
|
@ -17,6 +19,8 @@ public class FocusExchange implements MessageExchange {
|
||||||
@Builder
|
@Builder
|
||||||
@Value
|
@Value
|
||||||
public static class Request implements RequestMessage {
|
public static class Request implements RequestMessage {
|
||||||
|
@NonNull
|
||||||
|
XPipeDaemonMode mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Jacksonized
|
@Jacksonized
|
||||||
|
|
|
@ -3,6 +3,7 @@ package io.xpipe.beacon.exchange.cli;
|
||||||
import io.xpipe.beacon.RequestMessage;
|
import io.xpipe.beacon.RequestMessage;
|
||||||
import io.xpipe.beacon.ResponseMessage;
|
import io.xpipe.beacon.ResponseMessage;
|
||||||
import io.xpipe.beacon.exchange.MessageExchange;
|
import io.xpipe.beacon.exchange.MessageExchange;
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
|
@ -20,12 +21,14 @@ public class ModeExchange implements MessageExchange {
|
||||||
@Value
|
@Value
|
||||||
public static class Request implements RequestMessage {
|
public static class Request implements RequestMessage {
|
||||||
@NonNull
|
@NonNull
|
||||||
String modeId;
|
XPipeDaemonMode mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Jacksonized
|
@Jacksonized
|
||||||
@Builder
|
@Builder
|
||||||
@Value
|
@Value
|
||||||
public static class Response implements ResponseMessage {
|
public static class Response implements ResponseMessage {
|
||||||
|
@NonNull
|
||||||
|
XPipeDaemonMode usedMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,17 @@ import java.util.ServiceLoader;
|
||||||
|
|
||||||
public abstract class SecretProvider {
|
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[] encrypt(byte[] c);
|
||||||
|
|
||||||
public abstract byte[] decrypt(byte[] c);
|
public abstract byte[] decrypt(byte[] c);
|
||||||
|
|
||||||
public static SecretProvider get() {
|
public static SecretProvider get() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
INSTANCE = ServiceLoader.load(ModuleLayer.boot(), SecretProvider.class).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
42
core/src/main/java/io/xpipe/core/util/XPipeDaemonMode.java
Normal file
42
core/src/main/java/io/xpipe/core/util/XPipeDaemonMode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,15 +11,15 @@ import java.util.List;
|
||||||
|
|
||||||
public class XPipeInstallation {
|
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 : "");
|
var suffix = (arguments != null ? " " + arguments : "");
|
||||||
if (OsType.getLocal().equals(OsType.LINUX)) {
|
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)) {
|
} 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) {
|
public static String createExternalLaunchCommand(String command, String arguments) {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import io.xpipe.api.DataSource;
|
||||||
import io.xpipe.beacon.BeaconDaemonController;
|
import io.xpipe.beacon.BeaconDaemonController;
|
||||||
import io.xpipe.core.store.DataStore;
|
import io.xpipe.core.store.DataStore;
|
||||||
import io.xpipe.core.util.JacksonMapper;
|
import io.xpipe.core.util.JacksonMapper;
|
||||||
|
import io.xpipe.core.util.XPipeDaemonMode;
|
||||||
import io.xpipe.core.util.XPipeSession;
|
import io.xpipe.core.util.XPipeSession;
|
||||||
import io.xpipe.extension.XPipeServiceProviders;
|
import io.xpipe.extension.XPipeServiceProviders;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
@ -30,7 +31,7 @@ public class DaemonExtensionTest extends ExtensionTest {
|
||||||
JacksonMapper.initModularized(ModuleLayer.boot());
|
JacksonMapper.initModularized(ModuleLayer.boot());
|
||||||
XPipeServiceProviders.load(ModuleLayer.boot());
|
XPipeServiceProviders.load(ModuleLayer.boot());
|
||||||
XPipeSession.init(UUID.randomUUID());
|
XPipeSession.init(UUID.randomUUID());
|
||||||
BeaconDaemonController.start();
|
BeaconDaemonController.start(XPipeDaemonMode.TRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
|
|
Loading…
Reference in a new issue