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);
}
}
Aggregations