From 10c1d1bed9df2dd6a5c219caabc9e48a5da0057b Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 27 May 2022 15:08:20 +0200 Subject: [PATCH] add debug and error event listening options --- .../com/exaroton/api/ws/DebugListener.java | 5 +++ .../com/exaroton/api/ws/ErrorListener.java | 5 +++ .../com/exaroton/api/ws/WebSocketClient.java | 3 ++ .../com/exaroton/api/ws/WebSocketManager.java | 39 +++++++++++++++++++ .../exaroton/api/ws/stream/ConsoleStream.java | 1 - .../exaroton/api/ws/stream/HeapStream.java | 1 - .../api/ws/stream/ServerStatusStream.java | 1 - .../exaroton/api/ws/stream/StatsStream.java | 1 - .../exaroton/api/ws/stream/TickStream.java | 1 - 9 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/exaroton/api/ws/DebugListener.java create mode 100644 src/main/java/com/exaroton/api/ws/ErrorListener.java diff --git a/src/main/java/com/exaroton/api/ws/DebugListener.java b/src/main/java/com/exaroton/api/ws/DebugListener.java new file mode 100644 index 0000000..cb1c2d3 --- /dev/null +++ b/src/main/java/com/exaroton/api/ws/DebugListener.java @@ -0,0 +1,5 @@ +package com.exaroton.api.ws; + +public interface DebugListener { + public void onDebug(String message); +} diff --git a/src/main/java/com/exaroton/api/ws/ErrorListener.java b/src/main/java/com/exaroton/api/ws/ErrorListener.java new file mode 100644 index 0000000..6d7debd --- /dev/null +++ b/src/main/java/com/exaroton/api/ws/ErrorListener.java @@ -0,0 +1,5 @@ +package com.exaroton.api.ws; + +public interface ErrorListener { + public void onError(String error, Throwable t); +} \ No newline at end of file diff --git a/src/main/java/com/exaroton/api/ws/WebSocketClient.java b/src/main/java/com/exaroton/api/ws/WebSocketClient.java index 922802c..4745044 100644 --- a/src/main/java/com/exaroton/api/ws/WebSocketClient.java +++ b/src/main/java/com/exaroton/api/ws/WebSocketClient.java @@ -30,6 +30,7 @@ public WebSocketClient(URI uri, WebSocketManager manager) { @Override public void onOpen(ServerHandshake handshakedata) { + manager.sendDebug("Websocket opened with status " + handshakedata.getHttpStatus() + ": " + handshakedata.getHttpStatusMessage()); manager.handleOpen(); } @@ -51,10 +52,12 @@ public void onMessage(String message) { @Override public void onError(Exception ex) { logger.error("A websocket error ocurred", ex); + manager.onError("A websocket error ocurred: " + ex.getMessage(), ex); } @Override public void onClose(int code, String reason, boolean remote) { + manager.sendDebug("Websocket closed with code " + code + ": " + reason); manager.handleClose(code, reason, remote); } } diff --git a/src/main/java/com/exaroton/api/ws/WebSocketManager.java b/src/main/java/com/exaroton/api/ws/WebSocketManager.java index d16abc7..dc2a367 100644 --- a/src/main/java/com/exaroton/api/ws/WebSocketManager.java +++ b/src/main/java/com/exaroton/api/ws/WebSocketManager.java @@ -38,6 +38,10 @@ public class WebSocketManager { */ private final Server server; + private ErrorListener errorListener = null; + + private DebugListener debugListener = null; + public WebSocketManager(String uri, String apiToken, Server server) { try { URI u = new URI(uri); @@ -302,4 +306,39 @@ public boolean serverHasStatus(int... status) { } return this.server.hasStatus(status); } + + /** + * Listen to websocket errors + * @param errorListener the only error listener + */ + public void setErrorListener(ErrorListener errorListener) { + this.errorListener = errorListener; + } + + /** + * listen to websocket debug information + * @param debugListener the only debug listener + */ + public void setDebugListener(DebugListener debugListener) { + this.debugListener = debugListener; + } + + + /** + * send debug information to listeners + */ + void sendDebug(String message) { + if (this.debugListener != null) { + this.debugListener.onDebug(message); + } + } + + /** + * send error to listeners + */ + void onError(String error, Throwable throwable) { + this.errorListener.onError(error, throwable); + } + + } diff --git a/src/main/java/com/exaroton/api/ws/stream/ConsoleStream.java b/src/main/java/com/exaroton/api/ws/stream/ConsoleStream.java index 97be29f..baa0eff 100644 --- a/src/main/java/com/exaroton/api/ws/stream/ConsoleStream.java +++ b/src/main/java/com/exaroton/api/ws/stream/ConsoleStream.java @@ -1,7 +1,6 @@ package com.exaroton.api.ws.stream; import com.exaroton.api.ws.WebSocketManager; -import com.exaroton.api.ws.subscriber.ConsoleSubscriber; public class ConsoleStream extends Stream { diff --git a/src/main/java/com/exaroton/api/ws/stream/HeapStream.java b/src/main/java/com/exaroton/api/ws/stream/HeapStream.java index 5af8753..089b003 100644 --- a/src/main/java/com/exaroton/api/ws/stream/HeapStream.java +++ b/src/main/java/com/exaroton/api/ws/stream/HeapStream.java @@ -1,7 +1,6 @@ package com.exaroton.api.ws.stream; import com.exaroton.api.ws.WebSocketManager; -import com.exaroton.api.ws.subscriber.HeapSubscriber; public class HeapStream extends Stream { diff --git a/src/main/java/com/exaroton/api/ws/stream/ServerStatusStream.java b/src/main/java/com/exaroton/api/ws/stream/ServerStatusStream.java index 9306d20..86e3cbf 100644 --- a/src/main/java/com/exaroton/api/ws/stream/ServerStatusStream.java +++ b/src/main/java/com/exaroton/api/ws/stream/ServerStatusStream.java @@ -2,7 +2,6 @@ import com.exaroton.api.ws.WebSocketManager; -import com.exaroton.api.ws.subscriber.ServerStatusSubscriber; public class ServerStatusStream extends Stream { diff --git a/src/main/java/com/exaroton/api/ws/stream/StatsStream.java b/src/main/java/com/exaroton/api/ws/stream/StatsStream.java index 547c3e3..09c8054 100644 --- a/src/main/java/com/exaroton/api/ws/stream/StatsStream.java +++ b/src/main/java/com/exaroton/api/ws/stream/StatsStream.java @@ -1,7 +1,6 @@ package com.exaroton.api.ws.stream; import com.exaroton.api.ws.WebSocketManager; -import com.exaroton.api.ws.subscriber.StatsSubscriber; public class StatsStream extends Stream { diff --git a/src/main/java/com/exaroton/api/ws/stream/TickStream.java b/src/main/java/com/exaroton/api/ws/stream/TickStream.java index 2edf072..34c35d2 100644 --- a/src/main/java/com/exaroton/api/ws/stream/TickStream.java +++ b/src/main/java/com/exaroton/api/ws/stream/TickStream.java @@ -1,7 +1,6 @@ package com.exaroton.api.ws.stream; import com.exaroton.api.ws.WebSocketManager; -import com.exaroton.api.ws.subscriber.TickSubscriber; public class TickStream extends Stream {