Skip to content

Commit

Permalink
Push realtime data from the internal server to the webmap (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
granny authored May 1, 2024
1 parent 024e464 commit 1891263
Show file tree
Hide file tree
Showing 29 changed files with 789 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void onEnable() {
}

getServer().getScheduler().runTaskTimer(this, () ->
this.pl3xmap.getScheduler().tick(), 20, 20);
this.pl3xmap.getScheduler().tick(), 20, 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ How many scroll pixels (as reported by L.DomEvent.getWheelDelta) mean
for security reasons. But you do you, boo boo.""")
public static boolean HTTPD_FOLLOW_SYMLINKS = false;

@Key("settings.performance.live-update-threads")
@Comment("""
The number of process-threads to use for real-time marker updates on the map.
Value of -1 will use 50% of the available cpu-threads. (recommended)""")
public static int LIVE_UPDATE_THREADS = -1;

@Key("settings.performance.render-threads")
@Comment("""
The number of process-threads to use for loading and scanning chunks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ public final class PlayersLayerConfig extends AbstractConfig {

@Key("settings.layer.update-interval")
@Comment("""
How often (in seconds) to update the marker.
Setting to 0 is the same as setting it to 1.""")
How often (in seconds) to update the marker.""")
public static int UPDATE_INTERVAL = 0;
@Key("settings.layer.live-update")
@Comment("""
Whether to push this layer through SSE or not.""")
public static boolean LIVE_UPDATE = true;
@Key("settings.layer.show-controls")
@Comment("""
Whether the players layer control shows up in the layers list or not.""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public final class SpawnLayerConfig extends AbstractConfig {

@Key("settings.layer.update-interval")
@Comment("""
How often (in seconds) to update the marker.
Setting to 0 is the same as setting it to 1.""")
How often (in seconds) to update the marker.""")
public static int UPDATE_INTERVAL = 30;
@Key("settings.layer.live-update")
@Comment("""
Whether to push this layer through SSE or not.""")
public static boolean LIVE_UPDATE = true;
@Key("settings.layer.show-controls")
@Comment("""
Whether the spawn layer control shows up in the layers list or not.""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public final class WorldBorderLayerConfig extends AbstractConfig {

@Key("settings.layer.update-interval")
@Comment("""
How often (in seconds) to update the marker.
Setting to 0 is the same as setting it to 1.""")
How often (in seconds) to update the marker.""")
public static int UPDATE_INTERVAL = 30;
@Key("settings.layer.live-update")
@Comment("""
Whether to push this layer through SSE or not.""")
public static boolean LIVE_UPDATE = true;
@Key("settings.layer.show-controls")
@Comment("""
Whether the vanilla world border layer control shows up in the layers list or not.""")
Expand Down
76 changes: 66 additions & 10 deletions core/src/main/java/net/pl3x/map/core/httpd/HttpdServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,40 @@
*/
package net.pl3x.map.core.httpd;

import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.UndertowLogger;
import io.undertow.UndertowOptions;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.resource.PathResourceManager;
import io.undertow.server.handlers.resource.ResourceHandler;
import io.undertow.server.handlers.resource.ResourceManager;
import io.undertow.util.ETag;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.StatusCodes;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.stream.Collectors;
import net.pl3x.map.core.Pl3xMap;
import net.pl3x.map.core.configuration.Config;
import net.pl3x.map.core.configuration.Lang;
import net.pl3x.map.core.log.LogFilter;
import net.pl3x.map.core.log.Logger;
import net.pl3x.map.core.registry.WorldRegistry;
import net.pl3x.map.core.util.FileUtil;
import net.pl3x.map.core.world.World;

public class HttpdServer {
private HttpString X_ACCEL_BUFFERING = new HttpString("X-Accel-Buffering");
private Undertow server;
private LiveDataHandler liveDataHandler = new LiveDataHandler();

public LiveDataHandler getLiveDataHandler() {
return liveDataHandler;
}

public void startServer() {
if (!Config.HTTPD_ENABLED) {
Expand Down Expand Up @@ -81,16 +95,48 @@ public void startServer() {
this.server = Undertow.builder()
.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
.addHttpListener(Config.HTTPD_PORT, Config.HTTPD_BIND)
.setHandler(exchange -> {
if (exchange.getRelativePath().startsWith("/tiles")) {
exchange.getResponseHeaders().put(Headers.CACHE_CONTROL, "max-age=0, must-revalidate, no-cache");
}
if (exchange.getRelativePath().endsWith(".gz")) {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, "gzip");
}
resourceHandler.handleRequest(exchange);
})
.setHandler(
Handlers.path(exchange -> {
if (exchange.getRelativePath().startsWith("/tiles")) {
exchange.getResponseHeaders().put(Headers.CACHE_CONTROL, "max-age=0, must-revalidate, no-cache");
}
if (exchange.getRelativePath().endsWith(".gz")) {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, "gzip");
}
resourceHandler.handleRequest(exchange);
})
.addPrefixPath("/sse",
Handlers.pathTemplate()
.add("{world}", exchange -> {
String worldName = exchange.getQueryParameters().get("world").peek();
if (worldName == null || worldName.isEmpty()) {
exchange.getResponseHeaders().put(X_ACCEL_BUFFERING, "no");
liveDataHandler.handle(exchange);
return;
}

WorldRegistry worldRegistry = Pl3xMap.api().getWorldRegistry();
World world = worldRegistry.get(worldName);
if (world == null || !world.isEnabled()) {
String listOfValidWorlds = worldRegistry.values().stream()
.filter(World::isEnabled)
.map(World::getName).collect(Collectors.joining(", "));
handleError(exchange, "Could not find world named '%s'. Available worlds: %s"
.formatted(worldName, listOfValidWorlds));
exchange.endExchange();
return;
}

if (exchange.isInIoThread()) {
exchange.dispatch(world.getServerSentEventHandler().get());
} else {
exchange.getResponseHeaders().put(X_ACCEL_BUFFERING, "no");
world.getServerSentEventHandler().handle(exchange);
}
})
)
)
.build();
this.server.start();
LogFilter.HIDE_UNDERTOW_LOGS = false;
Expand All @@ -105,6 +151,12 @@ public void startServer() {
}
}

private void handleError(HttpServerExchange exchange, String errorMessage) {
exchange.setStatusCode(StatusCodes.NOT_FOUND);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseSender().send("{\"error\": \"" + errorMessage + "\"}");
}

public void stopServer() {
if (!Config.HTTPD_ENABLED) {
return;
Expand All @@ -116,6 +168,10 @@ public void stopServer() {
}

LogFilter.HIDE_UNDERTOW_LOGS = true;
this.liveDataHandler.closeConnections();
Pl3xMap.api().getWorldRegistry().forEach(world -> {
world.getServerSentEventHandler().closeConnections();
});
this.server.stop();
LogFilter.HIDE_UNDERTOW_LOGS = false;

Expand Down
152 changes: 152 additions & 0 deletions core/src/main/java/net/pl3x/map/core/httpd/LiveDataHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* MIT License
*
* Copyright (c) 2020-2023 William Blake Galbreath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.pl3x.map.core.httpd;

import io.undertow.Handlers;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.sse.ServerSentEventConnection;
import io.undertow.server.handlers.sse.ServerSentEventHandler;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class LiveDataHandler {
private ServerSentEventHandler serverSentEventHandler;

public LiveDataHandler() {
this.serverSentEventHandler = Handlers.serverSentEvents();
}

/**
*
* @param event The message event
* @param data The message data
* @param success The callback that is called when a message is sucessfully sent.
* @param failure The callback that is called when a message send fails.
*/
public void send(String event, String data, SuccessCallback success, FailureCallback failure) {
if (serverSentEventHandler == null) {
return;
}

Callback callback = new Callback(success, failure);
for (ServerSentEventConnection connection : this.serverSentEventHandler.getConnections()) {
connection.send(data, event, null, callback);
}
}

/**
*
* @param event The message event
* @param data The message data
* @param success The callback that is called when a message is sucessfully sent.
*/
public void send(String event, String data, SuccessCallback success) {
this.send(event, data, success, null);
}

/**
*
* @param event The message event
* @param data The message data
*/
public void send(String event, String data) {
this.send(event, data, null, null);
}

/**
*
* @param data The message data
*/
public void send(String data) {
this.send(null, data);
}

public void closeConnections() {
for (ServerSentEventConnection connection : serverSentEventHandler.getConnections()) {
connection.shutdown();
}
}

public void handle(HttpServerExchange exchange) throws Exception {
this.serverSentEventHandler.handleRequest(exchange);
}

public ServerSentEventHandler get() {
return this.serverSentEventHandler;
}

/**
* Notification that is called when a message is sucessfully sent
*/
@FunctionalInterface
public interface SuccessCallback {
/**
* @param connection The connection
* @param data The message data
* @param event The message event
* @param id The message id
*/
void apply(@NotNull ServerSentEventConnection connection, @Nullable String data, @Nullable String event, @Nullable String id);
}

/**
* Notification that is called when a message send fails.
*/
@FunctionalInterface
public interface FailureCallback {
/**
* @param connection The connection
* @param data The message data
* @param event The message event
* @param id The message id
* @param exception The exception
*/
void apply(@NotNull ServerSentEventConnection connection, @Nullable String data, @Nullable String event, @Nullable String id, @NotNull IOException exception);
}

private class Callback implements ServerSentEventConnection.EventCallback {
private SuccessCallback success;
private FailureCallback failure;

public Callback(SuccessCallback success, FailureCallback failure) {
this.success = success;
this.failure = failure;
}

@Override
public void done(ServerSentEventConnection connection, String data, String event, String id) {
if (success != null) {
success.apply(connection, data, event, id);
}
}

@Override
public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
if (failure != null) {
failure.apply(connection, data, event, id, e);
}
}
}
}
Loading

0 comments on commit 1891263

Please sign in to comment.