BungeeServerInfo management

This commit is contained in:
Rsl1122 2017-09-18 15:47:01 +03:00
parent 06eb66047b
commit fdf2a650df
13 changed files with 236 additions and 46 deletions

View File

@ -41,7 +41,7 @@ import main.java.com.djrapitops.plan.systems.cache.GeolocationCache;
import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager;
import main.java.com.djrapitops.plan.systems.info.ImporterManager;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.info.server.BukkitServerInfoManager;
import main.java.com.djrapitops.plan.systems.listeners.*;
import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter;
@ -80,7 +80,7 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
private WebServer webServer;
private InformationManager infoManager;
private ServerInfoManager serverInfoManager;
private BukkitServerInfoManager serverInfoManager;
private ServerVariableHolder serverVariableHolder;
private TPSCountTimer tpsCountTimer;
@ -112,7 +112,11 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
}
public static UUID getServerUUID() {
return getInstance().getServerInfoManager().getServerUUID();
return getInstance().getServerUuid();
}
public UUID getServerUuid() {
return serverInfoManager.getServerUUID();
}
/**
@ -161,7 +165,7 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
processingQueue = new ProcessingQueue();
serverInfoManager = new ServerInfoManager(this);
serverInfoManager = new BukkitServerInfoManager(this);
infoManager = new BukkitInformationManager(this);
webServer.initServer();
@ -402,10 +406,10 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
/**
* Used to get the object storing server info
*
* @return ServerInfoManager
* @see ServerInfoManager
* @return BukkitServerInfoManager
* @see BukkitServerInfoManager
*/
public ServerInfoManager getServerInfoManager() {
public BukkitServerInfoManager getServerInfoManager() {
return serverInfoManager;
}

View File

@ -14,7 +14,7 @@ import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager;
import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener;
import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
@ -35,7 +35,7 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
private WebServer webServer;
private Database db;
private ServerInfoManager serverInfoManager;
private BungeeServerInfoManager serverInfoManager;
private InformationManager infoManager;
private ServerVariableHolder variableHolder;
@ -73,7 +73,7 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
Benchmark.start("WebServer Initialization");
webServer = new WebServer(this);
serverInfoManager = new ServerInfoManager(this);
serverInfoManager = new BungeeServerInfoManager(this);
infoManager = new BungeeInformationManager(this);
webServer.initServer();
@ -119,8 +119,7 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
return db;
}
@Override
public ServerInfoManager getServerInfoManager() {
public BungeeServerInfoManager getServerInfoManager() {
return serverInfoManager;
}
@ -158,6 +157,10 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
}
public static UUID getServerUUID() {
return getInstance().serverInfoManager.getServerUUID();
return getInstance().getServerUuid();
}
public UUID getServerUuid() {
return serverInfoManager.getServerUUID();
}
}

View File

@ -9,7 +9,6 @@ import com.djrapitops.plugin.config.IConfig;
import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
import main.java.com.djrapitops.plan.systems.webserver.WebServer;
@ -17,6 +16,7 @@ import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* Abstraction interface for both Plan and PlanBungee.
@ -28,7 +28,7 @@ public interface IPlan extends IPlugin {
ServerVariableHolder getVariable();
ServerInfoManager getServerInfoManager();
UUID getServerUuid();
InformationManager getInfoManager();

View File

@ -343,4 +343,28 @@ public class ServerTable extends Table {
close(set, statement);
}
}
public Optional<ServerInfo> getServerInfo(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
.where(columnServerUUID + "=?")
.toString());
statement.setString(1, serverUUID.toString());
set = statement.executeQuery();
if (set.next()) {
return Optional.of(new ServerInfo(
set.getInt(columnServerID),
UUID.fromString(set.getString(columnServerUUID)),
set.getString(columnServerName),
set.getString(columnWebserverAddress),
set.getInt(columnMaxPlayers)));
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}
}

View File

