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