Skip to content

Commit

Permalink
Use UnconnectedPeerException to consider Q device as sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 11, 2020
1 parent 55f8301 commit 36938b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import org.eclipse.californium.elements.RawData;
import org.eclipse.californium.elements.auth.PreSharedKeyIdentity;
import org.eclipse.californium.elements.exception.EndpointMismatchException;
import org.eclipse.californium.elements.exception.EndpointUnconnectedException;
import org.eclipse.californium.elements.util.SimpleMessageCallback;
import org.eclipse.californium.scandium.DTLSConnector;
import org.eclipse.californium.scandium.dtls.DTLSSession;
import org.eclipse.leshan.core.request.ReadRequest;
import org.eclipse.leshan.core.request.exception.SendFailedException;
import org.eclipse.leshan.core.request.exception.TimeoutException;
import org.eclipse.leshan.core.request.exception.UnconnectedPeerException;
import org.eclipse.leshan.core.response.ReadResponse;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.security.EditableSecurityStore;
Expand Down Expand Up @@ -337,8 +337,9 @@ public void server_does_not_initiate_dtls_handshake_with_queue_mode()
try {
helper.server.send(registration, new ReadRequest(3), 1000);
fail("Read request SHOULD have failed");
} catch (SendFailedException e) {
assertTrue(e.getCause() instanceof EndpointUnconnectedException);
} catch (UnconnectedPeerException e) {
// expected result
assertFalse("client is still awake", helper.server.getPresenceService().isClientAwake(registration));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.exception.ClientSleepingException;
import org.eclipse.leshan.core.request.exception.TimeoutException;
import org.eclipse.leshan.core.request.exception.UnconnectedPeerException;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
Expand Down Expand Up @@ -66,16 +67,23 @@ public <T extends LwM2mResponse> T send(final Registration destination, Downlink
}

// Use delegation to send the request
T response = delegatedSender.send(destination, request, timeout);
if (response != null) {
// Set the client awake. This will restart the timer.
presenceService.setAwake(destination);
} else {
// If the timeout expires, this means the client does not respond.
try {
T response = delegatedSender.send(destination, request, timeout);
if (response != null) {
// Set the client awake. This will restart the timer.
presenceService.setAwake(destination);
} else {
// If the timeout expires, this means the client does not respond.
presenceService.setSleeping(destination);
}

// Wait for response, then return it
return response;
} catch (UnconnectedPeerException e) {
// if peer is not connected (No DTLS connection available)
presenceService.setSleeping(destination);
throw e;
}
// Wait for response, then return it
return response;
}

/**
Expand Down Expand Up @@ -114,6 +122,9 @@ public void onError(Exception e) {
if (e instanceof TimeoutException) {
// If the timeout expires, this means the client does not respond.
presenceService.setSleeping(destination);
} else if (e instanceof UnconnectedPeerException) {
// if peer is not connected (No DTLS connection available)
presenceService.setSleeping(destination);
}

// Call the user's callback
Expand Down

0 comments on commit 36938b0

Please sign in to comment.