2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-09-20 23:48:01 +00:00

Lay linked-servers into the morgue.

This commit is contained in:
Tux 2014-04-03 23:03:27 -04:00
parent e1c5533b9d
commit df5f5ef2c9
2 changed files with 24 additions and 31 deletions

View File

@ -55,7 +55,6 @@ public final class RedisBungee extends Plugin implements Listener {
private static JedisPool pool; private static JedisPool pool;
private static RedisBungeeAPI api; private static RedisBungeeAPI api;
private static PubSubListener psl = null; private static PubSubListener psl = null;
private static List<String> serverIds;
private int globalCount; private int globalCount;
/** /**
@ -71,8 +70,16 @@ public final class RedisBungee extends Plugin implements Listener {
return configuration; return configuration;
} }
static List<String> getServerIds() { final List<String> getServerIds() {
return serverIds; Jedis jedis = pool.getResource();
try {
return ImmutableList.copyOf(jedis.smembers("servers"));
} catch (JedisConnectionException e) {
getLogger().log(Level.SEVERE, "Unable to fetch all server IDs", e);
return Collections.singletonList(configuration.getString("server-id"));
} finally {
pool.returnResource(jedis);
}
} }
static PubSubListener getPubSubListener() { static PubSubListener getPubSubListener() {
@ -98,7 +105,7 @@ public final class RedisBungee extends Plugin implements Listener {
if (pool != null) { if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
try { try {
for (String i : serverIds) { for (String i : rsc.smembers("servers")) {
if (i.equals(configuration.getString("server-id"))) continue; if (i.equals(configuration.getString("server-id"))) continue;
if (rsc.exists("server:" + i + ":playerCount")) if (rsc.exists("server:" + i + ":playerCount"))
try { try {
@ -132,7 +139,7 @@ public final class RedisBungee extends Plugin implements Listener {
if (pool != null) { if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
try { try {
for (String i : serverIds) { for (String i : rsc.smembers("servers")) {
if (i.equals(configuration.getString("server-id"))) continue; if (i.equals(configuration.getString("server-id"))) continue;
Set<String> users = rsc.smembers("server:" + i + ":usersOnline"); Set<String> users = rsc.smembers("server:" + i + ":usersOnline");
if (users != null && !users.isEmpty()) if (users != null && !users.isEmpty())
@ -187,7 +194,7 @@ public final class RedisBungee extends Plugin implements Listener {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
getLogger().info("I found a funny number for when " + name + " was last online!"); getLogger().info("I found a funny number for when " + name + " was last online!");
boolean found = false; boolean found = false;
for (String proxyId : serverIds) { for (String proxyId : getServerIds()) {
if (proxyId.equals(configuration.getString("server-id"))) continue; if (proxyId.equals(configuration.getString("server-id"))) continue;
if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", name)) { if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", name)) {
found = true; found = true;
@ -240,7 +247,7 @@ public final class RedisBungee extends Plugin implements Listener {
} }
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) { final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
checkArgument(serverIds.contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid"); checkArgument(getServerIds().contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid");
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
jedis.publish("redisbungee-" + proxyId, command); jedis.publish("redisbungee-" + proxyId, command);
@ -271,7 +278,7 @@ public final class RedisBungee extends Plugin implements Listener {
for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline")) { for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline")) {
// Are they simply on a different proxy? // Are they simply on a different proxy?
boolean found = false; boolean found = false;
for (String proxyId : serverIds) { for (String proxyId : tmpRsc.smembers("servers")) {
if (proxyId.equals(configuration.getString("server-id"))) continue; if (proxyId.equals(configuration.getString("server-id"))) continue;
if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) { if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
found = true; found = true;
@ -324,7 +331,7 @@ public final class RedisBungee extends Plugin implements Listener {
if (!players.contains(member)) { if (!players.contains(member)) {
// Are they simply on a different proxy? // Are they simply on a different proxy?
boolean found = false; boolean found = false;
for (String proxyId : serverIds) { for (String proxyId : getServerIds()) {
if (proxyId.equals(configuration.getString("server-id"))) continue; if (proxyId.equals(configuration.getString("server-id"))) continue;
if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) { if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
// Just clean up the set. // Just clean up the set.
@ -398,12 +405,6 @@ public final class RedisBungee extends Plugin implements Listener {
throw new RuntimeException("server-id is not specified in the configuration or is empty"); throw new RuntimeException("server-id is not specified in the configuration or is empty");
} }
if (configuration.getStringList("linked-servers").isEmpty()) {
throw new RuntimeException("linked-servers is not specified in the configuration or is empty");
}
serverIds = ImmutableList.copyOf(configuration.getStringList("linked-servers"));
if (redisServer != null) { if (redisServer != null) {
if (!redisServer.equals("")) { if (!redisServer.equals("")) {
JedisPoolConfig config = new JedisPoolConfig(); JedisPoolConfig config = new JedisPoolConfig();
@ -418,15 +419,12 @@ public final class RedisBungee extends Plugin implements Listener {
File crashFile = new File(getDataFolder(), "restarted_from_crash.txt"); File crashFile = new File(getDataFolder(), "restarted_from_crash.txt");
if (crashFile.exists()) if (crashFile.exists())
crashFile.delete(); crashFile.delete();
else if (rsc.exists("server:" + configuration.get("server-id") + ":playerCount")) { else if (rsc.sismember("servers", configuration.getString("server-id"))) {
if (Integer.valueOf(rsc.get("server:" + configuration.get("server-id") + ":playerCount")) > 0 &&
rsc.scard("server:" + configuration.getString("server-id") + ":usersOnline") > 0) {
getLogger().severe("You have launched a possible imposter BungeeCord instance. Another instance is already running."); getLogger().severe("You have launched a possible imposter BungeeCord instance. Another instance is already running.");
getLogger().severe("For data consistency reasons, RedisBungee will now disable itself."); getLogger().severe("For data consistency reasons, RedisBungee will now disable itself.");
getLogger().severe("If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check."); getLogger().severe("If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check.");
throw new RuntimeException("Possible imposter instance!"); throw new RuntimeException("Possible imposter instance!");
} }
}
getLogger().log(Level.INFO, "Successfully connected to Redis."); getLogger().log(Level.INFO, "Successfully connected to Redis.");
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
if (rsc != null) if (rsc != null)
@ -451,10 +449,11 @@ public final class RedisBungee extends Plugin implements Listener {
if (pool != null) { if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
try { try {
for (String server : serverIds) { for (String server : rsc.smembers("servers")) {
if (rsc.sismember("server:" + server + ":usersOnline", event.getConnection().getName())) { if (rsc.sismember("server:" + server + ":usersOnline", event.getConnection().getName())) {
event.setCancelled(true); event.setCancelled(true);
event.setCancelReason("You are already logged on to this server."); event.setCancelReason("You are already logged on to this server.");
break;
} }
} }
} finally { } finally {

View File

@ -20,9 +20,3 @@ canonical-glist: true
# Output all players in the server list. Recommended only for smaller networks. # Output all players in the server list. Recommended only for smaller networks.
player-list-in-ping: false player-list-in-ping: false
# All other RedisBungee server IDs in the network.
linked-servers:
- dastank
- americancoffee
- guavaissweet