Search in sources :

Example 11 with LwM2mNode

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);
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)

Example 12 with LwM2mNode

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");
    }
}
Also used : ResponseCode(org.eclipse.leshan.ResponseCode) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse)

Aggregations

LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)12 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)6 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)5 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)5 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)4 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)4 BootstrapWriteResponse (org.eclipse.leshan.core.response.BootstrapWriteResponse)4 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)4 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)3 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)3 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)3 CodecException (org.eclipse.leshan.core.node.codec.CodecException)3 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)3 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 JsonObject (com.eclipsesource.json.JsonObject)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2