Skip to content

Commit 55c2a23

Browse files
author
Achim Kraus
committed
#429: complete exchange on send errors.
Extract CleanupMessageObserver from ExchangeCleanupLayer and add CON response to cleanup into it. Add also a CleanupMessageObserver to blockwise notifies in BlockwiseLayer. Signed-off-by: Achim Kraus <[email protected]>
1 parent f8433b7 commit 55c2a23

File tree

3 files changed

+88
-21
lines changed

3 files changed

+88
-21
lines changed

californium-core/src/main/java/org/eclipse/californium/core/network/stack/BlockwiseLayer.java

+4
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,9 @@ private void handleBlock2Response(final Exchange exchange, final Response respon
889889
*/
890890
if (!response.isNotification()) {
891891
block.setToken(response.getToken());
892+
} else if (exchange.isNotification()) {
893+
// Recreate cleanup message observer
894+
request.addMessageObserver(new CleanupMessageObserver(exchange));
892895
}
893896

894897
// copy options
@@ -904,6 +907,7 @@ private void handleBlock2Response(final Exchange exchange, final Response respon
904907
// add an observer that cleans up the block2 transfer tracker if the
905908
// block request fails
906909
addBlock2CleanUpObserver(block, key, status);
910+
907911

908912
status.setCurrentNum(nextNum);
909913

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Bosch Software Innovations GmbH and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.html.
12+
*
13+
* Contributors:
14+
* Bosch Software Innovations GmbH - initial creation
15+
* extracted from ExchangeCleanupLayer
16+
******************************************************************************/
17+
package org.eclipse.californium.core.network.stack;
18+
19+
import org.eclipse.californium.core.coap.MessageObserverAdapter;
20+
import org.eclipse.californium.core.coap.Request;
21+
import org.eclipse.californium.core.coap.Response;
22+
import org.eclipse.californium.core.network.Exchange;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
/**
27+
* Cleanup exchange when user cancelled outgoing requests or messages which
28+
* failed to be send.
29+
*/
30+
class CleanupMessageObserver extends MessageObserverAdapter {
31+
32+
static final Logger LOGGER = LoggerFactory.getLogger(CleanupMessageObserver.class.getName());
33+
34+
private final Exchange exchange;
35+
36+
CleanupMessageObserver(final Exchange exchange) {
37+
this.exchange = exchange;
38+
}
39+
40+
@Override
41+
public void onCancel() {
42+
complete("canceled");
43+
}
44+
45+
@Override
46+
public void failed() {
47+
complete("failed");
48+
}
49+
50+
private void complete(final String action) {
51+
if (exchange.executeComplete()) {
52+
if (exchange.isOfLocalOrigin()) {
53+
Request request = exchange.getCurrentRequest();
54+
LOGGER.debug("{}, {} request [MID={}, {}]", action, exchange, request.getMID(), request.getToken());
55+
} else {
56+
Response response = exchange.getCurrentResponse();
57+
LOGGER.debug("{}, {} response [MID={}, {}]", action, exchange, response.getMID(), response.getToken());
58+
}
59+
}
60+
}
61+
}

californium-core/src/main/java/org/eclipse/californium/core/network/stack/ExchangeCleanupLayer.java

+23-21
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,49 @@
1616
******************************************************************************/
1717
package org.eclipse.californium.core.network.stack;
1818

19-
import org.eclipse.californium.core.coap.MessageObserverAdapter;
2019
import org.eclipse.californium.core.coap.Request;
20+
import org.eclipse.californium.core.coap.Response;
2121
import org.eclipse.californium.core.network.Exchange;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

2525
/**
26-
* A layer that reacts to user cancelled outgoing requests, and completes exchange, which causes state clean up.
26+
* A layer that reacts to user cancelled outgoing requests or messages which
27+
* failed to be send, and completes exchange, which causes state clean up.
2728
*/
2829
public class ExchangeCleanupLayer extends AbstractLayer {
2930

30-
private static final Logger LOGGER = LoggerFactory.getLogger(ExchangeCleanupLayer.class.getName());
31+
static final Logger LOGGER = LoggerFactory.getLogger(ExchangeCleanupLayer.class.getName());
3132

3233
/**
33-
* Adds a message observer to the request to be sent which
34-
* completes the exchange if the request gets canceled.
34+
* Adds a message observer to the request to be sent which completes the
35+
* exchange if the request gets canceled or failed.
3536
*
36-
* @param exchange The (locally originating) exchange that the request is part of.
37+
* @param exchange The (locally originating) exchange that the request is
38+
* part of.
3739
* @param request The outbound request.
3840
*/
3941
@Override
4042
public void sendRequest(final Exchange exchange, final Request request) {
4143

42-
request.addMessageObserver(new CancelledMessageObserver(exchange));
43-
lower().sendRequest(exchange, request);
44+
request.addMessageObserver(new CleanupMessageObserver(exchange));
45+
super.sendRequest(exchange, request);
4446
}
4547

46-
private static class CancelledMessageObserver extends MessageObserverAdapter {
47-
48-
private final Exchange exchange;
49-
50-
CancelledMessageObserver(final Exchange exchange) {
51-
this.exchange = exchange;
52-
}
48+
/**
49+
* Adds a message observer to a confirmable response to be sent which
50+
* completes the exchange if the response gets canceled or failed.
51+
*
52+
* @param exchange The (remotely originating) exchange that the response is
53+
* part of.
54+
* @param response The outbound response.
55+
*/
56+
@Override
57+
public void sendResponse(final Exchange exchange, final Response response) {
5358

54-
@Override
55-
public void onCancel() {
56-
if (exchange.executeComplete()) {
57-
LOGGER.debug("{}, canceled request [MID={}, {}]", exchange,
58-
exchange.getRequest().getMID(), exchange.getRequest().getToken());
59-
}
59+
if (response.isConfirmable() && !response.isNotification()) {
60+
response.addMessageObserver(new CleanupMessageObserver(exchange));
6061
}
62+
super.sendResponse(exchange, response);
6163
}
6264
}

0 commit comments

Comments
 (0)