Skip to content

Commit

Permalink
#429: Catch error and raise event when message failed to be sent
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 27, 2018
1 parent f9f8b91 commit cede7b1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
}
Expand All @@ -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);
}
}
});
}
Expand All @@ -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);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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,
Expand All @@ -492,7 +492,7 @@ private void handleInboundRequestForNextBlock(final Exchange exchange, final Req
}

exchange.setCurrentResponse(block);
lower().sendResponse(exchange, block);
super.sendResponse(exchange, block);
}
}

Expand Down Expand Up @@ -573,7 +573,7 @@ public void sendResponse(final Exchange exchange, final Response response) {
}

exchange.setCurrentResponse(responseToSend);
lower().sendResponse(exchange, responseToSend);
super.sendResponse(exchange, responseToSend);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void retransmit() {
}
});
}
lower().sendRequest(exchange, request);
super.sendRequest(exchange, request);
}

/**
Expand Down Expand Up @@ -156,7 +156,7 @@ public void retransmit() {
}
});
}
lower().sendResponse(exchange, response);
super.sendResponse(exchange, response);
}

/**
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cede7b1

Please sign in to comment.