Skip to content

Commit

Permalink
Merge pull request #37 from TheDeafCreeper/serverclosing
Browse files Browse the repository at this point in the history
Add the ability to close servers so that they're ignored by the load balancer.
  • Loading branch information
nathan-i-martin authored Oct 10, 2023
2 parents 47673cc + 9058c32 commit e327282
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package group.aelysium.rustyconnector.core.lib.packets;

import com.google.gson.*;
import group.aelysium.rustyconnector.core.lib.packets.variants.LockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.CoordinateRequestQueuePacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.UnlockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.SendPlayerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingResponsePacket;
Expand Down Expand Up @@ -187,6 +189,8 @@ public GenericPacket buildReceived() {
if (this.type == PacketType.PING_RESPONSE) return new ServerPingResponsePacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.SEND_PLAYER) return new SendPlayerPacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.COORDINATE_REQUEST_QUEUE) return new CoordinateRequestQueuePacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.UNLOCK_SERVER) return new UnlockServerPacket(this.address, this.origin, this.parameters);
if (this.type == PacketType.LOCK_SERVER) return new LockServerPacket(this.address, this.origin, this.parameters);

throw new IllegalStateException("Invalid RedisMessage type encountered!");
}
Expand All @@ -213,6 +217,8 @@ public GenericPacket buildSendable() {
if(this.type == PacketType.PING_RESPONSE) return new ServerPingResponsePacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.SEND_PLAYER) return new SendPlayerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.COORDINATE_REQUEST_QUEUE) return new CoordinateRequestQueuePacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.UNLOCK_SERVER) return new UnlockServerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.LOCK_SERVER) return new LockServerPacket(this.address, this.origin, this.parameters);

