|
16 | 16 | ******************************************************************************/
|
17 | 17 | package org.eclipse.californium.core.network.stack;
|
18 | 18 |
|
19 |
| -import org.eclipse.californium.core.coap.MessageObserverAdapter; |
20 | 19 | import org.eclipse.californium.core.coap.Request;
|
| 20 | +import org.eclipse.californium.core.coap.Response; |
21 | 21 | import org.eclipse.californium.core.network.Exchange;
|
22 | 22 | import org.slf4j.Logger;
|
23 | 23 | import org.slf4j.LoggerFactory;
|
24 | 24 |
|
25 | 25 | /**
|
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. |
27 | 28 | */
|
28 | 29 | public class ExchangeCleanupLayer extends AbstractLayer {
|
29 | 30 |
|
30 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(ExchangeCleanupLayer.class.getName()); |
| 31 | + static final Logger LOGGER = LoggerFactory.getLogger(ExchangeCleanupLayer.class.getName()); |
31 | 32 |
|
32 | 33 | /**
|
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. |
35 | 36 | *
|
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. |
37 | 39 | * @param request The outbound request.
|
38 | 40 | */
|
39 | 41 | @Override
|
40 | 42 | public void sendRequest(final Exchange exchange, final Request request) {
|
41 | 43 |
|
42 |
| - request.addMessageObserver(new CancelledMessageObserver(exchange)); |
43 |
| - lower().sendRequest(exchange, request); |
| 44 | + request.addMessageObserver(new CleanupMessageObserver(exchange)); |
| 45 | + super.sendRequest(exchange, request); |
44 | 46 | }
|
45 | 47 |
|
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) { |
53 | 58 |
|
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)); |
60 | 61 | }
|
| 62 | + super.sendResponse(exchange, response); |
61 | 63 | }
|
62 | 64 | }
|
0 commit comments