From 5cc3576d25e6a5d477fb6e2750b21030de40d356 Mon Sep 17 00:00:00 2001 From: Martin Graversen Date: Sun, 22 Oct 2023 22:14:37 +0200 Subject: [PATCH] :bug: Support cases where no response is emitted from server --- .../rust/rcon/DefaultRustRconClient.java | 25 ++++++++----------- .../graversen/rust/rcon/RustRconClient.java | 3 ++- .../graversen/rust/rcon/RustRconResponse.java | 3 ++- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/graversen/rust/rcon/DefaultRustRconClient.java b/src/main/java/io/graversen/rust/rcon/DefaultRustRconClient.java index 6ee82a9..76bc5d9 100644 --- a/src/main/java/io/graversen/rust/rcon/DefaultRustRconClient.java +++ b/src/main/java/io/graversen/rust/rcon/DefaultRustRconClient.java @@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.java_websocket.framing.CloseFrame; +import javax.annotation.Nullable; import java.io.IOException; import java.util.Objects; import java.util.concurrent.CompletableFuture; @@ -69,7 +70,7 @@ public Function mapRequest() { } @Override - public Function mapResponse(@NonNull RustRconRequest request) { + public Function mapResponse(@Nullable RustRconRequest request) { return rustRconResponseDTO -> new RustRconResponse( Objects.requireNonNullElse(rustRconResponseDTO.getIdentifier(), -1), rustRconResponseDTO.getMessage(), @@ -135,23 +136,19 @@ public void onWsMessage(@NonNull WsMessageEvent messageEvent) { final var rconMessage = jsonMapper().fromJson(messageEvent.getMessage(), RustRconResponseDTO.class); final var rconRequest = getAsyncRequests().getIfPresent(rconMessage.getIdentifier()); - if (rconRequest != null) { - final var mappedRconMessage = mapResponse(rconRequest).apply(rconMessage); + final var mappedRconMessage = mapResponse(rconRequest).apply(rconMessage); - if (rconMessage.getIdentifier() >= initialMessageIdentifier()) { - final var responseFutureOrNull = getAsyncResponses().getIfPresent(rconMessage.getIdentifier()); + if (rconMessage.getIdentifier() >= initialMessageIdentifier()) { + final var responseFutureOrNull = getAsyncResponses().getIfPresent(rconMessage.getIdentifier()); - if (responseFutureOrNull != null) { - responseFutureOrNull.complete(mappedRconMessage); - } else { - log.warn("Could not resolve response future for ID: {} (Discarding it)", rconMessage.getIdentifier()); - } + if (responseFutureOrNull != null) { + responseFutureOrNull.complete(mappedRconMessage); + } else { + log.warn("Could not resolve response future for ID: {} (Discarding it)", rconMessage.getIdentifier()); } - - eventBus.post(new RconReceivedEvent(name(), mappedRconMessage)); - } else { - log.warn("Could not resolve request for ID: {} (Discarding it)", rconMessage.getIdentifier()); } + + eventBus.post(new RconReceivedEvent(name(), mappedRconMessage)); } @Synchronized diff --git a/src/main/java/io/graversen/rust/rcon/RustRconClient.java b/src/main/java/io/graversen/rust/rcon/RustRconClient.java index 021d576..b42eaa8 100644 --- a/src/main/java/io/graversen/rust/rcon/RustRconClient.java +++ b/src/main/java/io/graversen/rust/rcon/RustRconClient.java @@ -4,6 +4,7 @@ import io.graversen.rust.rcon.util.JsonMapper; import lombok.NonNull; +import javax.annotation.Nullable; import java.io.Closeable; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -13,7 +14,7 @@ public interface RustRconClient extends EventEmitter, Closeable { Function mapRequest(); - Function mapResponse(@NonNull RustRconRequest request); + Function mapResponse(@Nullable RustRconRequest request); JsonMapper jsonMapper(); diff --git a/src/main/java/io/graversen/rust/rcon/RustRconResponse.java b/src/main/java/io/graversen/rust/rcon/RustRconResponse.java index a3152f0..c74af9f 100644 --- a/src/main/java/io/graversen/rust/rcon/RustRconResponse.java +++ b/src/main/java/io/graversen/rust/rcon/RustRconResponse.java @@ -2,6 +2,7 @@ import lombok.*; +import javax.annotation.Nullable; import java.time.LocalDateTime; @Getter @@ -13,5 +14,5 @@ public class RustRconResponse { private final @NonNull String message; private final @NonNull String type; private final @NonNull RustServer server; - private final @NonNull RustRconRequest request; + private final @Nullable RustRconRequest request; }