Further refactoring, move away from using future thread pool

This commit is contained in:
William278 committed 2023-03-04 21:38:53 +00:00
1 parent 4c90e5498c
commit 39e0b25d46
56 files changed
+737 -1713

No files matched your search

@@ -1,19 +1,14 @@
package net.william278.huskhomes.network;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.request.TeleportRequest;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.teleport.Teleport;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.CompletableFuture;
public abstract class Broker {
protected final HuskHomes plugin;
private final Map<UUID, CompletableFuture<Message>> outboundMessages = new HashMap<>();
/**
* Create a new broker
@@ -34,45 +29,31 @@ public abstract class Broker {
if (message.getSourceServer().equals(getServer()) || receiver == null) {
return;
}
if (message.getDirection() == Message.Direction.INBOUND) {
this.outboundMessages.getOrDefault(message.getUuid(), new CompletableFuture<>()).complete(message);
this.outboundMessages.remove(message.getUuid());
return;
}
switch (message.getType()) {
case TELEPORT_TO_POSITION_REQUEST -> {
if (message.getPayload().getPosition() != null) {
Teleport.builder(plugin, receiver)
.setTarget(message.getPayload().getPosition()).toTeleport()
.thenAccept(teleport -> teleport.execute()
.thenAccept(result -> message.reply(
this,
receiver,
Payload.withTeleportResult(result.getState())
)));
return;
}
}
case POSITION_REQUEST -> message.reply(this, receiver, Payload.withPosition(receiver.getPosition()));
case TELEPORT_REQUEST -> {
if (message.getPayload().getTeleportRequest() != null) {
message.reply(this, receiver, plugin.getRequestManager()
.sendLocalTeleportRequest(message.getPayload().getTeleportRequest(), receiver)
.map(Payload::withTeleportRequest)
.orElse(Payload.empty()));
return;
}
message.reply(this, receiver, Payload.empty());
}
case TELEPORT_REQUEST_RESPONSE -> {
if (message.getPayload().getTeleportRequest() != null) {
final TeleportRequest request = message.getPayload().getTeleportRequest();
plugin.getRequestManager().handleLocalRequestResponse(receiver, request);
message.reply(this, receiver, Payload.withTeleportRequest(request));
return;
}
message.reply(this, receiver, Payload.empty());
}
case TELEPORT_TO_POSITION -> message.getPayload()
.getPosition().ifPresent(position -> Teleport.builder(plugin)
.teleporter(receiver)
.target(position)
.toTeleport()
.execute());
case TELEPORT_TO_NETWORKED_POSITION -> Message.builder()
.type(Message.Type.TELEPORT_TO_POSITION)
.target(message.getSender())
.payload(Payload.withPosition(receiver.getPosition()))
.build().send(this, receiver);
case TELEPORT_TO_NETWORKED_USER -> message.getPayload()
.getString().ifPresent(target -> Message.builder()
.type(Message.Type.TELEPORT_TO_NETWORKED_POSITION)
.target(target)
.build().send(this, receiver));
case TELEPORT_REQUEST -> message.getPayload()
.getTeleportRequest()
.ifPresent(teleportRequest -> plugin.getManager().requests()
.sendLocalTeleportRequest(teleportRequest, receiver));
case TELEPORT_REQUEST_RESPONSE -> message.getPayload()
.getTeleportRequest()
.ifPresent(teleportRequest -> plugin.getManager().requests()
.handleLocalRequestResponse(receiver, teleportRequest));
}
}
@@ -99,11 +80,6 @@ public abstract class Broker {
*/
public abstract void changeServer(@NotNull OnlineUser user, @NotNull String server);
@NotNull
protected Map<UUID, CompletableFuture<Message>> getOutboundMessages() {
return outboundMessages;
}
/**
* Terminate the broker
*/
@@ -120,23 +96,23 @@ public abstract class Broker {
return plugin.getServerName();
}
/**
* Identifies types of message brokers
*/
public enum Type {
PLUGIN_MESSAGE("Plugin Messages"),
REDIS("Redis");
@NotNull
private final String displayName;
/**
* Identifies types of message brokers
*/
public enum Type {
PLUGIN_MESSAGE("Plugin Messages"),
REDIS("Redis");
@NotNull
private final String displayName;
Type(@NotNull String displayName) {
this.displayName = displayName;
}
@NotNull
public String getDisplayName() {
return displayName;
}
Type(@NotNull String displayName) {
this.displayName = displayName;
}
@NotNull
public String getDisplayName() {
return displayName;
}
}
}
@@ -5,7 +5,6 @@ import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* Represents a message sent by a {@link Broker} cross-server. See {@link #builder()} for
@@ -27,8 +26,6 @@ public class Message {
private String sender;
@Expose
private String sourceServer;
@Expose
private Direction direction;
private Message(@NotNull Type type, @NotNull String target, @NotNull Payload payload) {
this.type = type;
@@ -45,22 +42,10 @@ public class Message {
return new Builder();
}
public CompletableFuture<Message> send(@NotNull Broker broker, @NotNull OnlineUser sender) {
public void send(@NotNull Broker broker, @NotNull OnlineUser sender) {
this.sender = sender.getUsername();
this.sourceServer = broker.getServer();
this.direction = Direction.OUTBOUND;
final CompletableFuture<Message> future = new CompletableFuture<>();
broker.getOutboundMessages().put(getUuid(), future);
broker.send(this, sender);
return future;
}
public void reply(@NotNull Broker broker, @NotNull OnlineUser receiver, @NotNull Payload payload) {
this.sender = receiver.getUsername();
this.sourceServer = broker.getServer();
this.direction = Direction.INBOUND;
this.payload = payload;
broker.send(this, receiver);
}
@NotNull
@@ -88,11 +73,6 @@ public class Message {
return sourceServer;
}
@NotNull
public Direction getDirection() {
return direction;
}
@NotNull
public UUID getUuid() {
return id;
@@ -138,21 +118,10 @@ public class Message {
* Different types of cross-server messages
*/
public enum Type {
TELEPORT_TO_POSITION_REQUEST,
POSITION_REQUEST,
TELEPORT_TO_POSITION,
TELEPORT_TO_NETWORKED_POSITION,
TELEPORT_REQUEST,
POSITION_RESPONSE, TELEPORT_REQUEST_RESPONSE
}
public enum Direction {
/**
* The message is being sent from the sender to the target
*/
OUTBOUND,
/**
* The message is being sent from the target to the sender
*/
INBOUND
POSITION_RESPONSE, TELEPORT_TO_NETWORKED_USER, TELEPORT_REQUEST_RESPONSE
}
}
@@ -1,28 +1,33 @@
package net.william278.huskhomes.network;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.request.TeleportRequest;
import net.william278.huskhomes.teleport.TeleportRequest;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
/**
* Represents a payload sent in a cross-server {@link Request}
* Represents a payload sent in a cross-server {@link Message}
*/
public class Payload {
@Nullable
@Expose
private Position position;
@Nullable
@SerializedName("teleport_result")
private TeleportResult resultState;
@Nullable
@Expose
@SerializedName("teleport_request")
private TeleportRequest teleportRequest;
@Nullable
@Expose
private String string;
/**
* Returns an empty cross-server message payload
*
@@ -46,19 +51,6 @@ public class Payload {
return payload;
}
/**
* Returns a payload containing a {@link TeleportResult}
*
* @param resultState the teleport to send
* @return a payload containing the teleport result
*/
@NotNull
public static Payload withTeleportResult(@NotNull TeleportResult resultState) {
final Payload payload = new Payload();
payload.resultState = resultState;
return payload;
}
/**
* Returns a payload containing a {@link TeleportRequest}
*
@@ -72,30 +64,35 @@ public class Payload {
return payload;
}
/**
* A string field
*/
@NotNull
public static Payload withString(@NotNull String target) {
final Payload payload = new Payload();
payload.string = target;
return payload;
}
private Payload() {
}
/**
* A position field
*/
@Nullable
public Position getPosition() {
return position;
public Optional<Position> getPosition() {
return Optional.ofNullable(position);
}
public Optional<TeleportRequest> getTeleportRequest() {
return Optional.ofNullable(teleportRequest);
}
/**
* A teleport result field
* A string field
*/
@Nullable
public TeleportResult getResultState() {
return resultState;
}
/**
* A teleport request field
*/
@Nullable
public TeleportRequest getTeleportRequest() {
return teleportRequest;
public Optional<String> getString() {
return Optional.ofNullable(string);
}
}