Remove uneccesary lock, fix double server header, organize packages and class-names

This commit is contained in:
Lukas Rieger (Blue) 2023-02-24 13:55:21 +01:00
parent 0123041925
commit 892506f2f9
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
6 changed files with 68 additions and 81 deletions

View File

@ -38,6 +38,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerEventListener;
import de.bluecolored.bluemap.common.serverinterface.ServerInterface;
import de.bluecolored.bluemap.common.web.*;
import de.bluecolored.bluemap.common.web.http.HttpServer;
import de.bluecolored.bluemap.core.debug.StateDumper;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.map.BmMap;
@ -81,7 +82,7 @@ public class Plugin implements ServerEventListener {
private Map<String, BmMap> maps;
private RenderManager renderManager;
private WebServer webServer;
private HttpServer webServer;
private BlueMapAPIImpl api;
@ -192,7 +193,7 @@ private void load(@Nullable ResourcePack preloadedResourcePack) throws IOExcepti
}
try {
webServer = new WebServer(routingRequestHandler);
webServer = new HttpServer(routingRequestHandler);
webServer.bind(new InetSocketAddress(
webserverConfig.resolveIp(),
webserverConfig.getPort()
@ -581,7 +582,7 @@ public RenderManager getRenderManager() {
return renderManager;
}
public WebServer getWebServer() {
public HttpServer getWebServer() {
return webServer;
}

View File

@ -28,7 +28,6 @@
import de.bluecolored.bluemap.common.web.http.HttpRequestHandler;
import de.bluecolored.bluemap.common.web.http.HttpResponse;
import de.bluecolored.bluemap.common.web.http.HttpStatusCode;
import de.bluecolored.bluemap.core.BlueMap;
import java.util.function.Supplier;
@ -43,7 +42,6 @@ public JsonDataRequestHandler(Supplier<String> dataSupplier) {
@Override
public HttpResponse handle(HttpRequest request) {
HttpResponse response = new HttpResponse(HttpStatusCode.OK);
response.addHeader("Server", "BlueMap v" + BlueMap.VERSION);
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Content-Type", "application/json");
response.setData(dataSupplier.get());

View File

@ -1,23 +0,0 @@
package de.bluecolored.bluemap.common.web;
import de.bluecolored.bluemap.common.web.http.HttpConnection;
import de.bluecolored.bluemap.common.web.http.HttpRequestHandler;
import de.bluecolored.bluemap.common.web.http.SelectionConsumer;
import de.bluecolored.bluemap.common.web.http.Server;
import java.io.IOException;
public class WebServer extends Server {
private final HttpRequestHandler requestHandler;
public WebServer(HttpRequestHandler requestHandler) throws IOException {
this.requestHandler = requestHandler;
}
@Override
public SelectionConsumer createConnectionHandler() {
return new HttpConnection(requestHandler);
}
}

View File

@ -9,11 +9,9 @@
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.locks.ReentrantLock;
public class HttpConnection implements SelectionConsumer {
private final ReentrantLock processingLock = new ReentrantLock();
private final HttpRequestHandler requestHandler;
private HttpRequest request;
private HttpResponse response;
@ -25,62 +23,56 @@ public HttpConnection(HttpRequestHandler requestHandler) {
@Override
public void accept(SelectionKey selectionKey) {
if (!selectionKey.isValid()) return;
if (!processingLock.tryLock()) return;
SelectableChannel selChannel = selectionKey.channel();
if (!(selChannel instanceof SocketChannel)) return;
SocketChannel channel = (SocketChannel) selChannel;
try {
SelectableChannel selChannel = selectionKey.channel();
if (!(selChannel instanceof SocketChannel)) return;
SocketChannel channel = (SocketChannel) selChannel;
if (request == null) {
SocketAddress remote = channel.getRemoteAddress();
InetAddress remoteInet = null;
if (remote instanceof InetSocketAddress)
remoteInet = ((InetSocketAddress) remote).getAddress();
try {
if (request == null) {
SocketAddress remote = channel.getRemoteAddress();
InetAddress remoteInet = null;
if (remote instanceof InetSocketAddress)
remoteInet = ((InetSocketAddress) remote).getAddress();
request = new HttpRequest(remoteInet);
}
// receive request
if (!request.write(channel)) {
if (!selectionKey.isValid()) return;
selectionKey.interestOps(SelectionKey.OP_READ);
return;
}
// process request
if (response == null) {
this.response = requestHandler.handle(request);
}
if (!selectionKey.isValid()) return;
// send response
if (!response.read(channel)){
selectionKey.interestOps(SelectionKey.OP_WRITE);
return;
}
// reset to accept new request
request.clear();
response.close();
response = null;
selectionKey.interestOps(SelectionKey.OP_READ);
} catch (IOException e) {
Logger.global.logDebug("Failed to process selection: " + e);
try {
channel.close();
} catch (IOException e2) {
Logger.global.logWarning("Failed to close channel" + e2);
}
request = new HttpRequest(remoteInet);
}
} finally {
processingLock.unlock();
// receive request
if (!request.write(channel)) {
if (!selectionKey.isValid()) return;
selectionKey.interestOps(SelectionKey.OP_READ);
return;
}
// process request
if (response == null) {
this.response = requestHandler.handle(request);
}
if (!selectionKey.isValid()) return;
// send response
if (!response.read(channel)){
selectionKey.interestOps(SelectionKey.OP_WRITE);
return;
}
// reset to accept new request
request.clear();
response.close();
response = null;
selectionKey.interestOps(SelectionKey.OP_READ);
} catch (IOException e) {
Logger.global.logDebug("Failed to process selection: " + e);
try {
channel.close();
} catch (IOException e2) {
Logger.global.logWarning("Failed to close channel" + e2);
}
}
}

View File

@ -0,0 +1,18 @@
package de.bluecolored.bluemap.common.web.http;
import java.io.IOException;
public class HttpServer extends Server {
private final HttpRequestHandler requestHandler;
public HttpServer(HttpRequestHandler requestHandler) throws IOException {
this.requestHandler = requestHandler;
}
@Override
public SelectionConsumer createConnectionHandler() {
return new HttpConnection(requestHandler);
}
}

View File

@ -40,6 +40,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.common.web.*;
import de.bluecolored.bluemap.common.web.http.HttpRequestHandler;
import de.bluecolored.bluemap.common.web.http.HttpServer;
import de.bluecolored.bluemap.core.MinecraftVersion;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.logger.LoggerLogger;
@ -202,7 +203,7 @@ public void startWebserver(BlueMapService blueMap, boolean verbose) throws IOExc
HttpRequestHandler handler = new BlueMapResponseModifier(routingRequestHandler);
if (verbose) handler = new LoggingRequestHandler(handler);
WebServer webServer = new WebServer(handler);
HttpServer webServer = new HttpServer(handler);
webServer.bind(new InetSocketAddress(
config.resolveIp(),
config.getPort()