Skip to content

Commit

Permalink
#589: fix race condition about bootstrap finished at client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 7, 2018
1 parent 9cab0aa commit 05c3b0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.MessageObserverAdapter;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.servers.BootstrapHandler;
import org.eclipse.leshan.core.request.BootstrapFinishRequest;
import org.eclipse.leshan.core.response.BootstrapFinishResponse;
import org.eclipse.leshan.core.response.SendableResponse;

/**
* A CoAP {@link Resource} in charge of handling the Bootstrap Finish indication from the bootstrap server.
Expand All @@ -40,12 +44,25 @@ public BootstrapResource(BootstrapHandler bootstrapHandler) {

@Override
public void handlePOST(CoapExchange exchange) {
// Handle bootstrap request
ServerIdentity identity = ResourceUtil.extractServerIdentity(exchange, bootstrapHandler);
BootstrapFinishResponse response = bootstrapHandler.finished(identity, new BootstrapFinishRequest());
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(response.getCode()));
final SendableResponse<BootstrapFinishResponse> sendableResponse = bootstrapHandler.finished(identity,
new BootstrapFinishRequest());

// Create CoAP response
Response coapResponse = new Response(toCoapResponseCode(sendableResponse.getResponse().getCode()));
if (sendableResponse.getResponse().getCode().isError()) {
coapResponse.setPayload(sendableResponse.getResponse().getErrorMessage());
coapResponse.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
}

// Send response
coapResponse.addMessageObserver(new MessageObserverAdapter() {
@Override
public void onSent() {
sendableResponse.sent();
}
});
exchange.respond(coapResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*******************************************************************************/
package org.eclipse.leshan.client.servers;

import static org.eclipse.leshan.LwM2mId.SECURITY;
import static org.eclipse.leshan.LwM2mId.SERVER;
import static org.eclipse.leshan.LwM2mId.*;

import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand All @@ -31,6 +30,7 @@
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.response.BootstrapDeleteResponse;
import org.eclipse.leshan.core.response.BootstrapFinishResponse;
import org.eclipse.leshan.core.response.SendableResponse;

/**
* Handle bootstrap session state.
Expand All @@ -47,19 +47,25 @@ public BootstrapHandler(Map<Integer, LwM2mObjectEnabler> objectEnablers) {
objects = objectEnablers;
}

public synchronized BootstrapFinishResponse finished(ServerIdentity identity,
public synchronized SendableResponse<BootstrapFinishResponse> finished(ServerIdentity identity,
BootstrapFinishRequest finishedRequest) {
if (bootstrapping) {
// only if the request is from the bootstrap server
if (!isBootstrapServer(identity)) {
return BootstrapFinishResponse.badRequest("not from a bootstrap server");
return new SendableResponse<>(BootstrapFinishResponse.badRequest("not from a bootstrap server"));
}
// TODO delete bootstrap server (see 5.2.5.2 Bootstrap Delete)

bootstrappingLatch.countDown();
return BootstrapFinishResponse.success();
Runnable whenSent = new Runnable() {
@Override
public void run() {
bootstrappingLatch.countDown();
}
};

return new SendableResponse<>(BootstrapFinishResponse.success(), whenSent);
} else {
return BootstrapFinishResponse.badRequest("no pending bootstrap session");
return new SendableResponse<>(BootstrapFinishResponse.badRequest("no pending bootstrap session"));
}
}

Expand Down

0 comments on commit 05c3b0f

Please sign in to comment.