Skip to content

Commit

Permalink
Remove unnecessary executor in DefaultBootstrapHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent 405556b commit dc1a4c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
Expand Down Expand Up @@ -60,8 +58,6 @@ public class DefaultBootstrapHandler implements BootstrapHandler {

public static final long DEFAULT_LIFETIME = 15 * 60 * 1000l; // 15 minutes

protected final Executor e;

protected final BootstrapConfigStore store;

protected final LwM2mBootstrapRequestSender sender;
Expand All @@ -76,20 +72,14 @@ public class DefaultBootstrapHandler implements BootstrapHandler {

public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender,
BootstrapSessionManager sessionManager) {
this(store, sender, sessionManager, Executors.newFixedThreadPool(5), DEFAULT_TIMEOUT, DEFAULT_LIFETIME);
}

public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender,
BootstrapSessionManager sessionManager, Executor executor) {
this(store, sender, sessionManager, executor, DEFAULT_TIMEOUT, DEFAULT_LIFETIME);
this(store, sender, sessionManager, DEFAULT_TIMEOUT, DEFAULT_LIFETIME);
}

public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender,
BootstrapSessionManager sessionManager, Executor executor, long requestTimeout, long sessionLifetime) {
BootstrapSessionManager sessionManager, long requestTimeout, long sessionLifetime) {
this.store = store;
this.sender = sender;
this.sessionManager = sessionManager;
this.e = executor;
this.requestTimeout = requestTimeout;
this.sessionLifeTime = sessionLifetime;
}
Expand Down Expand Up @@ -123,28 +113,23 @@ public BootstrapResponse bootstrap(Identity sender, BootstrapRequest request) {
}
} while (oldSession != null);

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

// Start the bootstrap session
e.execute(new Runnable() {
@Override
public void run() {
try {
startBootstrap(session, cfg);
} catch (RuntimeException e) {
LOG.warn("Unexpected error at bootstrap start-up for {}", session, e);
stopSession(session, INTERNAL_SERVER_ERROR);
}
try {
// Get the desired bootstrap config for the endpoint
final BootstrapConfig cfg = store.get(endpoint, sender);
if (cfg == null) {
LOG.debug("No bootstrap config for {}", session);
stopSession(session, NO_BOOTSTRAP_CONFIG);
return BootstrapResponse.badRequest("no bootstrap config");
}
});

return BootstrapResponse.success();
startBootstrap(session, cfg);
return BootstrapResponse.success();

} catch (RuntimeException e) {
LOG.warn("Unexpected error at bootstrap start-up for {}", session, e);
stopSession(session, INTERNAL_SERVER_ERROR);
return BootstrapResponse.internalServerError(e.getMessage());
}
}

protected void startBootstrap(BootstrapSession session, BootstrapConfig cfg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.junit.Assert.*;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;

import org.eclipse.leshan.ResponseCode;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
Expand All @@ -45,7 +44,7 @@ public class BootstrapHandlerTest {
public void error_if_not_authorized() {
// prepare bootstrapHandler with a session manager which does not authorized any session
BootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(false);
BootstrapHandler bsHandler = new DefaultBootstrapHandler(null, null, bsSessionManager, new DirectExecutor());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(null, null, bsSessionManager);

// Try to bootstrap
BootstrapResponse response = bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"),
Expand All @@ -64,8 +63,7 @@ public void bootstrap_success() throws InvalidConfigurationException {
LwM2mBootstrapRequestSender requestSender = new MockRequestSender(Mode.ALWAYS_SUCCESS);
EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore();
bsStore.add("endpoint", new BootstrapConfig());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager,
new DirectExecutor());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager);

// Try to bootstrap
bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("endpoint"));
Expand All @@ -84,8 +82,7 @@ public void bootstrap_failed_because_of_sent_failure() throws InvalidConfigurati
LwM2mBootstrapRequestSender requestSender = new MockRequestSender(Mode.ALWAYS_FAILURE);
EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore();
bsStore.add("endpoint", new BootstrapConfig());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager,
new DirectExecutor());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager);

// Try to bootstrap
bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("endpoint"));
Expand All @@ -108,7 +105,7 @@ public void two_bootstrap_at_the_same_time_not_allowed()
EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore();
bsStore.add("endpoint", new BootstrapConfig());
BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager,
new DirectExecutor(), DefaultBootstrapHandler.DEFAULT_TIMEOUT, 1000);
DefaultBootstrapHandler.DEFAULT_TIMEOUT, 1000);

// First bootstrap : which will not end (because of sender)
BootstrapResponse first_response = bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"),
Expand Down Expand Up @@ -281,11 +278,4 @@ public void reset() {
failureCause = null;
}
}

private static class DirectExecutor implements Executor {
@Override
public void execute(Runnable command) {
command.run();
}
}
}

0 comments on commit dc1a4c8

Please sign in to comment.