Search in sources :

Example 11 with LwM2mResponse

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

LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)11 Request (org.eclipse.californium.core.coap.Request)6 Response (org.eclipse.californium.core.coap.Response)6 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)4 Test (org.junit.Test)4 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)3 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)3 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)3 Observation (org.eclipse.leshan.core.observation.Observation)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3 Registration (org.eclipse.leshan.server.registration.Registration)3 JsonObject (com.eclipsesource.json.JsonObject)2 Endpoint (org.eclipse.californium.core.network.Endpoint)2 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)2 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)2 HashMap (java.util.HashMap)1