Search in sources :

Example 1 with RawData

use of org.eclipse.californium.elements.RawData in project leshan by eclipse.

the class LockStepLwM2mClient method sendLwM2mRequest.

public void sendLwM2mRequest(UplinkRequest<? extends LwM2mResponse> lwm2mReq) {
    // create CoAP request
    CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination);
    lwm2mReq.accept(coapRequestBuilder);
    Request coapReq = coapRequestBuilder.getRequest();
    byte[] token = new byte[8];
    r.nextBytes(token);
    coapReq.setToken(token);
    // serialize request
    UdpDataSerializer serializer = new UdpDataSerializer();
    RawData raw = serializer.serializeRequest(coapReq);
    // send it
    super.send(raw);
}
Also used : RawData(org.eclipse.californium.elements.RawData) CoapRequestBuilder(org.eclipse.leshan.client.californium.impl.CoapRequestBuilder) Request(org.eclipse.californium.core.coap.Request) UplinkRequest(org.eclipse.leshan.core.request.UplinkRequest) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Example 2 with RawData

use of org.eclipse.californium.elements.RawData in project leshan by eclipse.

the class ObserveTest method sendNotification.

private void sendNotification(Connector connector, byte[] payload, Response firstCoapResponse, int contentFormat) {
    // create observe response
    Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT);
    response.setType(Type.NON);
    response.setPayload(payload);
    response.setMID(firstCoapResponse.getMID() + 1);
    response.setToken(firstCoapResponse.getToken());
    OptionSet options = new OptionSet().setContentFormat(contentFormat).setObserve(firstCoapResponse.getOptions().getObserve() + 1);
    response.setOptions(options);
    EndpointContext context = new AddressEndpointContext(helper.server.getUnsecuredAddress().getAddress(), helper.server.getUnsecuredAddress().getPort());
    response.setDestinationContext(context);
    // serialize response
    UdpDataSerializer serializer = new UdpDataSerializer();
    RawData data = serializer.serializeResponse(response);
    // send it
    connector.send(data);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RawData(org.eclipse.californium.elements.RawData) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) OptionSet(org.eclipse.californium.core.coap.OptionSet) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Example 3 with RawData

use of org.eclipse.californium.elements.RawData in project leshan by eclipse.

the class ObservationSerDes method deserialize.

public static Observation deserialize(byte[] data) {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    EndpointContext endpointContext = EndpointContextSerDes.deserialize(v.get("peer").asObject());
    byte[] req = Hex.decodeHex(v.getString("request", null).toCharArray());
    RawData rawData = RawData.outbound(req, endpointContext, null, false);
    Request request = (Request) parser.parseMessage(rawData);
    request.setDestinationContext(endpointContext);
    JsonValue ctxValue = v.get("context");
    if (ctxValue != null) {
        Map<String, String> context = new HashMap<>();
        JsonObject ctxObject = (JsonObject) ctxValue;
        for (String name : ctxObject.names()) {
            context.put(name, ctxObject.getString(name, null));
        }
        request.setUserContext(context);
    }
    return new Observation(request, endpointContext);
}
Also used : RawData(org.eclipse.californium.elements.RawData) EndpointContext(org.eclipse.californium.elements.EndpointContext) HashMap(java.util.HashMap) Request(org.eclipse.californium.core.coap.Request) JsonValue(com.eclipsesource.json.JsonValue) Observation(org.eclipse.californium.core.observe.Observation) JsonObject(com.eclipsesource.json.JsonObject)

Aggregations

RawData (org.eclipse.californium.elements.RawData)3 Request (org.eclipse.californium.core.coap.Request)2 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)2 EndpointContext (org.eclipse.californium.elements.EndpointContext)2 JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 HashMap (java.util.HashMap)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1 Response (org.eclipse.californium.core.coap.Response)1 Observation (org.eclipse.californium.core.observe.Observation)1 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)1 CoapRequestBuilder (org.eclipse.leshan.client.californium.impl.CoapRequestBuilder)1 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)1 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)1 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)1 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)1