Search in sources :

Example 1 with UplinkRequest

use of org.eclipse.leshan.core.request.UplinkRequest in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(InetSocketAddress serverAddress, boolean secure, final UplinkRequest<T> request, long timeout) throws InterruptedException {
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
    request.accept(coapClientRequestBuilder);
    Request coapRequest = coapClientRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Send CoAP request asynchronously
    if (secure)
        securedEndpoint.sendRequest(coapRequest);
    else
        unsecuredEndpoint.sendRequest(coapRequest);
    // Wait for response, then return it
    return syncMessageObserver.waitForResponse();
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Request(org.eclipse.californium.core.coap.Request) UplinkRequest(org.eclipse.leshan.core.request.UplinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver)

Example 2 with UplinkRequest

use of org.eclipse.leshan.core.request.UplinkRequest in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> void send(InetSocketAddress serverAddress, boolean secure, final UplinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
    request.accept(coapClientRequestBuilder);
    Request coapRequest = coapClientRequestBuilder.getRequest();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(obs);
    // Send CoAP request asynchronously
    if (secure)
        securedEndpoint.sendRequest(coapRequest);
    else
        unsecuredEndpoint.sendRequest(coapRequest);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) MessageObserver(org.eclipse.californium.core.coap.MessageObserver) Request(org.eclipse.californium.core.coap.Request) UplinkRequest(org.eclipse.leshan.core.request.UplinkRequest) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Example 3 with UplinkRequest

use of org.eclipse.leshan.core.request.UplinkRequest 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)

Aggregations

Request (org.eclipse.californium.core.coap.Request)3 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)3 Response (org.eclipse.californium.core.coap.Response)2 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)2 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)1 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)1 RawData (org.eclipse.californium.elements.RawData)1 CoapRequestBuilder (org.eclipse.leshan.client.californium.impl.CoapRequestBuilder)1 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)1 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)1