throw new IllegalStateException("Invalid RedisMessage type encountered!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@ public class PacketType {
*/
public static Mapping COORDINATE_REQUEST_QUEUE = new Mapping(300, "TPA_QUEUE_PLAYER");

/**
* `Server > Proxy` | Tells the proxy to open a server.
*/
public static Mapping UNLOCK_SERVER = new Mapping(400, "UNLOCK_SERVER");

/**
* `Server > Proxy` | Tells the proxy to close a server.
*/
public static Mapping LOCK_SERVER = new Mapping(401, "LOCK_SERVER");

public static List<Mapping> toList() {
List<Mapping> list = new ArrayList<>();
list.add(PING);
list.add(PING_RESPONSE);
list.add(SEND_PLAYER);
list.add(COORDINATE_REQUEST_QUEUE);
list.add(UNLOCK_SERVER);
list.add(LOCK_SERVER);

return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package group.aelysium.rustyconnector.core.lib.packets.variants;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketOrigin;
import group.aelysium.rustyconnector.core.lib.packets.PacketType;
import io.lettuce.core.KeyValue;

import java.net.InetSocketAddress;
import java.util.List;

public class LockServerPacket extends GenericPacket {
private String serverName;

public String serverName() { return this.serverName; }

public LockServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.LOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(UnlockServerPacket.ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
}
public LockServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.LOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(UnlockServerPacket.ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
}

@Override
public JsonObject toJSON() {
JsonObject object = super.toJSON();
JsonObject parameters = new JsonObject();

parameters.add(UnlockServerPacket.ValidParameters.SERVER_NAME, new JsonPrimitive(this.serverName));

object.add(MasterValidParameters.PARAMETERS, parameters);

return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package group.aelysium.rustyconnector.core.lib.packets.variants;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketOrigin;
import group.aelysium.rustyconnector.core.lib.packets.PacketType;
import io.lettuce.core.KeyValue;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

public class UnlockServerPacket extends GenericPacket {
private String serverName;

public String serverName() { return this.serverName; }

public UnlockServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.UNLOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
}
public UnlockServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.UNLOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
}

@Override
public JsonObject toJSON() {
JsonObject object = super.toJSON();
JsonObject parameters = new JsonObject();

parameters.add(ValidParameters.SERVER_NAME, new JsonPrimitive(this.serverName));

object.add(MasterValidParameters.PARAMETERS, parameters);

return object;
}

public interface ValidParameters {
String SERVER_NAME = "s";

static List<String> toList() {
List<String> list = new ArrayList<>();
list.add(SERVER_NAME);

return list;
}
}
}
10 changes: 9 additions & 1 deletion plugin/core/src/main/resources/en_us/language.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ velocity:
scalar_family:
panel:
no_registered_servers: "<dark_gray>There are no registered servers.</dark_gray>"
no_locked_servers: "<dark_gray>There are no locked servers.</dark_gray>"
no_unlocked_servers: "<dark_gray>There are no unlocked servers.</dark_gray>"
info:
- " ---| Online Players: <player_count>"
- " ---| Registered Servers: <server_count>"
- " ---| Joinable Servers: <joinable_count>"
- " ---| Parent Family: <parent_family_name>"
- " ---| Load Balancing:"
- " | - Algorithm: <balancing_algorithm>"
Expand All @@ -111,15 +114,19 @@ velocity:
commands:
sort: "<dark_gray>Will cause the family to completely resort itself in accordance with it's load balancing algorithm.</dark_gray>"
reset_index: "<dark_gray>Will reset the family's input to the first server in the family.</dark_gray>"
locked: "<dark_gray>Shows all locked servers instead of unlocked.</dark_gray>"
static_family:
residence:
missing: "<red>The server you were meant to be connected to is unavailable! In the meantime you've been connected to a fallback server!</red>"
blocked_join_attempt: "<red>The server you were meant to be connected to is unavailable! Please try again later!</red>"
panel:
no_registered_servers: "There are no registered servers."
no_registered_servers: "<dark_gray>There are no registered servers.</dark_gray>"
no_locked_servers: "<dark_gray>There are no locked servers.</dark_gray>"
no_unlocked_servers: "<dark_gray>There are no unlocked servers.</dark_gray>"
info:
- " ---| Online Players: <player_count>"
- " ---| Registered Servers: <server_count>"
- " ---| Joinable Servers: <joinable_count>"
- " ---| Parent Family: <parent_family_name>"
- " ---| Resident Server Expiration: <residence_expiration>"
- " ---| Load Balancing:"
Expand All @@ -131,6 +138,7 @@ velocity:
commands:
sort: "<dark_gray>Will cause the family to completely resort itself in accordance with it's load balancing algorithm.</dark_gray>"
reset_index: "<dark_gray>Will reset the family's input to the first server in the family.</dark_gray>"
locked: "<dark_gray>Shows all locked servers instead of unlocked.</dark_gray>"
party:
receiver_invite_query:
query: "<gray>Hey! <username> wants you to join their party!</gray>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static void create(PaperCommandManager<CommandSender> manager) {
manager.command(messageList(manager));
manager.command(messageGet(manager));
manager.command(send(manager));
manager.command(unlock(manager));
manager.command(lock(manager));
}

private static Command.Builder<CommandSender> messageGet(PaperCommandManager<CommandSender> manager) {
Expand Down Expand Up @@ -114,4 +116,46 @@ private static Command.Builder<CommandSender> send(PaperCommandManager<CommandSe
}
}).execute());
}

private static Command.Builder<CommandSender> unlock(PaperCommandManager<CommandSender> manager) {
Tinder api = Tinder.get();
PluginLogger logger = api.logger();

final Command.Builder<CommandSender> builder = api.commandManager().commandBuilder("rc", "/rc");

return builder.literal("unlock")
.senderType(ConsoleCommandSender.class)
.handler(context -> manager.taskRecipe().begin(context)
.asynchronous(commandContext -> {
try {
api.services().packetBuilder().unlockServer();
logger.log("Unlocking server.");
} catch (NullPointerException e) {
PaperLang.RC_SEND_USAGE.send(logger);
} catch (Exception e) {
logger.log("An error stopped us from processing the request!", e);
}
}).execute());
}

