Search in sources :

Example 1 with BootstrapResponse

use of org.eclipse.leshan.core.response.BootstrapResponse in project leshan by eclipse.

the class RegistrationEngine method bootstrap.

private boolean bootstrap() throws InterruptedException {
    ServersInfo serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
    if (serversInfo.bootstrap == null) {
        LOG.error("Trying to bootstrap device but there is no bootstrap server config.");
        return false;
    }
    if (bootstrapHandler.tryToInitSession(serversInfo.bootstrap)) {
        LOG.info("Trying to start bootstrap session to {} ...", serversInfo.bootstrap.getFullUri());
        try {
            // Send bootstrap request
            ServerInfo bootstrapServer = serversInfo.bootstrap;
            BootstrapResponse response = sender.send(bootstrapServer.getAddress(), bootstrapServer.isSecure(), new BootstrapRequest(endpoint), DEFAULT_TIMEOUT);
            if (response == null) {
                LOG.error("Unable to start bootstrap session: Timeout.");
                if (observer != null) {
                    observer.onBootstrapTimeout(bootstrapServer);
                }
                return false;
            } else if (response.isSuccess()) {
                LOG.info("Bootstrap started");
                // wait until it is finished (or too late)
                boolean timeout = !bootstrapHandler.waitBoostrapFinished(BS_TIMEOUT);
                if (timeout) {
                    LOG.error("Bootstrap sequence aborted: Timeout.");
                    if (observer != null) {
                        observer.onBootstrapTimeout(bootstrapServer);
                    }
                    return false;
                } else {
                    serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
                    LOG.info("Bootstrap finished {}.", serversInfo);
                    if (observer != null) {
                        observer.onBootstrapSuccess(bootstrapServer);
                    }
                    return true;
                }
            } else {
                LOG.error("Bootstrap failed: {} {}.", response.getCode(), response.getErrorMessage());
                if (observer != null) {
                    observer.onBootstrapFailure(bootstrapServer, response.getCode(), response.getErrorMessage());
                }
                return false;
            }
        } finally {
            bootstrapHandler.closeSession();
        }
    } else {
        LOG.warn("Bootstrap sequence already started.");
        return false;
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) BootstrapResponse(org.eclipse.leshan.core.response.BootstrapResponse)

Example 2 with BootstrapResponse

use of org.eclipse.leshan.core.response.BootstrapResponse in project leshan by eclipse.

the class BootstrapResource method handlePOST.

@Override
public void handlePOST(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();
    LOG.debug("POST received : {}", request);
    // messages
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    // which endpoint?
    String endpoint = null;
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
            endpoint = param.substring(QUERY_PARAM_ENDPOINT.length());
            break;
        }
    }
    // Extract client identity
    Identity clientIdentity = EndpointContextUtil.extractIdentity(request.getSourceContext());
    // handle bootstrap request
    BootstrapResponse response = bootstrapHandler.bootstrap(clientIdentity, new BootstrapRequest(endpoint));
    if (response.isSuccess()) {
        exchange.respond(toCoapResponseCode(response.getCode()));
    } else {
        exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) Request(org.eclipse.californium.core.coap.Request) BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) BootstrapResponse(org.eclipse.leshan.core.response.BootstrapResponse) Identity(org.eclipse.leshan.core.request.Identity)

Example 3 with BootstrapResponse

use of org.eclipse.leshan.core.response.BootstrapResponse in project leshan by eclipse.

the class BootstrapHandlerTest method error_if_not_authorized.

@Test
public void error_if_not_authorized() {
    final BootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(false);
    BootstrapHandler bsHandler = new BootstrapHandler(null, null, bsSessionManager);
    BootstrapResponse bootstrapResponse = bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint"));
    assertEquals(ResponseCode.BAD_REQUEST, bootstrapResponse.getCode());
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) InetSocketAddress(java.net.InetSocketAddress) BootstrapResponse(org.eclipse.leshan.core.response.BootstrapResponse) Test(org.junit.Test)

Aggregations

BootstrapRequest (org.eclipse.leshan.core.request.BootstrapRequest)3 BootstrapResponse (org.eclipse.leshan.core.response.BootstrapResponse)3 InetSocketAddress (java.net.InetSocketAddress)1 Request (org.eclipse.californium.core.coap.Request)1 Identity (org.eclipse.leshan.core.request.Identity)1 Test (org.junit.Test)1