mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
networking/refactor: Rename messages to requests, use builder class
This commit is contained in:
1 parent
10a3eebc55
commit
0c1ad832ea
16 files changed
+429
-342
No files matched your search
@@ -20,9 +20,9 @@ import net.william278.huskhomes.event.EventDispatcher;
|
|||||||
import net.william278.huskhomes.hook.*;
|
import net.william278.huskhomes.hook.*;
|
||||||
import net.william278.huskhomes.listener.BukkitEventListener;
|
import net.william278.huskhomes.listener.BukkitEventListener;
|
||||||
import net.william278.huskhomes.listener.EventListener;
|
import net.william278.huskhomes.listener.EventListener;
|
||||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
import net.william278.huskhomes.network.Messenger;
|
||||||
import net.william278.huskhomes.messenger.PluginMessenger;
|
import net.william278.huskhomes.network.PluginMessenger;
|
||||||
import net.william278.huskhomes.messenger.RedisMessenger;
|
import net.william278.huskhomes.network.RedisMessenger;
|
||||||
import net.william278.huskhomes.migrator.LegacyMigrator;
|
import net.william278.huskhomes.migrator.LegacyMigrator;
|
||||||
import net.william278.huskhomes.migrator.Migrator;
|
import net.william278.huskhomes.migrator.Migrator;
|
||||||
import net.william278.huskhomes.player.BukkitPlayer;
|
import net.william278.huskhomes.player.BukkitPlayer;
|
||||||
@@ -83,7 +83,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
private List<Migrator> migrators;
|
private List<Migrator> migrators;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private NetworkMessenger networkMessenger;
|
private Messenger messenger;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Server server;
|
private Server server;
|
||||||
@@ -148,11 +148,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
// Initialize the network messenger if proxy mode is enabled
|
// Initialize the network messenger if proxy mode is enabled
|
||||||
if (getSettings().crossServer) {
|
if (getSettings().crossServer) {
|
||||||
getLoggingAdapter().log(Level.INFO, "Initializing the network messenger...");
|
getLoggingAdapter().log(Level.INFO, "Initializing the network messenger...");
|
||||||
networkMessenger = switch (settings.messengerType) {
|
messenger = switch (settings.messengerType) {
|
||||||
case PLUGIN_MESSAGE -> new PluginMessenger();
|
case PLUGIN_MESSAGE -> new PluginMessenger();
|
||||||
case REDIS -> new RedisMessenger();
|
case REDIS -> new RedisMessenger();
|
||||||
};
|
};
|
||||||
networkMessenger.initialize(this);
|
messenger.initialize(this);
|
||||||
getLoggingAdapter().log(Level.INFO, "Successfully initialized the network messenger.");
|
getLoggingAdapter().log(Level.INFO, "Successfully initialized the network messenger.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,8 +293,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
if (database != null) {
|
if (database != null) {
|
||||||
database.terminate();
|
database.terminate();
|
||||||
}
|
}
|
||||||
if (networkMessenger != null) {
|
if (messenger != null) {
|
||||||
networkMessenger.terminate();
|
messenger.close();
|
||||||
}
|
}
|
||||||
if (audiences != null) {
|
if (audiences != null) {
|
||||||
audiences.close();
|
audiences.close();
|
||||||
@@ -361,11 +361,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public NetworkMessenger getNetworkMessenger() {
|
public Messenger getMessenger() {
|
||||||
if (networkMessenger == null) {
|
if (messenger == null) {
|
||||||
throw new HuskHomesException("Attempted to access network messenger when it was not initialized");
|
throw new HuskHomesException("Attempted to access network messenger when it was not initialized");
|
||||||
}
|
}
|
||||||
return networkMessenger;
|
return messenger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -476,7 +476,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
if (!getSettings().crossServer || this.server != null) {
|
if (!getSettings().crossServer || this.server != null) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
return getNetworkMessenger().fetchServerName(requester).orTimeout(5, TimeUnit.SECONDS).exceptionally(throwable -> null).thenAccept(serverName -> {
|
return getMessenger().fetchServerName(requester).orTimeout(5, TimeUnit.SECONDS).exceptionally(throwable -> null).thenAccept(serverName -> {
|
||||||
if (serverName == null) {
|
if (serverName == null) {
|
||||||
throw new HuskHomesException("GetServer plugin message call operation timed out");
|
throw new HuskHomesException("GetServer plugin message call operation timed out");
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-14
@@ -1,4 +1,4 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
import com.google.common.io.ByteArrayDataInput;
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
@@ -23,7 +23,7 @@ import java.util.logging.Level;
|
|||||||
/**
|
/**
|
||||||
* <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/">Plugin Messaging channel</a> messenger implementation
|
* <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/">Plugin Messaging channel</a> messenger implementation
|
||||||
*/
|
*/
|
||||||
public class PluginMessenger extends NetworkMessenger implements PluginMessageListener {
|
public class PluginMessenger extends Messenger implements PluginMessageListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of BungeeCord's provided plugin channel.
|
* The name of BungeeCord's provided plugin channel.
|
||||||
@@ -108,30 +108,30 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> dispatchMessage(@NotNull OnlineUser sender, @NotNull Message message) {
|
public CompletableFuture<Request> dispatchMessage(@NotNull OnlineUser sender, @NotNull Request request) {
|
||||||
final CompletableFuture<Message> repliedMessage = new CompletableFuture<>();
|
final CompletableFuture<Request> repliedMessage = new CompletableFuture<>();
|
||||||
processingMessages.put(message.uuid, repliedMessage);
|
processingMessages.put(request.getUuid(), repliedMessage);
|
||||||
sendPluginMessage((BukkitPlayer) sender, message);
|
sendPluginMessage((BukkitPlayer) sender, request);
|
||||||
return repliedMessage;
|
return repliedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void sendReply(@NotNull OnlineUser replier, @NotNull Message reply) {
|
protected void sendReply(@NotNull OnlineUser replier, @NotNull Request reply) {
|
||||||
sendPluginMessage((BukkitPlayer) replier, reply);
|
sendPluginMessage((BukkitPlayer) replier, reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("UnstableApiUsage")
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
private void sendPluginMessage(@NotNull BukkitPlayer player, @NotNull Message message) {
|
private void sendPluginMessage(@NotNull BukkitPlayer player, @NotNull Request request) {
|
||||||
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
||||||
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
|
||||||
messageWriter.writeUTF("ForwardToPlayer");
|
messageWriter.writeUTF("ForwardToPlayer");
|
||||||
messageWriter.writeUTF(message.targetPlayer);
|
messageWriter.writeUTF(request.getTargetPlayer());
|
||||||
messageWriter.writeUTF(NETWORK_MESSAGE_CHANNEL);
|
messageWriter.writeUTF(NETWORK_MESSAGE_CHANNEL);
|
||||||
|
|
||||||
// Write the plugin message
|
// Write the plugin message
|
||||||
try (final ByteArrayOutputStream messageByteOutputStream = new ByteArrayOutputStream()) {
|
try (final ByteArrayOutputStream messageByteOutputStream = new ByteArrayOutputStream()) {
|
||||||
try (DataOutputStream messageDataOutputStream = new DataOutputStream(messageByteOutputStream)) {
|
try (DataOutputStream messageDataOutputStream = new DataOutputStream(messageByteOutputStream)) {
|
||||||
messageDataOutputStream.writeUTF(message.toJson());
|
messageDataOutputStream.writeUTF(request.toJson());
|
||||||
messageWriter.writeShort(messageByteOutputStream.toByteArray().length);
|
messageWriter.writeShort(messageByteOutputStream.toByteArray().length);
|
||||||
messageWriter.write(messageByteOutputStream.toByteArray());
|
messageWriter.write(messageByteOutputStream.toByteArray());
|
||||||
}
|
}
|
||||||
@@ -145,7 +145,7 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void terminate() {
|
public void close() {
|
||||||
Bukkit.getMessenger().unregisterIncomingPluginChannel((BukkitHuskHomes) plugin);
|
Bukkit.getMessenger().unregisterIncomingPluginChannel((BukkitHuskHomes) plugin);
|
||||||
Bukkit.getMessenger().unregisterIncomingPluginChannel((BukkitHuskHomes) plugin);
|
Bukkit.getMessenger().unregisterIncomingPluginChannel((BukkitHuskHomes) plugin);
|
||||||
}
|
}
|
||||||
@@ -167,14 +167,14 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
|
|
||||||
// Read the message in byte-by-byte and parse it
|
// Read the message in byte-by-byte and parse it
|
||||||
try (DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(messageBody))) {
|
try (DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(messageBody))) {
|
||||||
final Message message = Message.fromJson(messageReader.readUTF());
|
final Request request = Request.fromJson(messageReader.readUTF());
|
||||||
|
|
||||||
// Ignore plugin messages to other clusters
|
// Ignore plugin messages to other clusters
|
||||||
if (!message.clusterId.equals(clusterId)) {
|
if (!request.getClusterId().equals(clusterId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMessage(BukkitPlayer.adapt(player), message);
|
handleMessage(BukkitPlayer.adapt(player), request);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
plugin.getLoggingAdapter().log(Level.SEVERE,
|
plugin.getLoggingAdapter().log(Level.SEVERE,
|
||||||
"Failed to read an inbound plugin message", e);
|
"Failed to read an inbound plugin message", e);
|
||||||
+12
-12
@@ -1,4 +1,4 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.player.BukkitPlayer;
|
import net.william278.huskhomes.player.BukkitPlayer;
|
||||||
@@ -36,15 +36,15 @@ public class RedisMessenger extends PluginMessenger {
|
|||||||
if (!channel.equals(NETWORK_MESSAGE_CHANNEL)) {
|
if (!channel.equals(NETWORK_MESSAGE_CHANNEL)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Message message = Message.fromJson(encodedMessage);
|
final Request request = Request.fromJson(encodedMessage);
|
||||||
if (!message.clusterId.equals(clusterId)) {
|
if (!request.getClusterId().equals(clusterId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Optional<BukkitPlayer> receiver = BukkitPlayer.get(message.targetPlayer);
|
final Optional<BukkitPlayer> receiver = BukkitPlayer.get(request.getTargetPlayer());
|
||||||
if (receiver.isEmpty()) {
|
if (receiver.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
handleMessage(receiver.get(), message);
|
handleMessage(receiver.get(), request);
|
||||||
}
|
}
|
||||||
}, NETWORK_MESSAGE_CHANNEL);
|
}, NETWORK_MESSAGE_CHANNEL);
|
||||||
}
|
}
|
||||||
@@ -53,21 +53,21 @@ public class RedisMessenger extends PluginMessenger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> dispatchMessage(@NotNull OnlineUser sender, @NotNull Message message) {
|
public CompletableFuture<Request> dispatchMessage(@NotNull OnlineUser sender, @NotNull Request request) {
|
||||||
final CompletableFuture<Message> repliedMessage = new CompletableFuture<>();
|
final CompletableFuture<Request> repliedMessage = new CompletableFuture<>();
|
||||||
processingMessages.put(message.uuid, repliedMessage);
|
processingMessages.put(request.getUuid(), repliedMessage);
|
||||||
redisWorker.sendMessage(message);
|
redisWorker.sendMessage(request);
|
||||||
return repliedMessage;
|
return repliedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void sendReply(@NotNull OnlineUser replier, @NotNull Message reply) {
|
protected void sendReply(@NotNull OnlineUser replier, @NotNull Request reply) {
|
||||||
redisWorker.sendMessage(reply);
|
redisWorker.sendMessage(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void terminate() {
|
public void close() {
|
||||||
redisWorker.terminate();
|
redisWorker.terminate();
|
||||||
super.terminate();
|
super.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,7 +106,7 @@ public class Cache {
|
|||||||
*/
|
*/
|
||||||
public CompletableFuture<List<String>> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
|
public CompletableFuture<List<String>> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
|
return plugin.getMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
|
||||||
players.clear();
|
players.clear();
|
||||||
players.addAll(List.of(returnedPlayerList));
|
players.addAll(List.of(returnedPlayerList));
|
||||||
return players;
|
return players;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import net.william278.huskhomes.event.EventDispatcher;
|
|||||||
import net.william278.huskhomes.hook.EconomyHook;
|
import net.william278.huskhomes.hook.EconomyHook;
|
||||||
import net.william278.huskhomes.hook.MapHook;
|
import net.william278.huskhomes.hook.MapHook;
|
||||||
import net.william278.huskhomes.hook.PluginHook;
|
import net.william278.huskhomes.hook.PluginHook;
|
||||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
import net.william278.huskhomes.network.Messenger;
|
||||||
import net.william278.huskhomes.migrator.Migrator;
|
import net.william278.huskhomes.migrator.Migrator;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.position.*;
|
import net.william278.huskhomes.position.*;
|
||||||
@@ -86,7 +86,7 @@ public interface HuskHomes {
|
|||||||
return CompletableFuture.completedFuture(Optional.of(localPlayer.get().username));
|
return CompletableFuture.completedFuture(Optional.of(localPlayer.get().username));
|
||||||
}
|
}
|
||||||
if (getSettings().crossServer) {
|
if (getSettings().crossServer) {
|
||||||
return getNetworkMessenger().findPlayer(requester, targetPlayerName);
|
return getMessenger().findPlayer(requester, targetPlayerName);
|
||||||
}
|
}
|
||||||
return CompletableFuture.completedFuture(Optional.empty());
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
}
|
}
|
||||||
@@ -139,12 +139,12 @@ public interface HuskHomes {
|
|||||||
SavedPositionManager getSavedPositionManager();
|
SavedPositionManager getSavedPositionManager();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link NetworkMessenger} that sends cross-network messages
|
* The {@link Messenger} that sends cross-network messages
|
||||||
*
|
*
|
||||||
* @return the {@link NetworkMessenger} implementation
|
* @return the {@link Messenger} implementation
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
NetworkMessenger getNetworkMessenger() throws HuskHomesException;
|
Messenger getMessenger() throws HuskHomesException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link RandomTeleportEngine} that manages random teleports
|
* The {@link RandomTeleportEngine} that manages random teleports
|
||||||
|
|||||||
@@ -1,112 +0,0 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
|
||||||
|
|
||||||
import com.google.gson.*;
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A message sent cross-proxy to a player
|
|
||||||
*/
|
|
||||||
public class Message {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A unique ID representing this message
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public UUID uuid;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The type of the message
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public MessageType type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the context under which this message is being relayed; an outbound {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to one.
|
|
||||||
*/
|
|
||||||
@SerializedName("relay_type")
|
|
||||||
@NotNull
|
|
||||||
public RelayType relayType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Username of the target player the message is bound for
|
|
||||||
*/
|
|
||||||
@SerializedName("target_player")
|
|
||||||
@NotNull
|
|
||||||
public String targetPlayer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Username of the sender of the message
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public String sender;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ID of the cluster this message is for
|
|
||||||
*/
|
|
||||||
@SerializedName("cluster_id")
|
|
||||||
@NotNull
|
|
||||||
public String clusterId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Data to send in the message
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public MessagePayload payload;
|
|
||||||
|
|
||||||
public Message(@NotNull MessageType type, @NotNull String sender, @NotNull String targetPlayer,
|
|
||||||
@NotNull MessagePayload payload, @NotNull Message.RelayType relayType, @NotNull String clusterId) {
|
|
||||||
this.type = type;
|
|
||||||
this.sender = sender;
|
|
||||||
this.targetPlayer = targetPlayer;
|
|
||||||
this.payload = payload;
|
|
||||||
this.relayType = relayType;
|
|
||||||
this.clusterId = clusterId;
|
|
||||||
this.uuid = UUID.randomUUID();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public String toJson() {
|
|
||||||
return new GsonBuilder().create().toJson(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static Message fromJson(@NotNull String json) {
|
|
||||||
return new GsonBuilder().create().fromJson(json, Message.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Identifies the type of message or reply
|
|
||||||
*/
|
|
||||||
public enum MessageType {
|
|
||||||
TELEPORT_TO_POSITION_REQUEST,
|
|
||||||
POSITION_REQUEST,
|
|
||||||
TELEPORT_REQUEST,
|
|
||||||
TELEPORT_REQUEST_RESPONSE
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Identifies the source of the message being relayed - a {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to a message.
|
|
||||||
*/
|
|
||||||
public enum RelayType {
|
|
||||||
/**
|
|
||||||
* An outbound message to/from a server
|
|
||||||
*/
|
|
||||||
MESSAGE,
|
|
||||||
/**
|
|
||||||
* A reply to an outbound message
|
|
||||||
*/
|
|
||||||
REPLY
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(@NotNull Object other) {
|
|
||||||
if (other instanceof Message message) {
|
|
||||||
return message.uuid.equals(this.uuid);
|
|
||||||
}
|
|
||||||
return super.equals(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+59
-75
@@ -1,4 +1,4 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
@@ -11,7 +11,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public abstract class NetworkMessenger {
|
public abstract class Messenger implements AutoCloseable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the network message channel for HuskHomes messages
|
* Name of the network message channel for HuskHomes messages
|
||||||
@@ -21,7 +21,7 @@ public abstract class NetworkMessenger {
|
|||||||
/**
|
/**
|
||||||
* Map of message UUIDs to messages being processed
|
* Map of message UUIDs to messages being processed
|
||||||
*/
|
*/
|
||||||
protected HashMap<UUID, CompletableFuture<Message>> processingMessages;
|
protected HashMap<UUID, CompletableFuture<Request>> processingMessages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of pending futures for processing {@link #fetchServerName(OnlineUser)} requests
|
* List of pending futures for processing {@link #fetchServerName(OnlineUser)} requests
|
||||||
@@ -123,54 +123,54 @@ public abstract class NetworkMessenger {
|
|||||||
* Send a network message
|
* Send a network message
|
||||||
*
|
*
|
||||||
* @param sender {@link OnlineUser} sending the message
|
* @param sender {@link OnlineUser} sending the message
|
||||||
* @param message {@link Message} to send
|
* @param request {@link Request} to send
|
||||||
* @return Future returning the {@link Message} sent, that will time out after
|
* @return Future returning the {@link Request} sent, that will time out after
|
||||||
* @apiNote This method invokes {@link #dispatchMessage(OnlineUser, Message)}, applying a time-to-live timeOut
|
* @apiNote This method invokes {@link #dispatchMessage(OnlineUser, Request)}, applying a time-to-live timeOut
|
||||||
*/
|
*/
|
||||||
public final CompletableFuture<Optional<Message>> sendMessage(@NotNull OnlineUser sender, @NotNull Message message) {
|
protected final CompletableFuture<Optional<Request>> sendMessage(@NotNull OnlineUser sender, @NotNull Request request) {
|
||||||
return dispatchMessage(sender, message)
|
return dispatchMessage(sender, request)
|
||||||
.orTimeout(MESSAGE_TIME_OUT, TimeUnit.SECONDS)
|
.orTimeout(MESSAGE_TIME_OUT, TimeUnit.SECONDS)
|
||||||
.exceptionally(e -> {
|
.exceptionally(e -> {
|
||||||
plugin.getLoggingAdapter().log(Level.WARNING, "Message dispatch after " + MESSAGE_TIME_OUT + " seconds", e);
|
plugin.getLoggingAdapter().log(Level.WARNING, "Message dispatch after " + MESSAGE_TIME_OUT + " seconds", e);
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.thenApply(result -> {
|
.thenApply(result -> {
|
||||||
processingMessages.remove(message.uuid);
|
processingMessages.remove(request.getUuid());
|
||||||
return Optional.ofNullable(result);
|
return Optional.ofNullable(result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch a {@link Message} via the appropriate handler
|
* Dispatch a {@link Request} via the appropriate handler
|
||||||
*
|
*
|
||||||
* @param sender The {@link OnlineUser} that sent the {@link Message}
|
* @param sender The {@link OnlineUser} that sent the {@link Request}
|
||||||
* @param message The {@link Message} to dispatch
|
* @param request The {@link Request} to dispatch
|
||||||
* @return A future containing a reply to the {@link Message} that was sent
|
* @return A future containing a reply to the {@link Request} that was sent
|
||||||
*/
|
*/
|
||||||
protected abstract CompletableFuture<Message> dispatchMessage(@NotNull OnlineUser sender, @NotNull Message message);
|
protected abstract CompletableFuture<Request> dispatchMessage(@NotNull OnlineUser sender, @NotNull Request request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a reply to a received {@link Message}
|
* Send a reply to a received {@link Request}
|
||||||
*
|
*
|
||||||
* @param reply The reply {@link Message} to send
|
* @param reply The reply {@link Request} to send
|
||||||
*/
|
*/
|
||||||
protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Message reply);
|
protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Request reply);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle and action received network {@link Message}s
|
* Handle and action received network {@link Request}s
|
||||||
*
|
*
|
||||||
* @param receiver The online {@link OnlineUser} receiving the message
|
* @param receiver The online {@link OnlineUser} receiving the message
|
||||||
* @param message The received {@link Message}
|
* @param request The received {@link Request}
|
||||||
*/
|
*/
|
||||||
protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Message message) {
|
protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Request request) {
|
||||||
switch (message.relayType) {
|
switch (request.getRelayType()) {
|
||||||
// Handle a message and send reply
|
// Handle a message and send reply
|
||||||
case MESSAGE -> prepareReply(receiver, message).thenAccept(reply -> sendReply(receiver, message));
|
case MESSAGE -> handleRequest(receiver, request);
|
||||||
// Handle a reply message
|
// Handle a reply message
|
||||||
case REPLY -> {
|
case REPLY -> {
|
||||||
if (processingMessages.containsKey(message.uuid)) {
|
if (processingMessages.containsKey(request.getUuid())) {
|
||||||
final Message finalMessage = message;
|
final Request finalRequest = request;
|
||||||
processingMessages.get(message.uuid).completeAsync(() -> finalMessage);
|
processingMessages.get(request.getUuid()).completeAsync(() -> finalRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
plugin.getLoggingAdapter().log(Level.WARNING, "Received a reply to a message that was not sent by this server");
|
plugin.getLoggingAdapter().log(Level.WARNING, "Received a reply to a message that was not sent by this server");
|
||||||
@@ -183,63 +183,47 @@ public abstract class NetworkMessenger {
|
|||||||
* to the sender
|
* to the sender
|
||||||
*
|
*
|
||||||
* @param receiver The {@link OnlineUser} receiving the message
|
* @param receiver The {@link OnlineUser} receiving the message
|
||||||
* @param message The received {@link Message}
|
* @param request The received {@link Request}
|
||||||
* @return A future containing the reply {@link Message}
|
|
||||||
*/
|
*/
|
||||||
private CompletableFuture<Message> prepareReply(@NotNull final OnlineUser receiver, @NotNull final Message message) {
|
private void handleRequest(@NotNull OnlineUser receiver, @NotNull Request request) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
switch (request.getType()) {
|
||||||
switch (message.type) {
|
case TELEPORT_TO_POSITION_REQUEST -> {
|
||||||
case TELEPORT_TO_POSITION_REQUEST -> {
|
if (request.getPayload().position != null) {
|
||||||
if (message.payload.position != null) {
|
Teleport.builder(plugin, receiver)
|
||||||
message.payload = MessagePayload.withTeleportResult(Teleport.builder(plugin, receiver)
|
.setTarget(request.getPayload().position).toTeleport()
|
||||||
.setTarget(message.payload.position)
|
.thenAccept(teleport -> teleport.execute()
|
||||||
.toTeleport().join().execute().join().getState());
|
.thenAccept(result -> request.reply(receiver,
|
||||||
} else {
|
Payload.withTeleportResult(result.getState()), plugin)));
|
||||||
message.payload = MessagePayload.empty();
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition());
|
|
||||||
case TELEPORT_REQUEST -> {
|
|
||||||
if (message.payload.teleportRequest != null) {
|
|
||||||
plugin.getRequestManager().sendLocalTeleportRequest(message.payload.teleportRequest, receiver);
|
|
||||||
} else {
|
|
||||||
message.payload = MessagePayload.empty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case TELEPORT_REQUEST_RESPONSE -> {
|
|
||||||
if (message.payload.teleportRequest != null) {
|
|
||||||
plugin.getRequestManager().handleLocalRequestResponse(receiver, message.payload.teleportRequest);
|
|
||||||
} else {
|
|
||||||
message.payload = MessagePayload.empty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
request.reply(receiver, Payload.empty(), plugin);
|
||||||
}
|
}
|
||||||
return message;
|
case POSITION_REQUEST -> request.reply(receiver, Payload.withPosition(receiver.getPosition()), plugin);
|
||||||
}).exceptionally(exception -> {
|
case TELEPORT_REQUEST -> {
|
||||||
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to prepare message reply", exception);
|
if (request.getPayload().teleportRequest != null) {
|
||||||
message.payload = MessagePayload.empty();
|
request.reply(receiver, plugin.getRequestManager()
|
||||||
return message;
|
.sendLocalTeleportRequest(request.getPayload().teleportRequest, receiver)
|
||||||
}).thenApply(reply -> {
|
.map(Payload::withTeleportRequest)
|
||||||
formatReplyMessage(message, receiver);
|
.orElse(Payload.empty()), plugin);
|
||||||
return reply;
|
return;
|
||||||
});
|
}
|
||||||
}
|
request.reply(receiver, Payload.empty(), plugin);
|
||||||
|
}
|
||||||
/**
|
case TELEPORT_REQUEST_RESPONSE -> {
|
||||||
* Prepare a {@link Message} to be sent as a reply to a received {@link Message}
|
if (request.getPayload().teleportRequest != null) {
|
||||||
*
|
plugin.getRequestManager().handleLocalRequestResponse(receiver, request.getPayload().teleportRequest);
|
||||||
* @param message The received {@link Message}
|
request.reply(receiver, plugin);
|
||||||
* @param receiver The {@link OnlineUser} receiving the message
|
return;
|
||||||
*/
|
}
|
||||||
private void formatReplyMessage(@NotNull Message message, @NotNull OnlineUser receiver) {
|
request.reply(receiver, Payload.empty(), plugin);
|
||||||
message.targetPlayer = message.sender;
|
}
|
||||||
message.sender = receiver.username;
|
}
|
||||||
message.relayType = Message.RelayType.REPLY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the network messenger
|
* Close the network messenger
|
||||||
*/
|
*/
|
||||||
public abstract void terminate();
|
@Override
|
||||||
|
public abstract void close();
|
||||||
|
|
||||||
}
|
}
|
||||||
+12
-12
@@ -1,4 +1,4 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
@@ -8,9 +8,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a payload sent in a cross-server {@link Message}
|
* Represents a payload sent in a cross-server {@link Request}
|
||||||
*/
|
*/
|
||||||
public class MessagePayload {
|
public class Payload {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A position field
|
* A position field
|
||||||
@@ -38,8 +38,8 @@ public class MessagePayload {
|
|||||||
* @return an empty payload
|
* @return an empty payload
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static MessagePayload empty() {
|
public static Payload empty() {
|
||||||
return new MessagePayload();
|
return new Payload();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,8 +49,8 @@ public class MessagePayload {
|
|||||||
* @return a payload containing the position
|
* @return a payload containing the position
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static MessagePayload withPosition(@NotNull Position position) {
|
public static Payload withPosition(@NotNull Position position) {
|
||||||
final MessagePayload payload = new MessagePayload();
|
final Payload payload = new Payload();
|
||||||
payload.position = position;
|
payload.position = position;
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
@@ -62,8 +62,8 @@ public class MessagePayload {
|
|||||||
* @return a payload containing the teleport result
|
* @return a payload containing the teleport result
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static MessagePayload withTeleportResult(@NotNull TeleportResult resultState) {
|
public static Payload withTeleportResult(@NotNull TeleportResult resultState) {
|
||||||
final MessagePayload payload = new MessagePayload();
|
final Payload payload = new Payload();
|
||||||
payload.resultState = resultState;
|
payload.resultState = resultState;
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
@@ -75,13 +75,13 @@ public class MessagePayload {
|
|||||||
* @return a payload containing the teleport request
|
* @return a payload containing the teleport request
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static MessagePayload withTeleportRequest(@NotNull TeleportRequest teleportRequest) {
|
public static Payload withTeleportRequest(@NotNull TeleportRequest teleportRequest) {
|
||||||
final MessagePayload payload = new MessagePayload();
|
final Payload payload = new Payload();
|
||||||
payload.teleportRequest = teleportRequest;
|
payload.teleportRequest = teleportRequest;
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessagePayload() {
|
private Payload() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
import com.google.gson.annotations.Expose;
|
||||||
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import net.william278.huskhomes.HuskHomes;
|
||||||
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A message sent cross-proxy to a player
|
||||||
|
*/
|
||||||
|
public class Request {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A unique ID representing this message
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private UUID uuid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the message
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private MessageType type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the context under which this message is being relayed; an outbound {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to one.
|
||||||
|
*/
|
||||||
|
@SerializedName("relay_type")
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private RelayType relayType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Username of the target player the message is bound for
|
||||||
|
*/
|
||||||
|
@SerializedName("target_player")
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private String targetPlayer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID of the cluster this message is for
|
||||||
|
*/
|
||||||
|
@SerializedName("cluster_id")
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private String clusterId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data to send in the message
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private Payload payload;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Username of the sender of the message
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Expose
|
||||||
|
private String sender;
|
||||||
|
|
||||||
|
protected Request(@NotNull MessageType type, @NotNull String targetPlayer, @NotNull Payload payload,
|
||||||
|
@NotNull Request.RelayType relayType) {
|
||||||
|
this.type = type;
|
||||||
|
this.targetPlayer = targetPlayer;
|
||||||
|
this.payload = payload;
|
||||||
|
this.relayType = relayType;
|
||||||
|
this.uuid = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private Request() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static RequestBuilder builder() {
|
||||||
|
return new RequestBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String toJson() {
|
||||||
|
return new GsonBuilder().create().toJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Request fromJson(@NotNull String json) {
|
||||||
|
return new GsonBuilder().create().fromJson(json, Request.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies the type of message or reply
|
||||||
|
*/
|
||||||
|
public enum MessageType {
|
||||||
|
TELEPORT_TO_POSITION_REQUEST,
|
||||||
|
POSITION_REQUEST,
|
||||||
|
TELEPORT_REQUEST,
|
||||||
|
TELEPORT_REQUEST_RESPONSE
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies the source of the message being relayed - a {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to a message.
|
||||||
|
*/
|
||||||
|
public enum RelayType {
|
||||||
|
/**
|
||||||
|
* An outbound message to/from a server
|
||||||
|
*/
|
||||||
|
MESSAGE,
|
||||||
|
/**
|
||||||
|
* A reply to an outbound message
|
||||||
|
*/
|
||||||
|
REPLY
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public MessageType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public RelayType getRelayType() {
|
||||||
|
return relayType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getTargetPlayer() {
|
||||||
|
return targetPlayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getClusterId() {
|
||||||
|
return clusterId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Payload getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CompletableFuture<Optional<Request>> send(@NotNull OnlineUser sender, @NotNull HuskHomes plugin) {
|
||||||
|
this.sender = sender.username;
|
||||||
|
this.clusterId = plugin.getSettings().clusterId;
|
||||||
|
return plugin.getMessenger().sendMessage(sender, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reply(@NotNull OnlineUser sender, @NotNull Payload payload, @NotNull HuskHomes plugin) {
|
||||||
|
this.targetPlayer = this.sender;
|
||||||
|
this.sender = sender.username;
|
||||||
|
this.clusterId = plugin.getSettings().clusterId;
|
||||||
|
this.payload = payload;
|
||||||
|
this.relayType = RelayType.REPLY;
|
||||||
|
plugin.getMessenger().sendReply(sender, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reply(@NotNull OnlineUser sender, @NotNull HuskHomes plugin) {
|
||||||
|
this.reply(sender, this.payload, plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(@NotNull Object other) {
|
||||||
|
if (other instanceof Request request) {
|
||||||
|
return request.uuid.equals(this.uuid);
|
||||||
|
}
|
||||||
|
return super.equals(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class RequestBuilder {
|
||||||
|
|
||||||
|
private Request.MessageType type;
|
||||||
|
|
||||||
|
private String targetPlayer;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Payload payload = Payload.empty();
|
||||||
|
|
||||||
|
protected RequestBuilder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public RequestBuilder withType(Request.MessageType type) {
|
||||||
|
this.type = type;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public RequestBuilder withTargetPlayer(String targetPlayer) {
|
||||||
|
this.targetPlayer = targetPlayer;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public RequestBuilder withPayload(Payload payload) {
|
||||||
|
this.payload = payload;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Request build() {
|
||||||
|
return new Request(type, targetPlayer, payload, Request.RelayType.MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package net.william278.huskhomes.redis;
|
package net.william278.huskhomes.redis;
|
||||||
|
|
||||||
import net.william278.huskhomes.config.Settings;
|
import net.william278.huskhomes.config.Settings;
|
||||||
import net.william278.huskhomes.messenger.Message;
|
import net.william278.huskhomes.network.Request;
|
||||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
import net.william278.huskhomes.network.Messenger;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
@@ -43,10 +43,10 @@ public class RedisWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompletableFuture<Void> sendMessage(@NotNull Message message) {
|
public CompletableFuture<Void> sendMessage(@NotNull Request request) {
|
||||||
return CompletableFuture.runAsync(() -> {
|
return CompletableFuture.runAsync(() -> {
|
||||||
try (Jedis jedis = jedisPool.getResource()) {
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
jedis.publish(NetworkMessenger.NETWORK_MESSAGE_CHANNEL, message.toJson());
|
jedis.publish(Messenger.NETWORK_MESSAGE_CHANNEL, request.toJson());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package net.william278.huskhomes.request;
|
package net.william278.huskhomes.request;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.messenger.Message;
|
import net.william278.huskhomes.network.Request;
|
||||||
import net.william278.huskhomes.messenger.MessagePayload;
|
import net.william278.huskhomes.network.Payload;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.player.User;
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
@@ -140,23 +140,21 @@ public class RequestManager {
|
|||||||
// If the player couldn't be found locally, send the request cross-server
|
// If the player couldn't be found locally, send the request cross-server
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
// Find the matching networked target
|
// Find the matching networked target
|
||||||
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApplyAsync(networkedTarget -> {
|
return plugin.getMessenger().findPlayer(requester, targetUser).thenApplyAsync(networkedTarget -> {
|
||||||
if (networkedTarget.isEmpty()) {
|
if (networkedTarget.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the network messenger to send the request
|
// Use the network messenger to send the request
|
||||||
request.recipientName = networkedTarget.get();
|
request.recipientName = networkedTarget.get();
|
||||||
return plugin.getNetworkMessenger()
|
return Request.builder()
|
||||||
.sendMessage(requester, new Message(Message.MessageType.TELEPORT_REQUEST,
|
.withType(Request.MessageType.TELEPORT_REQUEST)
|
||||||
requester.username,
|
.withPayload(Payload.withTeleportRequest(request))
|
||||||
networkedTarget.get(),
|
.withTargetPlayer(networkedTarget.get())
|
||||||
MessagePayload.withTeleportRequest(request),
|
.build().send(requester, plugin)
|
||||||
Message.RelayType.MESSAGE,
|
|
||||||
plugin.getSettings().clusterId))
|
|
||||||
.thenApply(reply -> {
|
.thenApply(reply -> {
|
||||||
if (reply.isPresent()) {
|
if (reply.isPresent()) {
|
||||||
final TeleportRequest result = reply.get().payload.teleportRequest;
|
final TeleportRequest result = reply.get().getPayload().teleportRequest;
|
||||||
if (result == null || result.status == TeleportRequest.RequestStatus.IGNORED) {
|
if (result == null || result.status == TeleportRequest.RequestStatus.IGNORED) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -196,7 +194,7 @@ public class RequestManager {
|
|||||||
// Add the request and display a message to the recipient
|
// Add the request and display a message to the recipient
|
||||||
addTeleportRequest(request, recipient);
|
addTeleportRequest(request, recipient);
|
||||||
plugin.getLocales().getLocale((request.type == TeleportRequest.RequestType.TPA ? "tpa" : "tpahere")
|
plugin.getLocales().getLocale((request.type == TeleportRequest.RequestType.TPA ? "tpa" : "tpahere")
|
||||||
+ "_request_received", request.requesterName)
|
+ "_request_received", request.requesterName)
|
||||||
.ifPresent(recipient::sendMessage);
|
.ifPresent(recipient::sendMessage);
|
||||||
plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName)
|
plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName)
|
||||||
.ifPresent(recipient::sendMessage);
|
.ifPresent(recipient::sendMessage);
|
||||||
@@ -283,7 +281,7 @@ public class RequestManager {
|
|||||||
handleLocalRequestResponse(localRequester.get(), request);
|
handleLocalRequestResponse(localRequester.get(), request);
|
||||||
} else if (plugin.getSettings().crossServer) {
|
} else if (plugin.getSettings().crossServer) {
|
||||||
// Ensure the sender is still online
|
// Ensure the sender is still online
|
||||||
plugin.getNetworkMessenger()
|
plugin.getMessenger()
|
||||||
.findPlayer(recipient, request.requesterName)
|
.findPlayer(recipient, request.requesterName)
|
||||||
.thenApplyAsync(networkedTarget -> {
|
.thenApplyAsync(networkedTarget -> {
|
||||||
if (networkedTarget.isEmpty()) {
|
if (networkedTarget.isEmpty()) {
|
||||||
@@ -292,13 +290,11 @@ public class RequestManager {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugin.getNetworkMessenger()
|
return Request.builder()
|
||||||
.sendMessage(recipient, new Message(Message.MessageType.TELEPORT_REQUEST_RESPONSE,
|
.withType(Request.MessageType.TELEPORT_REQUEST_RESPONSE)
|
||||||
recipient.username,
|
.withPayload(Payload.withTeleportRequest(request))
|
||||||
networkedTarget.get(),
|
.withTargetPlayer(networkedTarget.get())
|
||||||
MessagePayload.withTeleportRequest(request),
|
.build().send(recipient, plugin)
|
||||||
Message.RelayType.MESSAGE,
|
|
||||||
plugin.getSettings().clusterId))
|
|
||||||
.thenApply(Optional::isPresent)
|
.thenApply(Optional::isPresent)
|
||||||
.join();
|
.join();
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package net.william278.huskhomes.teleport;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.config.Settings;
|
import net.william278.huskhomes.config.Settings;
|
||||||
import net.william278.huskhomes.messenger.Message;
|
import net.william278.huskhomes.network.Request;
|
||||||
import net.william278.huskhomes.messenger.MessagePayload;
|
import net.william278.huskhomes.network.Payload;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.player.User;
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
@@ -187,7 +187,7 @@ public class Teleport {
|
|||||||
plugin.getDatabase()
|
plugin.getDatabase()
|
||||||
.setCurrentTeleport(teleporter, this)
|
.setCurrentTeleport(teleporter, this)
|
||||||
.thenApply(ignored -> plugin
|
.thenApply(ignored -> plugin
|
||||||
.getNetworkMessenger()
|
.getMessenger()
|
||||||
.sendPlayer(teleporter, target.server)
|
.sendPlayer(teleporter, target.server)
|
||||||
.thenApply(completed -> completed
|
.thenApply(completed -> completed
|
||||||
? TeleportResult.COMPLETED_CROSS_SERVER
|
? TeleportResult.COMPLETED_CROSS_SERVER
|
||||||
@@ -215,19 +215,16 @@ public class Teleport {
|
|||||||
assert teleporter != null;
|
assert teleporter != null;
|
||||||
|
|
||||||
// Send a network message to a user on another server to teleport them to the target position
|
// Send a network message to a user on another server to teleport them to the target position
|
||||||
return plugin.getNetworkMessenger()
|
return Request.builder()
|
||||||
.sendMessage(executor,
|
.withType(Request.MessageType.TELEPORT_TO_POSITION_REQUEST)
|
||||||
new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST,
|
.withPayload(Payload.withPosition(target))
|
||||||
executor.username,
|
.withTargetPlayer(teleporter.username)
|
||||||
teleporter.username,
|
.build().send(executor, plugin)
|
||||||
MessagePayload.withPosition(target),
|
|
||||||
Message.RelayType.MESSAGE,
|
|
||||||
plugin.getSettings().clusterId))
|
|
||||||
.thenApply(result -> {
|
.thenApply(result -> {
|
||||||
if (result.isPresent()) {
|
if (result.isPresent()) {
|
||||||
final Message reply = result.get();
|
final Request reply = result.get();
|
||||||
if (reply.payload.resultState != null) {
|
if (reply.getPayload().resultState != null) {
|
||||||
return reply.payload.resultState;
|
return reply.getPayload().resultState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
|
return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ package net.william278.huskhomes.teleport;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.config.Settings;
|
import net.william278.huskhomes.config.Settings;
|
||||||
import net.william278.huskhomes.messenger.Message;
|
import net.william278.huskhomes.network.Request;
|
||||||
import net.william278.huskhomes.messenger.MessagePayload;
|
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.player.User;
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Location;
|
import net.william278.huskhomes.position.Location;
|
||||||
@@ -91,7 +90,7 @@ public class TeleportBuilder {
|
|||||||
.map(onlineUser -> (User) onlineUser)
|
.map(onlineUser -> (User) onlineUser)
|
||||||
.or(() -> {
|
.or(() -> {
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
return plugin.getNetworkMessenger()
|
return plugin.getMessenger()
|
||||||
.findPlayer(executor, teleporterUsername).join()
|
.findPlayer(executor, teleporterUsername).join()
|
||||||
.map(username -> new User(UUID.randomUUID(), username));
|
.map(username -> new User(UUID.randomUUID(), username));
|
||||||
}
|
}
|
||||||
@@ -234,17 +233,17 @@ public class TeleportBuilder {
|
|||||||
return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition()));
|
return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition()));
|
||||||
}
|
}
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
return plugin.getNetworkMessenger()
|
return plugin.getMessenger()
|
||||||
.findPlayer(executor, playerName)
|
.findPlayer(executor, playerName)
|
||||||
.thenApplyAsync(foundPlayer -> {
|
.thenApplyAsync(foundPlayer -> {
|
||||||
if (foundPlayer.isEmpty()) {
|
if (foundPlayer.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
return plugin.getNetworkMessenger()
|
return Request.builder()
|
||||||
.sendMessage(executor, new Message(Message.MessageType.POSITION_REQUEST,
|
.withType(Request.MessageType.POSITION_REQUEST)
|
||||||
executor.username, playerName, MessagePayload.empty(),
|
.withTargetPlayer(playerName)
|
||||||
Message.RelayType.MESSAGE, plugin.getSettings().clusterId))
|
.build().send(executor, plugin)
|
||||||
.thenApply(reply -> reply.map(message -> message.payload.position)).join();
|
.thenApply(reply -> reply.map(message -> message.getPayload().position)).join();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return CompletableFuture.supplyAsync(Optional::empty);
|
return CompletableFuture.supplyAsync(Optional::empty);
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
package net.william278.huskhomes.messenger;
|
|
||||||
|
|
||||||
import net.william278.huskhomes.position.Position;
|
|
||||||
import net.william278.huskhomes.position.Server;
|
|
||||||
import net.william278.huskhomes.position.World;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class MessageTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMessageSerializationWithEmptyPayload() {
|
|
||||||
final Message message = new Message(
|
|
||||||
Message.MessageType.TELEPORT_REQUEST,
|
|
||||||
"TestSender",
|
|
||||||
"TestTarget",
|
|
||||||
MessagePayload.empty(),
|
|
||||||
Message.RelayType.MESSAGE,
|
|
||||||
"TestClusterId");
|
|
||||||
Assertions.assertNotNull(message.toJson());
|
|
||||||
|
|
||||||
final Message readMessage = Message.fromJson(message.toJson());
|
|
||||||
Assertions.assertEquals(message.uuid, readMessage.uuid);
|
|
||||||
Assertions.assertEquals(message.sender, readMessage.sender);
|
|
||||||
Assertions.assertEquals(message.targetPlayer, readMessage.targetPlayer);
|
|
||||||
Assertions.assertEquals(message.type, readMessage.type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMessageSerializationWithPositionPayload() {
|
|
||||||
final Message message = new Message(
|
|
||||||
Message.MessageType.POSITION_REQUEST,
|
|
||||||
"TestSender",
|
|
||||||
"TestTarget",
|
|
||||||
MessagePayload.withPosition(new Position(63.25, 127.43, -32, 180f, -94.3f,
|
|
||||||
new World("TestWorld", UUID.randomUUID()), new Server("TestServer"))),
|
|
||||||
Message.RelayType.REPLY,
|
|
||||||
"TestClusterId");
|
|
||||||
Assertions.assertNotNull(message.toJson());
|
|
||||||
|
|
||||||
final Message readMessage = Message.fromJson(message.toJson());
|
|
||||||
Assertions.assertEquals(message.uuid, readMessage.uuid);
|
|
||||||
Assertions.assertNotNull(readMessage.payload.position);
|
|
||||||
|
|
||||||
assert message.payload.position != null;
|
|
||||||
Assertions.assertEquals(message.payload.position.x, readMessage.payload.position.x);
|
|
||||||
Assertions.assertEquals(message.payload.position.y, readMessage.payload.position.y);
|
|
||||||
Assertions.assertEquals(message.payload.position.z, readMessage.payload.position.z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
|
import net.william278.huskhomes.position.Position;
|
||||||
|
import net.william278.huskhomes.position.Server;
|
||||||
|
import net.william278.huskhomes.position.World;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class RequestTests {
|
||||||
|
|
||||||
|
private final static Request DUMMY_NO_PAYLOAD = Request.builder()
|
||||||
|
.withType(Request.MessageType.TELEPORT_REQUEST)
|
||||||
|
.withTargetPlayer("TestTarget")
|
||||||
|
.withPayload(Payload.empty())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private final static Request DUMMY_PAYLOAD = Request.builder()
|
||||||
|
.withType(Request.MessageType.TELEPORT_REQUEST)
|
||||||
|
.withTargetPlayer("TestTarget")
|
||||||
|
.withPayload(Payload.withPosition(
|
||||||
|
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||||
|
new World("TestWorld", UUID.randomUUID()), new Server("TestServer"))))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageSerializationWithEmptyPayload() {
|
||||||
|
Assertions.assertNotNull(DUMMY_NO_PAYLOAD.toJson());
|
||||||
|
|
||||||
|
final Request readRequest = Request.fromJson(DUMMY_NO_PAYLOAD.toJson());
|
||||||
|
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getUuid(), readRequest.getUuid());
|
||||||
|
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getTargetPlayer(), readRequest.getTargetPlayer());
|
||||||
|
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getType(), readRequest.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageSerializationWithPositionPayload() {
|
||||||
|
Assertions.assertNotNull(DUMMY_PAYLOAD.toJson());
|
||||||
|
|
||||||
|
final Request readRequest = Request.fromJson(DUMMY_PAYLOAD.toJson());
|
||||||
|
Assertions.assertEquals(DUMMY_PAYLOAD.getUuid(), readRequest.getUuid());
|
||||||
|
Assertions.assertNotNull(readRequest.getPayload().position);
|
||||||
|
|
||||||
|
assert readRequest.getPayload().position != null && DUMMY_PAYLOAD.getPayload().position != null;
|
||||||
|
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.x, readRequest.getPayload().position.x);
|
||||||
|
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.y, readRequest.getPayload().position.y);
|
||||||
|
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.z, readRequest.getPayload().position.z);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user