Skip to content

Commit

Permalink
Add "start" event on LwM2mClientObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 9, 2020
1 parent a74bd6a commit 31f9cda
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,18 @@ private Collection<Server> clientInitiatedBootstrap() throws InterruptedExceptio
Server bootstrapServer = endpointsManager.createEndpoint(bootstrapServerInfo);

// Send bootstrap request
BootstrapRequest request = null;
try {
request = new BootstrapRequest(endpoint);
if (observer != null) {
observer.onBootstrapStarted(bootstrapServer, request);
}
BootstrapResponse response = sender.send(bootstrapServerInfo.getAddress(),
bootstrapServerInfo.isSecure(), new BootstrapRequest(endpoint), requestTimeoutInMs);
bootstrapServerInfo.isSecure(), request, requestTimeoutInMs);
if (response == null) {
LOG.info("Unable to start bootstrap session: Timeout.");
if (observer != null) {
observer.onBootstrapTimeout(bootstrapServer);
observer.onBootstrapTimeout(bootstrapServer, request);
}
return null;
} else if (response.isSuccess()) {
Expand All @@ -209,7 +214,7 @@ private Collection<Server> clientInitiatedBootstrap() throws InterruptedExceptio
if (timeout) {
LOG.info("Bootstrap sequence aborted: Timeout.");
if (observer != null) {
observer.onBootstrapTimeout(bootstrapServer);
observer.onBootstrapTimeout(bootstrapServer, request);
}
return null;
} else {
Expand All @@ -220,22 +225,22 @@ private Collection<Server> clientInitiatedBootstrap() throws InterruptedExceptio
dmServers = endpointsManager.createEndpoints(serverInfos.deviceManagements.values());
}
if (observer != null) {
observer.onBootstrapSuccess(bootstrapServer);
observer.onBootstrapSuccess(bootstrapServer, request);
}
return dmServers;
}
} else {
LOG.info("Bootstrap failed: {} {}.", response.getCode(), response.getErrorMessage());
if (observer != null) {
observer.onBootstrapFailure(bootstrapServer, response.getCode(), response.getErrorMessage(),
null);
observer.onBootstrapFailure(bootstrapServer, request, response.getCode(),
response.getErrorMessage(), null);
}
return null;
}
} catch (RuntimeException e) {
logExceptionOnSendRequest("Unable to send Bootstrap request", e);
if (observer != null) {
observer.onBootstrapFailure(bootstrapServer, null, null, e);
observer.onBootstrapFailure(bootstrapServer, request, null, null, e);
}
return null;
} finally {
Expand Down Expand Up @@ -268,16 +273,20 @@ private Status register(Server server) throws InterruptedException {

// Send register request
LOG.info("Trying to register to {} ...", server.getUri());
RegisterRequest request = null;
try {
RegisterRequest regRequest = new RegisterRequest(endpoint, dmInfo.lifetime, LwM2m.VERSION, dmInfo.binding,
null, LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes);
RegisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), regRequest,
request = new RegisterRequest(endpoint, dmInfo.lifetime, LwM2m.VERSION, dmInfo.binding, null,
LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes);
if (observer != null) {
observer.onRegistrationStarted(server, request);
}
RegisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), request,
requestTimeoutInMs);

if (response == null) {
LOG.info("Registration failed: Timeout.");
if (observer != null) {
observer.onRegistrationTimeout(server);
observer.onRegistrationTimeout(server, request);
}
return Status.TIMEOUT;
} else if (response.isSuccess()) {
Expand All @@ -291,20 +300,21 @@ private Status register(Server server) throws InterruptedException {
scheduleUpdate(server, registrationID, new RegistrationUpdate(), delay);

if (observer != null) {
observer.onRegistrationSuccess(server, registrationID);
observer.onRegistrationSuccess(server, request, registrationID);
}
return Status.SUCCESS;
} else {
LOG.info("Registration failed: {} {}.", response.getCode(), response.getErrorMessage());
if (observer != null) {
observer.onRegistrationFailure(server, response.getCode(), response.getErrorMessage(), null);
observer.onRegistrationFailure(server, request, response.getCode(), response.getErrorMessage(),
null);
}
return Status.FAILURE;
}
} catch (RuntimeException e) {
logExceptionOnSendRequest("Unable to send register request", e);
if (observer != null) {
observer.onRegistrationFailure(server, null, null, e);
observer.onRegistrationFailure(server, request, null, null, e);
}
return Status.FAILURE;
}
Expand All @@ -322,14 +332,19 @@ private boolean deregister(Server server, String registrationID) throws Interrup

