Search in sources :

Example 1 with BootstrapFinishResponse

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

the class BootstrapResource method handlePOST.

@Override
public void handlePOST(CoapExchange exchange) {
    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()));
    }
}
Also used : ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) BootstrapFinishResponse(org.eclipse.leshan.core.response.BootstrapFinishResponse) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest)

Example 2 with BootstrapFinishResponse

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

the class BootstrapHandler method sendServers.

private void sendServers(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // get next config
        Integer key = toSend.remove(0);
        ServerConfig serverConfig = cfg.servers.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(1, key);
        final LwM2mNode serverInstance = convertToServerInstance(key, serverConfig);
        final BootstrapWriteRequest writeServerRequest = new BootstrapWriteRequest(path, serverInstance, session.getContentFormat());
        send(session, writeServerRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendServers(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.warn(String.format("Error during bootstrap write of server instance %s on %s", serverInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SERVER_FAILED, writeServerRequest);
            }
        });
    } else {
        final BootstrapFinishRequest finishBootstrapRequest = new BootstrapFinishRequest();
        send(session, finishBootstrapRequest, new ResponseCallback<BootstrapFinishResponse>() {

            @Override
            public void onResponse(BootstrapFinishResponse response) {
                LOG.trace("Bootstrap Finished {} return code {}", session.getEndpoint(), response.getCode());
                if (response.isSuccess()) {
                    sessionManager.end(session);
                } else {
                    sessionManager.failed(session, FINISHED_WITH_ERROR, finishBootstrapRequest);
                }
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap finished on %s", session.getEndpoint()), e);
                sessionManager.failed(session, SEND_FINISH_FAILED, finishBootstrapRequest);
            }
        });
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest) ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) BootstrapFinishResponse(org.eclipse.leshan.core.response.BootstrapFinishResponse) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Aggregations

BootstrapFinishRequest (org.eclipse.leshan.core.request.BootstrapFinishRequest)2 BootstrapFinishResponse (org.eclipse.leshan.core.response.BootstrapFinishResponse)2 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)1 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)1 BootstrapWriteResponse (org.eclipse.leshan.core.response.BootstrapWriteResponse)1 ErrorCallback (org.eclipse.leshan.core.response.ErrorCallback)1 ServerConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig)1