This commit is contained in:
Christopher Schnick 2021-12-02 09:42:33 +01:00
parent 63cdfb40c5
commit a4ec9126af
14 changed files with 50 additions and 83 deletions

View file

@ -1,10 +1,9 @@
package io.xpipe.beacon;
import io.xpipe.app.Main;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.beacon.socket.SocketServer;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.nio.file.Files;
@ -52,7 +51,15 @@ public class XPipeDaemon {
throw new IllegalArgumentException("Unable to find xpipe daemon installation");
}
private static void startInProcess() {
ThreadHelper.create("XPipe daemon", false, () -> Main.main(new String[0])).start();
private static void startInProcess() throws Exception {
var mainClass = Class.forName("io.xpipe.app.Main");
var method = mainClass.getDeclaredMethod("main", String[].class);
new Thread(() -> {
try {
method.invoke(null, (Object) new String[0]);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}).start();
}
}

View file

@ -5,7 +5,7 @@ import io.xpipe.beacon.socket.SocketServer;
import java.io.InputStream;
import java.net.Socket;
public interface MessageProvider<RQ extends RequestMessage, RP extends ResponseMessage> {
public interface MessageExchange<RQ extends RequestMessage, RP extends ResponseMessage> {
String getId();

View file

@ -5,37 +5,39 @@ import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
public class MessageProviders {
public class MessageExchanges {
private static Set<MessageProvider> ALL;
private static Set<MessageExchange<?,?>> ALL;
private static void loadAll() {
if (ALL == null) {
ALL = ServiceLoader.load(MessageProvider.class).stream()
.map(ServiceLoader.Provider::get).collect(Collectors.toSet());
ALL = ServiceLoader.load(MessageExchange.class).stream()
.map(s -> (MessageExchange<?,?>) s.get()).collect(Collectors.toSet());
}
}
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageProvider<RQ, RP>> byId(String name) {
@SuppressWarnings("unchecked")
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageExchange<RQ, RP>> byId(String name) {
loadAll();
var r = ALL.stream().filter(d -> d.getId().equals(name)).findAny();
return Optional.ofNullable(r.orElse(null));
return Optional.ofNullable((MessageExchange<RQ, RP>) r.orElse(null));
}
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageProvider<RQ, RP>> byRequest(RQ req) {
@SuppressWarnings("unchecked")
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageExchange<RQ, RP>> byRequest(RQ req) {
loadAll();
var r = ALL.stream().filter(d -> d.getRequestClass().equals(req.getClass())).findAny();
return Optional.ofNullable(r.orElse(null));
return Optional.ofNullable((MessageExchange<RQ, RP>) r.orElse(null));
}
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageProvider<RQ, RP>> byResponse(RP rep) {
@SuppressWarnings("unchecked")
public static <RQ extends RequestMessage, RP extends ResponseMessage> Optional<MessageExchange<RQ, RP>> byResponse(RP rep) {
loadAll();
var r = ALL.stream().filter(d -> d.getResponseClass().equals(rep.getClass())).findAny();
return Optional.ofNullable(r.orElse(null));
return Optional.ofNullable((MessageExchange<RQ, RP>) r.orElse(null));
}
public static Set<MessageProvider> getAll() {
public static Set<MessageExchange<?,?>> getAll() {
loadAll();
return ALL;
}

View file

@ -1,15 +1,12 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;
public class ListCollectionsExchange implements MessageProvider<ListCollectionsExchange.Request, ListCollectionsExchange.Response> {
public class ListCollectionsExchange implements MessageExchange<ListCollectionsExchange.Request, ListCollectionsExchange.Response> {
@Override
public String getId() {

View file

@ -1,16 +1,12 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.storage.DataSourceStorage;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;
public class ListEntriesExchange implements MessageProvider<ListEntriesExchange.Request, ListEntriesExchange.Response> {
public class ListEntriesExchange implements MessageExchange<ListEntriesExchange.Request, ListEntriesExchange.Response> {
@Override
public String getId() {

View file

@ -1,16 +1,10 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.app.core.OperationMode;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import java.io.InputStream;
import java.net.Socket;
import java.util.stream.Collectors;
public class ModeExchange implements MessageProvider<ModeExchange.Request, ModeExchange.Response> {
public class ModeExchange implements MessageExchange<ModeExchange.Request, ModeExchange.Response> {
@Override
public String getId() {

View file

@ -1,16 +1,11 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.storage.DataSourceStorage;
import java.io.InputStream;
import java.net.Socket;
public class ReadStructureExchange implements MessageProvider<ReadStructureExchange.Request, ReadStructureExchange.Response> {
public class ReadStructureExchange implements MessageExchange<ReadStructureExchange.Request, ReadStructureExchange.Response> {
@Override
public String getId() {

View file

@ -1,16 +1,11 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.storage.DataSourceStorage;
import java.io.InputStream;
import java.net.Socket;
public class ReadTableDataExchange implements MessageProvider<ReadTableDataExchange.Request, ReadTableDataExchange.Response> {
public class ReadTableDataExchange implements MessageExchange<ReadTableDataExchange.Request, ReadTableDataExchange.Response> {
@Override
public String getId() {

View file

@ -1,17 +1,12 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.core.data.type.DataType;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.storage.DataSourceStorage;
import java.io.InputStream;
import java.net.Socket;
public class ReadTableInfoExchange implements MessageProvider<ReadTableInfoExchange.Request, ReadTableInfoExchange.Response> {
public class ReadTableInfoExchange implements MessageExchange<ReadTableInfoExchange.Request, ReadTableInfoExchange.Response> {
@Override
public String getId() {

View file

@ -1,15 +1,10 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.app.core.OperationMode;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import java.io.InputStream;
import java.net.Socket;
public class StatusExchange implements MessageProvider<StatusExchange.Request, StatusExchange.Response> {
public class StatusExchange implements MessageExchange<StatusExchange.Request, StatusExchange.Response> {
@Override
public String getId() {

View file

@ -1,15 +1,10 @@
package io.xpipe.beacon.message.impl;
import io.xpipe.app.core.AppInstallation;
import io.xpipe.beacon.socket.SocketServer;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.MessageExchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import java.io.InputStream;
import java.net.Socket;
public class VersionExchange implements MessageProvider<VersionExchange.Request, VersionExchange.Response> {
public class VersionExchange implements MessageExchange<VersionExchange.Request, VersionExchange.Response> {
@Override
public String getId() {

View file

@ -88,7 +88,7 @@ public class SocketClient {
private <T extends RequestMessage> void sendRequest(T req) throws ClientException, ConnectorException {
ObjectNode json = JacksonHelper.newMapper().valueToTree(req);
var prov = MessageProviders.byRequest(req);
var prov = MessageExchanges.byRequest(req);
if (prov.isEmpty()) {
throw new ClientException("Unknown request class " + req.getClass());
}
@ -177,7 +177,7 @@ public class SocketClient {
content.remove("phase");
//content.remove("id");
var prov = MessageProviders.byId(type);
var prov = MessageExchanges.byId(type);
if (prov.isEmpty()) {
throw new IllegalArgumentException("Unknown response id " + type);
}

View file

@ -98,8 +98,8 @@ public class SocketServer {
logger.debug("Received request: \n" + read.toPrettyString());
var req = parseRequest(read);
var prov = MessageProviders.byRequest(req).get();
prov.onRequestReceived(this, req, in, clientSocket);
var prov = MessageExchanges.byRequest(req).get();
prov.handleRequest(this, req, in, clientSocket);
} catch (SocketException ex) {
try {
ex.printStackTrace();
@ -131,7 +131,7 @@ public class SocketServer {
public <T extends ResponseMessage> void sendResponse(Socket outSocket, T obj) throws Exception {
ObjectNode json = JacksonHelper.newMapper().valueToTree(obj);
var prov = MessageProviders.byResponse(obj).get();
var prov = MessageExchanges.byResponse(obj).get();
json.set("type", new TextNode(prov.getId()));
json.set("phase", new TextNode("response"));
var msg = JsonNodeFactory.instance.objectNode();
@ -175,7 +175,7 @@ public class SocketServer {
content.remove("type");
content.remove("phase");
var prov = MessageProviders.byId(type);
var prov = MessageExchanges.byId(type);
if (prov.isEmpty()) {
throw new IllegalArgumentException();
}

View file

@ -1,7 +1,4 @@
import io.xpipe.app.core.BeaconProvider;
import io.xpipe.beacon.BeaconProviderImpl;
import io.xpipe.beacon.message.MessageProvider;
import io.xpipe.beacon.message.impl.*;
import io.xpipe.beacon.message.MessageExchange;
module io.xpipe.beacon {
exports io.xpipe.beacon;
@ -23,6 +20,5 @@ module io.xpipe.beacon {
requires org.apache.commons.lang;
uses MessageProvider;
provides MessageProvider with ListCollectionsExchange, ListEntriesExchange, ReadTableDataExchange, VersionExchange, StatusExchange, ModeExchange, ReadTableInfoExchange;
uses MessageExchange;
}