From f153a11717022298f3df6ea6af4d27c1c976eb55 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Fri, 12 Jun 2020 14:56:51 -0500 Subject: [PATCH] Stuff --- build.gradle | 8 ++- src/main/java/mdnet/base/MangaDexClient.java | 12 +++-- src/main/java/mdnet/base/Statistics.java | 24 +++++++-- .../mdnet/base/settings/ClientSettings.java | 2 +- .../java/mdnet/base/settings/WebSettings.java | 24 ++++++--- .../{base => cache}/CachingInputStream.java | 2 +- src/main/java/mdnet/cache/DiskLruCache.java | 7 ++- src/main/java/mdnet/webui/WebConsole.java | 52 +++++++++---------- .../mdnet/base/{ => web}/Application.kt | 37 +++---------- src/main/kotlin/mdnet/base/web/WebUi.kt | 37 +++++++++++++ src/main/kotlin/mdnet/base/web/common.kt | 43 +++++++++++++++ 11 files changed, 170 insertions(+), 78 deletions(-) rename src/main/java/mdnet/{base => cache}/CachingInputStream.java (99%) rename src/main/kotlin/mdnet/base/{ => web}/Application.kt (90%) create mode 100644 src/main/kotlin/mdnet/base/web/WebUi.kt create mode 100644 src/main/kotlin/mdnet/base/web/common.kt diff --git a/build.gradle b/build.gradle index 5ce2bc6..09482d7 100644 --- a/build.gradle +++ b/build.gradle @@ -22,8 +22,12 @@ dependencies { implementation group: "org.http4k", name: "http4k-core", version: "$http_4k_version" implementation group: "org.http4k", name: "http4k-server-netty", version: "$http_4k_version" implementation group: "org.http4k", name: "http4k-client-apache", version: "$http_4k_version" + implementation group: "org.http4k", name: "http4k-format-gson", version: "3.249.0" + implementation group: "commons-io", name: "commons-io", version: "2.7" - compile "org.java-websocket:Java-WebSocket:1.5.1" + + implementation group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.1' + implementation "ch.qos.logback:logback-classic:$logback_version" runtimeOnly 'io.netty:netty-tcnative-boringssl-static:2.0.30.Final' } @@ -35,12 +39,14 @@ java { spotless { java { + indentWithSpaces(4) eclipse() removeUnusedImports() trimTrailingWhitespace() endWithNewline() } kotlin { + indentWithSpaces(4) ktlint() trimTrailingWhitespace() endWithNewline() diff --git a/src/main/java/mdnet/base/MangaDexClient.java b/src/main/java/mdnet/base/MangaDexClient.java index e818258..3a09e17 100644 --- a/src/main/java/mdnet/base/MangaDexClient.java +++ b/src/main/java/mdnet/base/MangaDexClient.java @@ -3,6 +3,8 @@ package mdnet.base; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import mdnet.base.settings.ClientSettings; +import mdnet.base.web.ApplicationKt; +import mdnet.base.web.WebUiKt; import mdnet.cache.DiskLruCache; import mdnet.webui.WebConsole; import org.http4k.server.Http4kServer; @@ -29,6 +31,7 @@ public class MangaDexClient { // if this is null, then the server has shutdown private Http4kServer engine; + private Http4kServer webUi; private DiskLruCache cache; public MangaDexClient(ClientSettings clientSettings) { @@ -46,7 +49,7 @@ public class MangaDexClient { // This function also does most of the program initialization. public void runLoop() { - statistics.set(new Statistics()); + statistics.set(new Statistics(0)); loginAndStartServer(); if (serverSettings.getLatestBuild() > Constants.CLIENT_BUILD) { if (LOGGER.isWarnEnabled()) { @@ -59,6 +62,9 @@ public class MangaDexClient { LOGGER.info("MDNet initialization completed successfully. Starting normal operation."); } + webUi = WebUiKt.getUiServer(clientSettings.getWebSettings(), statistics); + webUi.start(); + // we don't really care about the Atomic part here AtomicInteger counter = new AtomicInteger(); // ping keep-alive every 45 seconds @@ -71,7 +77,7 @@ public class MangaDexClient { if (LOGGER.isInfoEnabled()) { LOGGER.info("Hourly update: refreshing statistics"); } - statistics.set(new Statistics()); + statistics.set(new Statistics(statistics.get().getSequenceNumber() + 1)); if (engine == null) { if (LOGGER.isInfoEnabled()) { @@ -218,7 +224,7 @@ public class MangaDexClient { // TODO: system.out redirect ClientSettings finalSettings = settings; new Thread(() -> { - WebConsole webConsole = new WebConsole(finalSettings.getWebSettings().getClientWebsocketPort()) { + WebConsole webConsole = new WebConsole(finalSettings.getWebSettings().getUiWebsocketPort()) { @Override protected void parseMessage(String message) { System.out.println(message); diff --git a/src/main/java/mdnet/base/Statistics.java b/src/main/java/mdnet/base/Statistics.java index 29651d3..1da1fee 100644 --- a/src/main/java/mdnet/base/Statistics.java +++ b/src/main/java/mdnet/base/Statistics.java @@ -1,19 +1,28 @@ package mdnet.base; +import com.google.gson.annotations.SerializedName; + import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; public class Statistics { + @SerializedName("requests_served") private final AtomicInteger requestsServed; + @SerializedName("cache_hits") private final AtomicInteger cacheHits; + @SerializedName("cache_misses") private final AtomicInteger cacheMisses; + @SerializedName("bytes_sent") private final AtomicLong bytesSent; + @SerializedName("sequence_number") + private final int sequenceNumber; - public Statistics() { + public Statistics(int sequenceNumber) { requestsServed = new AtomicInteger(); cacheHits = new AtomicInteger(); cacheMisses = new AtomicInteger(); bytesSent = new AtomicLong(); + this.sequenceNumber = sequenceNumber; } public AtomicInteger getRequestsServed() { @@ -32,9 +41,18 @@ public class Statistics { return bytesSent; } + public int getSequenceNumber() { + return sequenceNumber; + } + @Override public String toString() { - return "Statistics{" + "requestsServed=" + requestsServed + ", cacheHits=" + cacheHits + ", cacheMisses=" - + cacheMisses + ", bytesSent=" + bytesSent + '}'; + return "Statistics{" + + "requestsServed=" + requestsServed + + ", cacheHits=" + cacheHits + + ", cacheMisses=" + cacheMisses + + ", bytesSent=" + bytesSent + + ", sequenceNumber=" + sequenceNumber + + '}'; } } diff --git a/src/main/java/mdnet/base/settings/ClientSettings.java b/src/main/java/mdnet/base/settings/ClientSettings.java index 3e424e2..8e854c5 100644 --- a/src/main/java/mdnet/base/settings/ClientSettings.java +++ b/src/main/java/mdnet/base/settings/ClientSettings.java @@ -32,7 +32,7 @@ public final class ClientSettings { } public ClientSettings(long maxCacheSizeMib, long maxBandwidthMibPerHour, long maxBurstRateKibPerSecond, - int clientPort, String clientSecret, int threads, WebSettings webSettings) { + int clientPort, String clientSecret, int threads, WebSettings webSettings) { this.maxCacheSizeMib = maxCacheSizeMib; this.maxBandwidthMibPerHour = maxBandwidthMibPerHour; this.maxBurstRateKibPerSecond = maxBurstRateKibPerSecond; diff --git a/src/main/java/mdnet/base/settings/WebSettings.java b/src/main/java/mdnet/base/settings/WebSettings.java index 7708789..03ea12d 100644 --- a/src/main/java/mdnet/base/settings/WebSettings.java +++ b/src/main/java/mdnet/base/settings/WebSettings.java @@ -3,23 +3,31 @@ package mdnet.base.settings; import com.google.gson.annotations.SerializedName; public final class WebSettings { - @SerializedName("client_websocket_port") - private final int clientWebsocketPort; + @SerializedName("ui_websocket_port") + private final int uiWebsocketPort; + @SerializedName("ui_port") + private final int uiPort; public WebSettings() { - this.clientWebsocketPort = 33333; + this.uiWebsocketPort = 33333; + this.uiPort = 8080; } - public WebSettings(int clientWebsocketPort) { - this.clientWebsocketPort = clientWebsocketPort; + public WebSettings(int uiWebsocketPort, int uiPort) { + this.uiWebsocketPort = uiWebsocketPort; + this.uiPort = uiPort; } - public int getClientWebsocketPort() { - return clientWebsocketPort; + public int getUiWebsocketPort() { + return uiWebsocketPort; + } + + public int getUiPort() { + return uiPort; } @Override public String toString() { - return "WebSettings{" + "clientWebsocketPort=" + clientWebsocketPort + '}'; + return "WebSettings{" + "uiWebsocketPort=" + uiWebsocketPort + ", uiPort=" + uiPort + '}'; } } diff --git a/src/main/java/mdnet/base/CachingInputStream.java b/src/main/java/mdnet/cache/CachingInputStream.java similarity index 99% rename from src/main/java/mdnet/base/CachingInputStream.java rename to src/main/java/mdnet/cache/CachingInputStream.java index c544e2b..9ea5fcd 100644 --- a/src/main/java/mdnet/base/CachingInputStream.java +++ b/src/main/java/mdnet/cache/CachingInputStream.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package mdnet.base; +package mdnet.cache; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.ProxyInputStream; diff --git a/src/main/java/mdnet/cache/DiskLruCache.java b/src/main/java/mdnet/cache/DiskLruCache.java index 91ad816..e8c0556 100644 --- a/src/main/java/mdnet/cache/DiskLruCache.java +++ b/src/main/java/mdnet/cache/DiskLruCache.java @@ -82,10 +82,9 @@ import java.util.regex.Pattern; *
* Clients call {@link #get} to read a snapshot of an entry. The read will
diff --git a/src/main/java/mdnet/webui/WebConsole.java b/src/main/java/mdnet/webui/WebConsole.java
index 9515793..7644b82 100644
--- a/src/main/java/mdnet/webui/WebConsole.java
+++ b/src/main/java/mdnet/webui/WebConsole.java
@@ -2,8 +2,6 @@ package mdnet.webui;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
-import java.util.concurrent.atomic.AtomicReference;
-import mdnet.base.Statistics;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
@@ -78,30 +76,30 @@ public abstract class WebConsole extends WebSocketServer {
// }
public void sendMessage(String type, Object message) {
-// JSONObject out = new JSONObject();
-// switch (type) {
-// case "command" :
-// out.put("type", "command");
-// out.put("data", message.toString());
-// break;
-// case "stats" :
-// out.put("type", "stats");
-// AtomicReference