Skip to content

Commit

Permalink
Stop Bootstrap session on unexpected exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent 0e924e0 commit 405556b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum BootstrapFailureCause {
*/
NO_BOOTSTRAP_CONFIG,
/**
* The "/" object could not be deleted on the device
* A delete request failed
*/
DELETE_FAILED,
/**
Expand All @@ -48,7 +48,11 @@ public enum BootstrapFailureCause {
*/
WRITE_SECURITY_FAILED,
/**
* 'Bootstrap Finish' message count not be sent to the device
* 'Bootstrap Finish' failed
*/
FINISH_FAILED,
/**
* An unexpected error occured
*/
INTERNAL_SERVER_ERROR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ public BootstrapResponse bootstrap(Identity sender, BootstrapRequest request) {
e.execute(new Runnable() {
@Override
public void run() {
startBootstrap(session, cfg);
try {
startBootstrap(session, cfg);
} catch (RuntimeException e) {
LOG.warn("Unexpected error at bootstrap start-up for {}", session, e);
stopSession(session, INTERNAL_SERVER_ERROR);
}
}
});

Expand Down Expand Up @@ -164,9 +169,9 @@ protected void delete(final BootstrapSession session, final BootstrapConfig cfg,
String path = pathToDelete.get(0);

final BootstrapDeleteRequest deleteRequest = new BootstrapDeleteRequest(path);
send(session, deleteRequest, new ResponseCallback<BootstrapDeleteResponse>() {
send(session, deleteRequest, new SafeResponseCallback<BootstrapDeleteResponse>(session) {
@Override
public void onResponse(BootstrapDeleteResponse response) {
public void safeOnResponse(BootstrapDeleteResponse response) {
if (response.isSuccess()) {
LOG.trace("{} receives {} for {}", session, response, deleteRequest);
sessionManager.onResponseSuccess(deleteRequest);
Expand All @@ -177,9 +182,9 @@ public void onResponse(BootstrapDeleteResponse response) {
afterDelete(session, cfg, pathToDelete, policy);
}
}
}, new ErrorCallback() {
}, new SafeErrorCallback(session) {
@Override
public void onError(Exception e) {
public void safeOnError(Exception e) {
LOG.debug("Error for {} while sending {} ", session, deleteRequest, e);
BootstrapPolicy policy = sessionManager.onRequestFailure(deleteRequest, e);
afterDelete(session, cfg, pathToDelete, policy);
Expand Down Expand Up @@ -230,9 +235,9 @@ protected void writeSecurities(final BootstrapSession session, final BootstrapCo
session.getContentFormat());

// sent it
send(session, writeBootstrapRequest, new ResponseCallback<BootstrapWriteResponse>() {
send(session, writeBootstrapRequest, new SafeResponseCallback<BootstrapWriteResponse>(session) {
@Override
public void onResponse(BootstrapWriteResponse response) {
public void safeOnResponse(BootstrapWriteResponse response) {
if (response.isSuccess()) {
LOG.trace("{} receives {} for {}", session, response, writeBootstrapRequest);
sessionManager.onResponseSuccess(writeBootstrapRequest);
Expand All @@ -243,9 +248,9 @@ public void onResponse(BootstrapWriteResponse response) {
afterWriteSecurities(session, cfg, securityInstancesToWrite, policy);
}
}
}, new ErrorCallback() {
}, new SafeErrorCallback(session) {
@Override
public void onError(Exception e) {
public void safeOnError(Exception e) {
LOG.debug("Error for {} while sending {} ", session, writeBootstrapRequest, e);
BootstrapPolicy policy = sessionManager.onRequestFailure(writeBootstrapRequest, e);
afterWriteSecurities(session, cfg, securityInstancesToWrite, policy);
Expand Down Expand Up @@ -296,9 +301,9 @@ protected void writeServers(final BootstrapSession session, final BootstrapConfi
session.getContentFormat());

// sent it
send(session, writeServerRequest, new ResponseCallback<BootstrapWriteResponse>() {
send(session, writeServerRequest, new SafeResponseCallback<BootstrapWriteResponse>(session) {
@Override
public void onResponse(BootstrapWriteResponse response) {
public void safeOnResponse(BootstrapWriteResponse response) {
if (response.isSuccess()) {
LOG.trace("{} receives {} for {}", session, response, writeServerRequest);
sessionManager.onResponseSuccess(writeServerRequest);
Expand All @@ -309,9 +314,9 @@ public void onResponse(BootstrapWriteResponse response) {
afterWriteServers(session, cfg, serverInstancesToWrite, policy);
}
}
}, new ErrorCallback() {
}, new SafeErrorCallback(session) {
@Override
public void onError(Exception e) {
public void safeOnError(Exception e) {
LOG.debug("Error for {} while sending {} ", session, writeServerRequest, e);
BootstrapPolicy policy = sessionManager.onRequestFailure(writeServerRequest, e);
afterWriteServers(session, cfg, serverInstancesToWrite, policy);
Expand Down Expand Up @@ -362,9 +367,9 @@ protected void writedAcls(final BootstrapSession session, final BootstrapConfig
session.getContentFormat());

// sent it
send(session, writeACLRequest, new ResponseCallback<BootstrapWriteResponse>() {
send(session, writeACLRequest, new SafeResponseCallback<BootstrapWriteResponse>(session) {
@Override
public void onResponse(BootstrapWriteResponse response) {
public void safeOnResponse(BootstrapWriteResponse response) {
if (response.isSuccess()) {
LOG.trace("{} receives {} for {}", session, response, writeACLRequest);
sessionManager.onResponseSuccess(writeACLRequest);
Expand All @@ -375,9 +380,9 @@ public void onResponse(BootstrapWriteResponse response) {
afterWritedAcls(session, cfg, aclInstancesToWrite, policy);
}
}
}, new ErrorCallback() {
}, new SafeErrorCallback(session) {
@Override
public void onError(Exception e) {
public void safeOnError(Exception e) {
LOG.debug("Error for {} while sending {} ", session, writeACLRequest, e);
BootstrapPolicy policy = sessionManager.onRequestFailure(writeACLRequest, e);
afterWritedAcls(session, cfg, aclInstancesToWrite, policy);
Expand Down Expand Up @@ -416,9 +421,9 @@ protected void afterWritedAcls(BootstrapSession session, BootstrapConfig cfg, Li
protected void bootstrapFinished(final BootstrapSession session, final BootstrapConfig cfg) {

final BootstrapFinishRequest finishBootstrapRequest = new BootstrapFinishRequest();
send(session, finishBootstrapRequest, new ResponseCallback<BootstrapFinishResponse>() {
send(session, finishBootstrapRequest, new SafeResponseCallback<BootstrapFinishResponse>(session) {
@Override
public void onResponse(BootstrapFinishResponse response) {
public void safeOnResponse(BootstrapFinishResponse response) {
if (response.isSuccess()) {
LOG.trace("{} receives {} for {}", session, response, finishBootstrapRequest);
sessionManager.onResponseSuccess(finishBootstrapRequest);
Expand All @@ -429,9 +434,9 @@ public void onResponse(BootstrapFinishResponse response) {
afterBootstrapFinished(session, cfg, policy);
}
}
}, new ErrorCallback() {
}, new SafeErrorCallback(session) {
@Override
public void onError(Exception e) {
public void safeOnError(Exception e) {
LOG.debug("Error for {} while sending {} ", session, finishBootstrapRequest, e);
BootstrapPolicy policy = sessionManager.onRequestFailure(finishBootstrapRequest, e);
afterBootstrapFinished(session, cfg, policy);
Expand Down Expand Up @@ -466,4 +471,46 @@ protected <T extends LwM2mResponse> void send(BootstrapSession session, Downlink
sender.send(session.getEndpoint(), session.getIdentity(), request, requestTimeout, responseCallback,
errorCallback);
}

protected abstract class SafeResponseCallback<T extends LwM2mResponse> implements ResponseCallback<T> {

private BootstrapSession session;

public SafeResponseCallback(BootstrapSession session) {
this.session = session;
}

@Override
public void onResponse(T response) {
try {
safeOnResponse(response);
} catch (RuntimeException e) {
LOG.warn("Unexpected error on response callback for {}", session, e);
stopSession(session, INTERNAL_SERVER_ERROR);
}
}

public abstract void safeOnResponse(T response);
}

protected abstract class SafeErrorCallback implements ErrorCallback {

private BootstrapSession session;

public SafeErrorCallback(BootstrapSession session) {
this.session = session;
}

@Override
public void onError(Exception error) {
try {
safeOnError(error);
} catch (RuntimeException e) {
LOG.warn("Unexpected error on error callback for {}", session, e);
stopSession(session, INTERNAL_SERVER_ERROR);
}
}

public abstract void safeOnError(Exception e);
}
}

0 comments on commit 405556b

Please sign in to comment.