Skip to content

Commit

Permalink
✨ Include RustRconRequest inside RustRconResponse
Browse files Browse the repository at this point in the history
For traceability
  • Loading branch information
MrGraversen committed Oct 22, 2023
1 parent ae5343f commit 0e12501
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
44 changes: 31 additions & 13 deletions src/main/java/io/graversen/rust/rcon/DefaultRustRconClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class DefaultRustRconClient implements RustRconClient {
private final @NonNull RustWebSocketClient webSocketClient;
private final @NonNull EventBus eventBus;

private Cache<Integer, CompletableFuture<RustRconResponse>> asyncRequests;
private Cache<Integer, CompletableFuture<RustRconResponse>> asyncResponses;
private Cache<Integer, RustRconRequest> asyncRequests;
private JsonMapper jsonMapper;

@Override
Expand All @@ -46,7 +47,8 @@ public CompletableFuture<RustRconResponse> send(@NonNull RustRconRequest request
final var json = jsonMapper().toJson(mappedRequest);

final var responseFuture = new CompletableFuture<RustRconResponse>();
getAsyncRequests().put(request.getIdentifier(), responseFuture);
getAsyncRequests().put(request.getIdentifier(), request);
getAsyncResponses().put(request.getIdentifier(), responseFuture);
webSocketClient.send(json);

return responseFuture;
Expand All @@ -67,12 +69,13 @@ public Function<RustRconRequest, RustRconRequestDTO> mapRequest() {
}

@Override
public Function<RustRconResponseDTO, RustRconResponse> mapResponse() {
public Function<RustRconResponseDTO, RustRconResponse> mapResponse(@NonNull RustRconRequest request) {
return rustRconResponseDTO -> new RustRconResponse(
Objects.requireNonNullElse(rustRconResponseDTO.getIdentifier(), -1),
rustRconResponseDTO.getMessage(),
rustRconResponseDTO.getType(),
rustServer.get()
rustServer.get(),
request
);
}

Expand Down Expand Up @@ -130,23 +133,38 @@ public void onWsOpenedEvent(@NonNull WsOpenedEvent wsOpenedEvent) {
@Subscribe
public void onWsMessage(@NonNull WsMessageEvent messageEvent) {
final var rconMessage = jsonMapper().fromJson(messageEvent.getMessage(), RustRconResponseDTO.class);
final var mappedRconMessage = mapResponse().apply(rconMessage);
final var rconRequest = getAsyncRequests().getIfPresent(rconMessage.getIdentifier());

if (rconMessage.getIdentifier() >= initialMessageIdentifier()) {
final var responseFutureOrNull = asyncRequests.getIfPresent(rconMessage.getIdentifier());
if (rconRequest != null) {
final var mappedRconMessage = mapResponse(rconRequest).apply(rconMessage);

if (responseFutureOrNull != null) {
responseFutureOrNull.complete(mappedRconMessage);
} else {
log.warn("Could not resolve response future for ID: {} (Discarding it)", 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());
}
}

eventBus.post(new RconReceivedEvent(name(), mappedRconMessage));
} else {
log.warn("Could not resolve request for ID: {} (Discarding it)", rconMessage.getIdentifier());
}
}

@Synchronized
private Cache<Integer, CompletableFuture<RustRconResponse>> getAsyncResponses() {
if (asyncResponses == null) {
asyncResponses = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build();
}

eventBus.post(new RconReceivedEvent(name(), mappedRconMessage));
return asyncResponses;
}

@Synchronized
private Cache<Integer, CompletableFuture<RustRconResponse>> getAsyncRequests() {
private Cache<Integer, RustRconRequest> getAsyncRequests() {
if (asyncRequests == null) {
asyncRequests = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/graversen/rust/rcon/RustRconClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface RustRconClient extends EventEmitter, Closeable {

Function<RustRconRequest, RustRconRequestDTO> mapRequest();

Function<RustRconResponseDTO, RustRconResponse> mapResponse();
Function<RustRconResponseDTO, RustRconResponse> mapResponse(@NonNull RustRconRequest request);

JsonMapper jsonMapper();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/graversen/rust/rcon/RustRconResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class RustRconResponse {
private final @NonNull String message;
private final @NonNull String type;
private final @NonNull RustServer server;
private final @NonNull RustRconRequest request;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class TestRustRconResponse extends RustRconResponse {
public TestRustRconResponse(@NonNull String message) {
super(0, message, "type", testServer());
super(0, message, "type", testServer(), new RustRconRequest(0, message));
}

private static RustServer testServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.graversen.rust.rcon.RustPlayer;
import io.graversen.rust.rcon.TestRustRconResponse;
import io.graversen.rust.rcon.protocol.AdminCodec;
import io.graversen.rust.rcon.protocol.dto.RustDtoMappers;
import io.graversen.rust.rcon.protocol.util.PlayerName;
import io.graversen.rust.rcon.protocol.util.SteamId64;
import org.junit.jupiter.api.Test;
Expand All @@ -18,6 +19,9 @@ class DefaultPlayerManagementTest {
@Mock
private AdminCodec adminCodec;

@Mock
private RustDtoMappers rustDtoMappers;

@InjectMocks
private DefaultPlayerManagement defaultPlayerManagement;

Expand Down

0 comments on commit 0e12501

Please sign in to comment.