Skip to content

Commit

Permalink
Avoid to handle 2 bootstrap session at the same time.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent 22fbc99 commit 717ffc9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public enum BootstrapFailureCause {
* The device presented wrong credentials
*/
UNAUTHORIZED,
/**
* A bootstrap session is already started for the given device.
*/
ALREADY_STARTED,
/**
* The Bootstrap Server could not find a configuration to send to the device
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -63,6 +64,7 @@ public class DefaultBootstrapHandler implements BootstrapHandler {
protected final LwM2mBootstrapRequestSender sender;
protected final BootstrapSessionManager sessionManager;
protected final long requestTimeout;
protected final ConcurrentHashMap<String, BootstrapSession> onGoingSession = new ConcurrentHashMap<>();

public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender,
BootstrapSessionManager sessionManager) {
Expand Down Expand Up @@ -95,11 +97,19 @@ public BootstrapResponse bootstrap(Identity sender, BootstrapRequest request) {
return BootstrapResponse.badRequest("Unauthorized");
}

// check if there is not an ongoing session.
BootstrapSession oldSession = onGoingSession.putIfAbsent(endpoint, session);
if (oldSession != null) {
// Do not start the session if there is already a started one
sessionManager.failed(session, ALREADY_STARTED);
return BootstrapResponse.badRequest("session already started");
}

// Get the desired bootstrap config for the endpoint
final BootstrapConfig cfg = store.get(endpoint, sender);
if (cfg == null) {
LOG.debug("No bootstrap config for {}/{}", endpoint, sender);
sessionManager.failed(session, NO_BOOTSTRAP_CONFIG);
stopSession(session, NO_BOOTSTRAP_CONFIG);
return BootstrapResponse.badRequest("no bootstrap config");
}

Expand All @@ -118,6 +128,19 @@ protected void startBootstrap(BootstrapSession session, BootstrapConfig cfg) {
delete(session, cfg, new ArrayList<>(cfg.toDelete));
}

protected void stopSession(BootstrapSession session, BootstrapFailureCause cause) {
if (!onGoingSession.remove(session.getEndpoint(), session)) {
LOG.warn("Session for endpoint {} / identity {} was already removed", session.getEndpoint(),
session.getIdentity());
}
// if there is no cause of failure, this is a success
if (cause == null) {
sessionManager.end(session);
} else {
sessionManager.failed(session, cause);
}
}

protected void delete(final BootstrapSession session, final BootstrapConfig cfg, final List<String> pathToDelete) {
if (!pathToDelete.isEmpty()) {
// get next Security configuration
Expand Down Expand Up @@ -170,7 +193,7 @@ protected void afterDelete(BootstrapSession session, BootstrapConfig cfg, List<S
bootstrapFinished(session, cfg);
break;
case STOP:
sessionManager.failed(session, DELETE_FAILED);
stopSession(session, DELETE_FAILED);
break;
default:
throw new IllegalStateException("unknown policy :" + policy);
Expand Down Expand Up @@ -238,7 +261,7 @@ protected void afterWriteSecurities(BootstrapSession session, BootstrapConfig cf
bootstrapFinished(session, cfg);
break;
case STOP:
sessionManager.failed(session, WRITE_SECURITY_FAILED);
stopSession(session, WRITE_SECURITY_FAILED);
break;
default:
throw new IllegalStateException("unknown policy :" + policy);
Expand Down Expand Up @@ -306,7 +329,7 @@ protected void afterWriteServers(BootstrapSession session, BootstrapConfig cfg,
bootstrapFinished(session, cfg);
break;
case STOP:
sessionManager.failed(session, WRITE_SERVER_FAILED);
stopSession(session, WRITE_SERVER_FAILED);
break;
default:
throw new IllegalStateException("unknown policy :" + policy);
Expand Down Expand Up @@ -373,7 +396,7 @@ protected void afterWritedAcls(BootstrapSession session, BootstrapConfig cfg, Li
bootstrapFinished(session, cfg);
break;
case STOP:
sessionManager.failed(session, WRITE_ACL_FAILED);
stopSession(session, WRITE_ACL_FAILED);
break;
default:
throw new IllegalStateException("unknown policy :" + policy);
Expand Down Expand Up @@ -410,7 +433,7 @@ public void onError(Exception e) {
protected void afterBootstrapFinished(BootstrapSession session, BootstrapConfig cfg, BootstrapPolicy policy) {
switch (policy) {
case CONTINUE:
sessionManager.end(session);
stopSession(session, null);
break;
case RETRY:
bootstrapFinished(session, cfg);
Expand All @@ -422,7 +445,7 @@ protected void afterBootstrapFinished(BootstrapSession session, BootstrapConfig
bootstrapFinished(session, cfg);
break;
case STOP:
sessionManager.failed(session, FINISH_FAILED);
stopSession(session, FINISH_FAILED);
break;
default:
throw new IllegalStateException("unknown policy :" + policy);
Expand Down

0 comments on commit 717ffc9

Please sign in to comment.