Search in sources :

Example 1 with ResponseCallback

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

Aggregations

JsonObject (com.eclipsesource.json.JsonObject)1 ErrorCallback (org.eclipse.leshan.core.response.ErrorCallback)1 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)1 ResponseCallback (org.eclipse.leshan.core.response.ResponseCallback)1 Registration (org.eclipse.leshan.server.registration.Registration)1