From cede7b196d3591bcf99a174fca6f1288bfb93928 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Wed, 27 Jun 2018 17:47:22 +0200 Subject: [PATCH] #429: Catch error and raise event when message failed to be sent --- .../core/network/CoapEndpoint.java | 18 ++++++++++++++--- .../core/network/stack/AbstractLayer.java | 20 +++++++++++++++---- .../core/network/stack/BaseCoapStack.java | 4 ++-- .../core/network/stack/BlockwiseLayer.java | 16 +++++++-------- .../network/stack/ExchangeCleanupLayer.java | 2 +- .../core/network/stack/ObserveLayer.java | 2 +- .../core/network/stack/ReliabilityLayer.java | 6 +++--- .../core/network/stack/TcpObserveLayer.java | 4 ++-- 8 files changed, 48 insertions(+), 24 deletions(-) diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/CoapEndpoint.java b/californium-core/src/main/java/org/eclipse/californium/core/network/CoapEndpoint.java index 41892d8ef4..9f4802c66e 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/CoapEndpoint.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/CoapEndpoint.java @@ -591,7 +591,11 @@ public void sendRequest(final Request request) { @Override public void runStriped() { - coapstack.sendRequest(exchange, request); + try { + coapstack.sendRequest(exchange, request); + } catch (RuntimeException e) { + request.setSendError(e); + } } }); } @@ -602,7 +606,11 @@ public void sendResponse(final Exchange exchange, final Response response) { @Override public void runStriped() { - coapstack.sendResponse(exchange, response); + try { + coapstack.sendResponse(exchange, response); + } catch (RuntimeException e) { + response.setSendError(e); + } } }); } @@ -613,7 +621,11 @@ public void sendEmptyMessage(final Exchange exchange, final EmptyMessage message @Override public void runStriped() { - coapstack.sendEmptyMessage(exchange, message); + try { + coapstack.sendEmptyMessage(exchange, message); + } catch (RuntimeException e) { + message.setSendError(e); + } } }); } diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/AbstractLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/AbstractLayer.java index 01c0ec56d1..562a25d5ca 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/AbstractLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/AbstractLayer.java @@ -63,17 +63,29 @@ public abstract class AbstractLayer implements Layer { @Override public void sendRequest(final Exchange exchange, final Request request) { - lowerLayer.sendRequest(exchange, request); + try { + lowerLayer.sendRequest(exchange, request); + } catch(RuntimeException e) { + request.setSendError(e); + } } @Override public void sendResponse(final Exchange exchange, final Response response) { - lowerLayer.sendResponse(exchange, response); + try { + lowerLayer.sendResponse(exchange, response); + } catch(RuntimeException e) { + response.setSendError(e); + } } @Override public void sendEmptyMessage(final Exchange exchange, final EmptyMessage message) { - lowerLayer.sendEmptyMessage(exchange, message); + try { + lowerLayer.sendEmptyMessage(exchange, message); + } catch(RuntimeException e) { + message.setSendError(e); + } } @Override @@ -151,7 +163,7 @@ public final void reject(final Exchange exchange, final Message message) { if (message.getType() == Type.ACK || message.getType() == Type.RST) { throw new IllegalArgumentException("Can only reject CON/NON messages"); } else { - lower().sendEmptyMessage(exchange, EmptyMessage.newRST(message)); + this.sendEmptyMessage(exchange, EmptyMessage.newRST(message)); } } diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BaseCoapStack.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BaseCoapStack.java index dc29e292bb..03b60df2ee 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BaseCoapStack.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BaseCoapStack.java @@ -139,13 +139,13 @@ private class StackTopAdapter extends AbstractLayer { @Override public void sendRequest(final Exchange exchange, final Request request) { exchange.setRequest(request); - lower().sendRequest(exchange, request); + super.sendRequest(exchange, request); } @Override public void sendResponse(final Exchange exchange, final Response response) { exchange.setResponse(response); - lower().sendResponse(exchange, response); + super.sendResponse(exchange, response); } @Override diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BlockwiseLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BlockwiseLayer.java index 45ab815d9d..10135f15fe 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BlockwiseLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/BlockwiseLayer.java @@ -291,7 +291,7 @@ public void sendRequest(final Exchange exchange, final Request request) { } exchange.setCurrentRequest(requestToSend); - lower().sendRequest(exchange, requestToSend); + super.sendRequest(exchange, requestToSend); } private Request startBlockwiseUpload(final Exchange exchange, final Request request) { @@ -385,7 +385,7 @@ private void handleInboundBlockwiseUpload(final Exchange exchange, final Request error.setPayload(String.format("body too large, can process %d bytes max", maxResourceBodySize)); error.getOptions().setSize1(maxResourceBodySize); exchange.setCurrentResponse(error); - lower().sendResponse(exchange, error); + super.sendResponse(exchange, error); } else { @@ -430,7 +430,7 @@ private void handleInboundBlockwiseUpload(final Exchange exchange, final Request piggybacked.getOptions().setBlock1(block1.getSzx(), true, block1.getNum()); exchange.setCurrentResponse(piggybacked); - lower().sendResponse(exchange, piggybacked); + super.sendResponse(exchange, piggybacked); } else { @@ -472,7 +472,7 @@ private void sendBlock1ErrorResponse(KeyUri key, Block1BlockwiseStatus status, E error.setPayload(message); clearBlock1Status(key, status); exchange.setCurrentResponse(error); - lower().sendResponse(exchange, error); + super.sendResponse(exchange, error); } private void handleInboundRequestForNextBlock(final Exchange exchange, final Request request, @@ -492,7 +492,7 @@ private void handleInboundRequestForNextBlock(final Exchange exchange, final Req } exchange.setCurrentResponse(block); - lower().sendResponse(exchange, block); + super.sendResponse(exchange, block); } } @@ -573,7 +573,7 @@ public void sendResponse(final Exchange exchange, final Response response) { } exchange.setCurrentResponse(responseToSend); - lower().sendResponse(exchange, responseToSend); + super.sendResponse(exchange, responseToSend); } /** @@ -798,7 +798,7 @@ private void sendNextBlock(final Exchange exchange, final Response response, fin exchange.setCurrentRequest(nextBlock); prepareBlock1Cleanup(status, key); - lower().sendRequest(exchange, nextBlock); + super.sendRequest(exchange, nextBlock); } /** @@ -910,7 +910,7 @@ private void handleBlock2Response(final Exchange exchange, final Response respon LOGGER.debug("requesting next Block2 [num={}]: {}", nextNum, block); exchange.setCurrentRequest(block); prepareBlock2Cleanup(status, key); - lower().sendRequest(exchange, block); + super.sendRequest(exchange, block); } else { diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ExchangeCleanupLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ExchangeCleanupLayer.java index 3cf01f8b70..a2f4c48080 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ExchangeCleanupLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ExchangeCleanupLayer.java @@ -40,7 +40,7 @@ public class ExchangeCleanupLayer extends AbstractLayer { public void sendRequest(final Exchange exchange, final Request request) { request.addMessageObserver(new CancelledMessageObserver(exchange)); - lower().sendRequest(exchange, request); + super.sendRequest(exchange, request); } private static class CancelledMessageObserver extends MessageObserverAdapter { diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ObserveLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ObserveLayer.java index dae055d8ae..f549c8a088 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ObserveLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ObserveLayer.java @@ -127,7 +127,7 @@ public void sendResponse(final Exchange exchange, final Response response) { } } // else no observe was requested or the resource does not allow it - lower().sendResponse(exchange, response); + super.sendResponse(exchange, response); } /** diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ReliabilityLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ReliabilityLayer.java index b8449bfd2d..2744d9a936 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ReliabilityLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/ReliabilityLayer.java @@ -104,7 +104,7 @@ public void retransmit() { } }); } - lower().sendRequest(exchange, request); + super.sendRequest(exchange, request); } /** @@ -156,7 +156,7 @@ public void retransmit() { } }); } - lower().sendResponse(exchange, response); + super.sendResponse(exchange, response); } /** @@ -234,7 +234,7 @@ public void receiveRequest(final Exchange exchange, final Request request) { } LOGGER.debug("{} respond with the current response to the duplicate request", exchange); // Do not restart retransmission cycle - lower().sendResponse(exchange, currentResponse); + super.sendResponse(exchange, currentResponse); } else if (exchange.getCurrentRequest().isAcknowledged()) { LOGGER.debug("{} duplicate request was acknowledged but no response computed yet. Retransmit ACK", diff --git a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/TcpObserveLayer.java b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/TcpObserveLayer.java index a7051214d3..fb94521392 100644 --- a/californium-core/src/main/java/org/eclipse/californium/core/network/stack/TcpObserveLayer.java +++ b/californium-core/src/main/java/org/eclipse/californium/core/network/stack/TcpObserveLayer.java @@ -52,7 +52,7 @@ public void sendRequest(final Exchange exchange, final Request request) { if (CANCEL.equals(request.getOptions().getObserve())) { /* TODO: don't send, if connection is not available */ } - lower().sendRequest(exchange, request); + super.sendRequest(exchange, request); } @Override @@ -64,7 +64,7 @@ public void sendResponse(final Exchange exchange, final Response response) { relation.cancel(); } } // else no observe was requested or the resource does not allow it - lower().sendResponse(exchange, response); + super.sendResponse(exchange, response); } @Override