use of org.eclipse.leshan.core.node.LwM2mNode 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);
}
}
use of org.eclipse.leshan.core.node.LwM2mNode in project leshan by eclipse.
the class ResponseSerDes method deserialize.
public static LwM2mResponse deserialize(JsonObject o) {
String sCode = o.getString("code", null);
if (sCode == null)
throw new IllegalStateException("Invalid response missing code attribute");
ResponseCode code = ResponseCode.fromName(sCode);
String errorMessage = o.getString("errorMessage", null);
String kind = o.getString("kind", null);
switch(kind) {
case "observe":
{
// TODO ser Observation
LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
return new ObserveResponse(code, content, null, null, errorMessage);
}
case "delete":
return new DeleteResponse(code, errorMessage);
case "discover":
String objectLinks = o.getString("objectLinks", "");
return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage);
case "create":
{
String location = o.getString("location", null);
return new CreateResponse(code, location, errorMessage);
}
case "execute":
return new ExecuteResponse(code, errorMessage);
case "writeAttributes":
{
return new WriteAttributesResponse(code, errorMessage);
}
case "write":
{
return new WriteResponse(code, errorMessage);
}
case "read":
{
LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
return new ReadResponse(code, content, errorMessage);
}
default:
throw new IllegalStateException("Invalid response missing kind attribute");
}
}
Aggregations