@ -14,6 +14,7 @@ import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalyzeWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.InspectWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.IsOnlineWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestPluginsTabWebAPI;
import main.java.com.djrapitops.plan.utilities.html.HtmlStructure;
@ -77,16 +78,31 @@ public class BungeeInformationManager extends InformationManager {
@Override
public void cachePlayer(UUID uuid) {
// TODO Request Inspect from server where the player is online or any if offline
// PageCache.loadPage("inspectPage: " + uuid, () -> new InspectPageResponse(this, uuid));
getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequestsToBukkitServers(plugin, uuid);
ServerInfo inspectServer = null;
try {
inspectServer = getInspectRequestProcessorServer(uuid);
WebAPIManager apiManager = getWebAPI();
apiManager.getAPI(InspectWebAPI.class).sendRequest(inspectServer.getWebAddress(), uuid);
apiManager.getAPI(RequestPluginsTabWebAPI.class).sendRequestsToBukkitServers(plugin, uuid);
} catch (IllegalStateException e) {
Log.error("Attempted to process Inspect request with 0 Bukkit servers online.");
} catch (WebAPIException e) {
plugin.getServerInfoManager().attemptConnection(inspectServer);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public ServerInfo getInspectRequestProcessorServer(UUID uuid) throws SQLException {
List<ServerInfo> bukkitServers = plugin.getDB().getServerTable().getBukkitServers();
if (bukkitServers.isEmpty()) {
throw new IllegalStateException("No Bukkit Servers.");
}
Collection<ServerInfo> bukkitServers = plugin.getServerInfoManager().getOnlineBukkitServers();
for (ServerInfo server : bukkitServers) {
try {
getWebAPI().getAPI(IsOnlineWebAPI.class).sendRequest(server.getWebAddress());
getWebAPI().getAPI(IsOnlineWebAPI.class).sendRequest(server.getWebAddress(), uuid);
return server;
} catch (WebAPINotFoundException e) {
/*continue*/
@ -94,7 +110,8 @@ public class BungeeInformationManager extends InformationManager {
Log.toLog(this.getClass().getName(), e);
}
}
return bukkitServers.get(new Random().nextInt(bukkitServers.size()));
return bukkitServers.stream().findAny().get();
}
@Override
@ -104,7 +121,15 @@ public class BungeeInformationManager extends InformationManager {
@Override
public boolean attemptConnection() {
return false;
try {
List<ServerInfo> bukkitServers = plugin.getDB().getServerTable().getBukkitServers();
for (ServerInfo server : bukkitServers) {
plugin.getServerInfoManager().attemptConnection(server);
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
return true;
}
@Override

View File

@ -6,10 +6,8 @@ package main.java.com.djrapitops.plan.systems.info.server;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.PlanBungee;
import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.ServerTable;
@ -26,14 +24,14 @@ import java.util.UUID;
*
* @author Rsl1122
*/
public class ServerInfoManager {
public class BukkitServerInfoManager {
private final IPlan plugin;
private final Plan plugin;
private ServerInfo serverInfo;
private ServerInfoFile serverInfoFile;
private final ServerTable serverTable;
public ServerInfoManager(Plan plugin) throws PlanEnableException {
public BukkitServerInfoManager(Plan plugin) throws PlanEnableException {
this.plugin = plugin;
Database db = plugin.getDB();
serverTable = db.getServerTable();
@ -59,11 +57,6 @@ public class ServerInfoManager {
}
}
public ServerInfoManager(PlanBungee plugin) throws PlanEnableException {
this.plugin = plugin;
serverTable = plugin.getDB().getServerTable();
}
private void updateDbInfo(UUID serverUUID) throws SQLException, IOException {
Optional<Integer> serverID = serverTable.getServerID(serverUUID);
if (!serverID.isPresent()) {

View File

@ -0,0 +1,120 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.info.server;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.PlanBungee;
import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.ServerTable;
import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebAPI;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Manages Server information on the Bungee instance.
*
* @author Rsl1122
*/
public class BungeeServerInfoManager {
private final PlanBungee plugin;
private ServerInfo serverInfo;
private final Database db;
private final Map<UUID, ServerInfo> bukkitServers;
private final Set<UUID> onlineServers;
private ServerTable serverTable;
public BungeeServerInfoManager(PlanBungee plugin) throws PlanEnableException {
this.plugin = plugin;
this.db = plugin.getDB();
serverTable = db.getServerTable();
try {
bukkitServers = new HashMap<>();
onlineServers = new HashSet<>();
Optional<ServerInfo> bungeeInfo = db.getServerTable().getBungeeInfo();
if (bungeeInfo.isPresent()) {
serverInfo = bungeeInfo.get();
} else {
serverInfo = registerBungeeInfo();
}
} catch (SQLException e) {
throw new PlanEnableException("Failed to read Database for ServerInfo");
}
}
private ServerInfo registerBungeeInfo() throws SQLException, PlanEnableException {
ServerVariableHolder variable = plugin.getVariable();
UUID serverUUID = generateNewUUID(variable);
String accessAddress = plugin.getWebServer().getAccessAddress();
serverTable.saveCurrentServerInfo(
new ServerInfo(-1, serverUUID, "BungeeCord", accessAddress, variable.getMaxPlayers())
);
Optional<ServerInfo> bungeeInfo = db.getServerTable().getBungeeInfo();
if (bungeeInfo.isPresent()) {
return bungeeInfo.get();
}
throw new PlanEnableException("BungeeCord registration failed (DB)");
}
private UUID generateNewUUID(ServerVariableHolder variableHolder) {
String seed = variableHolder.getName() + variableHolder.getIp() + variableHolder.getPort() + variableHolder.getVersion() + variableHolder.getImplVersion();
return UUID.nameUUIDFromBytes(seed.getBytes());
}
public UUID getServerUUID() {
return serverInfo.getUuid();
}
public void attemptConnection(ServerInfo server) {
if (server == null) {
return;
}
try {
plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(server.getWebAddress());
connectedToServer(server);
} catch (WebAPIException e) {
serverHasGoneOffline(server.getUuid());
}
}
public void connectedToServer(ServerInfo server) {
bukkitServers.put(server.getUuid(), server);
onlineServers.add(server.getUuid());
}
public void serverConnected(UUID serverUUID) {
try {
Optional<ServerInfo> serverInfo = db.getServerTable().getServerInfo(serverUUID);
serverInfo.ifPresent(this::attemptConnection);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public Collection<ServerInfo> getOnlineBukkitServers() {
return bukkitServers.entrySet().stream()
.filter(entry -> onlineServers.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());
}
public Collection<ServerInfo> getBukkitServers() {
return bukkitServers.values();
}
public void serverHasGoneOffline(UUID serverUUID) {
onlineServers.remove(serverUUID);
}
}

View File

@ -92,9 +92,9 @@ public abstract class Importer {
return;
}
Plan plan = Plan.getInstance();
UUID uuid = plan.getServerInfoManager().getServerUUID();
Database db = plan.getDB();
Plan plugin = Plan.getInstance();
UUID uuid = plugin.getServerInfoManager().getServerUUID();
Database db = plugin.getDB();
ExecutorService service = Executors.newCachedThreadPool();
@ -144,13 +144,13 @@ public abstract class Importer {
return;
}
Plan plan = Plan.getInstance();
Plan plugin = Plan.getInstance();
UserImportRefiner userImportRefiner = new UserImportRefiner(plan, userImportData);
UserImportRefiner userImportRefiner = new UserImportRefiner(plugin, userImportData);
userImportData = userImportRefiner.refineData();
UUID serverUUID = plan.getServerInfoManager().getServerUUID();
Database db = plan.getDB();
UUID serverUUID = plugin.getServerInfoManager().getServerUUID();
Database db = plugin.getDB();
Set<UUID> existingUUIDs = db.getSavedUUIDs();
Set<UUID> existingUserInfoTableUUIDs = db.getUserInfoTable().getSavedUUIDs().get(serverUUID);

View File

@ -80,7 +80,7 @@ public class APIResponseHandler {
Log.debug(request.toString());
Log.debug(requestBody);
Response response = api.onRequest(Plan.getInstance(), variables);
Response response = api.processRequest(Plan.getInstance(), variables);
Log.debug(response.toString());

View File

@ -57,7 +57,7 @@ public class ResponseHandler extends APIResponseHandler {
return PageCache.loadPage(target + "js", () -> new JavaScriptResponse(fileName));
}
UUID serverUUID = MiscUtils.getIPlan().getServerInfoManager().getServerUUID();
UUID serverUUID = MiscUtils.getIPlan().getServerUuid();
if (usingHttps) {
if (!request.hasAuth()) {

View File

@ -27,6 +27,7 @@ import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author Rsl1122
@ -39,6 +40,19 @@ public abstract class WebAPI {
this.variables = new HashMap<>();
}
public Response processRequest(IPlan plugin, Map<String, String> variables) {
String sender = variables.get("sender");
if (sender == null) {
return badRequest("Sender not present");
}
try {
UUID.fromString(sender);
} catch (Exception e) {
return badRequest("Faulty Sender value");
}
return onRequest(plugin, variables);
}
public abstract Response onRequest(IPlan plugin, Map<String, String> variables);
public void sendRequest(String address) throws WebAPIException {
@ -78,7 +92,7 @@ public abstract class WebAPI {
connection.setRequestProperty("charset", "ISO-8859-1");
StringBuilder parameters = new StringBuilder();
String serverUUID = MiscUtils.getIPlan().getServerInfoManager().getServerUUID().toString();
String serverUUID = MiscUtils.getIPlan().getServerUuid().toString();
parameters.append("sender=").append(serverUUID).append("&");
for (Map.Entry<String, String> entry : variables.entrySet()) {
parameters.append("&").append(entry.getKey()).append(entry.getValue());
@ -146,6 +160,7 @@ public abstract class WebAPI {
protected Response success() {
return PageCache.loadPage("success", SuccessResponse::new);
}
protected Response fail(String reason) {
return PageCache.loadPage("fail", () -> new NotFoundResponse(reason));
}

View File

@ -4,11 +4,14 @@
*/
package main.java.com.djrapitops.plan.systems.webserver.webapi.universal;
import com.djrapitops.plugin.utilities.Compatibility;
import main.java.com.djrapitops.plan.PlanBungee;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.util.Map;
import java.util.UUID;
/**
* @author Fuzzlemann
@ -16,6 +19,9 @@ import java.util.Map;
public class PingWebAPI extends WebAPI {
@Override
public Response onRequest(IPlan plugin, Map<String, String> variables) {
if (Compatibility.isBungeeAvailable()) {
((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender")));
}
return success();
}
}

View File

@ -15,7 +15,7 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.info.server.BukkitServerInfoManager;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
@ -123,10 +123,10 @@ public class TestInit {
when(planMock.benchmark()).thenReturn(bench);
when(planMock.getVariable()).thenReturn(serverVariableHolder);
when(planMock.fetch()).thenReturn(fetch);
ServerInfoManager serverInfoManager = PowerMockito.mock(ServerInfoManager.class);
BukkitServerInfoManager bukkitServerInfoManager = PowerMockito.mock(BukkitServerInfoManager.class);
when(serverInfoManager.getServerUUID()).thenReturn(serverUUID);
when(planMock.getServerInfoManager()).thenReturn(serverInfoManager);
when(bukkitServerInfoManager.getServerUUID()).thenReturn(serverUUID);
when(planMock.getServerInfoManager()).thenReturn(bukkitServerInfoManager);
RunnableFactory<Plan> runnableFactory = mockRunnableFactory();
when(planMock.getRunnableFactory()).thenReturn(runnableFactory);
ColorScheme cs = new ColorScheme(ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK);