Scheduled server update

This commit is contained in:
Jaime Martinez Rincon 2017-02-19 12:01:58 +01:00
parent cd1dd959e4
commit d50f3d6362
8 changed files with 138 additions and 69 deletions

View File

@ -116,7 +116,7 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.2" level="project" />
<orderEntry type="library" name="Maven: org.bstats:bstats-bungeecord:1.0" level="project" />
<orderEntry type="library" name="Maven: org.bstats:bstats-bungeecord:1.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>

View File

@ -1,7 +1,6 @@
package me.jaimemartz.lobbybalancer;
import com.fasterxml.jackson.databind.ext.Java7Support;
import com.google.gson.Gson;
import me.jaimemartz.faucet.ConfigFactory;
import me.jaimemartz.lobbybalancer.commands.FallbackCommand;
import me.jaimemartz.lobbybalancer.commands.MainCommand;
@ -31,7 +30,6 @@ public class LobbyBalancer extends Plugin {
public static final String NONCE_ID = "%%__NONCE__%%";
private static final int LAST_VER_CONFIG_UPDATE = 20600;
private final Gson gson = new Gson();
private boolean failed = false;
private ConfigFactory factory;
@ -212,10 +210,6 @@ public class LobbyBalancer extends Plugin {
getLogger().info(String.format("The plugin has been reloaded, took %sms", ending));
}
public Gson getGson() {
return gson;
}
public GeolocationManager getGeolocationManager() {
return geolocationManager;
}

View File

@ -19,6 +19,7 @@ import net.md_5.bungee.api.plugin.Command;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
public class ManageCommand extends Command {
private final LobbyBalancer plugin;
@ -57,7 +58,7 @@ public class ManageCommand extends Command {
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
}
} else {
sendHelper(msgr);
help.accept(msgr);
}
break;
}
@ -133,7 +134,7 @@ public class ManageCommand extends Command {
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
}
} else {
sendHelper(msgr);
help.accept(msgr);
}
break;
}
@ -174,25 +175,23 @@ public class ManageCommand extends Command {
default: {
msgr.send("&cThis is not a valid argument for this command!");
sendHelper(msgr);
help.accept(msgr);
}
}
} else {
sendHelper(msgr);
help.accept(msgr);
}
} else {
msgr.send(ChatColor.RED + "You do not have permission to execute this command!");
}
}
private void sendHelper(Messager msgr) {
msgr.send(
"&7&m-----------------------------------------------------",
"&7Available commands:",
"&3/section list &7- &cTells you which sections are configured in the plugin",
"&3/section info <section> &7- &cTells you info about the section",
"&3/section connect <section> [player] &7- &cConnects you or the specified player to that section",
"&7&m-----------------------------------------------------"
);
}
private static final Consumer<Messager> help = (msgr) -> msgr.send(
"&7&m-----------------------------------------------------",
"&7Available commands:",
"&3/section list &7- &cTells you which sections are configured in the plugin",
"&3/section info <section> &7- &cTells you info about the section",
"&3/section connect <section> [player] &7- &cConnects you or the specified player to that section",
"&7&m-----------------------------------------------------"
);
}

View File

