Search in sources :

Example 1 with ErrorCallback

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

the class BootstrapHandler method sendDelete.

private void sendDelete(final BootstrapSession session, final BootstrapConfig cfg) {
    final BootstrapDeleteRequest deleteRequest = new BootstrapDeleteRequest();
    send(session, deleteRequest, new ResponseCallback<BootstrapDeleteResponse>() {

        @Override
        public void onResponse(BootstrapDeleteResponse response) {
            LOG.trace("Bootstrap delete {} return code {}", session.getEndpoint(), response.getCode());
            List<Integer> toSend = new ArrayList<>(cfg.security.keySet());
            sendBootstrap(session, cfg, toSend);
        }
    }, new ErrorCallback() {

        @Override
        public void onError(Exception e) {
            LOG.debug(String.format("Error during bootstrap delete '/' on %s", session.getEndpoint()), e);
            sessionManager.failed(session, DELETE_FAILED, deleteRequest);
        }
    });
}
Also used : BootstrapDeleteRequest(org.eclipse.leshan.core.request.BootstrapDeleteRequest) BootstrapDeleteResponse(org.eclipse.leshan.core.response.BootstrapDeleteResponse) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ErrorCallback

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

the class RedisRequestResponseHandler method sendRequest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void sendRequest(final String message) {
    // Parse JSON and extract ticket
    final String ticket;
    JsonObject jMessage;
    try {
        jMessage = (JsonObject) Json.parse(message);
        ticket = jMessage.getString("ticket", null);
    } catch (RuntimeException t) {
        LOG.error(String.format("Unexpected exception during request message handling. (%s)", message), t);
        return;
    }
    // Now if an error occurred we can prevent message sender
    try {
        // Check if we must handle this request
        String endpoint = jMessage.getString("ep", null);
        if (!isResponsibleFor(endpoint))
            return;
        // Get the registration for this endpoint
        final Registration destination = registrationService.getByEndpoint(endpoint);
        if (destination == null) {
            sendError(ticket, String.format("No registration for this endpoint %s.", endpoint));
        }
        // Deserialize Request
        DownlinkRequest<?> request = DownlinkRequestSerDes.deserialize((JsonObject) jMessage.get("req"));
        // Ack we will handle this request
        sendAck(ticket);
        // Send it
        server.send(destination, request, new ResponseCallback() {

            @Override
            public void onResponse(LwM2mResponse response) {
                handleResponse(destination.getEndpoint(), ticket, response);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                handlerError(destination.getEndpoint(), ticket, e);
            }
        });
    } catch (RuntimeException t) {
        String errorMessage = String.format("Unexpected exception during request message handling.(%s:%s)", t.toString(), t.getMessage());
        LOG.error(errorMessage, t);
        sendError(ticket, errorMessage);
    }
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ResponseCallback(org.eclipse.leshan.core.response.ResponseCallback) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse)

Example 3 with ErrorCallback

use of org.eclipse.leshan.core.response.ErrorCallback 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)

Example 4 with ErrorCallback

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

the class BootstrapHandler method sendBootstrap.

private void sendBootstrap(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // 1st encode them into a juicy TLV binary
        Integer key = toSend.remove(0);
        ServerSecurity securityConfig = cfg.security.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(0, key);
        final LwM2mNode securityInstance = convertToSecurityInstance(key, securityConfig);
        final BootstrapWriteRequest writeBootstrapRequest = new BootstrapWriteRequest(path, securityInstance, session.getContentFormat());
        send(session, writeBootstrapRequest, 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
                sendBootstrap(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap write of security instance %s on %s", securityInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SECURITY_FAILED, writeBootstrapRequest);
            }
        });
    } else {
        // we are done, send the servers
        List<Integer> serversToSend = new ArrayList<>(cfg.servers.keySet());
        sendServers(session, cfg, serversToSend);
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)

Aggregations

ErrorCallback (org.eclipse.leshan.core.response.ErrorCallback)4 ArrayList (java.util.ArrayList)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)2 BootstrapWriteResponse (org.eclipse.leshan.core.response.BootstrapWriteResponse)2 JsonObject (com.eclipsesource.json.JsonObject)1 List (java.util.List)1 BootstrapDeleteRequest (org.eclipse.leshan.core.request.BootstrapDeleteRequest)1 BootstrapFinishRequest (org.eclipse.leshan.core.request.BootstrapFinishRequest)1 BootstrapDeleteResponse (org.eclipse.leshan.core.response.BootstrapDeleteResponse)1 BootstrapFinishResponse (org.eclipse.leshan.core.response.BootstrapFinishResponse)1 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)1 ResponseCallback (org.eclipse.leshan.core.response.ResponseCallback)1 ServerConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig)1 ServerSecurity (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)1 Registration (org.eclipse.leshan.server.registration.Registration)1