Since servers is mutable, lock on another Object: serversLock (#42)

* Since servers is mutable, lock on another Object: serversLock

* Make serversLock final
This commit is contained in:
Black Hole 2016-06-30 02:47:00 +02:00 committed by fuzzybot
parent d024f19ba5
commit 1e2071e6a8

View File

@ -1,6 +1,6 @@
From f003385fe169b5be68b0acdf41a82a55f4140fb8 Mon Sep 17 00:00:00 2001 From d7d5585dd6576c5ee2d50c88f5b020ab96726138 Mon Sep 17 00:00:00 2001
From: Troy Frew <fuzzy_bot@arenaga.me> From: Troy Frew <fuzzy_bot@arenaga.me>
Date: Tue, 28 Jun 2016 21:29:25 -0500 Date: Wed, 29 Jun 2016 04:29:25 +0200
Subject: [PATCH] Add dynamic server addition/removal api. 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. 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.
@ -168,7 +168,7 @@ index fc5ca3a..92ea9b5 100644
@Override @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 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
index db9ebbd..112420a 100644 index db9ebbd..4d16fa3 100644
--- a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java --- a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java
+++ b/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 @@ @@ -1,6 +1,7 @@
@ -187,15 +187,23 @@ index db9ebbd..112420a 100644
import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.Favicon; import net.md_5.bungee.api.Favicon;
@@ -61,6 +63,7 @@ public abstract class Configuration implements ProxyConfig @@ -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.
*/
@@ -61,6 +64,7 @@ public abstract class Configuration implements ProxyConfig
private Favicon favicon; private Favicon favicon;
private int compressionThreshold = 256; private int compressionThreshold = 256;
+ @Synchronized("servers") // Waterfall + @Synchronized("serversLock") // Waterfall
public void load() public void load()
{ {
ConfigurationAdapter adapter = ProxyServer.getInstance().getConfigurationAdapter(); ConfigurationAdapter adapter = ProxyServer.getInstance().getConfigurationAdapter();
@@ -100,7 +103,7 @@ public abstract class Configuration implements ProxyConfig @@ -100,7 +104,7 @@ public abstract class Configuration implements ProxyConfig
servers = new CaseInsensitiveMap<>( newServers ); servers = new CaseInsensitiveMap<>( newServers );
} else } else
{ {
@ -204,34 +212,34 @@ index db9ebbd..112420a 100644
this.servers = new CaseInsensitiveMap<>(newServers); this.servers = new CaseInsensitiveMap<>(newServers);
for ( ServerInfo oldServer : oldServers.values() ) for ( ServerInfo oldServer : oldServers.values() )
@@ -158,4 +161,71 @@ public abstract class Configuration implements ProxyConfig @@ -158,4 +162,71 @@ public abstract class Configuration implements ProxyConfig
{ {
return favicon; return favicon;
} }
+ +
+ // Waterfall start + // Waterfall start
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public Map<String, ServerInfo> getServersCopy() { + public Map<String, ServerInfo> getServersCopy() {
+ return ImmutableMap.copyOf( servers ); + return ImmutableMap.copyOf( servers );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public ServerInfo getServerInfo(String name) + public ServerInfo getServerInfo(String name)
+ { + {
+ return this.servers.get( name ); + return this.servers.get( name );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public ServerInfo addServer(ServerInfo server) + public ServerInfo addServer(ServerInfo server)
+ { + {
+ return this.servers.put( server.getName(), server ); + return this.servers.put( server.getName(), server );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public boolean addServers(Collection<ServerInfo> servers) + public boolean addServers(Collection<ServerInfo> servers)
+ { + {
+ boolean changed = false; + boolean changed = false;
@ -243,28 +251,28 @@ index db9ebbd..112420a 100644
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public ServerInfo removeServerNamed(String name) + public ServerInfo removeServerNamed(String name)
+ { + {
+ return this.servers.remove( name ); + return this.servers.remove( name );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public ServerInfo removeServer(ServerInfo server) + public ServerInfo removeServer(ServerInfo server)
+ { + {
+ return this.servers.remove( server.getName() ); + return this.servers.remove( server.getName() );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public boolean removeServersNamed(Collection<String> names) + public boolean removeServersNamed(Collection<String> names)
+ { + {
+ return this.servers.keySet().removeAll( names ); + return this.servers.keySet().removeAll( names );
+ } + }
+ +
+ @Override + @Override
+ @Synchronized("servers") + @Synchronized("serversLock")
+ public boolean removeServers(Collection<ServerInfo> servers) + public boolean removeServers(Collection<ServerInfo> servers)
+ { + {
+ boolean changed = false; + boolean changed = false;
@ -277,5 +285,5 @@ index db9ebbd..112420a 100644
+ // Waterfall end + // Waterfall end
} }
-- --
2.7.4 (Apple Git-66) 2.9.0.windows.1