2018-02-20 14:20:51 +01:00
From 0a3a3e40c649e188cf4aa5ff1d1fbcc028df9ab5 Mon Sep 17 00:00:00 2001
2016-06-29 05:00:02 +02:00
From: Troy Frew <fuzzy_bot@arenaga.me>
2016-06-30 02:47:00 +02:00
Date: Wed, 29 Jun 2016 04:29:25 +0200
2016-06-29 05:00:02 +02:00
Subject: [PATCH] Add dynamic server addition/removal api.
The provided methods will not move a player if a server is removed or the server ip/port is changed, however the methods return the old ServerInfo object for you to handle that yourself if needed.
Thanks to Overcast for the idea
diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
2017-09-26 18:50:48 +02:00
index 5a49050a..f04e2bf4 100644
2016-06-29 05:00:02 +02:00
--- a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
+++ b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
@@ -32,9 +32,83 @@ public interface ProxyConfig
/**
* Set of all servers.
+ *
+ * @deprecated The returned map may be modified concurrently by the proxy.
+ * The safe alternative is {@link #getServersCopy()}.
*/
+ @Deprecated // Waterfall
Map<String, ServerInfo> getServers();
+ // Waterfall start - Dynamic server addition/removal api
+ /**
+ * Return all servers registered to this proxy, keyed by name. The returned map
+ * is an immutable snapshot of the actual server collection. It cannot be modified,
+ * and it will not change.
+ *
+ * @return all registered remote server destinations
+ */
+ Map<String, ServerInfo> getServersCopy();
+
+ /**
+ * Gets the server info of a server.
+ *
+ * @param name the name of the configured server
+ * @return the server info belonging to the specified server
+ */
+ ServerInfo getServerInfo(String name);
+
+ /**
+ * Register the given server to the proxy.
+ * Any currently registered server with the same name will be replaced.
+ * This change is not saved to config.yml
+ *
+ * @return the previously registered server with the same name, or null if there was no such server.
+ */
+ ServerInfo addServer(ServerInfo server);
+
+ /**
+ * Register all of the given servers to the proxy.
+ * This change is not saved to config.yml
+ *
+ * @return true if any servers were added or replaced.
+ */
+ boolean addServers(Collection<ServerInfo> servers);
+
+ /**
+ * Un-register the server with the given name from the proxy.
+ * This change is not saved to config.yml
+ *
+ * @return the server that was removed, or null if there is no server with the given name.
+ */
+ ServerInfo removeServerNamed(String name);
+
+ /**
+ * Un-register the given server from the proxy.
+ * The server is matched by name only, other fields in the given {@link ServerInfo} are ignored.
+ * This change is not saved to config.yml
+ *
+ * @return the server that was removed, or null if there is no server with a matching name.
+ */
+ ServerInfo removeServer(ServerInfo server);
+
+ /**
+ * Un-register servers with any of the given names from the proxy.
+ * This change is not saved to config.yml
+ *
+ * @return true if any servers were removed.
+ */
+ boolean removeServersNamed(Collection<String> names);
+
+ /**
+ * Un-register all of the given servers from the proxy.
+ * The servers are matched by name only, other fields in the given {@link ServerInfo} are ignored.
+ * This change is not saved to config.yml
+ *
+ * @return true if any servers were removed.
+ */
+ boolean removeServers(Collection<ServerInfo> servers);
+ // Waterfall end
+
/**
* Does the server authenticate with mojang
*/
diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java
2017-09-26 18:50:48 +02:00
index 11c5b685..1c011d08 100644
2016-06-29 05:00:02 +02:00
--- a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java
+++ b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java
@@ -93,9 +93,25 @@ public abstract class ProxyServer
* return a fresh map each time.
*
* @return all registered remote server destinations
+ *
+ * @deprecated The returned map is part of the proxy's internal state,
+ * and may be modified concurrently by the proxy.
+ * The safe alternative is {@link #getServersCopy()}.
*/
+ @Deprecated // Waterfall
public abstract Map<String, ServerInfo> getServers();
+ // Waterfall begin - Cloned servers map
+ /**
+ * Return all servers registered to this proxy, keyed by name. The returned map
+ * is an immutable snapshot of the actual server collection. It cannot be modified,
+ * and it will not change.
+ *
+ * @return all registered remote server destinations
+ */
+ public abstract Map<String, ServerInfo> getServersCopy();
+ // Waterfall end
+
/**
* Gets the server info of a server.
*
diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java
2018-02-20 14:20:51 +01:00
index 262b29c8..d7ed3e11 100644
2016-06-29 05:00:02 +02:00
--- a/config/src/main/java/net/md_5/bungee/config/Configuration.java
+++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java
2016-08-16 01:47:02 +02:00
@@ -44,6 +44,13 @@ public final class Configuration
}
2016-06-29 05:00:02 +02:00
}
+ // Waterfall start - Allow configuration objects to be cloned
+ public Configuration(Configuration values, Configuration defaults)
+ {
+ this( values.self, defaults );
+ }
+ // Waterfall end
+
private Configuration getSectionFor(String path)
{
int index = path.indexOf( SEPARATOR );
2017-10-23 20:53:51 +02:00
diff --git a/module/cmd-server/src/main/java/net/md_5/bungee/module/cmd/server/CommandServer.java b/module/cmd-server/src/main/java/net/md_5/bungee/module/cmd/server/CommandServer.java
2017-10-28 21:51:33 +02:00
index b3ff750f..43d11cf2 100644
2017-10-23 20:53:51 +02:00
--- a/module/cmd-server/src/main/java/net/md_5/bungee/module/cmd/server/CommandServer.java
+++ b/module/cmd-server/src/main/java/net/md_5/bungee/module/cmd/server/CommandServer.java
2017-10-28 21:51:33 +02:00
@@ -83,7 +83,7 @@ public class CommandServer extends Command implements TabExecutor
2017-10-23 20:53:51 +02:00
@Override
public Iterable<String> onTabComplete(final CommandSender sender, final String[] args)
{
- return ( args.length > 1 ) ? Collections.EMPTY_LIST : Iterables.transform( Iterables.filter( ProxyServer.getInstance().getServers().values(), new Predicate<ServerInfo>()
+ return ( args.length > 1 ) ? Collections.EMPTY_LIST : Iterables.transform( Iterables.filter( ProxyServer.getInstance().getServersCopy().values(), new Predicate<ServerInfo>() // Waterfall: use #getServersCopy()
{
private final String lower = ( args.length == 0 ) ? "" : args[0].toLowerCase();
2016-06-29 05:00:02 +02:00
diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
2018-02-20 14:20:51 +01:00
index 40f28a12..f14821b4 100644
2016-06-29 05:00:02 +02:00
--- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
+++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
2018-02-20 14:20:51 +01:00
@@ -588,10 +588,18 @@ public class BungeeCord extends ProxyServer
2016-06-29 05:00:02 +02:00
return config.getServers();
}
+ // Waterfall start
+ @Override
+ public Map<String, ServerInfo> getServersCopy()
+ {
+ return config.getServersCopy();
+ }
+ // Waterfall end
+
@Override
public ServerInfo getServerInfo(String name)
{
- return getServers().get( name );
+ return config.getServerInfo( name ); // Waterfall
}
@Override
diff --git a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java
2017-09-26 18:50:48 +02:00
index d131c02c..a755121a 100644
2016-06-29 05:00:02 +02:00
--- a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java
+++ b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java
@@ -1,6 +1,7 @@
package net.md_5.bungee.conf;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap; // Waterfall
import gnu.trove.map.TMap;
import java.io.File;
import java.io.IOException;
@@ -11,6 +12,7 @@ import java.util.UUID;
import java.util.logging.Level;
import javax.imageio.ImageIO;
import lombok.Getter;
+import lombok.Synchronized; // Waterfall
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.Favicon;
2016-06-30 02:47:00 +02:00
@@ -42,6 +44,7 @@ public abstract class Configuration implements ProxyConfig
* Set of all listeners.
*/
private Collection<ListenerInfo> listeners;
+ private final Object serversLock = new Object(); // Waterfall
/**
* Set of all servers.
*/
2017-04-28 22:22:02 +02:00
@@ -62,6 +65,7 @@ public abstract class Configuration implements ProxyConfig
2016-06-29 05:00:02 +02:00
private int compressionThreshold = 256;
2017-04-28 22:22:02 +02:00
private boolean preventProxyConnections;
2016-06-29 05:00:02 +02:00
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock") // Waterfall
2016-06-29 05:00:02 +02:00
public void load()
{
ConfigurationAdapter adapter = ProxyServer.getInstance().getConfigurationAdapter();
2017-04-28 22:22:02 +02:00
@@ -102,7 +106,7 @@ public abstract class Configuration implements ProxyConfig
2016-06-29 05:00:02 +02:00
servers = new CaseInsensitiveMap<>( newServers );
} else
{
- Map<String, ServerInfo> oldServers = this.servers;
+ Map<String, ServerInfo> oldServers = getServersCopy();
this.servers = new CaseInsensitiveMap<>(newServers);
for ( ServerInfo oldServer : oldServers.values() )
2017-04-28 22:22:02 +02:00
@@ -160,4 +164,71 @@ public abstract class Configuration implements ProxyConfig
2016-06-29 05:00:02 +02:00
{
return favicon;
}
+
+ // Waterfall start
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public Map<String, ServerInfo> getServersCopy() {
+ return ImmutableMap.copyOf( servers );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public ServerInfo getServerInfo(String name)
+ {
+ return this.servers.get( name );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public ServerInfo addServer(ServerInfo server)
+ {
+ return this.servers.put( server.getName(), server );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public boolean addServers(Collection<ServerInfo> servers)
+ {
+ boolean changed = false;
+ for ( ServerInfo server : servers )
+ {
+ if ( server != this.servers.put( server.getName(), server ) ) changed = true;
+ }
+ return changed;
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public ServerInfo removeServerNamed(String name)
+ {
+ return this.servers.remove( name );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public ServerInfo removeServer(ServerInfo server)
+ {
+ return this.servers.remove( server.getName() );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public boolean removeServersNamed(Collection<String> names)
+ {
+ return this.servers.keySet().removeAll( names );
+ }
+
+ @Override
2016-06-30 02:47:00 +02:00
+ @Synchronized("serversLock")
2016-06-29 05:00:02 +02:00
+ public boolean removeServers(Collection<ServerInfo> servers)
+ {
+ boolean changed = false;
+ for ( ServerInfo server : servers )
+ {
+ if ( null != this.servers.remove( server.getName() ) ) changed = true;
+ }
+ return changed;
+ }
+ // Waterfall end
}
--
2018-02-20 14:20:51 +01:00
2.16.1
2016-06-29 05:00:02 +02:00