From 999424c5a0d0555950ebff06a69a181ebf2325ea Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 1 Aug 2022 22:06:28 +0800 Subject: Bind and initialize ApiEndpoint. --- src/main/java/com/keuin/crosslink/api/ApiServer.java | 19 +++++++++++++++---- .../com/keuin/crosslink/plugin/common/PluginMain.java | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/keuin/crosslink/api/ApiServer.java b/src/main/java/com/keuin/crosslink/api/ApiServer.java index 8a3bbce..2570cb0 100644 --- a/src/main/java/com/keuin/crosslink/api/ApiServer.java +++ b/src/main/java/com/keuin/crosslink/api/ApiServer.java @@ -7,6 +7,8 @@ import com.keuin.crosslink.api.request.JsonHttpExchange; import com.keuin.crosslink.api.request.JsonReqHandler; import com.keuin.crosslink.data.PlayerInfo; import com.keuin.crosslink.data.ServerInfo; +import com.keuin.crosslink.messaging.endpoint.system.ApiEndpoint; +import com.keuin.crosslink.messaging.sender.ISender; import com.keuin.crosslink.plugin.common.ICoreAccessor; import com.keuin.crosslink.util.LoggerNaming; import com.sun.net.httpserver.HttpServer; @@ -16,7 +18,9 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.Objects; +import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @@ -105,16 +109,23 @@ public class ApiServer implements IApiServer { @Override protected void handle(JsonHttpExchange exchange) throws IOException { var req = exchange.getRequestBody(); - var sender = req.get("sender"); - var message = req.get("message"); - if (!sender.isTextual() || !message.isTextual()) { + var sender = req.get("sender").textValue(); + var message = req.get("message").textValue(); + if (sender == null || message == null) { exchange.setResponseCode(400); var resp = exchange.getResponseBody(); resp.put("success", false); resp.put("message", "Illegal parameter type."); return; } - + // the message will be sent from ApiEndpoint (system:api) + ApiEndpoint.INSTANCE.offerMessage( + ISender.create( + sender, + UUID.nameUUIDFromBytes(sender.getBytes(StandardCharsets.UTF_8)) + ), + message + ); } }) .build().forEach((p, h) -> server.createContext(p).setHandler(h)); diff --git a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java index 69b4299..dff53a5 100644 --- a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java +++ b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java @@ -12,6 +12,7 @@ import com.keuin.crosslink.messaging.config.remote.InvalidEndpointConfigurationE import com.keuin.crosslink.messaging.config.remote.RemoteEndpointFactory; import com.keuin.crosslink.messaging.config.router.RouterConfigurer; import com.keuin.crosslink.messaging.endpoint.IEndpoint; +import com.keuin.crosslink.messaging.endpoint.system.ApiEndpoint; import com.keuin.crosslink.messaging.router.IRouter; import com.keuin.crosslink.plugin.common.environ.PluginEnvironment; import com.keuin.crosslink.util.LoggerNaming; @@ -103,7 +104,8 @@ public final class PluginMain { } // API endpoint (send messages from HTTP api) - + ApiEndpoint.INSTANCE.setRouter(messageRouter); + messageRouter.addEndpoint(ApiEndpoint.INSTANCE); // register all endpoints on the router for (IEndpoint ep : endpoints) { -- cgit v1.2.3