private static Command.Builder<CommandSender> lock(PaperCommandManager<CommandSender> manager) {
Tinder api = Tinder.get();
PluginLogger logger = api.logger();

final Command.Builder<CommandSender> builder = api.commandManager().commandBuilder("rc", "/rc");

return builder.literal("lock")
.senderType(ConsoleCommandSender.class)
.handler(context -> manager.taskRecipe().begin(context)
.asynchronous(commandContext -> {
try {
api.services().packetBuilder().lockServer();
logger.log("Locking server.");
} catch (NullPointerException e) {
PaperLang.RC_SEND_USAGE.send(logger);
} catch (Exception e) {
logger.log("An error stopped us from processing the request!", e);
}
}).execute());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketOrigin;
import group.aelysium.rustyconnector.core.lib.packets.PacketType;
import group.aelysium.rustyconnector.core.lib.packets.variants.LockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.UnlockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.SendPlayerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingPacket;
import group.aelysium.rustyconnector.core.lib.lang.Lang;
Expand Down Expand Up @@ -56,6 +58,40 @@ public void sendToOtherFamily(Player player, String familyName) {
api.flame().backbone().connection().orElseThrow().publish(message);
}

/**
* Tells the proxy to open the server running the command.
*/
public void unlockServer() {
Tinder api = Tinder.get();
ServerInfoService serverInfoService = api.services().serverInfo();

UnlockServerPacket message = (UnlockServerPacket) new GenericPacket.Builder()
.setType(PacketType.UNLOCK_SERVER)
.setOrigin(PacketOrigin.SERVER)
.setAddress(serverInfoService.address())
.setParameter(UnlockServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.buildSendable();

api.flame().backbone().connection().orElseThrow().publish(message);
}

/**
* Tells the proxy to close the server running the command.
*/
public void lockServer() {
Tinder api = Tinder.get();
ServerInfoService serverInfoService = api.services().serverInfo();

LockServerPacket message = (LockServerPacket) new GenericPacket.Builder()
.setType(PacketType.LOCK_SERVER)
.setOrigin(PacketOrigin.SERVER)
.setAddress(serverInfoService.address())
.setParameter(UnlockServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.buildSendable();

api.flame().backbone().connection().orElseThrow().publish(message);
}

@Override
public void kill() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
import group.aelysium.rustyconnector.plugin.velocity.lib.load_balancing.LoadBalancingService;
import group.aelysium.rustyconnector.plugin.velocity.lib.magic_link.MagicLinkService;
import group.aelysium.rustyconnector.plugin.velocity.lib.magic_link.handlers.MagicLinkPingHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.LockServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.SendPlayerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.UnlockServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.parties.PartyService;
import group.aelysium.rustyconnector.plugin.velocity.lib.parties.config.PartyConfig;
import group.aelysium.rustyconnector.plugin.velocity.lib.players.PlayerService;
Expand Down Expand Up @@ -354,6 +356,8 @@ public Callable<Runnable> connectors(AESCryptor cryptor, MessageCacheService cac
Map<PacketType.Mapping, PacketHandler> handlers = new HashMap<>();
handlers.put(PacketType.PING, new MagicLinkPingHandler());
handlers.put(PacketType.SEND_PLAYER, new SendPlayerHandler());
handlers.put(PacketType.LOCK_SERVER, new LockServerHandler());
handlers.put(PacketType.UNLOCK_SERVER, new UnlockServerHandler());

connectorsService.messengers().forEach(connector -> {
if(connector.connection().isEmpty()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ public static BrigadierCommand create(Flame flame, PluginLogger logger) {
return 1;
})
)
.then(LiteralArgumentBuilder.<CommandSource>literal("locked")
.executes(context -> {
try {
String familyName = context.getArgument("familyName", String.class);
BaseServerFamily family = flame.services().familyService().find(familyName);
if(family == null) throw new NullPointerException();

if(family instanceof ScalarServerFamily)
VelocityLang.RC_SCALAR_FAMILY_INFO_LOCKED.send(logger, (ScalarServerFamily) family);
if(family instanceof StaticServerFamily)
VelocityLang.RC_STATIC_FAMILY_INFO_LOCKED.send(logger, (StaticServerFamily) family);
} catch (NullPointerException e) {
VelocityLang.RC_FAMILY_ERROR.send(logger,"A family with that name doesn't exist!");
} catch (Exception e) {
VelocityLang.RC_FAMILY_ERROR.send(logger,"Something prevented us from doing that!\n"+e.getMessage());
}
return 1;
})
)
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("parties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand Down
Loading

0 comments on commit e327282

Please sign in to comment.