// Send deregister request
LOG.info("Trying to deregister to {} ...", server.getUri());
DeregisterRequest request = null;
try {
request = new DeregisterRequest(registrationID);
if (observer != null) {
observer.onDeregistrationStarted(server, request);
}
DeregisterResponse response = sender.send(server.getIdentity().getPeerAddress(),
server.getIdentity().isSecure(), new DeregisterRequest(registrationID), deregistrationTimeoutInMs);
server.getIdentity().isSecure(), request, deregistrationTimeoutInMs);
if (response == null) {
registrationID = null;
LOG.info("Deregistration failed: Timeout.");
if (observer != null) {
observer.onDeregistrationTimeout(server);
observer.onDeregistrationTimeout(server, request);
}
return false;
} else if (response.isSuccess() || response.getCode() == ResponseCode.NOT_FOUND) {
Expand All @@ -339,23 +354,25 @@ private boolean deregister(Server server, String registrationID) throws Interrup
LOG.info("De-register response {} {}.", response.getCode(), response.getErrorMessage());
if (observer != null) {
if (response.isSuccess()) {
observer.onDeregistrationSuccess(server, registrationID);
observer.onDeregistrationSuccess(server, request);
} else {
observer.onDeregistrationFailure(server, response.getCode(), response.getErrorMessage(), null);
observer.onDeregistrationFailure(server, request, response.getCode(),
response.getErrorMessage(), null);
}
}
return true;
} else {
LOG.info("Deregistration failed: {} {}.", response.getCode(), response.getErrorMessage());
if (observer != null) {
observer.onDeregistrationFailure(server, response.getCode(), response.getErrorMessage(), null);
observer.onDeregistrationFailure(server, request, response.getCode(), response.getErrorMessage(),
null);
}
return false;
}
} catch (RuntimeException e) {
logExceptionOnSendRequest("Unable to send deregister request", e);
if (observer != null) {
observer.onDeregistrationFailure(server, null, null, e);
observer.onDeregistrationFailure(server, request, null, null, e);
}
return false;
}
Expand Down Expand Up @@ -384,20 +401,23 @@ private Status update(Server server, String registrationID, RegistrationUpdate r

// Send update
LOG.info("Trying to update registration to {} (response timeout {}ms)...", server.getUri(), requestTimeoutInMs);
UpdateRequest request = null;
try {
request = new UpdateRequest(registrationID, registrationUpdate.getLifeTimeInSec(),
registrationUpdate.getSmsNumber(), registrationUpdate.getBindingMode(),
registrationUpdate.getObjectLinks(), registrationUpdate.getAdditionalAttributes());
if (observer != null) {
observer.onUpdateStarted(server, request);
}
if (reconnectOnUpdate) {
endpointsManager.forceReconnection(server, resumeOnConnect);
}
UpdateResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(),
new UpdateRequest(registrationID, registrationUpdate.getLifeTimeInSec(),
registrationUpdate.getSmsNumber(), registrationUpdate.getBindingMode(),
registrationUpdate.getObjectLinks(), registrationUpdate.getAdditionalAttributes()),
requestTimeoutInMs);
UpdateResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), request, requestTimeoutInMs);
if (response == null) {
registrationID = null;
LOG.info("Registration update failed: Timeout.");
if (observer != null) {
observer.onUpdateTimeout(server);
observer.onUpdateTimeout(server, request);
}
return Status.TIMEOUT;
} else if (response.getCode() == ResponseCode.CHANGED) {
Expand All @@ -406,21 +426,21 @@ private Status update(Server server, String registrationID, RegistrationUpdate r
long delay = calculateNextUpdate(server, dmInfo.lifetime);
scheduleUpdate(server, registrationID, new RegistrationUpdate(), delay);
if (observer != null) {
observer.onUpdateSuccess(server, registrationID);
observer.onUpdateSuccess(server, request);
}
return Status.SUCCESS;
} else {
LOG.info("Registration update failed: {} {}.", response.getCode(), response.getErrorMessage());
if (observer != null) {
observer.onUpdateFailure(server, response.getCode(), response.getErrorMessage(), null);
observer.onUpdateFailure(server, request, response.getCode(), response.getErrorMessage(), null);
}
registeredServers.remove(registrationID);
return Status.FAILURE;
}
} catch (RuntimeException e) {
logExceptionOnSendRequest("Unable to send update request", e);
if (observer != null) {
observer.onUpdateFailure(server, null, null, e);
observer.onUpdateFailure(server, request, null, null, e);
}
return Status.FAILURE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,56 @@

import org.eclipse.leshan.ResponseCode;
import org.eclipse.leshan.client.servers.Server;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.DeregisterRequest;
import org.eclipse.leshan.core.request.RegisterRequest;
import org.eclipse.leshan.core.request.UpdateRequest;

/**
* Allow to observer the registration life cycle of a LwM2m client.
*/
public interface LwM2mClientObserver {
// ============== Bootstrap =================

void onBootstrapSuccess(Server bsserver);
void onBootstrapStarted(Server bsserver, BootstrapRequest request);

void onBootstrapFailure(Server bsserver, ResponseCode responseCode, String errorMessage, Exception cause);
void onBootstrapSuccess(Server bsserver, BootstrapRequest request);

void onBootstrapTimeout(Server bsserver);
void onBootstrapFailure(Server bsserver, BootstrapRequest request, ResponseCode responseCode, String errorMessage,
Exception cause);

void onRegistrationSuccess(Server server, String registrationID);
void onBootstrapTimeout(Server bsserver, BootstrapRequest request);

void onRegistrationFailure(Server server, ResponseCode responseCode, String errorMessage, Exception cause);
// ============== Registration =================

void onRegistrationTimeout(Server server);
void onRegistrationStarted(Server server, RegisterRequest request);

void onUpdateSuccess(Server server, String registrationID);
void onRegistrationSuccess(Server server, RegisterRequest request, String registrationID);

void onUpdateFailure(Server server, ResponseCode responseCode, String errorMessage, Exception cause);
void onRegistrationFailure(Server server, RegisterRequest request, ResponseCode responseCode, String errorMessage,
Exception cause);

void onUpdateTimeout(Server server);
void onRegistrationTimeout(Server server, RegisterRequest request);

void onDeregistrationSuccess(Server server, String registrationID);
// ============== Registration Update =================

void onDeregistrationFailure(Server server, ResponseCode responseCode, String errorMessage, Exception cause);
void onUpdateStarted(Server server, UpdateRequest request);

void onDeregistrationTimeout(Server server);
void onUpdateSuccess(Server server, UpdateRequest request);

void onUpdateFailure(Server server, UpdateRequest request, ResponseCode responseCode, String errorMessage,
Exception cause);

void onUpdateTimeout(Server server, UpdateRequest request);

// ============== Deregistration Update =================

void onDeregistrationStarted(Server server, DeregisterRequest request);

void onDeregistrationSuccess(Server server, DeregisterRequest request);

void onDeregistrationFailure(Server server, DeregisterRequest request, ResponseCode responseCode,
String errorMessage, Exception cause);

void onDeregistrationTimeout(Server server, DeregisterRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import org.eclipse.leshan.ResponseCode;
import org.eclipse.leshan.client.servers.Server;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.DeregisterRequest;
import org.eclipse.leshan.core.request.RegisterRequest;
import org.eclipse.leshan.core.request.UpdateRequest;

/**
* An abstract adapter class for observing registration life cycle. The methods in this class are empty. This class
Expand All @@ -25,51 +29,70 @@
public class LwM2mClientObserverAdapter implements LwM2mClientObserver {

@Override
public void onBootstrapSuccess(Server bsserver) {
public void onBootstrapStarted(Server bsserver, BootstrapRequest request) {
}

@Override
public void onBootstrapFailure(Server bsserver, ResponseCode responseCode, String errorMessage, Exception cause) {
public void onBootstrapSuccess(Server bsserver, BootstrapRequest request) {
}

@Override
public void onBootstrapTimeout(Server bsserver) {
public void onBootstrapFailure(Server bsserver, BootstrapRequest request, ResponseCode responseCode,
String errorMessage, Exception cause) {
}

@Override
public void onRegistrationSuccess(Server server, String registrationID) {
public void onBootstrapTimeout(Server bsserver, BootstrapRequest request) {
}

@Override
public void onRegistrationFailure(Server server, ResponseCode responseCode, String errorMessage, Exception cause) {
public void onRegistrationStarted(Server server, RegisterRequest request) {
}

@Override
public void onRegistrationTimeout(Server server) {
public void onRegistrationSuccess(Server server, RegisterRequest request, String registrationID) {
}

@Override
public void onUpdateSuccess(Server server, String registrationID) {
public void onRegistrationFailure(Server server, RegisterRequest request, ResponseCode responseCode,
String errorMessage, Exception cause) {
}

@Override
public void onUpdateFailure(Server server, ResponseCode responseCode, String errorMessage, Exception cause) {
public void onRegistrationTimeout(Server server, RegisterRequest request) {
}

@Override
public void onUpdateTimeout(Server server) {
public void onUpdateStarted(Server server, UpdateRequest request) {
}

@Override
public void onDeregistrationSuccess(Server server, String registrationID) {
public void onUpdateSuccess(Server server, UpdateRequest request) {
}

@Override
public void onDeregistrationFailure(Server server, ResponseCode responseCode, String errorMessage,
public void onUpdateFailure(Server server, UpdateRequest request, ResponseCode responseCode, String errorMessage,
Exception cause) {
}

@Override
public void onDeregistrationTimeout(Server server) {
public void onUpdateTimeout(Server server, UpdateRequest request) {
}

@Override
public void onDeregistrationStarted(Server server, DeregisterRequest request) {
}

@Override
public void onDeregistrationSuccess(Server server, DeregisterRequest request) {
}

@Override
public void onDeregistrationFailure(Server server, DeregisterRequest request, ResponseCode responseCode,
String errorMessage, Exception cause) {
}

@Override
public void onDeregistrationTimeout(Server server, DeregisterRequest request) {
}
}
Loading

0 comments on commit 31f9cda

Please sign in to comment.