Search in sources :

Example 1 with BootstrapRequest

use of org.eclipse.leshan.core.request.BootstrapRequest in project leshan by eclipse.

the class BootstrapHandlerTest method notifies_at_end_of_successful_bootstrap.

@Test
public void notifies_at_end_of_successful_bootstrap() {
    final MockBootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(true);
    final LwM2mBootstrapRequestSender requestSender = new MockRequestSender(true);
    final BootstrapStore bsStore = new BootstrapStore() {

        @Override
        public BootstrapConfig getBootstrap(String endpoint) {
            return new BootstrapConfig();
        }
    };
    BootstrapHandler bsHandler = new BootstrapHandler(bsStore, requestSender, bsSessionManager, new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    });
    bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint"));
    assertTrue(bsSessionManager.endWasCalled());
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) Executor(java.util.concurrent.Executor) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 2 with BootstrapRequest

use of org.eclipse.leshan.core.request.BootstrapRequest 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 3 with BootstrapRequest

use of org.eclipse.leshan.core.request.BootstrapRequest 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 4 with BootstrapRequest

use of org.eclipse.leshan.core.request.BootstrapRequest in project leshan by eclipse.

the class BootstrapHandlerTest method does_not_notifies_at_end_of_failed_bootstrap.

@Test
public void does_not_notifies_at_end_of_failed_bootstrap() {
    final MockBootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(true);
    final LwM2mBootstrapRequestSender requestSender = new MockRequestSender(false);
    final BootstrapStore bsStore = new BootstrapStore() {

        @Override
        public BootstrapConfig getBootstrap(String endpoint) {
            return new BootstrapConfig();
        }
    };
    BootstrapHandler bsHandler = new BootstrapHandler(bsStore, requestSender, bsSessionManager, new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    });
    bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint"));
    assertFalse(bsSessionManager.endWasCalled());
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) Executor(java.util.concurrent.Executor) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 5 with BootstrapRequest

use of org.eclipse.leshan.core.request.BootstrapRequest 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)5 InetSocketAddress (java.net.InetSocketAddress)3 BootstrapResponse (org.eclipse.leshan.core.response.BootstrapResponse)3 Test (org.junit.Test)3 Executor (java.util.concurrent.Executor)2 Request (org.eclipse.californium.core.coap.Request)1 Identity (org.eclipse.leshan.core.request.Identity)1