Search in sources :

Example 1 with SyncRequestObserver

use of org.eclipse.leshan.core.californium.SyncRequestObserver 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 SyncRequestObserver

use of org.eclipse.leshan.core.californium.SyncRequestObserver in project leshan by eclipse.

the class CaliforniumLwM2mBootstrapRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(final String endpointName, final Identity destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
    // Create the CoAP request
    // from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
    request.accept(coapClientRequestBuilder);
    final Request coapRequest = coapClientRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
            // LwM2mBootstrapResponseBuilder
            Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination, destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, registration, model, null, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Send CoAP request asynchronously
    if (destination.isSecure())
        secureEndpoint.sendRequest(coapRequest);
    else
        nonSecureEndpoint.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) Registration(org.eclipse.leshan.server.registration.Registration) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver)

Example 3 with SyncRequestObserver

use of org.eclipse.leshan.core.californium.SyncRequestObserver in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(final Registration destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
    // Retrieve the objects definition
    final LwM2mModel model = modelProvider.getObjectModel(destination);
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
    request.accept(coapRequestBuilder);
    final Request coapRequest = coapRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, destination, model, observationService, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.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) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Aggregations

Request (org.eclipse.californium.core.coap.Request)3 Response (org.eclipse.californium.core.coap.Response)3 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)2 Endpoint (org.eclipse.californium.core.network.Endpoint)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)1 Registration (org.eclipse.leshan.server.registration.Registration)1