From d38612ccda6e1fccd5e1be171884476dae95e7e6 Mon Sep 17 00:00:00 2001 From: Willem Date: Fri, 29 Nov 2024 13:29:03 +0100 Subject: [PATCH] Added no2all server code --- .../nostr/client/NoStrRelayFetchInfo.java | 32 +++ .../nostr/client/NoStrRelayReadType.java | 9 + .../react/nostr/client/NoStrRtsClient.java | 71 +++++++ .../react/nostr/server/NoStrRtsServer.java | 86 ++++++++ .../relay/NoStrArtServerRelayStoreEvent.java | 22 +++ .../server/relay/NoStrRtsServerRelay.java | 65 ++++++ .../server/relay/NoStrRtsServerRelaySubs.java | 39 ++++ .../react/nostr/client/NoStrClientTest.java | 104 ++++++++++ .../react/nostr/server/NoStrServerTest.java | 65 ++++++ .../react/wire/client/No2AllActClient.java | 16 ++ .../wire/client/No2AllActClientClose.java | 28 +++ .../wire/client/No2AllActClientConnect.java | 7 + .../client/No2AllActClientSendBinary.java | 16 ++ .../client/No2AllActClientSendMessage.java | 14 ++ .../react/wire/client/No2AllArtClient.java | 11 ++ .../wire/client/No2AllArtClientOnBinary.java | 16 ++ .../wire/client/No2AllArtClientOnClose.java | 26 +++ .../wire/client/No2AllArtClientOnError.java | 14 ++ .../wire/client/No2AllArtClientOnMessage.java | 14 ++ .../wire/client/No2AllArtClientOnOpen.java | 7 + .../wire/client/No2AllArtClientOnStart.java | 11 ++ .../react/wire/client/No2AllRtsClient.java | 90 +++++++++ .../react/wire/server/No2AllActServer.java | 16 ++ .../No2AllActServerBroadcastBinary.java | 16 ++ .../No2AllActServerBroadcastMessage.java | 14 ++ .../server/No2AllActServerSocketClose.java | 28 +++ .../No2AllActServerSocketSendBinary.java | 16 ++ .../No2AllActServerSocketSendMessage.java | 14 ++ .../react/wire/server/No2AllArtServer.java | 11 ++ .../wire/server/No2AllArtServerOnError.java | 14 ++ .../wire/server/No2AllArtServerOnStart.java | 7 + .../wire/server/No2AllArtServerSocket.java | 18 ++ .../server/No2AllArtServerSocketOnBinary.java | 16 ++ .../server/No2AllArtServerSocketOnClose.java | 26 +++ .../server/No2AllArtServerSocketOnError.java | 14 ++ .../No2AllArtServerSocketOnMessage.java | 14 ++ .../react/wire/server/No2AllRtsServer.java | 108 ++++++++++ .../react/wire/server/No2AllRtsServerLog.java | 38 ++++ .../no2all/react/No2AllReact.java | 17 ++ .../no2all/react/No2AllReactListener.java | 7 + .../no2all/react/No2AllReactSlot.java | 75 +++++++ .../no2all/react/No2AllReactSlotLoad.java | 7 + .../no2all/react/No2AllReactSlotSignal.java | 30 +++ .../no2all/react/No2AllReactTypeScript.java | 7 + .../react/warp/No2AllActAbstractSlot.java | 16 ++ .../react/warp/No2AllArtWarpSlotAdd.java | 10 + .../react/warp/No2AllArtWarpSlotRemove.java | 10 + .../react/warp/No2AllReactSlotContract.java | 29 +++ .../react/warp/No2AllReactWarpCore.java | 186 ++++++++++++++++++ .../react/warp/No2AllReactWarpFuel.java | 120 +++++++++++ .../react/warp/No2AllRtsWarpSpeedMonitor.java | 30 +++ .../no2all/wire/jetty/JettyWireClient.java | 70 +++++++ .../wire/jetty/JettyWireClientEndpoint.java | 30 +++ .../wire/jetty/JettyWireClientHandler.java | 43 ++++ .../no2all/wire/jetty/JettyWireServer.java | 41 ++++ .../wire/jetty/JettyWireServerEndpoint.java | 28 +++ .../jetty/JettyWireServerEndpointCreator.java | 22 +++ .../wire/jetty/JettyWireServerHandler.java | 45 +++++ .../wire/jetty/JettyWireServerSocket.java | 45 +++++ .../no2all/wire/jetty/EventClient.java | 77 ++++++++ .../no2all/wire/jetty/EventServer.java | 102 ++++++++++ .../no2all/wire/jre/JreWireClient.java | 54 +++++ .../wire/jre/JreWireClientEndpoint.java | 29 +++ .../no2all/wire/jre/JreWireClientHandler.java | 46 +++++ .../no2all/wire/ojw/OjwWireClient.java | 42 ++++ .../wire/ojw/OjwWireClientEndpoint.java | 35 ++++ .../no2all/wire/ojw/OjwWireClientHandler.java | 46 +++++ .../no2all/wire/ojw/OjwWireServer.java | 31 +++ .../wire/ojw/OjwWireServerEndpoint.java | 28 +++ .../no2all/wire/ojw/OjwWireServerHandler.java | 81 ++++++++ .../no2all/wire/ojw/OjwWireServerSocket.java | 36 ++++ .../no2all/wire/WireClient.java | 14 ++ .../no2all/wire/WireClientEndpoint.java | 8 + .../no2all/wire/WireClientHandler.java | 18 ++ .../no2all/wire/WireServer.java | 10 + .../no2all/wire/WireServerEndpoint.java | 8 + .../no2all/wire/WireServerHandler.java | 18 ++ .../no2all/wire/WireServerSocket.java | 14 ++ .../no2all/wire/fetch/WireFetch.java | 4 + .../no2all/wire/fetch/WireFetchFactory.java | 6 + .../wire/fetch/WireFetchFactoryDefault.java | 84 ++++++++ .../no2all/wire/fetch/WireFetchHandler.java | 14 ++ .../no2all/wire/fetch/WireFetchResource.java | 39 ++++ .../conf/logback-server-console.xml | 30 +++ .../conf/logback-server.xml | 23 +++ .../no2all/zerofungus/ZFunRtsServer.java | 56 ++++++ .../zerofungus/ZFunRtsServerBooted.java | 23 +++ .../no2all/zerofungus/ZeroFungus.java | 78 ++++++++ .../service/ZFunRtsConfigJetty.java | 133 +++++++++++++ .../service/ZFunRtsConfigLogger.java | 44 +++++ .../zerofungus/service/ZFunRtsInitJdbc.java | 62 ++++++ .../zerofungus/web/RedirectServlet.java | 17 ++ .../db/migration/V00/V00_01__init_db.sql | 37 ++++ .../db/migration/V00/V00_02__init_data.sql | 3 + .../migration/V00/V00_03__h2-wcontest-428.sql | 36 ++++ .../resources/static/powp/error/errmsg.css | 35 ++++ .../resources/static/powp/error/errmsg.html | 37 ++++ 97 files changed, 3459 insertions(+) create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayFetchInfo.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayReadType.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRtsClient.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/NoStrRtsServer.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrArtServerRelayStoreEvent.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelay.java create mode 100644 nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelaySubs.java create mode 100644 nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/client/NoStrClientTest.java create mode 100644 nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/server/NoStrServerTest.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClient.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientClose.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientConnect.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendBinary.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendMessage.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClient.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnBinary.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnClose.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnError.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnMessage.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnOpen.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnStart.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllRtsClient.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServer.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastBinary.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastMessage.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketClose.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendBinary.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendMessage.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServer.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnError.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnStart.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocket.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnBinary.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnClose.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnError.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnMessage.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServer.java create mode 100644 nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServerLog.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReact.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactListener.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlot.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotLoad.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotSignal.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactTypeScript.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllActAbstractSlot.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotAdd.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotRemove.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactSlotContract.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpCore.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpFuel.java create mode 100644 nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllRtsWarpSpeedMonitor.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClient.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientEndpoint.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientHandler.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServer.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpoint.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpointCreator.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerHandler.java create mode 100644 nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerSocket.java create mode 100644 nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventClient.java create mode 100644 nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventServer.java create mode 100644 nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClient.java create mode 100644 nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientEndpoint.java create mode 100644 nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientHandler.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClient.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientEndpoint.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientHandler.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServer.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerEndpoint.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerHandler.java create mode 100644 nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerSocket.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClient.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientEndpoint.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientHandler.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServer.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerEndpoint.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerHandler.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerSocket.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetch.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactory.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactoryDefault.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchHandler.java create mode 100644 nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchResource.java create mode 100644 nx01-no2all-zerofungus/conf/logback-server-console.xml create mode 100644 nx01-no2all-zerofungus/conf/logback-server.xml create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServer.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServerBooted.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZeroFungus.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigJetty.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigLogger.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsInitJdbc.java create mode 100644 nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/web/RedirectServlet.java create mode 100644 nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_01__init_db.sql create mode 100644 nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_02__init_data.sql create mode 100644 nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_03__h2-wcontest-428.sql create mode 100644 nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.css create mode 100644 nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.html diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayFetchInfo.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayFetchInfo.java new file mode 100644 index 0000000..d40fc8f --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayFetchInfo.java @@ -0,0 +1,32 @@ +package love.distributedrebirth.no2all.react.nostr.client; + +import java.io.IOException; +import java.io.StringReader; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + + +import love.distributedrebirth.no2all.nostr.model.NoStrRelayInfo; + +import java.net.http.HttpResponse.BodyHandlers; + +import jakarta.json.Json; +import jakarta.json.JsonReader; + +public class NoStrRelayFetchInfo { + + public NoStrRelayInfo fetchRelayInfo(URI uri) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder(uri).header("Accept", "application/nostr+json").build(); + HttpResponse response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); + if (response.statusCode() != 200) { + return null; + } + String contentJson = response.body(); + try (JsonReader read = Json.createReader(new StringReader(contentJson))) { + NoStrRelayInfo result = new NoStrRelayInfo(read.readObject()); + return result; + } + } +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayReadType.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayReadType.java new file mode 100644 index 0000000..ab04b48 --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRelayReadType.java @@ -0,0 +1,9 @@ +package love.distributedrebirth.no2all.react.nostr.client; + +public enum NoStrRelayReadType { + + NONE, + ALL, + + ; +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRtsClient.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRtsClient.java new file mode 100644 index 0000000..5c3354c --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/client/NoStrRtsClient.java @@ -0,0 +1,71 @@ +package love.distributedrebirth.no2all.react.nostr.client; + +import love.distributedrebirth.no2all.nostr.NoStr; +import love.distributedrebirth.no2all.nostr.fire.NoStrFirePipe; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelay; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientAuth; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientCount; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientEose; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientEvent; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientNotice; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientOk; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayAuth; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayClose; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayCount; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayEvent; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayReq; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactListener; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.wire.client.No2AllActClientClose; +import love.distributedrebirth.no2all.react.wire.client.No2AllActClientSendMessage; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClient; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnMessage; +import love.distributedrebirth.no2all.react.wire.client.No2AllRtsClient; + +public class NoStrRtsClient implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(NoStrRtsClient.class); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.registrate(No2AllRtsClient.API, No2AllArtClient.class, artClient -> { + No2AllReactSlot clientSlot = artClient.getData().getSlot(); + react.claimIn(clientSlot, NoStrMsgToRelayAuth.class); + react.claimIn(clientSlot, NoStrMsgToRelayClose.class); + react.claimIn(clientSlot, NoStrMsgToRelayCount.class); + react.claimIn(clientSlot, NoStrMsgToRelayEvent.class); + react.claimIn(clientSlot, NoStrMsgToRelayReq.class); + react.claimOut(clientSlot, NoStrMsgToClientAuth.class); + react.claimOut(clientSlot, NoStrMsgToClientCount.class); + react.claimOut(clientSlot, NoStrMsgToClientEose.class); + react.claimOut(clientSlot, NoStrMsgToClientEvent.class); + react.claimOut(clientSlot, NoStrMsgToClientNotice.class); + react.claimOut(clientSlot, NoStrMsgToClientOk.class); + NoStrFirePipe pipeReader = NoStr.FACTORY.pipeReaderClient(reason -> { + react.fire(clientSlot, No2AllActClientClose.ofRefuse(reason)); + }, message -> { + react.fire(clientSlot, message); + }); + react.registrate(clientSlot, No2AllArtClientOnMessage.class, v -> { + pipeReader.onFire(v.getData().getMessage()); + }); + react.registrate(clientSlot, NoStrMsgToRelayAuth.class, pipeWriter(react, clientSlot)); + react.registrate(clientSlot, NoStrMsgToRelayClose.class, pipeWriter(react, clientSlot)); + react.registrate(clientSlot, NoStrMsgToRelayCount.class, pipeWriter(react, clientSlot)); + react.registrate(clientSlot, NoStrMsgToRelayEvent.class, pipeWriter(react, clientSlot)); + react.registrate(clientSlot, NoStrMsgToRelayReq.class, pipeWriter(react, clientSlot)); + }); + } + + private No2AllReactListener pipeWriter(No2AllReact react, No2AllReactSlot clientSlot) { + return v -> { + react.fire(clientSlot, new No2AllActClientSendMessage(v.getData().toBible().toString())); + }; + } +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/NoStrRtsServer.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/NoStrRtsServer.java new file mode 100644 index 0000000..9a19f50 --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/NoStrRtsServer.java @@ -0,0 +1,86 @@ +package love.distributedrebirth.no2all.react.nostr.server; + +import love.distributedrebirth.no2all.nostr.NoStr; +import love.distributedrebirth.no2all.nostr.fire.NoStrFirePipe; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClient; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientAuth; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientCount; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientEose; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientEvent; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientNotice; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientOk; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayAuth; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayClose; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayCount; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayEvent; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayReq; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactListener; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServerBroadcastMessage; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServerSocketClose; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServerSocketSendMessage; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocket; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocketOnMessage; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; + +public class NoStrRtsServer implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(NoStrRtsServer.class); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.requireSlot(API, No2AllRtsServer.API); + react.claimIn(API, NoStrMsgToClientNotice.class); + react.registrate(API, NoStrMsgToClientNotice.class, v -> { + react.fireForEach(No2AllRtsServer.API, new No2AllActServerBroadcastMessage(v.getData().toBible().toString())); + }); + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, artServer -> { + No2AllReactSlot serverSlot = artServer.getData().getSlot(); + react.claimIn(serverSlot, NoStrMsgToClientNotice.class); + react.registrate(serverSlot, No2AllArtServerSocket.class, artSocket -> { + No2AllReactSlot socketSlot = artSocket.getData().getSlot(); + react.claimIn(socketSlot, NoStrMsgToClientAuth.class); + react.claimIn(socketSlot, NoStrMsgToClientCount.class); + react.claimIn(socketSlot, NoStrMsgToClientEose.class); + react.claimIn(socketSlot, NoStrMsgToClientEvent.class); + react.claimIn(socketSlot, NoStrMsgToClientNotice.class); + react.claimIn(socketSlot, NoStrMsgToClientOk.class); + react.claimOut(socketSlot, NoStrMsgToRelayAuth.class); + react.claimOut(socketSlot, NoStrMsgToRelayClose.class); + react.claimOut(socketSlot, NoStrMsgToRelayCount.class); + react.claimOut(socketSlot, NoStrMsgToRelayEvent.class); + react.claimOut(socketSlot, NoStrMsgToRelayReq.class); + NoStrFirePipe pipeReader = NoStr.FACTORY.pipeReaderRelay(reason -> { + react.fire(socketSlot, No2AllActServerSocketClose.ofRefuse(reason)); + }, message -> { + react.fire(socketSlot, message); + }); + react.registrate(socketSlot, No2AllArtServerSocketOnMessage.class, v -> { + pipeReader.onFire(v.getData().getMessage()); + }); + react.registrate(socketSlot, NoStrMsgToClientAuth.class, pipeWriter(react, socketSlot)); + react.registrate(socketSlot, NoStrMsgToClientCount.class, pipeWriter(react, socketSlot)); + react.registrate(socketSlot, NoStrMsgToClientEose.class, pipeWriter(react, socketSlot)); + react.registrate(socketSlot, NoStrMsgToClientEvent.class, pipeWriter(react, socketSlot)); + react.registrate(socketSlot, NoStrMsgToClientNotice.class, pipeWriter(react, socketSlot)); + react.registrate(socketSlot, NoStrMsgToClientOk.class, pipeWriter(react, socketSlot)); + }); + react.registrate(serverSlot, NoStrMsgToClientNotice.class, v -> { + react.fire(serverSlot, new No2AllActServerBroadcastMessage(v.getData().toBible().toString())); + }); + }); + } + + private No2AllReactListener pipeWriter(No2AllReact react, No2AllReactSlot clientSlot) { + return v -> { + react.fire(clientSlot, new No2AllActServerSocketSendMessage(v.getData().toBible().toString())); + }; + } +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrArtServerRelayStoreEvent.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrArtServerRelayStoreEvent.java new file mode 100644 index 0000000..edbc6a2 --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrArtServerRelayStoreEvent.java @@ -0,0 +1,22 @@ +package love.distributedrebirth.no2all.react.nostr.server.relay; + +import love.distributedrebirth.no2all.nostr.model.event.NoStrEvent; + +public final class NoStrArtServerRelayStoreEvent { + + private final NoStrEvent event; + private final String remoteAddr; + + protected NoStrArtServerRelayStoreEvent(NoStrEvent event, String remoteAddr) { + this.event = event; + this.remoteAddr = remoteAddr; + } + + public NoStrEvent getEvent() { + return event; + } + + public String getRemoteAddr() { + return remoteAddr; + } +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelay.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelay.java new file mode 100644 index 0000000..772c430 --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelay.java @@ -0,0 +1,65 @@ +package love.distributedrebirth.no2all.react.nostr.server.relay; + +import java.util.logging.Logger; + +import love.distributedrebirth.no2all.nostr.model.event.NoStrEvent; +import love.distributedrebirth.no2all.nostr.model.event.NoStrEventSignature; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToClientOk; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayEvent; +import love.distributedrebirth.no2all.nostr.nip.NoStrImplMessageOkReason; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.nostr.server.NoStrRtsServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocket; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; + +public class NoStrRtsServerRelay implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(NoStrRtsServerRelay.class); + private static final Logger logger = Logger.getLogger(NoStrRtsServerRelay.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.claimOut(API, NoStrArtServerRelayStoreEvent.class); + react.requireSlot(API, NoStrRtsServer.API); + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, artServer -> { + No2AllReactSlot serverSlot = artServer.getData().getSlot(); + react.registrate(serverSlot, No2AllArtServerSocket.class, artSocket -> { + No2AllReactSlot socketSlot = artSocket.getData().getSlot(); + react.registrate(socketSlot, NoStrMsgToRelayEvent.class, v -> { + handleRelayEvent(react, v.getData().getEvent(), artSocket.getData().getRemoteAddr(), socketSlot); + }); + }); + }); + } + + private void handleRelayEvent(No2AllReact react, NoStrEvent event, String remoteAddr, No2AllReactSlot socketSlot) { + boolean eventSaved = false; + String eventResponse = ""; + NoStrImplMessageOkReason eventReason = NoStrImplMessageOkReason.DEFAULT; + + // filter on + event.getPayload().getKind(); + event.getPayload().getPublicKey(); + event.getPayload().getTags(); + + eventSaved = NoStrEventSignature.verify(event); + if (!eventSaved) { + eventReason = NoStrImplMessageOkReason.INVALID; + } + + logger.fine("Validate event: " + eventSaved + " eventId: " + event.getId()); + + + if (eventSaved) { + react.fire(API, new NoStrArtServerRelayStoreEvent(event, remoteAddr)); + } + react.fire(socketSlot, new NoStrMsgToClientOk(event.getId(), eventSaved, eventReason, eventResponse)); + } +} diff --git a/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelaySubs.java b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelaySubs.java new file mode 100644 index 0000000..c22a6d8 --- /dev/null +++ b/nx01-no2all-react-nostr/src/main/java/love/distributedrebirth/no2all/react/nostr/server/relay/NoStrRtsServerRelaySubs.java @@ -0,0 +1,39 @@ +package love.distributedrebirth.no2all.react.nostr.server.relay; + +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayClose; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayReq; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocket; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; + +public class NoStrRtsServerRelaySubs implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(NoStrRtsServerRelaySubs.class); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.requireSlot(API, NoStrRtsServerRelay.API); + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, v -> { + No2AllReactSlot serverSlot = v.getData().getSlot(); + react.registrate(serverSlot, No2AllArtServerSocket.class, x -> { + No2AllReactSlot socketSlot = x.getData().getSlot(); + react.registrate(socketSlot, NoStrMsgToRelayReq.class, y -> { + String subId = y.getData().getSubscriptionId(); + y.getData().getFilters(); + System.out.println("sub new: " + subId); + }); + react.registrate(socketSlot, NoStrMsgToRelayClose.class, y -> { + String subId = y.getData().getSubscriptionId(); + System.out.println("sub clone: " + subId); + }); + }); + }); + } +} diff --git a/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/client/NoStrClientTest.java b/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/client/NoStrClientTest.java new file mode 100644 index 0000000..acb52e9 --- /dev/null +++ b/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/client/NoStrClientTest.java @@ -0,0 +1,104 @@ +package love.distributedrebirth.no2all.react.nostr.client; + +import java.net.URI; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import love.distributedrebirth.no2all.nostr.NoStr; +import love.distributedrebirth.no2all.nostr.fire.NoStrFireWaterBoiler; +import love.distributedrebirth.no2all.nostr.model.NoStrIdentity; +import love.distributedrebirth.no2all.nostr.model.NoStrIdentityPrivateKey; +import love.distributedrebirth.no2all.nostr.model.event.NoStrEvent; +import love.distributedrebirth.no2all.nostr.model.message.NoStrMsgToRelayEvent; +import love.distributedrebirth.no2all.nostr.nip.NoStrImplEventKind; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllReactWarpCore; +import love.distributedrebirth.no2all.react.warp.No2AllRtsWarpSpeedMonitor; +import love.distributedrebirth.no2all.react.wire.client.No2AllActClient; +import love.distributedrebirth.no2all.react.wire.client.No2AllActClientClose; +import love.distributedrebirth.no2all.react.wire.client.No2AllActClientConnect; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClient; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnClose; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnError; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnMessage; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnOpen; +import love.distributedrebirth.no2all.react.wire.client.No2AllArtClientOnStart; +import love.distributedrebirth.no2all.react.wire.client.No2AllRtsClient; +import love.distributedrebirth.no2all.wire.ojw.OjwWireClientEndpoint; + +public class NoStrClientTest { + + public static void main(String[] args) throws Exception { + ExecutorService executor = Executors.newFixedThreadPool(10); + NoStrIdentity nid = new NoStrIdentity(NoStrIdentityPrivateKey.ofRandom()); + No2AllReactWarpCore react = new No2AllReactWarpCore(); + react.load(new No2AllRtsWarpSpeedMonitor()); + react.load(new No2AllRtsClient()); + react.load(new NoStrRtsClient()); + + react.registrate(No2AllRtsClient.API, No2AllArtClient.class, v -> { + No2AllReactSlot clientSlot = v.getData().getSlot(); + System.out.println("NoStrClient created: " + clientSlot.getSlotPath()); + react.registrate(clientSlot, No2AllArtClientOnError.class, x -> { + System.out.println("NoStrClient socket error: " + x.getData().getError().getMessage()); + }); + react.registrate(clientSlot, No2AllArtClientOnClose.class, x -> { + System.out.println("NoStrClient socket close: " + x.getData().getCode() + " " + x.getData().getReason()); + }); + react.registrate(clientSlot, No2AllArtClientOnOpen.class, x -> { + System.out.println("NoStrClient socket open"); + + executor.submit(() -> { + try { + for (int i = 0; i < 10; i++) { + NoStrFireWaterBoiler userBoiler = NoStr.FACTORY.boiler(nid); + NoStrEvent event = userBoiler.smoker() + .tagSubject("Reply to all") + .tagR("") + .burn() + .water(NoStrImplEventKind.TEXT_NOTE, "Hello world:"+i); + + System.out.println("send msg"); + NoStrMsgToRelayEvent msg = new NoStrMsgToRelayEvent(event); + react.fire(clientSlot, msg); + Thread.sleep(1000); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + }); + }); + + react.registrate(clientSlot, No2AllArtClientOnMessage.class, y -> { + System.out.println("NoStrClient got msg1: " + y.getData().getMessage()); + }); + // Todo "OnStart" is not working... + react.registrate(clientSlot, No2AllArtClientOnStart.class, x -> { + System.out.println("NoStrClient socket start"); + }); + react.fire(clientSlot, new No2AllActClientConnect()); + }); + + OjwWireClientEndpoint clientEndpoint = new OjwWireClientEndpoint(new URI("ws://localhost:8080/event")); + System.out.println("NoStrClient start"); + react.fire(No2AllRtsClient.API, new No2AllActClient(clientEndpoint)); + + Thread.sleep(30000); + + executor.shutdown(); + executor.awaitTermination(60, TimeUnit.SECONDS); + Thread exitTimer = new Thread(() -> { + try { + Thread.sleep(2000); + } catch (InterruptedException e2) { + // TODO Auto-generated catch block + e2.printStackTrace(); + } + System.out.println("NoStrClient stopping"); + react.fireForEach(No2AllRtsClient.API, No2AllActClientClose.ofNormal("exit")); + System.out.println("NoStrClient stopped"); + }); + exitTimer.start(); + } +} diff --git a/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/server/NoStrServerTest.java b/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/server/NoStrServerTest.java new file mode 100644 index 0000000..17ff855 --- /dev/null +++ b/nx01-no2all-react-nostr/src/test/java/love/distributedrebirth/no2all/react/nostr/server/NoStrServerTest.java @@ -0,0 +1,65 @@ +package love.distributedrebirth.no2all.react.nostr.server; + +import java.net.InetSocketAddress; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrRtsServerRelay; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrRtsServerRelaySubs; +import love.distributedrebirth.no2all.react.warp.No2AllReactWarpCore; +import love.distributedrebirth.no2all.react.warp.No2AllRtsWarpSpeedMonitor; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerOnStart; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocket; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocketOnClose; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServerLog; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; +import love.distributedrebirth.no2all.wire.ojw.OjwWireServer; +import love.distributedrebirth.no2all.wire.ojw.OjwWireServerEndpoint; + +public class NoStrServerTest { + static OjwWireServer server = null; + public static void main(String[] args) throws Exception { + No2AllReactWarpCore react = new No2AllReactWarpCore(); + react.load(new No2AllRtsWarpSpeedMonitor()); + react.load(new No2AllRtsServer()); + react.load(new No2AllRtsServerLog()); + react.load(new NoStrRtsServer()); + react.load(new NoStrRtsServerRelay()); + react.load(new NoStrRtsServerRelaySubs()); + + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, artServer -> { + No2AllReactSlot serverSlot = artServer.getData().getSlot(); + react.registrate(serverSlot, No2AllArtServerOnStart.class, v -> { + System.out.println("RelayServer started: " + v.getTarget().getSlotSlug()); + }); + react.registrate(serverSlot, No2AllArtServerSocket.class, v -> { + System.out.println("RelayServer client-open: " + v.getData().getSlot().getSlotPath()); + react.registrate(v.getData().getSlot(), No2AllArtServerSocketOnClose.class, y -> { + System.out.println("RelayServer client-close: " + y.getData().getCode() + " of: " + y.getTarget().getSlotPath()); + }); + }); + react.registrate(serverSlot, OjwWireServer.class, x -> { // TODO: add interface matching ... + server = x.getData(); + }); + }); + System.out.println("RelayServer fire configs"); + OjwWireServerEndpoint serverEndpoint = new OjwWireServerEndpoint(new InetSocketAddress(8080), 120); + react.fire(No2AllRtsServer.API, new No2AllActServer(serverEndpoint)); + + for (int i=0;i<1000;i++) { + //react.fire(No2AllServerRTS.API, react); + Thread.sleep(100); + } + + Thread.sleep(40000); + + if (server != null) { + System.out.println("RelayServers stopping"); + server.getHandler().stop(); + } + + //react.fireForEach(No2AllServerRTS.API, new No2AllActServerStop()); + System.out.println("RelayServers stopped"); + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClient.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClient.java new file mode 100644 index 0000000..c1e7701 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClient.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import love.distributedrebirth.no2all.wire.WireClientEndpoint; + +public final class No2AllActClient { + + private final WireClientEndpoint endpoint; + + public No2AllActClient(WireClientEndpoint endpoint) { + this.endpoint = endpoint; + } + + public WireClientEndpoint getEndpoint() { + return endpoint; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientClose.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientClose.java new file mode 100644 index 0000000..d9a4cf5 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientClose.java @@ -0,0 +1,28 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllActClientClose { + + private final int code; + private final String message; + + public No2AllActClientClose(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + + public static No2AllActClientClose ofNormal(String message) { + return new No2AllActClientClose(1000, message); + } + + public static No2AllActClientClose ofRefuse(String message) { + return new No2AllActClientClose(1003, message); + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientConnect.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientConnect.java new file mode 100644 index 0000000..9047459 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientConnect.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllActClientConnect { + + public No2AllActClientConnect() { + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendBinary.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendBinary.java new file mode 100644 index 0000000..6baaefb --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendBinary.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import java.nio.ByteBuffer; + +public final class No2AllActClientSendBinary { + + private final ByteBuffer message; + + public No2AllActClientSendBinary(ByteBuffer message) { + this.message = message; + } + + public ByteBuffer getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendMessage.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendMessage.java new file mode 100644 index 0000000..72628d4 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllActClientSendMessage.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllActClientSendMessage { + + private final String message; + + public No2AllActClientSendMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClient.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClient.java new file mode 100644 index 0000000..0cc5786 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClient.java @@ -0,0 +1,11 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllActAbstractSlot; + +public final class No2AllArtClient extends No2AllActAbstractSlot { + + protected No2AllArtClient(No2AllReactSlot slot) { + super(slot); + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnBinary.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnBinary.java new file mode 100644 index 0000000..382115f --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnBinary.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import java.nio.ByteBuffer; + +public final class No2AllArtClientOnBinary { + + private final ByteBuffer message; + + protected No2AllArtClientOnBinary(ByteBuffer message) { + this.message = message; + } + + public ByteBuffer getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnClose.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnClose.java new file mode 100644 index 0000000..129fa9e --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnClose.java @@ -0,0 +1,26 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllArtClientOnClose { + + private final int code; + private final String reason; + private final boolean remote; + + protected No2AllArtClientOnClose(int code, String reason, boolean remote) { + this.code = code; + this.reason = reason; + this.remote = remote; + } + + public int getCode() { + return code; + } + + public String getReason() { + return reason; + } + + public boolean isRemote() { + return remote; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnError.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnError.java new file mode 100644 index 0000000..fa47579 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnError.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllArtClientOnError { + + private final Throwable error; + + protected No2AllArtClientOnError(Throwable error) { + this.error = error; + } + + public Throwable getError() { + return error; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnMessage.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnMessage.java new file mode 100644 index 0000000..7deeb1b --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnMessage.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllArtClientOnMessage { + + private final String message; + + protected No2AllArtClientOnMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnOpen.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnOpen.java new file mode 100644 index 0000000..7bb6613 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnOpen.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react.wire.client; + +public final class No2AllArtClientOnOpen { + + protected No2AllArtClientOnOpen() { + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnStart.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnStart.java new file mode 100644 index 0000000..f8010ac --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllArtClientOnStart.java @@ -0,0 +1,11 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllActAbstractSlot; + +public final class No2AllArtClientOnStart extends No2AllActAbstractSlot { + + protected No2AllArtClientOnStart(No2AllReactSlot slot) { + super(slot); + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllRtsClient.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllRtsClient.java new file mode 100644 index 0000000..bf48aae --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/client/No2AllRtsClient.java @@ -0,0 +1,90 @@ +package love.distributedrebirth.no2all.react.wire.client; + +import java.nio.ByteBuffer; + +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientEndpoint; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class No2AllRtsClient implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(No2AllRtsClient.class); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.claimIn(API, No2AllActClient.class); + react.claimOut(API, No2AllArtClient.class); + react.registrate(API, No2AllActClient.class, actClient -> { + WireClientEndpoint clientEndpoint = actClient.getData().getEndpoint(); + No2AllReactSlot clientSlot = No2AllReactSlot.of(API, "wireId=" + clientEndpoint.wireId()); + WireClient client = clientEndpoint.wireClient(wireHandler(react, clientSlot)); + react.claim(clientSlot); + react.claimIn(clientSlot, No2AllActClientConnect.class); + react.claimIn(clientSlot, No2AllActClientClose.class); + react.claimIn(clientSlot, No2AllActClientSendMessage.class); + react.claimIn(clientSlot, No2AllActClientSendBinary.class); + react.claimOut(clientSlot, No2AllArtClientOnStart.class); + react.claimOut(clientSlot, No2AllArtClientOnOpen.class); + react.claimOut(clientSlot, No2AllArtClientOnClose.class); + react.claimOut(clientSlot, No2AllArtClientOnError.class); + react.claimOut(clientSlot, No2AllArtClientOnMessage.class); + react.claimOut(clientSlot, No2AllArtClientOnBinary.class); + react.claimOut(clientSlot, WireClient.class); + react.registrate(clientSlot, No2AllActClientConnect.class, v -> { + client.connect(); + }); + react.registrate(clientSlot, No2AllActClientClose.class, v -> { + client.close(v.getData().getCode(), v.getData().getMessage()); + }); + react.registrate(clientSlot, No2AllActClientSendMessage.class, v -> { + client.sendMessage(v.getData().getMessage()); + }); + react.registrate(clientSlot, No2AllActClientSendBinary.class, v -> { + client.sendBinary(v.getData().getMessage()); + }); + react.fire(API, new No2AllArtClient(clientSlot)); + react.fire(clientSlot, client); + }); + } + + private WireClientHandler wireHandler(No2AllReact react, No2AllReactSlot clientSlot) { + return new WireClientHandler() { + @Override + public void onStart() { + react.fire(clientSlot, new No2AllArtClientOnStart(clientSlot)); + } + + @Override + public void onOpen() { + react.fire(clientSlot, new No2AllArtClientOnOpen()); + } + + @Override + public void onClose(int code, String reason, boolean remote) { + react.fire(clientSlot, new No2AllArtClientOnClose(code, reason, remote)); + } + + @Override + public void onError(Throwable error) { + react.fire(clientSlot, new No2AllArtClientOnError(error)); + } + + @Override + public void onMessage(String message) { + react.fire(clientSlot, new No2AllArtClientOnMessage(message)); + } + + @Override + public void onBinary(ByteBuffer message) { + react.fire(clientSlot, new No2AllArtClientOnBinary(message)); + } + }; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServer.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServer.java new file mode 100644 index 0000000..a1d952e --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServer.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import love.distributedrebirth.no2all.wire.WireServerEndpoint; + +public final class No2AllActServer { + + private final WireServerEndpoint endpoint; + + public No2AllActServer(WireServerEndpoint endpoint) { + this.endpoint = endpoint; + } + + public WireServerEndpoint getEndpoint() { + return endpoint; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastBinary.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastBinary.java new file mode 100644 index 0000000..8a29be6 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastBinary.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import java.nio.ByteBuffer; + +public final class No2AllActServerBroadcastBinary { + + private final ByteBuffer message; + + public No2AllActServerBroadcastBinary(ByteBuffer message) { + this.message = message; + } + + public ByteBuffer getMessage() { + return message; + } +} \ No newline at end of file diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastMessage.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastMessage.java new file mode 100644 index 0000000..fcbe07a --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerBroadcastMessage.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllActServerBroadcastMessage { + + private final String message; + + public No2AllActServerBroadcastMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} \ No newline at end of file diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketClose.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketClose.java new file mode 100644 index 0000000..24312f0 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketClose.java @@ -0,0 +1,28 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllActServerSocketClose { + + private final int code; + private final String messag; + + public No2AllActServerSocketClose(int code, String message) { + this.code = code; + this.messag = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return messag; + } + + public static No2AllActServerSocketClose ofNormal(String message) { + return new No2AllActServerSocketClose(1000, message); + } + + public static No2AllActServerSocketClose ofRefuse(String message) { + return new No2AllActServerSocketClose(1003, message); + } +} \ No newline at end of file diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendBinary.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendBinary.java new file mode 100644 index 0000000..01a656e --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendBinary.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import java.nio.ByteBuffer; + +public final class No2AllActServerSocketSendBinary { + + private final ByteBuffer message; + + public No2AllActServerSocketSendBinary(ByteBuffer message) { + this.message = message; + } + + public ByteBuffer getMessage() { + return message; + } +} \ No newline at end of file diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendMessage.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendMessage.java new file mode 100644 index 0000000..b147ce0 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllActServerSocketSendMessage.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllActServerSocketSendMessage { + + private final String message; + + public No2AllActServerSocketSendMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} \ No newline at end of file diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServer.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServer.java new file mode 100644 index 0000000..0176e7f --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServer.java @@ -0,0 +1,11 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllActAbstractSlot; + +public final class No2AllArtServer extends No2AllActAbstractSlot { + + protected No2AllArtServer(No2AllReactSlot slot) { + super(slot); + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnError.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnError.java new file mode 100644 index 0000000..b81751a --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnError.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllArtServerOnError { + + private final Throwable error; + + protected No2AllArtServerOnError(Throwable error) { + this.error = error; + } + + public Throwable getError() { + return error; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnStart.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnStart.java new file mode 100644 index 0000000..0a69368 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerOnStart.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllArtServerOnStart { + + protected No2AllArtServerOnStart() { + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocket.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocket.java new file mode 100644 index 0000000..793f8a7 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocket.java @@ -0,0 +1,18 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllActAbstractSlot; + +public final class No2AllArtServerSocket extends No2AllActAbstractSlot { + + private final String remoteAddr; + + protected No2AllArtServerSocket(No2AllReactSlot slot, String remoteAddr) { + super(slot); + this.remoteAddr = remoteAddr; + } + + public String getRemoteAddr() { + return remoteAddr; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnBinary.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnBinary.java new file mode 100644 index 0000000..8be8e6e --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnBinary.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import java.nio.ByteBuffer; + +public final class No2AllArtServerSocketOnBinary { + + private final ByteBuffer message; + + protected No2AllArtServerSocketOnBinary(ByteBuffer message) { + this.message = message; + } + + public ByteBuffer getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnClose.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnClose.java new file mode 100644 index 0000000..dc74bcd --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnClose.java @@ -0,0 +1,26 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllArtServerSocketOnClose { + + private final int code; + private final String reason; + private final boolean remote; + + protected No2AllArtServerSocketOnClose(int code, String reason, boolean remote) { + this.code = code; + this.reason = reason; + this.remote = remote; + } + + public int getCode() { + return code; + } + + public String getReason() { + return reason; + } + + public boolean isRemote() { + return remote; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnError.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnError.java new file mode 100644 index 0000000..1c5ee6a --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnError.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllArtServerSocketOnError { + + private final Throwable error; + + protected No2AllArtServerSocketOnError(Throwable error) { + this.error = error; + } + + public Throwable getError() { + return error; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnMessage.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnMessage.java new file mode 100644 index 0000000..d123dbb --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllArtServerSocketOnMessage.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.react.wire.server; + +public final class No2AllArtServerSocketOnMessage { + + private final String message; + + protected No2AllArtServerSocketOnMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServer.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServer.java new file mode 100644 index 0000000..3c548f6 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServer.java @@ -0,0 +1,108 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import java.nio.ByteBuffer; + +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.wire.WireServer; +import love.distributedrebirth.no2all.wire.WireServerEndpoint; +import love.distributedrebirth.no2all.wire.WireServerHandler; +import love.distributedrebirth.no2all.wire.WireServerSocket; + +public class No2AllRtsServer implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(No2AllRtsServer.class); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.claimIn(API, No2AllActServer.class); + react.claimOut(API, No2AllArtServer.class); + react.registrate(API, No2AllActServer.class, actServer -> { + WireServerEndpoint serverEndpoint = actServer.getData().getEndpoint(); + No2AllReactSlot serverSlot = No2AllReactSlot.of(API, "wireId=" + serverEndpoint.wireId()); + WireServer server = serverEndpoint.wireServer(wireHandler(react, serverSlot)); + react.claim(serverSlot); + react.claimIn(serverSlot, No2AllActServerBroadcastMessage.class); + react.claimIn(serverSlot, No2AllActServerBroadcastBinary.class); + react.claimOut(serverSlot, No2AllArtServerOnStart.class); + react.claimOut(serverSlot, No2AllArtServerSocket.class); + react.claimOut(serverSlot, No2AllArtServerOnError.class); + react.claimOut(serverSlot, WireServer.class); + react.registrate(serverSlot, No2AllActServerBroadcastMessage.class, v -> { + server.broadcastMessage(v.getData().getMessage()); + }); + react.registrate(serverSlot, No2AllActServerBroadcastBinary.class, v -> { + server.broadcastBinary(v.getData().getMessage()); + }); + react.fire(API, new No2AllArtServer(serverSlot)); + react.fire(serverSlot, server); + }); + } + + private WireServerHandler wireHandler(No2AllReact react, No2AllReactSlot serverSlot) { + return new WireServerHandler() { + private No2AllReactSlot socketSlug(WireServerSocket conn) { + return No2AllReactSlot.of(serverSlot, "hashCode=" + conn.hashCode()); + } + + @Override + public void onStart() { + react.fire(serverSlot, new No2AllArtServerOnStart()); + } + + @Override + public void onOpen(WireServerSocket conn) { + No2AllReactSlot socketSlot = socketSlug(conn); + react.claim(socketSlot); + react.claimIn(socketSlot, No2AllActServerSocketSendMessage.class); + react.claimIn(socketSlot, No2AllActServerSocketSendBinary.class); + react.claimIn(socketSlot, No2AllActServerSocketClose.class); + react.claimOut(socketSlot, No2AllArtServerSocketOnClose.class); + react.claimOut(socketSlot, No2AllArtServerSocketOnError.class); + react.claimOut(socketSlot, No2AllArtServerSocketOnMessage.class); + react.claimOut(socketSlot, No2AllArtServerSocketOnBinary.class); + react.registrate(socketSlot, No2AllActServerSocketSendMessage.class, v -> { + conn.sendMessage(v.getData().getMessage()); + }); + react.registrate(socketSlot, No2AllActServerSocketSendBinary.class, v -> { + conn.sendBinary(v.getData().getMessage()); + }); + react.registrate(socketSlot, No2AllActServerSocketClose.class, v -> { + conn.close(v.getData().getCode(), v.getData().getMessage()); + }); + react.fire(serverSlot, new No2AllArtServerSocket(socketSlot, conn.getRemoteAddress())); + } + + @Override + public void onClose(WireServerSocket conn, int code, String reason, boolean remote) { + No2AllReactSlot socketSlot = socketSlug(conn); + react.fire(socketSlot, new No2AllArtServerSocketOnClose(code, reason, remote)); + react.release(socketSlot); + } + + @Override + public void onError(WireServerSocket conn, Throwable error) { + if (conn != null) { + react.fire(socketSlug(conn), new No2AllArtServerSocketOnError(error)); + } else { + react.fire(serverSlot, new No2AllArtServerOnError(error)); + } + } + + @Override + public void onMessage(WireServerSocket conn, String message) { + react.fire(socketSlug(conn), new No2AllArtServerSocketOnMessage(message)); + } + + @Override + public void onBinary(WireServerSocket conn, ByteBuffer message) { + react.fire(socketSlug(conn), new No2AllArtServerSocketOnBinary(message)); + } + }; + } +} diff --git a/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServerLog.java b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServerLog.java new file mode 100644 index 0000000..93990f0 --- /dev/null +++ b/nx01-no2all-react-wire/src/main/java/love/distributedrebirth/no2all/react/wire/server/No2AllRtsServerLog.java @@ -0,0 +1,38 @@ +package love.distributedrebirth.no2all.react.wire.server; + +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; + +import java.util.logging.Logger; + +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; + +public class No2AllRtsServerLog implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(No2AllRtsServerLog.class); + private static final Logger logger = Logger.getLogger(No2AllRtsServerLog.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, artServer -> { + No2AllReactSlot serverSlot = artServer.getData().getSlot(); + react.registrate(serverSlot, No2AllActServerBroadcastMessage.class, v -> { + logger.info("broadcastMessage: " + v.getData().getMessage()); + }); + react.registrate(serverSlot, No2AllArtServerSocket.class, v -> { + No2AllReactSlot socketSlot = v.getData().getSlot(); + String wireId = socketSlot.getSlotSlug(); + react.registrate(socketSlot, No2AllArtServerSocketOnMessage.class, z -> { + logger.info(wireId+".socketOnMessage: " + z.getData().getMessage()); + }); + react.registrate(socketSlot, No2AllActServerSocketSendMessage.class, z -> { + logger.info(wireId+".socketSendMessage: " + z.getData().getMessage()); + }); + }); + }); + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReact.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReact.java new file mode 100644 index 0000000..f75807a --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReact.java @@ -0,0 +1,17 @@ +package love.distributedrebirth.no2all.react; + +public interface No2AllReact { + + void claim(No2AllReactSlot slot); + void claimIn(No2AllReactSlot slot, Class eventType); + void claimOut(No2AllReactSlot slot, Class eventType); + void requireSlot(No2AllReactSlot slot, No2AllReactSlot dep); + void requireService(No2AllReactSlot slot, Class serviceType); + + void fire(No2AllReactSlot slot, Object event); + void fireForEach(No2AllReactSlot slot, Object event); + void registrate(No2AllReactSlot slot, Class eventType, No2AllReactListener listener); + + T service(Class serviceType); + void release(No2AllReactSlot slot); +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactListener.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactListener.java new file mode 100644 index 0000000..dedcf5f --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactListener.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react; + +@FunctionalInterface +public interface No2AllReactListener { + + void onEvent(No2AllReactSlotSignal signal); +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlot.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlot.java new file mode 100644 index 0000000..d06b20b --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlot.java @@ -0,0 +1,75 @@ +package love.distributedrebirth.no2all.react; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public final class No2AllReactSlot { + + private final No2AllReactSlot parent; + private final String slotSlug; + private final String slotPath; + + private No2AllReactSlot(No2AllReactSlot parent, String slotSlug) { + this.parent = parent; + this.slotSlug = slotSlug; + this.slotPath = createSlotPath(); + } + + public No2AllReactSlot getParent() { + return parent; + } + + public String getSlotSlug() { + return slotSlug; + } + + public String getSlotPath() { + return slotPath; + } + + public String createSlotPath() { + List slugs = new ArrayList<>(); + No2AllReactSlot parentNode = this; + while (parentNode != null) { + slugs.add(parentNode.getSlotSlug()); + parentNode = parentNode.getParent(); + } + StringBuilder buf = new StringBuilder(); + for (int i = slugs.size() - 1; i >= 0; i--) { + buf.append(slugs.get(i)); + if (i > 0) { + buf.append(','); + } + } + return buf.toString(); + } + + @Override + public int hashCode() { + return Objects.hash(slotPath); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + No2AllReactSlot other = (No2AllReactSlot) obj; + return Objects.equals(slotPath, other.slotPath); + } + + public static No2AllReactSlot ofClass(Class scriptClz) { + return new No2AllReactSlot(null, scriptClz.getPackageName() + ":type=" + scriptClz.getSimpleName()); + } + + public static No2AllReactSlot of(No2AllReactSlot parent, String kvPair) { + return new No2AllReactSlot(parent, kvPair); + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotLoad.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotLoad.java new file mode 100644 index 0000000..a3dd090 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotLoad.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react; + +public final class No2AllReactSlotLoad { + + public No2AllReactSlotLoad() { + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotSignal.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotSignal.java new file mode 100644 index 0000000..aeccabf --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactSlotSignal.java @@ -0,0 +1,30 @@ +package love.distributedrebirth.no2all.react; + +public final class No2AllReactSlotSignal { + + private final No2AllReactSlot target; + private final T data; + private final No2AllReact react; + + public No2AllReactSlotSignal(No2AllReactSlot target, T data, No2AllReact react) { + this.target = target; + this.data = data; + this.react = react; + } + + public No2AllReactSlot getTarget() { + return target; + } + + public T getData() { + return data; + } + + public No2AllReact getReact() { + return react; + } + +// public T $() { +// return getData(); +// } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactTypeScript.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactTypeScript.java new file mode 100644 index 0000000..55ac95f --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/No2AllReactTypeScript.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.no2all.react; + +public interface No2AllReactTypeScript extends No2AllReactListener { + + default void onRelease(No2AllReactSlotSignal signal) { + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllActAbstractSlot.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllActAbstractSlot.java new file mode 100644 index 0000000..846f776 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllActAbstractSlot.java @@ -0,0 +1,16 @@ +package love.distributedrebirth.no2all.react.warp; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; + +public abstract class No2AllActAbstractSlot { + + private final No2AllReactSlot slot; + + public No2AllActAbstractSlot(No2AllReactSlot slot) { + this.slot = slot; + } + + public final No2AllReactSlot getSlot() { + return slot; + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotAdd.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotAdd.java new file mode 100644 index 0000000..5e61bd4 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotAdd.java @@ -0,0 +1,10 @@ +package love.distributedrebirth.no2all.react.warp; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; + +public final class No2AllArtWarpSlotAdd extends No2AllActAbstractSlot { + + protected No2AllArtWarpSlotAdd(No2AllReactSlot slot) { + super(slot); + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotRemove.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotRemove.java new file mode 100644 index 0000000..edf2ba2 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllArtWarpSlotRemove.java @@ -0,0 +1,10 @@ +package love.distributedrebirth.no2all.react.warp; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; + +public final class No2AllArtWarpSlotRemove extends No2AllActAbstractSlot { + + protected No2AllArtWarpSlotRemove(No2AllReactSlot slot) { + super(slot); + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactSlotContract.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactSlotContract.java new file mode 100644 index 0000000..b800bb0 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactSlotContract.java @@ -0,0 +1,29 @@ +package love.distributedrebirth.no2all.react.warp; + +import java.util.ArrayList; +import java.util.List; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; + +public class No2AllReactSlotContract { + + private final No2AllReactSlot slot; + final List> slotTypesIn = new ArrayList<>(); + final List> slotTypesOut = new ArrayList<>(); + + public No2AllReactSlotContract(No2AllReactSlot slot) { + this.slot = slot; + } + + public No2AllReactSlot getSlot() { + return slot; + } + + public List> getSlotTypesIn() { + return slotTypesIn; + } + + public List> getSlotTypesOut() { + return slotTypesOut; + } +} \ No newline at end of file diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpCore.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpCore.java new file mode 100644 index 0000000..1f1f266 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpCore.java @@ -0,0 +1,186 @@ +package love.distributedrebirth.no2all.react.warp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactListener; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; + +public class No2AllReactWarpCore implements No2AllReact { + + public static final No2AllReactSlot DILITHIUM = No2AllReactSlot.ofClass(No2AllReactWarpCore.class); + private final Map slots = new HashMap<>(); + private final Map, Object> services = new HashMap<>(); + private final List backlog = new ArrayList<>(); + private Object currentEvent = null; + + public No2AllReactWarpCore() { + claim(DILITHIUM); + toWarpFuel(DILITHIUM).registrateTypeIn(No2AllReactSlotLoad.class); + toWarpFuel(DILITHIUM).registrateTypeOut(No2AllArtWarpSlotAdd.class); + toWarpFuel(DILITHIUM).registrateTypeOut(No2AllArtWarpSlotRemove.class); + } + + public void load(No2AllReactTypeScript script) { + Objects.requireNonNull(script).onEvent(new No2AllReactSlotSignal<>(DILITHIUM, new No2AllReactSlotLoad(), this)); + } + + public No2AllReactSlotContract getSlotContract(No2AllReactSlot slot) { + return toWarpFuel(slot).readContract(); + } + + public List listChilds(No2AllReactSlot slot) { + Objects.requireNonNull(slot); + List result = new ArrayList<>(); + synchronized (slots) { + for (No2AllReactSlot slug : slots.keySet()) { + if (slot == null) { + result.add(slug); + continue; + } + if (slug.getParent() == null) { + continue; + } + if (slug.getParent().getSlotPath().equals(slot.getSlotPath())) { + result.add(slug); + } + } + } + return result; + } + + @Override + public void fireForEach(No2AllReactSlot slot, Object event) { + for (No2AllReactSlot target : listChilds(slot)) { + fire(target, event); + } + } + + @Override + public void fire(No2AllReactSlot slot, Object event) { + Objects.requireNonNull(slot); + Objects.requireNonNull(event); + if (currentEvent != null) { + backlog.add(new BacklogEvent(slot, event)); + return; + } + currentEvent = event; + try { + toWarpFuel(slot).fire(event, slot); + + while (!backlog.isEmpty()) { + List backlog2 = new ArrayList<>(backlog); + backlog.clear(); + for (BacklogEvent next : backlog2) { + toWarpFuel(next.slot).fire(next.event, next.slot); + } + } + } finally { + currentEvent = null; + } + } + + static class BacklogEvent { + No2AllReactSlot slot; + Object event; + public BacklogEvent(No2AllReactSlot slot, Object event) { + this.slot = slot; + this.event = event; + } + } + + @SuppressWarnings("unchecked") + @Override + public void registrate(No2AllReactSlot slot, Class eventType, No2AllReactListener listener) { + No2AllReactListener listenerObj = (No2AllReactListener) listener; + toWarpFuel(slot).registrateListener(eventType, listenerObj); + } + + @Override + public void release(No2AllReactSlot slot) { + toWarpFuel(slot).removeAll(); + synchronized (slots) { + slots.remove(slot); + } + fire(DILITHIUM, new No2AllArtWarpSlotRemove(slot)); + } + + @Override + public void claim(No2AllReactSlot slot) { + Objects.requireNonNull(slot); + synchronized (slots) { + if (slots.keySet().contains(slot)) { + throw new IllegalStateException("Slug already claimed: " + slot.getSlotPath()); + } + slots.put(slot, new No2AllReactWarpFuel(this, slot)); + } + fire(DILITHIUM, new No2AllArtWarpSlotAdd(slot)); + } + + @Override + public void claimOut(No2AllReactSlot slot, Class eventType) { + toWarpFuel(slot).registrateTypeOut(eventType); + } + + + @Override + public void claimIn(No2AllReactSlot slot, Class eventType) { + toWarpFuel(slot).registrateTypeIn(eventType); + } + + @Override + public void requireSlot(No2AllReactSlot slot, No2AllReactSlot dep) { + if (!isClaimed(dep)) { + throw new IllegalStateException("Script dependency missing: " + dep); + } + toWarpFuel(slot).addRequireSlot(dep); + } + + @Override + public void requireService(No2AllReactSlot slot, Class serviceType) { + toWarpFuel(slot).addRequireService(serviceType); + } + + @SuppressWarnings("unchecked") + @Override + public T service(Class serviceType) { + Objects.requireNonNull(serviceType); + synchronized (services) { + return (T) services.get(serviceType); + } + } + + public void addService(Class serviceType, T service) { + Objects.requireNonNull(serviceType); + Objects.requireNonNull(service); + synchronized (services) { + services.put(serviceType, service); + } + } + + public boolean isClaimed(No2AllReactSlot slug) { + Objects.requireNonNull(slug); + synchronized (slots) { + return slots.keySet().contains(slug); + } + } + + protected No2AllReactWarpFuel toWarpFuel(No2AllReactSlot slot) { + Objects.requireNonNull(slot); + No2AllReactWarpFuel result; + synchronized (slots) { + result = slots.get(slot); + if (result == null) { + throw new IllegalStateException("Slot not claimed: " + slot.getSlotPath()); + } + } + return result; + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpFuel.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpFuel.java new file mode 100644 index 0000000..2104b9f --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllReactWarpFuel.java @@ -0,0 +1,120 @@ +package love.distributedrebirth.no2all.react.warp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import love.distributedrebirth.no2all.react.No2AllReactListener; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; + +public class No2AllReactWarpFuel { + + private final No2AllReactWarpCore no2AllReactWarpCore; + private final No2AllReactSlot containerSlot; + private final Map, List>> listeners = new HashMap<>(); + private final Set> claimTypesOut = new HashSet<>(); + private final Set> claimTypesIn = new HashSet<>(); + private final Set> requireServices = new HashSet<>(); + private final Set requireSlots = new HashSet<>(); + + protected No2AllReactWarpFuel(No2AllReactWarpCore no2AllReactWarpCore, No2AllReactSlot containerSlot) { + this.no2AllReactWarpCore = Objects.requireNonNull(no2AllReactWarpCore); + this.containerSlot = Objects.requireNonNull(containerSlot); + } + + private List> toViewOfListeners(Class eventType) { + List> result = new ArrayList<>(); + synchronized (listeners) { + List> typeListeners = listeners.get(eventType); + if (typeListeners == null) { + return result; + } + result.addAll(typeListeners); + } + return result; + } + + public void fire(Object event, No2AllReactSlot slot) { + for (No2AllReactListener listener : toViewOfListeners(event.getClass())) { + listener.onEvent(new No2AllReactSlotSignal<>(slot, event, this.no2AllReactWarpCore)); + } + } + + public void registrateListener(Class eventType, No2AllReactListener listener) { + Objects.requireNonNull(eventType); + Objects.requireNonNull(listener); + synchronized (listeners) { + List> typeListeners = listeners.get(eventType); + if (typeListeners == null) { + typeListeners = new ArrayList<>(); + listeners.put(eventType, typeListeners); + } + typeListeners.add(listener); + } + } + + public void registrateTypeOut(Class eventType) { + Objects.requireNonNull(eventType); + synchronized (claimTypesOut) { + claimTypesOut.add(eventType); + } + } + + public void registrateTypeIn(Class eventType) { + Objects.requireNonNull(eventType); + synchronized (claimTypesIn) { + claimTypesIn.add(eventType); + } + } + + public No2AllReactSlotContract readContract() { + No2AllReactSlotContract result = new No2AllReactSlotContract(containerSlot); + synchronized (claimTypesIn) { + result.slotTypesIn.addAll(claimTypesIn); + } + synchronized (claimTypesOut) { + result.slotTypesOut.addAll(claimTypesOut); + } + return result; + } + + public void addRequireService(Class serviceType) { + Objects.requireNonNull(serviceType); + synchronized (requireServices) { + requireServices.add(serviceType); + } + } + + public void addRequireSlot(No2AllReactSlot dep) { + Objects.requireNonNull(dep); + synchronized (requireSlots) { + requireSlots.add(dep); + } + } + + public void removeAll() { + synchronized (listeners) { + for (Class eventType : listeners.keySet()) { + List> typeListeners = listeners.get(eventType); + typeListeners.clear(); + } + } + synchronized (claimTypesOut) { + claimTypesOut.clear(); + } + synchronized (claimTypesIn) { + claimTypesIn.clear(); + } + synchronized (requireServices) { + requireServices.clear(); + } + synchronized (requireSlots) { + requireSlots.clear(); + } + } +} diff --git a/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllRtsWarpSpeedMonitor.java b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllRtsWarpSpeedMonitor.java new file mode 100644 index 0000000..7b886e6 --- /dev/null +++ b/nx01-no2all-react/src/main/java/love/distributedrebirth/no2all/react/warp/No2AllRtsWarpSpeedMonitor.java @@ -0,0 +1,30 @@ +package love.distributedrebirth.no2all.react.warp; + +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; + +import java.util.logging.Logger; + +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; + +public class No2AllRtsWarpSpeedMonitor implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(No2AllRtsWarpSpeedMonitor.class); + private static final No2AllReactSlot API_DILITHIUM = No2AllReactWarpCore.DILITHIUM; + private static final Logger LOG = Logger.getLogger(No2AllRtsWarpSpeedMonitor.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + react.requireSlot(API, API_DILITHIUM); + react.registrate(API_DILITHIUM, No2AllArtWarpSlotAdd.class, v -> { + LOG.fine("slot-add: " + v.getData().getSlot().getSlotPath()); + }); + react.registrate(API_DILITHIUM, No2AllArtWarpSlotRemove.class, v -> { + LOG.fine("slot-remove: " + v.getData().getSlot().getSlotPath()); + }); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClient.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClient.java new file mode 100644 index 0000000..3105e92 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClient.java @@ -0,0 +1,70 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.io.IOException; +import java.net.URI; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutionException; + +import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.client.impl.JettyClientUpgradeRequest; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class JettyWireClient implements WireClient { + + private final URI uri; + private final WebSocketClient client; + private final JettyWireClientHandler handler; + + protected JettyWireClient(URI uri, WebSocketClient client, WireClientHandler handler) { + this.uri = uri; + this.client = client; + this.handler = new JettyWireClientHandler(handler); + } + + public JettyWireClientHandler getHandler() { + return handler; + } + + @Override + public void connect() { + try { + client.connect(handler, uri).get(); + } catch (IOException | InterruptedException | ExecutionException e) { + throw new IllegalStateException(e); + } + } + + @Override + public void close(int code, String message) { + client.getOpenSessions().forEach(v -> { + JettyClientUpgradeRequest req = JettyClientUpgradeRequest.class.cast(v.getUpgradeRequest()); + if (req.getURI().equals(uri)) { + v.close(code, message); + } + }); + } + + @Override + public void sendMessage(String message) { + client.getOpenSessions().forEach(v -> { + try { + v.getRemote().sendString(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } + + @Override + public void sendBinary(ByteBuffer message) { + client.getOpenSessions().forEach(v -> { + try { + v.getRemote().sendBytes(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientEndpoint.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientEndpoint.java new file mode 100644 index 0000000..a0ca71f --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientEndpoint.java @@ -0,0 +1,30 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.net.URI; + +import org.eclipse.jetty.websocket.client.WebSocketClient; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientEndpoint; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class JettyWireClientEndpoint implements WireClientEndpoint { + + private final URI uri; + private final WebSocketClient client; + + public JettyWireClientEndpoint(URI uri, WebSocketClient client) { + this.uri = uri; + this.client = client; + } + + @Override + public String wireId() { + return uri.toString(); + } + + @Override + public WireClient wireClient(WireClientHandler handler) { + return new JettyWireClient(uri, client, handler); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientHandler.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientHandler.java new file mode 100644 index 0000000..4414efe --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireClientHandler.java @@ -0,0 +1,43 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.WebSocketListener; + +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class JettyWireClientHandler implements WebSocketListener { + + private final WireClientHandler handler; + + protected JettyWireClientHandler(WireClientHandler handler) { + this.handler = handler; + this.handler.onStart(); + } + + @Override + public void onWebSocketConnect(Session session) { + handler.onOpen(); + } + + @Override + public void onWebSocketClose(int statusCode, String reason) { + handler.onClose(statusCode, reason, false); + } + + @Override + public void onWebSocketError(Throwable cause) { + handler.onError(cause); + } + + @Override + public void onWebSocketBinary(byte[] payload, int offset, int len) { + handler.onBinary(ByteBuffer.wrap(payload, offset, len)); + } + + @Override + public void onWebSocketText(String message) { + handler.onMessage(message); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServer.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServer.java new file mode 100644 index 0000000..27950b4 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServer.java @@ -0,0 +1,41 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer; + +import love.distributedrebirth.no2all.wire.WireServer; +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class JettyWireServer implements WireServer { + + private final JettyWebSocketServerContainer wsContainer; + + protected JettyWireServer(JettyWebSocketServerContainer wsContainer, String servletPath, WireServerHandler handler) { + this.wsContainer = wsContainer; + this.wsContainer.addMapping(servletPath, new JettyWireServerEndpointCreator(handler)); + } + + @Override + public void broadcastMessage(String message) { + wsContainer.getOpenSessions().forEach(v -> { + try { + v.getRemote().sendString(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } + + @Override + public void broadcastBinary(ByteBuffer message) { + wsContainer.getOpenSessions().forEach(v -> { + try { + v.getRemote().sendBytes(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpoint.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpoint.java new file mode 100644 index 0000000..595d32c --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpoint.java @@ -0,0 +1,28 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer; + +import love.distributedrebirth.no2all.wire.WireServer; +import love.distributedrebirth.no2all.wire.WireServerEndpoint; +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class JettyWireServerEndpoint implements WireServerEndpoint { + + private final JettyWebSocketServerContainer wsContainer; + private final String servletPath; + + public JettyWireServerEndpoint(JettyWebSocketServerContainer wsContainer, String servletPath) { + this.wsContainer = wsContainer; + this.servletPath = servletPath; + } + + @Override + public String wireId() { + return servletPath; + } + + @Override + public WireServer wireServer(WireServerHandler handler) { + return new JettyWireServer(wsContainer, servletPath, handler); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpointCreator.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpointCreator.java new file mode 100644 index 0000000..2e9b14c --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerEndpointCreator.java @@ -0,0 +1,22 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import org.eclipse.jetty.websocket.server.JettyServerUpgradeRequest; +import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse; +import org.eclipse.jetty.websocket.server.JettyWebSocketCreator; + +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class JettyWireServerEndpointCreator implements JettyWebSocketCreator { + + private final WireServerHandler handler; + + public JettyWireServerEndpointCreator(WireServerHandler handler) { + this.handler = handler; + } + + @Override + public Object createWebSocket(JettyServerUpgradeRequest jettyServerUpgradeRequest, + JettyServerUpgradeResponse jettyServerUpgradeResponse) { + return new JettyWireServerHandler(handler); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerHandler.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerHandler.java new file mode 100644 index 0000000..7103963 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerHandler.java @@ -0,0 +1,45 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.WebSocketListener; + +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class JettyWireServerHandler implements WebSocketListener { + + private final WireServerHandler handler; + private JettyWireServerSocket conn; + + protected JettyWireServerHandler(WireServerHandler handler) { + this.handler = handler; + this.handler.onStart(); + } + + @Override + public void onWebSocketConnect(Session session) { + this.conn = new JettyWireServerSocket(session); + handler.onOpen(conn); + } + + @Override + public void onWebSocketClose(int statusCode, String reason) { + handler.onClose(conn, statusCode, reason, false); + } + + @Override + public void onWebSocketError(Throwable cause) { + handler.onError(conn, cause); + } + + @Override + public void onWebSocketBinary(byte[] payload, int offset, int len) { + handler.onBinary(conn, ByteBuffer.wrap(payload, offset, len)); + } + + @Override + public void onWebSocketText(String message) { + handler.onMessage(conn, message); + } +} diff --git a/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerSocket.java b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerSocket.java new file mode 100644 index 0000000..0e7ee73 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/main/java/love/distributedrebirth/no2all/wire/jetty/JettyWireServerSocket.java @@ -0,0 +1,45 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.api.Session; + +import love.distributedrebirth.no2all.wire.WireServerSocket; + +public final class JettyWireServerSocket implements WireServerSocket { + + private final Session session; + + protected JettyWireServerSocket(Session session) { + this.session = session; + } + + @Override + public String getRemoteAddress() { + return session.getRemoteAddress().toString(); + } + + @Override + public void sendMessage(String message) { + try { + session.getRemote().sendString(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + @Override + public void sendBinary(ByteBuffer message) { + try { + session.getRemote().sendBytes(message); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + @Override + public void close(int code, String message) { + session.close(code, message); + } +} diff --git a/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventClient.java b/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventClient.java new file mode 100644 index 0000000..80d4026 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventClient.java @@ -0,0 +1,77 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class EventClient implements WireClientHandler { + + private static final Logger LOG = LoggerFactory.getLogger(EventClient.class); + + public static void main(String[] args) throws Exception { + EventClient client = new EventClient(); + client.start(); + client.sendHallo(); + client.stop(); + } + + private WebSocketClient client; + private JettyWireClientEndpoint endpoint1; + + public EventClient() throws URISyntaxException { + client = new WebSocketClient(); + endpoint1 = new JettyWireClientEndpoint(new URI("ws://localhost:8080"), client); + } + + public void start() throws Exception { + client.start(); + LOG.info("client startup"); + } + + public void stop() throws Exception { + client.stop(); + } + + public void sendHallo() { + WireClient client1 = endpoint1.wireClient(this); + client1.connect(); + client1.sendMessage("test me"); + } + + @Override + public void onStart() { + LOG.info("client.onStart()"); + } + + @Override + public void onOpen() { + LOG.info("client.onOpen()"); + } + + @Override + public void onClose(int code, String reason, boolean remote) { + LOG.info("client.onClose: "+code); + } + + @Override + public void onError(Throwable error) { + LOG.info("client.onError: "+error.getMessage()); + } + + @Override + public void onMessage(String message) { + LOG.info("client.onMessage: "+message); + } + + @Override + public void onBinary(ByteBuffer message) { + LOG.info("client.onBinary: "+message); + } +} diff --git a/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventServer.java b/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventServer.java new file mode 100644 index 0000000..fbe8c50 --- /dev/null +++ b/nx01-no2all-wire-jetty/src/test/java/love/distributedrebirth/no2all/wire/jetty/EventServer.java @@ -0,0 +1,102 @@ +package love.distributedrebirth.no2all.wire.jetty; + +import java.net.URI; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.util.component.LifeCycle.Listener; +import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.warp.No2AllReactWarpCore; +import love.distributedrebirth.no2all.react.warp.No2AllRtsWarpSpeedMonitor; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServerSocketSendMessage; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocket; +import love.distributedrebirth.no2all.react.wire.server.No2AllArtServerSocketOnMessage; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServerLog; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; + +public class EventServer { + + private static final Logger LOG = LoggerFactory.getLogger(EventServer.class); + + public static void main(String[] args) throws Exception { + EventServer server = new EventServer(); + server.setPort(8080); + server.start(); + server.join(); + } + + private final Server server; + private final ServerConnector connector; + private JettyWireServerEndpoint endpoint1; + private JettyWireServerEndpoint endpoint2; + private JettyWireServerEndpoint endpoint3; + + public EventServer() { + server = new Server(); + connector = new ServerConnector(server); + server.addConnector(connector); + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/"); + server.setHandler(context); + JettyWebSocketServletContainerInitializer.configure(context, (servletContext, wsContainer) -> { + wsContainer.setMaxTextMessageSize(65535); + endpoint1 = new JettyWireServerEndpoint(wsContainer, "/"); + endpoint2 = new JettyWireServerEndpoint(wsContainer, "/event"); + endpoint3 = new JettyWireServerEndpoint(wsContainer, "/other"); + }); + server.addEventListener(new Listener() { + + @Override + public void lifeCycleStarted(LifeCycle event) { + LOG.info("Startup No2AllReactWarpCore"); + No2AllReactWarpCore react = new No2AllReactWarpCore(); + react.load(new No2AllRtsWarpSpeedMonitor()); + react.load(new No2AllRtsServer()); + react.load(new No2AllRtsServerLog()); + + // auto reply + react.registrate(No2AllRtsServer.API, No2AllArtServer.class, artServer -> { + No2AllReactSlot serverSlot = artServer.getData().getSlot(); + react.registrate(serverSlot, No2AllArtServerSocket.class, artSocket -> { + No2AllReactSlot socketSlot = artSocket.getData().getSlot(); + react.registrate(socketSlot, No2AllArtServerSocketOnMessage.class, artMsg -> { + react.fire(socketSlot, new No2AllActServerSocketSendMessage("RE: "+artMsg.getData().getMessage())); + }); + }); + }); + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint1)); + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint2)); + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint3)); + } + }); + } + + public void setPort(int port) { + connector.setPort(port); + } + + public void start() throws Exception { + server.start(); + } + + public URI getURI() { + return server.getURI(); + } + + public void stop() throws Exception { + server.stop(); + } + + public void join() throws InterruptedException { + LOG.info("Use Ctrl+C to stop server"); + server.join(); + } +} diff --git a/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClient.java b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClient.java new file mode 100644 index 0000000..25ffc86 --- /dev/null +++ b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClient.java @@ -0,0 +1,54 @@ +package love.distributedrebirth.no2all.wire.jre; + +import java.net.URI; +import java.net.http.WebSocket; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutionException; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public final class JreWireClient implements WireClient { + + private final URI uri; + private final WebSocket.Builder wsBuilder; + private final WireClientHandler handler; + private WebSocket webSocket; + + protected JreWireClient(URI uri, WebSocket.Builder wsBuilder, WireClientHandler handler) { + this.uri = uri; + this.wsBuilder = wsBuilder; + this.handler = handler; + } + + @Override + public void connect() { + try { + webSocket = wsBuilder.buildAsync(uri, new JreWireClientHandler(handler)).get(); + } catch (InterruptedException | ExecutionException error) { + handler.onError(error); // handles: Connection refused + } + } + + @Override + public void close(int code, String message) { + if (webSocket != null) { + webSocket.sendClose(code, message); + webSocket = null; + } + } + + @Override + public void sendMessage(String message) { + if (webSocket != null) { + webSocket.sendText(message, true); + } + } + + @Override + public void sendBinary(ByteBuffer message) { + if (webSocket != null) { + webSocket.sendBinary(message, true); + } + } +} diff --git a/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientEndpoint.java b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientEndpoint.java new file mode 100644 index 0000000..b9524a1 --- /dev/null +++ b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientEndpoint.java @@ -0,0 +1,29 @@ +package love.distributedrebirth.no2all.wire.jre; + +import java.net.URI; +import java.net.http.WebSocket; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientEndpoint; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public final class JreWireClientEndpoint implements WireClientEndpoint { + + private final URI uri; + private final WebSocket.Builder wsBuilder; + + public JreWireClientEndpoint(URI uri, WebSocket.Builder wsBuilder) { + this.wsBuilder = wsBuilder; + this.uri = uri; + } + + @Override + public String wireId() { + return uri.toString(); + } + + @Override + public WireClient wireClient(WireClientHandler handler) { + return new JreWireClient(uri, wsBuilder, handler); + } +} diff --git a/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientHandler.java b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientHandler.java new file mode 100644 index 0000000..2d50112 --- /dev/null +++ b/nx01-no2all-wire-jre/src/main/java/love/distributedrebirth/no2all/wire/jre/JreWireClientHandler.java @@ -0,0 +1,46 @@ +package love.distributedrebirth.no2all.wire.jre; + +import java.net.http.WebSocket; +import java.nio.ByteBuffer; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; + +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public final class JreWireClientHandler implements WebSocket.Listener { + + private final WireClientHandler handler; + + protected JreWireClientHandler(WireClientHandler handler) { + this.handler = handler; + this.handler.onStart(); + } + + @Override + public void onOpen(WebSocket ws) { + handler.onOpen(); + } + + @Override + public CompletionStage onClose(WebSocket ws, int code, String reason) { + handler.onClose(code, reason, true); + return CompletableFuture.completedFuture(""); + } + + @Override + public void onError(WebSocket ws, Throwable error) { + handler.onError(error); + } + + @Override + public CompletionStage onText(WebSocket ws, CharSequence data, boolean last) { + handler.onMessage(data.toString()); + return CompletableFuture.completedFuture(""); + } + + @Override + public CompletionStage onBinary(WebSocket ws, ByteBuffer data, boolean last) { + handler.onBinary(data); + return CompletableFuture.completedFuture(""); + } +} \ No newline at end of file diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClient.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClient.java new file mode 100644 index 0000000..9103fe5 --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClient.java @@ -0,0 +1,42 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.URI; +import java.nio.ByteBuffer; +import java.util.Map; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class OjwWireClient implements WireClient { + + private final OjwWireClientHandler handler; + + protected OjwWireClient(URI uri, Map headers, int timeoutMs, WireClientHandler wireHandler) { + this.handler = new OjwWireClientHandler(uri, headers, timeoutMs, wireHandler); + wireHandler.onStart(); + } + + public OjwWireClientHandler getHandler() { + return handler; + } + + @Override + public void connect() { + handler.connect(); + } + + @Override + public void close(int code, String message) { + handler.close(code, message); + } + + @Override + public void sendMessage(String message) { + handler.send(message); + } + + @Override + public void sendBinary(ByteBuffer message) { + handler.send(message); + } +} diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientEndpoint.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientEndpoint.java new file mode 100644 index 0000000..d8776a2 --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientEndpoint.java @@ -0,0 +1,35 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.URI; +import java.util.Map; + +import love.distributedrebirth.no2all.wire.WireClient; +import love.distributedrebirth.no2all.wire.WireClientEndpoint; +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class OjwWireClientEndpoint implements WireClientEndpoint { + + private final URI uri; + private final Map headers; + private final int timeoutMs; + + public OjwWireClientEndpoint(URI uri) { + this(uri, null, 60 * 1000); + } + + public OjwWireClientEndpoint(URI uri, Map headers, int timeoutMs) { + this.uri = uri; + this.headers = headers; + this.timeoutMs = timeoutMs; + } + + @Override + public String wireId() { + return uri.toString(); + } + + @Override + public WireClient wireClient(WireClientHandler handler) { + return new OjwWireClient(uri, headers, timeoutMs, handler); + } +} diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientHandler.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientHandler.java new file mode 100644 index 0000000..fdda507 --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireClientHandler.java @@ -0,0 +1,46 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.URI; +import java.nio.ByteBuffer; +import java.util.Map; + +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.drafts.Draft_6455; +import org.java_websocket.handshake.ServerHandshake; + +import love.distributedrebirth.no2all.wire.WireClientHandler; + +public class OjwWireClientHandler extends WebSocketClient { + + private final WireClientHandler handler; + + protected OjwWireClientHandler(URI uri, Map headers, int timeoutMs, WireClientHandler handler) { + super(uri, new Draft_6455(), headers, timeoutMs); + this.handler = handler; + } + + @Override + public void onOpen(ServerHandshake handshakedata) { + handler.onOpen(); + } + + @Override + public void onClose(int code, String reason, boolean remote) { + handler.onClose(code, reason, remote); + } + + @Override + public void onError(Exception ex) { + handler.onError(ex); + } + + @Override + public void onMessage(String message) { + handler.onMessage(message); + } + + @Override + public void onMessage(ByteBuffer message) { + handler.onBinary(message); + } +} diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServer.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServer.java new file mode 100644 index 0000000..ab7283f --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServer.java @@ -0,0 +1,31 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; + +import love.distributedrebirth.no2all.wire.WireServer; +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class OjwWireServer implements WireServer { + + private final OjwWireServerHandler handler; + + protected OjwWireServer(InetSocketAddress port, int pingTimeoutSecs, WireServerHandler handler) { + this.handler = new OjwWireServerHandler(port, pingTimeoutSecs, handler); + this.handler.start(); + } + + public OjwWireServerHandler getHandler() { + return handler; + } + + @Override + public void broadcastMessage(String message) { + handler.broadcast(message); + } + + @Override + public void broadcastBinary(ByteBuffer message) { + handler.broadcast(message); + } +} diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerEndpoint.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerEndpoint.java new file mode 100644 index 0000000..be5164c --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerEndpoint.java @@ -0,0 +1,28 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.InetSocketAddress; + +import love.distributedrebirth.no2all.wire.WireServer; +import love.distributedrebirth.no2all.wire.WireServerEndpoint; +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class OjwWireServerEndpoint implements WireServerEndpoint { + + private final InetSocketAddress port; + private final int pingTimeoutSecs; + + public OjwWireServerEndpoint(InetSocketAddress port, int pingTimeoutSecs) { + this.port = port; + this.pingTimeoutSecs = pingTimeoutSecs; + } + + @Override + public String wireId() { + return port.toString(); + } + + @Override + public WireServer wireServer(WireServerHandler handler) { + return new OjwWireServer(port, pingTimeoutSecs, handler); + } +} diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerHandler.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerHandler.java new file mode 100644 index 0000000..a9815b2 --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerHandler.java @@ -0,0 +1,81 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.java_websocket.WebSocket; +import org.java_websocket.handshake.ClientHandshake; +import org.java_websocket.server.WebSocketServer; + +import love.distributedrebirth.no2all.wire.WireServerHandler; + +public class OjwWireServerHandler extends WebSocketServer { + + private final WireServerHandler handler; + private final Map wraps = new WeakHashMap<>(); + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + private final Lock lockRead = lock.readLock(); + private final Lock lockWrite = lock.writeLock(); + + protected OjwWireServerHandler(InetSocketAddress port, int pingTimeoutSecs, WireServerHandler handler) { + super(port); + setConnectionLostTimeout(pingTimeoutSecs); + this.handler = handler; + } + + @Override + public void onStart() { + handler.onStart(); + } + + @Override + public void onOpen(WebSocket conn, ClientHandshake handshake) { + handler.onOpen(wrap(conn)); + } + + @Override + public void onClose(WebSocket conn, int code, String reason, boolean remote) { + handler.onClose(wrap(conn), code, reason, remote); + } + + @Override + public void onError(WebSocket conn, Exception ex) { + handler.onError(wrap(conn), ex); + } + + @Override + public void onMessage(WebSocket conn, String message) { + handler.onMessage(wrap(conn), message); + } + + @Override + public void onMessage(WebSocket conn, ByteBuffer message) { + handler.onBinary(wrap(conn), message); + } + + private OjwWireServerSocket wrap(WebSocket conn) { + OjwWireServerSocket result = null; + lockRead.lock(); + try { + result = wraps.get(conn); + } finally { + lockRead.unlock(); + } + if (result != null) { + return result; + } + result = new OjwWireServerSocket(conn); + lockWrite.lock(); + try { + wraps.put(conn, result); + } finally { + lockWrite.unlock(); + } + return result; + } +} \ No newline at end of file diff --git a/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerSocket.java b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerSocket.java new file mode 100644 index 0000000..787692b --- /dev/null +++ b/nx01-no2all-wire-ojw/src/main/java/love/distributedrebirth/no2all/wire/ojw/OjwWireServerSocket.java @@ -0,0 +1,36 @@ +package love.distributedrebirth.no2all.wire.ojw; + +import java.nio.ByteBuffer; + +import org.java_websocket.WebSocket; + +import love.distributedrebirth.no2all.wire.WireServerSocket; + +public final class OjwWireServerSocket implements WireServerSocket { + + private final WebSocket socket; + + protected OjwWireServerSocket(WebSocket socket) { + this.socket = socket; + } + + @Override + public String getRemoteAddress() { + return socket.getRemoteSocketAddress().getAddress().getHostAddress(); + } + + @Override + public void sendMessage(String message) { + socket.send(message); + } + + @Override + public void sendBinary(ByteBuffer message) { + socket.send(message); + } + + @Override + public void close(int code, String message) { + socket.close(code, message); + } +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClient.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClient.java new file mode 100644 index 0000000..7136c36 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClient.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.wire; + +import java.nio.ByteBuffer; + +public interface WireClient { + + void connect(); + + void close(int code, String message); + + void sendMessage(String message); + + void sendBinary(ByteBuffer message); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientEndpoint.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientEndpoint.java new file mode 100644 index 0000000..c169993 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientEndpoint.java @@ -0,0 +1,8 @@ +package love.distributedrebirth.no2all.wire; + +public interface WireClientEndpoint { + + String wireId(); + + WireClient wireClient(WireClientHandler handler); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientHandler.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientHandler.java new file mode 100644 index 0000000..b34391f --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireClientHandler.java @@ -0,0 +1,18 @@ +package love.distributedrebirth.no2all.wire; + +import java.nio.ByteBuffer; + +public interface WireClientHandler { + + void onStart(); + + void onOpen(); + + void onClose(int code, String reason, boolean remote); + + void onError(Throwable error); + + void onMessage(String message); + + void onBinary(ByteBuffer message); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServer.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServer.java new file mode 100644 index 0000000..5f9b21c --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServer.java @@ -0,0 +1,10 @@ +package love.distributedrebirth.no2all.wire; + +import java.nio.ByteBuffer; + +public interface WireServer { + + void broadcastMessage(String message); + + void broadcastBinary(ByteBuffer message); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerEndpoint.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerEndpoint.java new file mode 100644 index 0000000..985b300 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerEndpoint.java @@ -0,0 +1,8 @@ +package love.distributedrebirth.no2all.wire; + +public interface WireServerEndpoint { + + String wireId(); + + WireServer wireServer(WireServerHandler handler); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerHandler.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerHandler.java new file mode 100644 index 0000000..31556d0 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerHandler.java @@ -0,0 +1,18 @@ +package love.distributedrebirth.no2all.wire; + +import java.nio.ByteBuffer; + +public interface WireServerHandler { + + void onStart(); + + void onOpen(WireServerSocket conn); + + void onClose(WireServerSocket conn, int code, String reason, boolean remote); + + void onError(WireServerSocket conn, Throwable error); + + void onMessage(WireServerSocket conn, String message); + + void onBinary(WireServerSocket conn, ByteBuffer message); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerSocket.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerSocket.java new file mode 100644 index 0000000..bab97b9 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/WireServerSocket.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.wire; + +import java.nio.ByteBuffer; + +public interface WireServerSocket { + + String getRemoteAddress(); + + void sendMessage(String message); + + void sendBinary(ByteBuffer message); + + void close(int code, String message); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetch.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetch.java new file mode 100644 index 0000000..12d0411 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetch.java @@ -0,0 +1,4 @@ +package love.distributedrebirth.no2all.wire.fetch; + +public interface WireFetch extends Runnable { +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactory.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactory.java new file mode 100644 index 0000000..b231d99 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactory.java @@ -0,0 +1,6 @@ +package love.distributedrebirth.no2all.wire.fetch; + +public interface WireFetchFactory { + + WireFetch buildFetcher(WireFetchResource resource, WireFetchHandler handler); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactoryDefault.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactoryDefault.java new file mode 100644 index 0000000..27814b7 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchFactoryDefault.java @@ -0,0 +1,84 @@ +package love.distributedrebirth.no2all.wire.fetch; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.Map.Entry; + +public final class WireFetchFactoryDefault implements WireFetchFactory { + + @Override + public WireFetch buildFetcher(WireFetchResource resource, WireFetchHandler handler) { + return new HttpDownloader(resource, handler); + } + + static protected final class HttpDownloader implements WireFetch { + + private final WireFetchResource resource; + private final WireFetchHandler handler; + + protected HttpDownloader(WireFetchResource resource, WireFetchHandler handler) { + this.resource = resource; + this.handler = handler; + } + + @Override + public void run() { + try { + handler.onStart(); + try (InputStream conn = openInputStream(resource.getUrId().toURL(), resource.getHeaders(), resource.getTimeoutMs(), 5)) { + try (ByteArrayOutputStream result = new ByteArrayOutputStream()) { + int totalBytes = 0; + byte[] buffer = new byte[4096]; + int length; + while ((length = conn.read(buffer)) != -1) { + result.write(buffer, 0, length); + totalBytes += length; + handler.onProgress(totalBytes); + } + handler.onReady(ByteBuffer.wrap(result.toByteArray())); + } + } + } catch (Exception error) { + handler.onError(error); + } + } + + private InputStream openInputStream(URL url, Map args, int timeoutMs, int redirectLimit) throws IOException { + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setConnectTimeout(timeoutMs); + con.setReadTimeout(timeoutMs); + if (args != null) { + for (Entry e : args.entrySet()) { + con.setRequestProperty(e.getKey(), e.getValue()); + } + } + con.connect(); + int responseCode = con.getResponseCode(); + if (responseCode < 400 && responseCode > 299) { + // for redirects but breaks switch protocols + redirectLimit--; + if (redirectLimit == 0) { + throw new IllegalStateException("Max redirect limit reached"); + } + String redirectLocation = con.getHeaderField("Location"); + if (redirectLocation == null) { + throw new IllegalStateException("No redirect location header"); + } + URL redirectUrl = null; + try { + redirectUrl = new URL(redirectLocation); + } catch (MalformedURLException e) { + redirectUrl = new URL(url.getProtocol() + "://" + url.getHost() + redirectLocation); + } + return openInputStream(redirectUrl, args, timeoutMs, redirectLimit); + } + return con.getInputStream(); + } + } +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchHandler.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchHandler.java new file mode 100644 index 0000000..d938306 --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchHandler.java @@ -0,0 +1,14 @@ +package love.distributedrebirth.no2all.wire.fetch; + +import java.nio.ByteBuffer; + +public interface WireFetchHandler { + + void onStart(); + + void onReady(ByteBuffer result); + + void onError(Throwable error); + + void onProgress(int totalBytes); +} diff --git a/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchResource.java b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchResource.java new file mode 100644 index 0000000..520b14b --- /dev/null +++ b/nx01-no2all-wire/src/main/java/love/distributedrebirth/no2all/wire/fetch/WireFetchResource.java @@ -0,0 +1,39 @@ +package love.distributedrebirth.no2all.wire.fetch; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +public final class WireFetchResource { + + private final Map headers = new HashMap<>(); + private final URI uri; + private int timeoutMs = 10; + + public WireFetchResource(URI uri) { + this.uri = uri; + this.timeoutMs = 10; + } + + public URI getUrId() { + return uri; + } + + public int getTimeoutMs() { + return timeoutMs; + } + + public Map getHeaders() { + return headers; + } + + public WireFetchResource withTimeoutMs(int timeoutMs) { + this.timeoutMs = timeoutMs; + return this; + } + + public WireFetchResource withHeader(String key, String value) { + headers.put(key, value); + return this; + } +} diff --git a/nx01-no2all-zerofungus/conf/logback-server-console.xml b/nx01-no2all-zerofungus/conf/logback-server-console.xml new file mode 100644 index 0000000..016b04a --- /dev/null +++ b/nx01-no2all-zerofungus/conf/logback-server-console.xml @@ -0,0 +1,30 @@ + + + + true + + + + logs/no2all-demo.log + + %date %level [%thread] %logger{10} %msg%n + + + + + %date %level [%thread] %logger{10} %msg%n + + + + + + + + + + + + + + + diff --git a/nx01-no2all-zerofungus/conf/logback-server.xml b/nx01-no2all-zerofungus/conf/logback-server.xml new file mode 100644 index 0000000..933acef --- /dev/null +++ b/nx01-no2all-zerofungus/conf/logback-server.xml @@ -0,0 +1,23 @@ + + + + true + + + + logs/no2all-demo.log + + logs/no2all-demo-%d{yyyy-MM-dd}.log.zip + + + %date %level [%thread] %logger{10} %msg%n + + + + + + + + + + diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServer.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServer.java new file mode 100644 index 0000000..66f2b49 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServer.java @@ -0,0 +1,56 @@ +package love.distributedrebirth.no2all.zerofungus; + +import java.io.StringWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.logging.Logger; + +import javax.sql.DataSource; + +import jakarta.json.Json; +import jakarta.json.JsonWriter; +import love.distributedrebirth.no2all.nostr.model.event.NoStrEvent; +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrArtServerRelayStoreEvent; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrRtsServerRelay; + +public class ZFunRtsServer implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(ZFunRtsServer.class); + private static final Logger logger = Logger.getLogger(ZFunRtsServer.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + No2AllReact react = signal.getReact(); + react.claim(API); + + DataSource ds = react.service(DataSource.class); + react.registrate(NoStrRtsServerRelay.API, NoStrArtServerRelayStoreEvent.class, storeEvent -> { + logger.info("Store event: " + storeEvent.getData().getEvent().getId().getHexDipavali()); + NoStrEvent event = storeEvent.getData().getEvent(); + StringWriter jsonStr = new StringWriter(); + try (JsonWriter writer = Json.createWriter(jsonStr)) { + writer.write(event.toBible()); + } + try (Connection conn = ds.getConnection()) { + String sql = "INSERT INTO \"ᔆʸᔆᐪᓫᔿ\".\"ᔆᐪᣔᒼᒽᑉ\" (\"ᑊᑊᐣ\",\"ᣖᓑᒃᒽᑉᓫᔾ\",\"ᒼᣗᓫᣔᐪᓫᐝᒄᐪ\",\"ᑊᑉᑊᣕᒄ\",\"ᒄᓫᒻᐪᣔᕐ\",\"ᓫᕁᣖᑊᣗᓫᐝᒄᐪ\",\"ᣗᣔᒡᒢ\") VALUES (?,?,?,?,?,?,?)"; + PreparedStatement prep = conn.prepareStatement(sql); + prep.setString(1, event.getId().getHex()); + prep.setString(2, event.getPayload().getPublicKey().getHex()); + prep.setDate(3, new java.sql.Date(event.getPayload().getCreatedAt().toEpochMilli())); + prep.setInt(4, event.getPayload().getKind().getNumber()); + prep.setString(5, null); // del tag + prep.setString(6, null); // exp date + prep.setString(7, jsonStr.toString()); + prep.executeUpdate(); + } catch (SQLException e) { + logger.warning(e.getMessage()); + } + }); + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServerBooted.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServerBooted.java new file mode 100644 index 0000000..564b8c0 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZFunRtsServerBooted.java @@ -0,0 +1,23 @@ +package love.distributedrebirth.no2all.zerofungus; + +import java.util.logging.Logger; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; + +public class ZFunRtsServerBooted implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(ZFunRtsServerBooted.class); + private static final Logger logger = Logger.getLogger(ZFunRtsServerBooted.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + signal.getReact().claim(API); + logger.info("╔═╗┌─┐┬─┐┌─┐ .╔═╗┬ ┬┌┐┌╔═╗┬ ┬┌─┐"); + logger.info("╔═╝├┤ ├┬┘│ │ . ╠╣ │ ││││║ ╦│ │└─┐"); + logger.info("╚═╝└─┘┴└─└─┘. ╚ └─┘┘└┘╚═╝└─┘└─┘"); + logger.info("Boot done."); + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZeroFungus.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZeroFungus.java new file mode 100644 index 0000000..1a2edc6 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/ZeroFungus.java @@ -0,0 +1,78 @@ +package love.distributedrebirth.no2all.zerofungus; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import love.distributedrebirth.no2all.react.nostr.client.NoStrRtsClient; +import love.distributedrebirth.no2all.react.nostr.server.NoStrRtsServer; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrRtsServerRelay; +import love.distributedrebirth.no2all.react.nostr.server.relay.NoStrRtsServerRelaySubs; +import love.distributedrebirth.no2all.react.warp.No2AllReactWarpCore; +import love.distributedrebirth.no2all.react.warp.No2AllRtsWarpSpeedMonitor; +import love.distributedrebirth.no2all.react.wire.client.No2AllRtsClient; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServerLog; +import love.distributedrebirth.no2all.zerofungus.service.ZFunRtsConfigJetty; +import love.distributedrebirth.no2all.zerofungus.service.ZFunRtsConfigLogger; +//import ᒢᐩᐩ.ᔿᐤᒄʸ.ᣔᒃᣔᒼᓑᔆ.BãßBȍőnAbacusInstanceMBeanʸᴰ; +import love.distributedrebirth.no2all.zerofungus.service.ZFunRtsInitJdbc; + +public enum ZeroFungus /* implements BãßBȍőnAbacusInstanceMBeanʸᴰ */ { + INSTANCE; + + private No2AllReactWarpCore foei = new No2AllReactWarpCore(); + private List jetties = new ArrayList<>(); + + public static void main(String[] args) { + Iterator arguments = Arrays.asList(args).iterator(); + boolean hasPort = arguments.hasNext() && arguments.next().equals("-p") && arguments.hasNext(); + + // todo move + new File("logs").mkdir(); + + // config jetty + INSTANCE.jetties.clear(); + if (hasPort) { + // note: multiple currently does not work... + INSTANCE.jetties.add(Arrays.asList(arguments.next().split(",")).stream().map(v -> Integer.parseInt(v)).findFirst().map(v -> new ZFunRtsConfigJetty(v)).get()); + } else { + INSTANCE.jetties.add(new ZFunRtsConfigJetty(8080)); + } + INSTANCE.start(); + } + + public void stop() { + jetties.forEach(v -> v.stop()); + // foei.fire(No2AllReactWarpCore.DILITHIUM, new EjectWrapCoreShutdown()); + } + + public void start() { + // setup logger + warp monitor + foei.load(new ZFunRtsConfigLogger()); + foei.load(new No2AllRtsWarpSpeedMonitor()); + + // add websocket wire support + log + foei.load(new No2AllRtsClient()); + foei.load(new No2AllRtsServer()); + foei.load(new No2AllRtsServerLog()); + + // add nostr support + foei.load(new NoStrRtsClient()); + foei.load(new NoStrRtsServer()); + foei.load(new NoStrRtsServerRelay()); + foei.load(new NoStrRtsServerRelaySubs()); + + // config zero fungus servers and clients + foei.load(new ZFunRtsInitJdbc(foei)); + foei.load(new ZFunRtsServer()); + // foei.load(new ZFunRtsClientReplay()); + + // load jetties and join + jetties.forEach(v -> foei.load(v)); + foei.load(new ZFunRtsServerBooted()); + jetties.stream().findFirst().ifPresent(v -> v.join()); + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigJetty.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigJetty.java new file mode 100644 index 0000000..5d482b7 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigJetty.java @@ -0,0 +1,133 @@ +package love.distributedrebirth.no2all.zerofungus.service; + +import java.io.IOException; +import java.net.URL; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +import org.eclipse.jetty.server.CustomRequestLog; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.util.component.LifeCycle.Listener; +import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.resource.ResourceCollection; +import org.eclipse.jetty.webapp.Configuration; +import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.webapp.WebInfConfiguration; +import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer; + +import love.distributedrebirth.no2all.react.No2AllReact; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.wire.server.No2AllActServer; +import love.distributedrebirth.no2all.react.wire.server.No2AllRtsServer; +import love.distributedrebirth.no2all.wire.jetty.JettyWireServerEndpoint; +import love.distributedrebirth.no2all.zerofungus.web.RedirectServlet; + +public final class ZFunRtsConfigJetty implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(ZFunRtsConfigJetty.class); + private static final Logger logger = Logger.getLogger(ZFunRtsConfigJetty.class.getName()); + private final Server server; + private final ServerConnector connector; + private JettyWireServerEndpoint endpoint1; + private JettyWireServerEndpoint endpoint2; + private JettyWireServerEndpoint endpoint3; + + public ZFunRtsConfigJetty(int port) { + server = new Server(); + connector = new ServerConnector(server); + connector.setPort(port); + server.addConnector(connector); + } + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + logger.info(" ╦┌─┐╔╦╗╔╦╗┬ ┬ ╔═╗┌─┐╦ ╦╔═╗┬─┐┌┬┐"); + logger.info(" ║├┤ ║ ║ └┬┘ ╠═╝│ │║║║║╣ ├┬┘ ││"); + logger.info("╚╝└─┘ ╩ ╩ ┴ ╩ └─┘╚╩╝╚═╝┴└──┴┘"); + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + context.setBaseResource(buildBaseResource(getClass().getClassLoader())); + context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/target/classes/|.*\\.jar"); + context.setConfigurations(new Configuration[]{ + //new AnnotationConfiguration(), + new WebInfConfiguration(), + }); + server.setHandler(context); + + JettyWebSocketServletContainerInitializer.configure(context, (servletContext, wsContainer) -> { + wsContainer.setMaxTextMessageSize(65535); + endpoint1 = new JettyWireServerEndpoint(wsContainer, "/con1"); + endpoint2 = new JettyWireServerEndpoint(wsContainer, "/event"); + endpoint3 = new JettyWireServerEndpoint(wsContainer, "/other"); + }); + + logger.info("Add H2 console on: /debug/jdbc/console"); + context.addServlet("org.h2.server.web.WebServlet", "/debug/jdbc/console/*"); + + context.addServlet(RedirectServlet.class, "/test"); + + // for static + ServletHolder staticHome = new ServletHolder("staticHome", DefaultServlet.class); + staticHome.setInitParameter("dirAllowed","false"); + context.addServlet(staticHome, "/"); + + logger.info("Add request logger console"); + CustomRequestLog log = new CustomRequestLog("logs/http.log", CustomRequestLog.EXTENDED_NCSA_FORMAT); + server.setRequestLog(log); + + No2AllReact react = signal.getReact(); + server.addEventListener(new Listener() { + @Override + public void lifeCycleStarted(LifeCycle event) { + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint1)); + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint2)); + react.fire(No2AllRtsServer.API, new No2AllActServer(endpoint3)); + } + }); + try { + logger.info("start jetty"); + server.start(); + logger.info("jetty started"); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + private Resource buildBaseResource(ClassLoader cl) { + try { + List paths = Collections.list(cl.getResources("META-INF/resources")); + Resource[] resources = new Resource[paths.size() + 1]; + for (int i = 0; i < paths.size(); i++) { + resources[i] = Resource.newResource(paths.get(i)); + } + resources[paths.size()] = Resource.newResource("target/classes/static"); + return new ResourceCollection(resources); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + public void join() { + try { + server.join(); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + + public void stop() { + try { + server.stop(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigLogger.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigLogger.java new file mode 100644 index 0000000..ebeaeab --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsConfigLogger.java @@ -0,0 +1,44 @@ +package love.distributedrebirth.no2all.zerofungus.service; + +import java.util.logging.Logger; + +import org.slf4j.LoggerFactory; +import org.slf4j.bridge.SLF4JBridgeHandler; + +import ch.qos.logback.core.status.NopStatusListener; +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; + +public class ZFunRtsConfigLogger implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(ZFunRtsConfigLogger.class); + private static final Logger logger = Logger.getLogger(ZFunRtsConfigLogger.class.getName()); + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + if (System.getProperty("logback.configurationFile") == null) { + String logConfig = null; + if (System.getProperty("java.class.path").contains("classes")) { + logConfig = "conf/logback-server-console.xml"; + } else { + logConfig = "conf/logback-server.xml"; + } + System.setProperty("logback.configurationFile", logConfig); + } + if (System.getProperty("logback.statusListenerClass") == null) { + System.setProperty("logback.statusListenerClass",NopStatusListener.class.getName()); + } + org.slf4j.Logger logger2 = LoggerFactory.getLogger(ZFunRtsConfigLogger.class); + logger2.info("Logging configured."); + + SLF4JBridgeHandler.removeHandlersForRootLogger(); + SLF4JBridgeHandler.install(); + logger.info(" ╦┬ ┬╦ ╦═╗┌─┐╦ ╦┬╦═╗┌─┐╔╦╗"); + logger.info(" ║│ │║ ╠╦╝├┤ ║║║│╠╦╝├┤ ║║"); + logger.info("╚╝└─┘╩═╝ ╩╚═└─┘╚╩╝┴╩╚═└─┘═╩╝"); + //https://patorjk.com/software/taag + // Calvin S + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsInitJdbc.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsInitJdbc.java new file mode 100644 index 0000000..51b0845 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/service/ZFunRtsInitJdbc.java @@ -0,0 +1,62 @@ +package love.distributedrebirth.no2all.zerofungus.service; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.logging.Logger; + +import javax.sql.DataSource; + +import org.flywaydb.core.Flyway; +import org.h2.jdbcx.JdbcConnectionPool; + +import love.distributedrebirth.no2all.react.No2AllReactSlot; +import love.distributedrebirth.no2all.react.No2AllReactSlotLoad; +import love.distributedrebirth.no2all.react.No2AllReactSlotSignal; +import love.distributedrebirth.no2all.react.No2AllReactTypeScript; +import love.distributedrebirth.no2all.react.warp.No2AllReactWarpCore; + +public class ZFunRtsInitJdbc implements No2AllReactTypeScript { + + public static final No2AllReactSlot API = No2AllReactSlot.ofClass(ZFunRtsInitJdbc.class); + private static final Logger logger = Logger.getLogger(ZFunRtsInitJdbc.class.getName()); + private final No2AllReactWarpCore foei; + + public ZFunRtsInitJdbc(No2AllReactWarpCore foei) { + this.foei = foei; + } + + @Override + public void onEvent(No2AllReactSlotSignal signal) { + signal.getReact().claim(API); + + JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:file:./target/h2db/zfun", "sa", "sa"); + foei.addService(JdbcConnectionPool.class, cp); + foei.addService(DataSource.class, cp); + + Flyway flyway = Flyway.configure().schemas("ᔆʸᔆᐪᓫᔿ", "other", "ssd").table("ᣔᒃᣔᒼᓑᔆ").dataSource(cp).load(); + flyway.migrate(); + logger.info("╔╦╗┌─┐╔╦╗┌─┐╔╗ ┌─┐╔═╗┌─┐ ╔═╗┌─┐╔╦╗┬ ┬┌─┐"); + logger.info(" ║║├─┤ ║ ├─┤╠╩╗├─┤╚═╗├┤ ╚═╗├┤ ║ │ │├─┘"); + logger.info("═╩╝┴ ┴ ╩ ┴ ┴╚═╝┴ ┴╚═╝└─┘ ╚═╝└─┘ ╩ └─┘┴ "); + try { + startDone(); + } catch (SQLException e) { + throw new IllegalStateException(e); + } + } + + public void startDone() throws SQLException { + JdbcConnectionPool cp = foei.service(JdbcConnectionPool.class); + try (Connection conn = cp.getConnection()) { + String sql = "SELECT COUNT(\"ᑊᑊᐣ\") FROM \"ᔆʸᔆᐪᓫᔿ\".\"ᔆᐪᣔᒼᒽᑉ\""; + PreparedStatement prep = conn.prepareStatement(sql); + ResultSet rs = prep.executeQuery(); + if (rs.first()) { + Long records = rs.getLong(1); + logger.info("DB Records: " + records + " from: " + sql); + } + } + } +} diff --git a/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/web/RedirectServlet.java b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/web/RedirectServlet.java new file mode 100644 index 0000000..a0d8008 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/java/love/distributedrebirth/no2all/zerofungus/web/RedirectServlet.java @@ -0,0 +1,17 @@ +package love.distributedrebirth.no2all.zerofungus.web; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class RedirectServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.sendRedirect("/powp/error/errmsg.html?title=NotHere&msg=TheEnd"); + } +} \ No newline at end of file diff --git a/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_01__init_db.sql b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_01__init_db.sql new file mode 100644 index 0000000..e8327ec --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_01__init_db.sql @@ -0,0 +1,37 @@ + +DROP TABLE IF EXISTS "ᒄᒃᓑᔆᓫᣗ"; +CREATE TABLE IF NOT EXISTS "ᒄᒃᓑᔆᓫᣗ" ( + "ᑊᑊᐣ" BIGINT NOT NULL AUTO_INCREMENT, + "ᣕᣔᔿᓫ" VARCHAR(100) NOT NULL, + "ᓑᔆᓫᣗᣕᣔᔿᓫ" VARCHAR(45) NOT NULL, + "ᣖᣔᔆᔆᒡᒢᐤᣗᒄ" VARCHAR(256) NOT NULL, + "ᓫᔿᣔᑊᒻ" VARCHAR(200) NOT NULL, + "ᔆᐪᣔᐪᓑᔆ" VARCHAR(45) NOT NULL, + delete_reason VARCHAR(500) NULL, + create_dt DATETIME NOT NULL DEFAULT now(), + create_uid BIGINT NOT NULL DEFAULT 1, + modify_dt DATE NULL, + modify_uid BIGINT NULL, + PRIMARY KEY ("ᑊᑊᐣ")) + ; + +CREATE UNIQUE INDEX "ᒄᒃᓑᔆᓫᣗᐨᓑᑊᐣᕁᐨᓑᔆᓫᣗᣕᣔᔿᓫ" ON "ᒄᒃᓑᔆᓫᣗ" ("ᓑᔆᓫᣗᣕᣔᔿᓫ" ASC); +CREATE INDEX "ᒄᒃᓑᔆᓫᣗᐨᑊᑊᐣᕁᐨᔆᐪᣔᐪᓑᔆ" ON "ᒄᒃᓑᔆᓫᣗ" ("ᔆᐪᣔᐪᓑᔆ" ASC); + +CREATE TABLE IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉ" ( + "ᑊᑊᐣ" text NOT NULL, + "ᣖᓑᒃᒽᑉᓫᔾ" TEXT NOT NULL, + "ᒼᣗᓫᣔᐪᓫᐝᒄᐪ" DATETIME NOT NULL DEFAULT now(), + "ᑊᑉᑊᣕᒄ" INTEGER NOT NULL, + "ᒄᓫᒻᐪᣔᕐ" TEXT NULL, + "ᓫᕁᣖᑊᣗᓫᐝᒄᐪ" DATETIME NULL, + "ᣗᣔᒡᒢ" JSON NOT NULL, + PRIMARY KEY ("ᑊᑊᐣ")) + ; + +CREATE UNIQUE INDEX IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉᐨᓑᑊᐣᕁᐨᑊᑊᐣ" ON "ᔆᐪᣔᒼᒽᑉ" USING btree ("ᑊᑊᐣ"); +CREATE INDEX IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉᐨᑊᑊᐣᕁᐨᣖᓑᒃᒽᑉᓫᔾ" ON "ᔆᐪᣔᒼᒽᑉ" USING btree ("ᣖᓑᒃᒽᑉᓫᔾ"); +CREATE INDEX IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉᐨᑊᑊᐣᕁᐨᒼᣗᓫᣔᐪᓫᐝᒄᐪ" ON "ᔆᐪᣔᒼᒽᑉ" ("ᒼᣗᓫᣔᐪᓫᐝᒄᐪ" DESC); +CREATE INDEX IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉᐨᑊᑊᐣᕁᐨᑊᑉᑊᣕᒄ" ON "ᔆᐪᣔᒼᒽᑉ" ("ᑊᑉᑊᣕᒄ"); +CREATE INDEX IF NOT EXISTS "ᔆᐪᣔᒼᒽᑉᐨᑊᑊᐣᕁᐨᓫᕁᣖᑊᣗᓫᐝᒄᐪ" ON "ᔆᐪᣔᒼᒽᑉ" ("ᓫᕁᣖᑊᣗᓫᐝᒄᐪ" DESC); + diff --git a/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_02__init_data.sql b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_02__init_data.sql new file mode 100644 index 0000000..79763fa --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_02__init_data.sql @@ -0,0 +1,3 @@ + +INSERT INTO "ᒄᒃᓑᔆᓫᣗ" ("ᑊᑊᐣ", "ᣕᣔᔿᓫ", "ᓑᔆᓫᣗᣕᣔᔿᓫ", "ᣖᣔᔆᔆᒡᒢᐤᣗᒄ", "ᓫᔿᣔᑊᒻ", "ᔆᐪᣔᐪᓑᔆ") VALUES (1, 'Technical User', 'tech', '00000000', 'technical@', 'TECHNICAL'); +INSERT INTO "ᒄᒃᓑᔆᓫᣗ" ("ᑊᑊᐣ", "ᣕᣔᔿᓫ", "ᓑᔆᓫᣗᣕᣔᔿᓫ", "ᣖᣔᔆᔆᒡᒢᐤᣗᒄ", "ᓫᔿᣔᑊᒻ", "ᔆᐪᣔᐪᓑᔆ") VALUES (2, 'Admin', 'admin', '000000000', 'admin@', 'ACTIVE'); diff --git a/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_03__h2-wcontest-428.sql b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_03__h2-wcontest-428.sql new file mode 100644 index 0000000..dea8b60 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/resources/db/migration/V00/V00_03__h2-wcontest-428.sql @@ -0,0 +1,36 @@ + +-- Test case for H2 webconsole unicode autocomplete escaping and quoting. +-- http://localhost:8080/debug/jdbc/console + +CREATE SCHEMA "Господь мой пастырь"; +CREATE TABLE "Господь мой пастырь"."Я не захочу." ( + "Он заставляет меня лечь" BIGINT NOT NULL AUTO_INCREMENT, + "на зеленых пастбищах" VARCHAR(500) NULL, + "Он ведет меня" DATETIME NOT NULL DEFAULT now(), + "возле стоячей воды" BIGINT NOT NULL DEFAULT 1, + PRIMARY KEY ("Он заставляет меня лечь")) + ; +CREATE TABLE "Господь мой пастырь"."Он восстанавливает мою душу." ( + "Он ведет меня путями" BIGINT NOT NULL AUTO_INCREMENT, + "праведности" VARCHAR(500) NULL, + "ради его имени" DATETIME NOT NULL DEFAULT now(), + PRIMARY KEY ("Он ведет меня путями")) + ; +CREATE SCHEMA "Хотя я иду по долине"; +CREATE TABLE "Хотя я иду по долине"."тени смерти" ( + "Я не буду бояться зла" BIGINT NOT NULL AUTO_INCREMENT, + "ибо ты со мной;" VARCHAR(500) NULL, + "твой жезл и твой посох," DATETIME NOT NULL DEFAULT now(), + "они меня утешают." BIGINT NOT NULL DEFAULT 1, + PRIMARY KEY ("Я не буду бояться зла")) + ; +CREATE INDEX "Ты готовишь стол раньше меня" ON "Хотя я иду по долине"."тени смерти" ("ибо ты со мной;" ASC); +CREATE INDEX "в присутствии моих врагов;" ON "Хотя я иду по долине"."тени смерти" ("твой жезл и твой посох," ASC); +CREATE TABLE "Хотя я иду по долине"."Ты помажешь мне голову маслом;" ( + "мой запас переполнен." BIGINT NOT NULL AUTO_INCREMENT, + "Воистину, добро и милосердие последуют за мной." VARCHAR(100) NULL, + "все дни моей жизни," VARCHAR(200) NULL, + "и буду жить в доме Господнем" VARCHAR(300) NULL, + "навсегда." VARCHAR(400) NULL, + PRIMARY KEY ("мой запас переполнен.")) + ; diff --git a/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.css b/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.css new file mode 100644 index 0000000..30a34c8 --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.css @@ -0,0 +1,35 @@ + +* { + margin: 0; + padding: 0; +} + +body { + font-family: "Helvetica Neue", "Helvetica", "Arial"; + font-size: 14px; + background-color: rgba(45, 67, 49, 0.32); +} + +.container { + width: 500px; + margin: 25px auto; + padding-top: 25px; +} + +.dialog { + padding: 20px; + background-color: #233927; + color: #fff; + -moz-border-radius: 10px; + -webkit-border-radius: 10px; + border-radius: 10px; + margin-top: 25px; +} + +form .dialog, #errorTitle { + color: #ff0000; +} + +.hidden { + display: none; +} diff --git a/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.html b/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.html new file mode 100644 index 0000000..648c57e --- /dev/null +++ b/nx01-no2all-zerofungus/src/main/resources/static/powp/error/errmsg.html @@ -0,0 +1,37 @@ + + + + +Error + + + + + + +
+
+

Error

+


+

We could not process your request because;

+

...

+ Home +
+
+ + + +