BungeeServerInfoManager javadoc

This commit is contained in:
Rsl1122 2017-09-26 11:01:41 +03:00
parent 3a3fd2cadc
commit b0fdc2130e
2 changed files with 149 additions and 30 deletions

View File

@ -12,6 +12,7 @@ import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPINotFoundException;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.info.parsing.NetworkPageParser;
import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager;
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.response.InspectPageResponse;
@ -43,30 +44,36 @@ public class BungeeInformationManager extends InformationManager {
private final Map<UUID, String> networkPageContent;
private final Map<UUID, Map<UUID, String>> pluginsTabContent;
private final BungeeServerInfoManager serverInfoManager;
public BungeeInformationManager(PlanBungee plugin) throws SQLException {
usingAnotherWebServer = false;
pluginsTabContent = new HashMap<>();
networkPageContent = new HashMap<>();
this.plugin = plugin;
serverInfoManager = plugin.getServerInfoManager();
refreshBukkitServerMap();
}
/**
* Refreshes the Offline Bukkit server Map (UUID - Server Address Map) from DB.
*
* @throws SQLException If DB Error occurs.
*/
private void refreshBukkitServerMap() throws SQLException {
bukkitServers = plugin.getDB().getServerTable().getBukkitServers().stream().collect(Collectors.toMap(ServerInfo::getUuid, Function.identity()));
}
/**
* Sends a "Refresh Analysis" WebAPI call to the appropriate Bukkit server.
* <p>
* if server is not online, api request will not be made.
*
* @param serverUUID Server UUID of the server in question.
*/
@Override
public void refreshAnalysis(UUID serverUUID) {
ServerInfo serverInfo = bukkitServers.get(serverUUID);
if (serverInfo == null) {
try {
refreshBukkitServerMap();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
serverInfo = bukkitServers.get(serverUUID);
}
ServerInfo serverInfo = getOnlineServerInfo(serverUUID);
if (serverInfo == null) {
return;
}
@ -81,6 +88,43 @@ public class BungeeInformationManager extends InformationManager {
}
}
/**
* Attempts to get info of a server that is online.
* <p>
* Returns null if server doesn't exist.
*
* @param serverUUID UUID of server
* @return Online ServerInfo or null
*/
private ServerInfo getOnlineServerInfo(UUID serverUUID) {
ServerInfo serverInfo = bukkitServers.get(serverUUID);
if (serverInfo == null) {
try {
refreshBukkitServerMap();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
serverInfo = bukkitServers.get(serverUUID);
}
if (serverInfo == null) {
return null;
}
if (serverInfoManager.getOnlineBukkitServers().contains(serverInfo) || serverInfoManager.attemptConnection(serverInfo)) {
return serverInfo;
}
return null;
}
/**
* Caches the inspect page for a matching player.
* <p>
* Attempt is made to use the server where the player is online.
* <p>
* If there is no Bukkit server to handle the request it is not fulfilled.
*
* @param uuid UUID of a player.
*/
@Override
public void cachePlayer(UUID uuid) {
ServerInfo inspectServer = null;
@ -91,49 +135,73 @@ public class BungeeInformationManager extends InformationManager {
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 (IllegalStateException ignored) {
/* Ignored */
} catch (WebAPIException e) {
plugin.getServerInfoManager().attemptConnection(inspectServer);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public ServerInfo getInspectRequestProcessorServer(UUID uuid) throws SQLException {
/**
* Get ServerInfo of an online server that should process an inspect request.
* <p>
* If the player is online, an attempt to use the server where the player resides is made.
* <p>
* If the player is offline or in the lobby, any server can be used.
*
* @param uuid UUID of the player
* @return ServerInfo of the server that should handle the request.
* @throws IllegalStateException If no Bukkit servers are online.
*/
private ServerInfo getInspectRequestProcessorServer(UUID uuid) {
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(), uuid);
return server;
} catch (WebAPINotFoundException e) {
/*continue*/
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
Collection<ServerInfo> onlineServers = serverInfoManager.getOnlineBukkitServers();
if (plugin.getProxy().getPlayer(uuid) != null) {
for (ServerInfo server : onlineServers) {
try {
getWebAPI().getAPI(IsOnlineWebAPI.class).sendRequest(server.getWebAddress(), uuid);
return server;
} catch (WebAPINotFoundException ignored) {
/*continue*/
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}
Optional<ServerInfo> bukkitServer = bukkitServers.stream().findAny();
Optional<ServerInfo> bukkitServer = onlineServers.stream().findAny();
if (bukkitServer.isPresent()) {
return bukkitServer.get();
}
throw new IllegalStateException("No Bukkit servers online");
}
/**
* PlanBungee has no DataCache so this method should not be used.
* <p>
* DataCache is meant for storing player data.
*
* @return null
*/
@Override
public DataCache getDataCache() {
return null;
}
/**
* Attempts a connection to every Bukkit server in the database.
*
* @return true (always)
*/
@Override
public boolean attemptConnection() {
try {
List<ServerInfo> bukkitServers = plugin.getDB().getServerTable().getBukkitServers();
for (ServerInfo server : bukkitServers) {
plugin.getServerInfoManager().attemptConnection(server);
serverInfoManager.attemptConnection(server);
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
@ -141,6 +209,14 @@ public class BungeeInformationManager extends InformationManager {
return true;
}
/**
* Check if analysis page for an UUID is cached.
* <p>
* If serverUUID is that of Bungee, network page state is returned.
*
* @param serverUUID UUID of the server
* @return true/false
*/
@Override
public boolean isAnalysisCached(UUID serverUUID) {
if (PlanBungee.getServerUUID().equals(serverUUID)) {
@ -150,6 +226,14 @@ public class BungeeInformationManager extends InformationManager {
}
}
/**
* Returns the Html players inspect page.
* <p>
* If no Bukkit servers are online a 404 is returned instead.
*
* @param uuid UUID of the player
* @return Html string (Full page)
*/
@Override
public String getPlayerHtml(UUID uuid) {
Response response = PageCache.loadPage("inspectPage:" + uuid,
@ -160,6 +244,11 @@ public class BungeeInformationManager extends InformationManager {
return response.getContent();
}
/**
* Get the Network page html.
*
* @return Html string (Full page)
*/
@Override
public String getAnalysisHtml() {
try {
@ -169,6 +258,12 @@ public class BungeeInformationManager extends InformationManager {
}
}
/**
* Used to parse the Plugins tab html String out of all sent to Bungee.
*
* @param uuid UUID of the player
* @return Html string.
*/
@Override
public String getPluginsTabContent(UUID uuid) {
Map<UUID, String> pluginsTab = pluginsTabContent.get(uuid);
@ -183,6 +278,13 @@ public class BungeeInformationManager extends InformationManager {
return builder.toString();
}
/**
* Places plugins tab content for a single player to the pluginsTabContent map.
*
* @param serverUUID UUID of the server
* @param uuid UUID of the player
* @param html Plugins tab html for the player on the server
*/
public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String html) {
Map<UUID, String> perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>());
perServerPluginsTab.put(serverUUID, html);
@ -193,10 +295,20 @@ public class BungeeInformationManager extends InformationManager {
}
}
/**
* Shortcut for getting WebAPIManager
*
* @return WebAPIManager
*/
private WebAPIManager getWebAPI() {
return plugin.getWebServer().getWebAPI();
}
/**
* Get address of Bungee WebServer.
*
* @return URL String
*/
@Override
public String getWebServerAddress() {
return plugin.getWebServer().getAccessAddress();
@ -218,10 +330,15 @@ public class BungeeInformationManager extends InformationManager {
return networkPageContent;
}
/**
* Send notification of analysis being ready to all online bukkit servers via WebAPI.
*
* @param serverUUID UUID of a server which analysis is ready.
*/
@Override
public void analysisReady(UUID serverUUID) {
AnalysisReadyWebAPI api = getWebAPI().getAPI(AnalysisReadyWebAPI.class);
for (ServerInfo serverInfo : bukkitServers.values()) {
for (ServerInfo serverInfo : serverInfoManager.getOnlineBukkitServers()) {
try {
api.sendRequest(serverInfo.getWebAddress(), serverUUID);
} catch (WebAPIException ignored) {

View File

@ -80,18 +80,20 @@ public class BungeeServerInfoManager {
return serverInfo.getUuid();
}
public void attemptConnection(ServerInfo server) {
public boolean attemptConnection(ServerInfo server) {
if (server == null) {
Log.debug("Attempted a connection to a null ServerInfo");
return;
return false;
}
try {
String webAddress = server.getWebAddress();
Log.info("Attempting to connect to Bukkit server.. (" + webAddress + ")");
Log.debug("Attempting to connect to Bukkit server.. (" + webAddress + ")");
plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(webAddress);
connectedToServer(server);
return true;
} catch (WebAPIException e) {
serverHasGoneOffline(server.getUuid());
return false;
}
}
@ -148,7 +150,7 @@ public class BungeeServerInfoManager {
}
public void serverHasGoneOffline(UUID serverUUID) {
Log.info("Bukkit Server Marked Offline");
Log.debug("Bukkit Server Marked Offline");
onlineServers.remove(serverUUID);
}
}