mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-15 14:54:43 +01:00
Cache regionlist and update config structure for servers
This commit is contained in:
@@ -14,8 +14,8 @@ public class Config {
|
|||||||
|
|
||||||
public KeystoreInfo keystore = new KeystoreInfo();
|
public KeystoreInfo keystore = new KeystoreInfo();
|
||||||
|
|
||||||
public ServerConfig httpServer = new ServerConfig("127.0.0.1", 443);
|
public HttpServerConfig httpServer = new HttpServerConfig(443);
|
||||||
public GameServerConfig gameServer = new GameServerConfig("127.0.0.1", 23301);
|
public GameServerConfig gameServer = new GameServerConfig(23301);
|
||||||
|
|
||||||
public ServerOptions serverOptions = new ServerOptions();
|
public ServerOptions serverOptions = new ServerOptions();
|
||||||
public LogOptions logOptions = new LogOptions();
|
public LogOptions logOptions = new LogOptions();
|
||||||
@@ -45,16 +45,24 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public static class ServerConfig {
|
private static class ServerConfig {
|
||||||
public String bindAddress = "0.0.0.0";
|
public String bindAddress = "0.0.0.0";
|
||||||
public String publicAddress = "127.0.0.1";
|
public String publicAddress = "127.0.0.1";
|
||||||
public int port;
|
public int port;
|
||||||
public boolean useSSL = true;
|
|
||||||
|
|
||||||
public ServerConfig(String address, int port) {
|
public ServerConfig(int port) {
|
||||||
this.publicAddress = address;
|
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public static class HttpServerConfig extends ServerConfig {
|
||||||
|
public boolean useSSL = true;
|
||||||
|
public long regionListRefresh = 60_000; // Time in milliseconds to wait before refreshing region list cache again
|
||||||
|
|
||||||
|
public HttpServerConfig(int port) {
|
||||||
|
super(port);
|
||||||
|
}
|
||||||
|
|
||||||
public String getDisplayAddress() {
|
public String getDisplayAddress() {
|
||||||
return (useSSL ? "https" : "http") + "://" + publicAddress + ":" + port;
|
return (useSSL ? "https" : "http") + "://" + publicAddress + ":" + port;
|
||||||
@@ -67,8 +75,8 @@ public class Config {
|
|||||||
public String name = "Test";
|
public String name = "Test";
|
||||||
public String description = "Test Server";
|
public String description = "Test Server";
|
||||||
|
|
||||||
public GameServerConfig(String address, int port) {
|
public GameServerConfig(int port) {
|
||||||
super(address, port);
|
super(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ public class RegionInfo {
|
|||||||
private String desc;
|
private String desc;
|
||||||
|
|
||||||
private String gateAddress;
|
private String gateAddress;
|
||||||
private String gameAddress;
|
|
||||||
|
|
||||||
@Setter private boolean up;
|
@Setter private boolean up;
|
||||||
|
|
||||||
@@ -29,7 +28,6 @@ public class RegionInfo {
|
|||||||
this.name = server.getServerConfig().getName();
|
this.name = server.getServerConfig().getName();
|
||||||
this.desc = server.getServerConfig().getDescription();
|
this.desc = server.getServerConfig().getDescription();
|
||||||
this.gateAddress = LunarRail.getHttpServer().getServerConfig().getDisplayAddress();
|
this.gateAddress = LunarRail.getHttpServer().getServerConfig().getDisplayAddress();
|
||||||
this.gameAddress = server.getServerConfig().getDisplayAddress();
|
|
||||||
this.up = true;
|
this.up = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package emu.lunarcore.server.http;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.server.HttpConfiguration;
|
import org.eclipse.jetty.server.HttpConfiguration;
|
||||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||||
@@ -11,11 +10,13 @@ import org.eclipse.jetty.server.SecureRequestCustomizer;
|
|||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
|
||||||
import emu.lunarcore.Config.ServerConfig;
|
import emu.lunarcore.Config.HttpServerConfig;
|
||||||
import emu.lunarcore.LunarRail;
|
import emu.lunarcore.LunarRail;
|
||||||
import emu.lunarcore.LunarRail.ServerType;
|
import emu.lunarcore.LunarRail.ServerType;
|
||||||
|
import emu.lunarcore.proto.DispatchRegionDataOuterClass.DispatchRegionData;
|
||||||
import emu.lunarcore.server.game.RegionInfo;
|
import emu.lunarcore.server.game.RegionInfo;
|
||||||
import emu.lunarcore.server.http.handlers.*;
|
import emu.lunarcore.server.http.handlers.*;
|
||||||
|
import emu.lunarcore.util.Utils;
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
import io.javalin.http.ContentType;
|
import io.javalin.http.ContentType;
|
||||||
import io.javalin.http.Context;
|
import io.javalin.http.Context;
|
||||||
@@ -31,6 +32,7 @@ public class HttpServer {
|
|||||||
|
|
||||||
private long nextRegionUpdate;
|
private long nextRegionUpdate;
|
||||||
private Object2ObjectMap<String, RegionInfo> regions;
|
private Object2ObjectMap<String, RegionInfo> regions;
|
||||||
|
private String regionList;
|
||||||
|
|
||||||
public HttpServer(ServerType type) {
|
public HttpServer(ServerType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@@ -49,7 +51,7 @@ public class HttpServer {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerConfig getServerConfig() {
|
public HttpServerConfig getServerConfig() {
|
||||||
return LunarRail.getConfig().getHttpServer();
|
return LunarRail.getConfig().getHttpServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,21 +76,30 @@ public class HttpServer {
|
|||||||
this.nextRegionUpdate = 0;
|
this.nextRegionUpdate = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object2ObjectMap<String, RegionInfo> getRegions() {
|
public String getRegionList() {
|
||||||
synchronized (this.regions) {
|
synchronized (this.regions) {
|
||||||
if (System.currentTimeMillis() > this.nextRegionUpdate) {
|
// Check if region list needs to be cached
|
||||||
|
if (System.currentTimeMillis() > this.nextRegionUpdate || this.regionList == null) {
|
||||||
|
// Clear regions first
|
||||||
this.regions.clear();
|
this.regions.clear();
|
||||||
|
|
||||||
|
// Pull region infos from database
|
||||||
LunarRail.getAccountDatabase().getObjects(RegionInfo.class)
|
LunarRail.getAccountDatabase().getObjects(RegionInfo.class)
|
||||||
.forEach(region -> {
|
.forEach(region -> {
|
||||||
this.regions.put(region.getId(), region);
|
this.regions.put(region.getId(), region);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.nextRegionUpdate = System.currentTimeMillis() + 60_000;
|
// Serialize to proto
|
||||||
}
|
DispatchRegionData regionData = DispatchRegionData.newInstance();
|
||||||
|
regions.values().stream().map(RegionInfo::toProto).forEach(regionData::addRegionList);
|
||||||
|
|
||||||
return regions;
|
// Set region list cache
|
||||||
|
this.regionList = Utils.base64Encode(regionData.toByteArray());
|
||||||
|
this.nextRegionUpdate = System.currentTimeMillis() + getServerConfig().regionListRefresh;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return regionList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|||||||
@@ -3,11 +3,8 @@ package emu.lunarcore.server.http.handlers;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import emu.lunarcore.LunarRail;
|
import emu.lunarcore.LunarRail;
|
||||||
import emu.lunarcore.proto.DispatchRegionDataOuterClass.DispatchRegionData;
|
|
||||||
import emu.lunarcore.proto.RegionEntryOuterClass.RegionEntry;
|
|
||||||
import emu.lunarcore.server.game.RegionInfo;
|
|
||||||
import emu.lunarcore.server.http.HttpServer;
|
import emu.lunarcore.server.http.HttpServer;
|
||||||
import emu.lunarcore.util.Utils;
|
|
||||||
import io.javalin.http.Context;
|
import io.javalin.http.Context;
|
||||||
import io.javalin.http.Handler;
|
import io.javalin.http.Handler;
|
||||||
|
|
||||||
@@ -25,18 +22,8 @@ public class QueryDispatchHandler implements Handler {
|
|||||||
LunarRail.getLogger().info("Client request: query_dispatch");
|
LunarRail.getLogger().info("Client request: query_dispatch");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build region list
|
// Send region list to client
|
||||||
DispatchRegionData regions = DispatchRegionData.newInstance();
|
ctx.result(server.getRegionList());
|
||||||
|
|
||||||
// Get regions
|
|
||||||
var regionMap = server.getRegions();
|
|
||||||
|
|
||||||
synchronized (regionMap) {
|
|
||||||
regionMap.values().stream().map(RegionInfo::toProto).forEach(regions::addRegionList);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode to base64 and send to client
|
|
||||||
ctx.result(Utils.base64Encode(regions.toByteArray()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user