Skip to content

Commit

Permalink
Consider DTLS Handshake timeout as leshan timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 11, 2020
1 parent faa3ab0 commit 623eecf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.scandium.dtls.DtlsHandshakeTimeoutException;
import org.eclipse.leshan.core.request.exception.RequestCanceledException;
import org.eclipse.leshan.core.request.exception.RequestRejectedException;
import org.eclipse.leshan.core.request.exception.SendFailedException;
import org.eclipse.leshan.core.request.exception.TimeoutException;
import org.eclipse.leshan.core.request.exception.TimeoutException.Type;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -111,8 +114,8 @@ public void onReadyToSend() {
public void onTimeout() {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
errorCallback.onError(new org.eclipse.leshan.core.request.exception.TimeoutException("Request %s timed out",
coapRequest.getURI()));
errorCallback.onError(new TimeoutException(Type.COAP_TIMEOUT,
"Request %s timed out : CoAP or blockwise timeout", coapRequest.getURI()));
} else {
LOG.debug("OnTimeout callback ignored because an event was already raised for this request {}",
coapRequest);
Expand All @@ -124,8 +127,8 @@ public void onCancel() {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
if (responseTimedOut.get()) {
errorCallback.onError(new org.eclipse.leshan.core.request.exception.TimeoutException(
"Request %s timed out", coapRequest.getURI()));
errorCallback.onError(new TimeoutException(Type.RESPONSE_TIMEOUT,
"Request %s timed out : no response received", coapRequest.getURI()));
} else {
errorCallback.onError(new RequestCanceledException("Request %s cancelled", coapRequest.getURI()));
}
Expand All @@ -150,7 +153,13 @@ public void onReject() {
public void onSendError(Throwable error) {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
errorCallback.onError(new SendFailedException(error, "Unable to send request %s", coapRequest.getURI()));
if (error instanceof DtlsHandshakeTimeoutException) {
errorCallback.onError(new TimeoutException(Type.DTLS_HANDSHAKE_TIMEOUT, error,
"Request %s timeout : dtls handshake timeout", coapRequest.getURI()));
} else {
errorCallback
.onError(new SendFailedException(error, "Unable to send request %s", coapRequest.getURI()));
}
} else {
LOG.debug("onSendError callback ignored because an event was already raised for this request {}",
coapRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.scandium.dtls.DtlsHandshakeTimeoutException;
import org.eclipse.leshan.core.request.exception.RequestCanceledException;
import org.eclipse.leshan.core.request.exception.RequestRejectedException;
import org.eclipse.leshan.core.request.exception.SendFailedException;
Expand Down Expand Up @@ -94,7 +95,11 @@ public void onReject() {

@Override
public void onSendError(Throwable error) {
exception.set(new SendFailedException(error, "Request %s cannot be sent", coapRequest, error.getMessage()));
if (error instanceof DtlsHandshakeTimeoutException) {
coapTimeout.set(true);
} else {
exception.set(new SendFailedException(error, "Request %s cannot be sent", coapRequest, error.getMessage()));
}
latch.countDown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,35 @@

public class TimeoutException extends Exception {

public enum Type {
RESPONSE_TIMEOUT, COAP_TIMEOUT, DTLS_HANDSHAKE_TIMEOUT
}

private static final long serialVersionUID = -8966041387554358975L;

public TimeoutException(String message, Object... args) {
private Type type;

public TimeoutException(Type type, String message, Object... args) {
super(String.format(message, args));
this.type = type;
}

public TimeoutException(Type type, Throwable cause, String message, Object... args) {
super(String.format(message, args), cause);
this.type = type;
}

/**
* Get the kind of timeout.
* <p>
* See https://github.com/eclipse/leshan/wiki/Request-Timeout for more details.
* <p>
* Current implementation is not able to make differences between CoAP and Blockwise timeout, all is regroup over
* <code>COAP_TIMEOUT</code>.
*
* @return the kind of timeout
*/
public Type getType() {
return type;
}
}

0 comments on commit 623eecf

Please sign in to comment.