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; package io.xpipe.beacon;
import io.xpipe.app.Main;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.beacon.socket.SocketServer; import io.xpipe.beacon.socket.SocketServer;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.nio.file.Files; import java.nio.file.Files;
@ -52,7 +51,15 @@ public class XPipeDaemon {
throw new IllegalArgumentException("Unable to find xpipe daemon installation"); throw new IllegalArgumentException("Unable to find xpipe daemon installation");
} }
private static void startInProcess() { private static void startInProcess() throws Exception {
ThreadHelper.create("XPipe daemon", false, () -> Main.main(new String[0])).start(); 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.io.InputStream;
import java.net.Socket; import java.net.Socket;
public interface MessageProvider<RQ extends RequestMessage, RP extends ResponseMessage> { public interface MessageExchange<RQ extends RequestMessage, RP extends ResponseMessage> {
String getId(); String getId();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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