|
| 1 | +package com.exaroton.api.ws.stream; |
| 2 | + |
| 3 | +import com.exaroton.api.server.ServerStatus; |
| 4 | +import com.exaroton.api.ws.WebSocketConnection; |
| 5 | +import com.exaroton.api.ws.subscriber.ManagementNotificationSubscriber; |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.JsonElement; |
| 8 | +import com.google.gson.JsonObject; |
| 9 | +import org.jetbrains.annotations.ApiStatus; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +import java.util.*; |
| 14 | +import java.util.concurrent.CompletableFuture; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | + |
| 17 | +@ApiStatus.Internal |
| 18 | +public final class ServerManagementStream extends Stream<ManagementNotificationSubscriber> { |
| 19 | + private final Map<UUID, CompletableFuture<JsonElement>> waitingForResponse = new ConcurrentHashMap<>(); |
| 20 | + |
| 21 | + public ServerManagementStream(@NotNull WebSocketConnection ws, @NotNull Gson gson) { |
| 22 | + super(ws, gson); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public StreamType getType() { |
| 27 | + return StreamType.MANAGEMENT; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void onDataMessage(String type, JsonObject message) { |
| 32 | + var data = message.getAsJsonObject("data"); |
| 33 | + switch (type) { |
| 34 | + case "notification": |
| 35 | + for (ManagementNotificationSubscriber subscriber : getSubscribers()) { |
| 36 | + subscriber.handleNotification(data.get("name").getAsString(), data.get("data")); |
| 37 | + } |
| 38 | + break; |
| 39 | + case "response": |
| 40 | + var id = UUID.fromString(data.get("id").getAsString()); |
| 41 | + var future = waitingForResponse.remove(id); |
| 42 | + if (future != null) { |
| 43 | + future.complete(data.get("data")); |
| 44 | + } |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Set<ServerStatus> getStartableStatuses() { |
| 51 | + return Set.of(ServerStatus.ONLINE); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean hasNoSubscribers() { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + public CompletableFuture<JsonElement> sendRequest(@NotNull String method, @Nullable JsonElement params) { |
| 60 | + return this.shouldBeStarted().thenCompose(shouldStart -> { |
| 61 | + if (!shouldStart) { |
| 62 | + throw new IllegalStateException("The management stream is not active."); |
| 63 | + } |
| 64 | + |
| 65 | + var future = new CompletableFuture<JsonElement>(); |
| 66 | + var id = UUID.randomUUID(); |
| 67 | + waitingForResponse.put(id, future); |
| 68 | + var item = new Request(id, method, params); |
| 69 | + this.sendWhenStarted(messageData("request", item)); |
| 70 | + return future; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 75 | + private static final class Request { |
| 76 | + private final UUID id; |
| 77 | + private final String method; |
| 78 | + private final JsonElement params; |
| 79 | + |
| 80 | + public Request(UUID id, String method, JsonElement params) { |
| 81 | + this.id = id; |
| 82 | + this.method = method; |
| 83 | + this.params = params; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments