diff --git a/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/nodes/ZscriptBasicNode.java b/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/nodes/ZscriptBasicNode.java index ec3c76688..0fceb3f73 100644 --- a/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/nodes/ZscriptBasicNode.java +++ b/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/nodes/ZscriptBasicNode.java @@ -61,7 +61,7 @@ class ZscriptBasicNode implements ZscriptNode { try { if (r.hasAddress()) { if (!addressingSystem.response(r)) { - callbackPool.sendCallback(unknownResponseHandler, r); + callbackPool.sendCallback(unknownResponseHandler, r, callbackExceptionHandler); } } else { response(r); @@ -110,12 +110,11 @@ public void send(AddressedCommand addr) { public void checkTimeouts() { Collection timedOut = connectionBuffer.checkTimeouts(); if (!timedOut.isEmpty()) { - long nanoTime = System.nanoTime(); for (CommandSequence seq : timedOut) { if (fullSequenceCallbacks.get(seq) != null) { - callbackPool.sendCallback(fullSequenceCallbacks.get(seq), ResponseSequence.blank()); + callbackPool.sendCallback(fullSequenceCallbacks.get(seq), ResponseSequence.blank(), callbackExceptionHandler); } else if (pathCallbacks.get(seq.getExecutionPath()) != null) { - callbackPool.sendCallback(pathCallbacks.get(seq.getExecutionPath()), ResponseExecutionPath.blank()); + callbackPool.sendCallback(pathCallbacks.get(seq.getExecutionPath()), ResponseExecutionPath.blank(), callbackExceptionHandler); } } } @@ -125,9 +124,9 @@ private void response(AddressedResponse resp) { if (resp.getContent().getResponseValue() != 0) { Consumer handler = notificationHandlers.get(resp.getContent().getResponseValue()); if (handler != null) { - callbackPool.sendCallback(handler, resp.getContent()); + callbackPool.sendCallback(handler, resp.getContent(), callbackExceptionHandler); } else { - callbackPool.sendCallback(unknownResponseHandler, resp); + callbackPool.sendCallback(unknownResponseHandler, resp, callbackExceptionHandler); } return; } @@ -137,20 +136,20 @@ private void response(AddressedResponse resp) { if (resp.getContent().hasEchoValue() && echoSystem.unmatchedReceive(resp.getContent().getEchoValue())) { return; } - callbackPool.sendCallback(unknownResponseHandler, resp); + callbackPool.sendCallback(unknownResponseHandler, resp, callbackExceptionHandler); return; } strategy.mayHaveSpace(); parentConnection.responseReceived(found); Consumer seqCallback = fullSequenceCallbacks.remove(found.getContent()); if (seqCallback != null) { - callbackPool.sendCallback(seqCallback, resp.getContent()); + callbackPool.sendCallback(seqCallback, resp.getContent(), callbackExceptionHandler); } else { Consumer pathCallback = pathCallbacks.remove(found.getContent().getExecutionPath()); if (pathCallback != null) { - callbackPool.sendCallback(pathCallback, resp.getContent().getExecutionPath()); + callbackPool.sendCallback(pathCallback, resp.getContent().getExecutionPath(), callbackExceptionHandler); } else { - callbackPool.sendCallback(unknownResponseHandler, resp); + callbackPool.sendCallback(unknownResponseHandler, resp, callbackExceptionHandler); } } } diff --git a/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/threading/ZscriptCallbackThreadpool.java b/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/threading/ZscriptCallbackThreadpool.java index 64fa799d3..4d137fd60 100644 --- a/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/threading/ZscriptCallbackThreadpool.java +++ b/clients/java-client-lib/client-core/src/main/java/net/zscript/javaclient/threading/ZscriptCallbackThreadpool.java @@ -15,11 +15,27 @@ public ZscriptCallbackThreadpool(ExecutorService callbackPool) { this.callbackPool = callbackPool; } - public void sendCallback(Consumer callback, T content) { + public void sendCallback(Consumer callback, T content, Consumer handler) { + callbackPool.submit(() -> { + try { + callback.accept(content); + } catch (Exception e) { + handler.accept(e); + } + }); + } + + public void sendCallback(Consumer callback, T content) { callbackPool.submit(() -> callback.accept(content)); } - public void sendCallback(Runnable callback) { - callbackPool.submit(callback); + public void sendCallback(Runnable callback, Consumer handler) { + callbackPool.submit(() -> { + try { + callback.run(); + } catch (Exception e) { + handler.accept(e); + } + }); } }