From a4ec9126afaf6b2a73521b50a6c61861aee17396 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Thu, 2 Dec 2021 09:42:33 +0100 Subject: [PATCH] Rework --- .../java/io/xpipe/beacon/XPipeDaemon.java | 15 ++++++++--- ...sageProvider.java => MessageExchange.java} | 2 +- ...geProviders.java => MessageExchanges.java} | 26 ++++++++++--------- .../message/impl/ListCollectionsExchange.java | 7 ++--- .../message/impl/ListEntriesExchange.java | 8 ++---- .../beacon/message/impl/ModeExchange.java | 10 ++----- .../message/impl/ReadStructureExchange.java | 9 ++----- .../message/impl/ReadTableDataExchange.java | 9 ++----- .../message/impl/ReadTableInfoExchange.java | 9 ++----- .../beacon/message/impl/StatusExchange.java | 9 ++----- .../beacon/message/impl/VersionExchange.java | 9 ++----- .../io/xpipe/beacon/socket/SocketClient.java | 4 +-- .../io/xpipe/beacon/socket/SocketServer.java | 8 +++--- beacon/src/main/java/module-info.java | 8 ++---- 14 files changed, 50 insertions(+), 83 deletions(-) rename beacon/src/main/java/io/xpipe/beacon/message/{MessageProvider.java => MessageExchange.java} (85%) rename beacon/src/main/java/io/xpipe/beacon/message/{MessageProviders.java => MessageExchanges.java} (50%) diff --git a/beacon/src/main/java/io/xpipe/beacon/XPipeDaemon.java b/beacon/src/main/java/io/xpipe/beacon/XPipeDaemon.java index fa42203d9..53e404cd8 100644 --- a/beacon/src/main/java/io/xpipe/beacon/XPipeDaemon.java +++ b/beacon/src/main/java/io/xpipe/beacon/XPipeDaemon.java @@ -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(); } } diff --git a/beacon/src/main/java/io/xpipe/beacon/message/MessageProvider.java b/beacon/src/main/java/io/xpipe/beacon/message/MessageExchange.java similarity index 85% rename from beacon/src/main/java/io/xpipe/beacon/message/MessageProvider.java rename to beacon/src/main/java/io/xpipe/beacon/message/MessageExchange.java index 26eb1fe46..7e56a054d 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/MessageProvider.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/MessageExchange.java @@ -5,7 +5,7 @@ import io.xpipe.beacon.socket.SocketServer; import java.io.InputStream; import java.net.Socket; -public interface MessageProvider { +public interface MessageExchange { String getId(); diff --git a/beacon/src/main/java/io/xpipe/beacon/message/MessageProviders.java b/beacon/src/main/java/io/xpipe/beacon/message/MessageExchanges.java similarity index 50% rename from beacon/src/main/java/io/xpipe/beacon/message/MessageProviders.java rename to beacon/src/main/java/io/xpipe/beacon/message/MessageExchanges.java index fca4a74d6..a37448e0c 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/MessageProviders.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/MessageExchanges.java @@ -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 ALL; + private static Set> 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 Optional> byId(String name) { + @SuppressWarnings("unchecked") + public static Optional> 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) r.orElse(null)); } - - public static Optional> byRequest(RQ req) { + @SuppressWarnings("unchecked") + public static Optional> 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) r.orElse(null)); } - public static Optional> byResponse(RP rep) { + @SuppressWarnings("unchecked") + public static Optional> 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) r.orElse(null)); } - public static Set getAll() { + public static Set> getAll() { loadAll(); return ALL; } diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ListCollectionsExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ListCollectionsExchange.java index 522e2305c..24f2955c3 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ListCollectionsExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ListCollectionsExchange.java @@ -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 { +public class ListCollectionsExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ListEntriesExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ListEntriesExchange.java index f30edd107..f3a380a13 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ListEntriesExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ListEntriesExchange.java @@ -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 { +public class ListEntriesExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ModeExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ModeExchange.java index 520fd3fdb..23a3b1009 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ModeExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ModeExchange.java @@ -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 { +public class ModeExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadStructureExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadStructureExchange.java index 85a671f6b..b43ec3168 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadStructureExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadStructureExchange.java @@ -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 { +public class ReadStructureExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableDataExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableDataExchange.java index 9375abc04..5f985f74d 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableDataExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableDataExchange.java @@ -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 { +public class ReadTableDataExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableInfoExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableInfoExchange.java index c725df800..27993f7e0 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableInfoExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/ReadTableInfoExchange.java @@ -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 { +public class ReadTableInfoExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/StatusExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/StatusExchange.java index 74a6be683..cc5d1b01f 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/StatusExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/StatusExchange.java @@ -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 { +public class StatusExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/message/impl/VersionExchange.java b/beacon/src/main/java/io/xpipe/beacon/message/impl/VersionExchange.java index e008520dc..910ae3bda 100644 --- a/beacon/src/main/java/io/xpipe/beacon/message/impl/VersionExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/message/impl/VersionExchange.java @@ -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 { +public class VersionExchange implements MessageExchange { @Override public String getId() { diff --git a/beacon/src/main/java/io/xpipe/beacon/socket/SocketClient.java b/beacon/src/main/java/io/xpipe/beacon/socket/SocketClient.java index 8f2ca4467..af2adefcd 100644 --- a/beacon/src/main/java/io/xpipe/beacon/socket/SocketClient.java +++ b/beacon/src/main/java/io/xpipe/beacon/socket/SocketClient.java @@ -88,7 +88,7 @@ public class SocketClient { private 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); } diff --git a/beacon/src/main/java/io/xpipe/beacon/socket/SocketServer.java b/beacon/src/main/java/io/xpipe/beacon/socket/SocketServer.java index 6b88a9ad2..b2818496c 100644 --- a/beacon/src/main/java/io/xpipe/beacon/socket/SocketServer.java +++ b/beacon/src/main/java/io/xpipe/beacon/socket/SocketServer.java @@ -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 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(); } diff --git a/beacon/src/main/java/module-info.java b/beacon/src/main/java/module-info.java index 861899a3c..bafee2e45 100644 --- a/beacon/src/main/java/module-info.java +++ b/beacon/src/main/java/module-info.java @@ -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; } \ No newline at end of file