@ -44,6 +44,7 @@ public class ConfigEntries implements ConfigEntryHolder {
public static final ConfigEntry<Boolean> REDIS_BUNGEE_ENABLED = new ConfigEntry<>(0, "settings.redis-bungee", false);
public static final ConfigEntry<Boolean> ASSIGN_TARGETS_ENABLED = new ConfigEntry<>(0, "settings.assign-targets", false);
public static final ConfigEntry<Boolean> FALLBACK_PRINCIPAL_ENABLED = new ConfigEntry<>(0, "settings.fallback-principal", true);
public static final ConfigEntry<Boolean> SERVERS_UPDATE = new ConfigEntry<>(0, "settings.servers-update", true);
public static final ConfigEntry<String> CONNECTING_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting", null);
public static final ConfigEntry<String> FAILURE_MESSAGE = new ConfigEntry<>(0, "settings.messages.failure", null);

View File

@ -2,6 +2,8 @@ package me.jaimemartz.lobbybalancer.listener;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.connection.ConnectionIntent;
import me.jaimemartz.lobbybalancer.section.ServerSection;
@ -18,9 +20,14 @@ import java.io.IOException;
public class PluginMessageListener implements Listener {
private final LobbyBalancer plugin;
private final Gson gson;
public PluginMessageListener(LobbyBalancer plugin) {
this.plugin = plugin;
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
builder.excludeFieldsWithoutExposeAnnotation();
gson = builder.create();
}
@EventHandler
@ -69,7 +76,7 @@ public class PluginMessageListener implements Listener {
}
try {
String output = plugin.getGson().toJson(section);
String output = gson.toJson(section);
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();
@ -94,7 +101,7 @@ public class PluginMessageListener implements Listener {
}
try {
String output = plugin.getGson().toJson(section);
String output = gson.toJson(section);
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();

View File

@ -1,15 +1,20 @@
package me.jaimemartz.lobbybalancer.section;
import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.config.Configuration;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SectionManager {
private ServerSection principal;
private ScheduledTask updateTask;
private final LobbyBalancer plugin;
private final Map<String, ServerSection> sectionStorage = new ConcurrentHashMap<>();
private final Map<ServerInfo, ServerSection> sectionServers = new ConcurrentHashMap<>();
@ -26,25 +31,44 @@ public class SectionManager {
sections.getKeys().forEach(name -> {
plugin.getLogger().info(String.format("Construction of section with name \"%s\"", name));
Configuration section = sections.getSection(name);
ServerSection object = new ServerSection(name, section);
ServerSection object = new ServerSection(plugin, name, section);
sectionStorage.put(name, object);
});
sectionStorage.forEach((name, section) -> {
plugin.getLogger().info(String.format("Pre-Initialization of section with name \"%s\"", name));
section.preInit(plugin);
section.preInit();
});
sectionStorage.forEach((name, section) -> {
plugin.getLogger().info(String.format("Initialization of section with name \"%s\"", name));
section.load(plugin);
section.load();
});
sectionStorage.forEach((name, section) -> {
plugin.getLogger().info(String.format("Post-Initialization of section with name \"%s\"", name));
section.postInit(plugin);
section.postInit();
});
if (ConfigEntries.SERVERS_UPDATE.get()) {
updateTask = plugin.getProxy().getScheduler().schedule(plugin, () -> {
sectionStorage.forEach((name, section) -> {
section.getConfiguration().getStringList("servers").forEach(entry -> {
Pattern pattern = Pattern.compile(entry);
plugin.getProxy().getServers().forEach((key, value) -> {
Matcher matcher = pattern.matcher(key);
if (matcher.matches()) {
if (!section.getServers().contains(value)) {
plugin.getLogger().info(String.format("Found a new match with \"%s\" for entry \"%s\"", key, entry));
this.register(value, section);
section.getServers().add(value);
}
}
});
});
});
}, 1, 1, TimeUnit.MINUTES);
}
long ending = System.currentTimeMillis() - starting;
plugin.getLogger().info(String.format("A total of %s section(s) have been loaded in %sms", sectionStorage.size(), ending));
@ -67,6 +91,10 @@ public class SectionManager {
});
principal = null;
if (updateTask != null) {
updateTask.cancel();
updateTask = null;
}
sectionStorage.clear();
sectionServers.clear();
}

View File

@ -1,5 +1,6 @@
package me.jaimemartz.lobbybalancer.section;
import com.google.gson.annotations.Expose;
import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.connection.ProviderType;
import me.jaimemartz.lobbybalancer.utils.FixedAdapter;
@ -8,7 +9,6 @@ import net.md_5.bungee.config.Configuration;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
@ -16,28 +16,30 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ServerSection {
private transient final Configuration section;
private final LobbyBalancer plugin;
private Configuration configuration;
@Expose private final String name;
@Expose private boolean principal;
@Expose private int position;
@Expose private boolean dummy;
@Expose private ServerSection parent;
@Expose private boolean inherited = false;
@Expose private List<ServerInfo> servers;
@Expose private ProviderType provider;
@Expose private ServerInfo server;
@Expose private SectionCommand command;
@Expose private boolean valid = false;
private final String name;
private boolean principal;
private int position;
private boolean dummy;
private ServerSection parent;
private boolean inherited = false;
private List<ServerInfo> servers;
private ProviderType provider;
private ServerInfo server;
private SectionCommand command;
private boolean valid = false;
public ServerSection(String name, Configuration section) {
public ServerSection(LobbyBalancer plugin, String name, Configuration configuration) {
this.plugin = plugin;
this.name = name;
this.section = section;
this.configuration = configuration;
this.servers = new ArrayList<>();
}
public ServerSection(String name, boolean principal, int position, boolean dummy, ServerSection parent, boolean inherited, List<ServerInfo> servers, ProviderType provider, ServerInfo server, SectionCommand command, boolean valid) {
this.section = null;
public ServerSection(LobbyBalancer plugin, String name, boolean principal, int position, boolean dummy, ServerSection parent, boolean inherited, List<ServerInfo> servers, ProviderType provider, ServerInfo server, SectionCommand command, boolean valid) {
this.plugin = plugin;
this.configuration = null;
this.name = name;
this.principal = principal;
this.position = position;
@ -51,12 +53,12 @@ public class ServerSection {
this.valid = valid;
}
public void preInit(LobbyBalancer plugin) {
if (section == null) {
public void preInit() {
if (configuration == null) {
throw new IllegalStateException("Tried to call an init method with null configuration section");
}
principal = section.getBoolean("principal", false);
principal = configuration.getBoolean("principal", false);
if (principal) {
ServerSection section = plugin.getSectionManager().getPrincipal();
@ -67,18 +69,18 @@ public class ServerSection {
}
}
dummy = section.getBoolean("dummy", false);
dummy = configuration.getBoolean("dummy", false);
if (section.contains("parent")) {
parent = plugin.getSectionManager().getByName(section.getString("parent"));
if (configuration.contains("parent")) {
parent = plugin.getSectionManager().getByName(configuration.getString("parent"));
if (parent == null) {
throw new IllegalArgumentException(String.format("The section \"%s\" has an invalid parent set", name));
}
}
if (section.contains("servers")) {
section.getStringList("servers").forEach(entry -> {
if (configuration.contains("servers")) {
configuration.getStringList("servers").forEach(entry -> {
Pattern pattern = Pattern.compile(entry);
AtomicBoolean matches = new AtomicBoolean(false);
plugin.getProxy().getServers().forEach((key, value) -> {
@ -96,15 +98,15 @@ public class ServerSection {
}
});
plugin.getLogger().info(String.format("Recognized %s server(s) out of %s entries on the section \"%s\"", servers.size(), section.getStringList("servers").size(), this.name));
plugin.getLogger().info(String.format("Recognized %s server(s) out of %s entries on the section \"%s\"", servers.size(), configuration.getStringList("servers").size(), this.name));
} else {
throw new IllegalArgumentException(String.format("The section \"%s\" does not have any servers set", name));
}
}
public void load(LobbyBalancer plugin) {
if (section == null) {
public void load() {
if (configuration == null) {
throw new IllegalStateException("Tried to call an init method with null configuration section");
}
@ -112,9 +114,9 @@ public class ServerSection {
throw new IllegalStateException(String.format("The sections \"%s\" and \"%s\" are parents of each other", this.name, parent.name));
}
if (section.contains("provider")) {
if (configuration.contains("provider")) {
try {
provider = ProviderType.valueOf(section.getString("provider").toUpperCase());
provider = ProviderType.valueOf(configuration.getString("provider").toUpperCase());
if (provider == ProviderType.LOCALIZED) {
Configuration rules = plugin.getConfig().getSection("settings.geolocation.rules");
if (!rules.contains(name)) {
@ -131,15 +133,14 @@ public class ServerSection {
}
}
public void postInit(LobbyBalancer plugin) {
if (section == null) {
public void postInit() {
if (configuration == null) {
throw new IllegalStateException("Tried to call an init method with null configuration section");
}
Callable<Integer> callable = () -> {
int iterations = 0;
//Calculate above principal
int iterations = 0;
ServerSection current = this;
while (current != null) {
if (current.isPrincipal()) {
@ -191,16 +192,16 @@ public class ServerSection {
throw new IllegalStateException(String.format("The section \"%s\" does not have a provider", name));
}
if (section.contains("section-server")) {
if (configuration.contains("section-server")) {
int port = (int) Math.floor(Math.random() * (0xFFFF + 1)); //Get a random valid port for our fake server
server = plugin.getProxy().constructServerInfo("@" + section.getString("section-server"), new InetSocketAddress("0.0.0.0", port), String.format("Server of Section %s", name), false);
server = plugin.getProxy().constructServerInfo("@" + configuration.getString("section-server"), new InetSocketAddress("0.0.0.0", port), String.format("Server of Section %s", name), false);
plugin.getSectionManager().register(server, this);
FixedAdapter.getFakeServers().put(server.getName(), server);
plugin.getProxy().getServers().put(server.getName(), server);
}
if (section.contains("section-command")) {
Configuration other = section.getSection("section-command");
if (configuration.contains("section-command")) {
Configuration other = configuration.getSection("section-command");
String name = other.getString("name");
String permission = other.getString("permission");
@ -217,8 +218,8 @@ public class ServerSection {
return name;
}
protected Configuration getSection() {
return section;
public Configuration getConfiguration() {
return configuration;
}
public boolean isPrincipal() {
@ -276,4 +277,40 @@ public class ServerSection {
public void setValid(boolean valid) {
this.valid = valid;
}
public void setPrincipal(boolean principal) {
this.principal = principal;
}
public void setPosition(int position) {
this.position = position;
}
public void setDummy(boolean dummy) {
this.dummy = dummy;
}
public void setParent(ServerSection parent) {
this.parent = parent;
}
public void setInherited(boolean inherited) {
this.inherited = inherited;
}
public void setServers(List<ServerInfo> servers) {
this.servers = servers;
}
public void setProvider(ProviderType provider) {
this.provider = provider;
}
public void setServer(ServerInfo server) {
this.server = server;
}
public void setCommand(SectionCommand command) {
this.command = command;
}
}

View File

@ -128,6 +128,9 @@ settings:
# This affects both the fallback command and reconnect kick features
fallback-principal: true
# This will update the recognized servers every minute
servers-update: true
# Comment a message to disable it
messages:
connecting: '&aConnecting to {server}'