Search in sources :

Example 1 with Request

use of org.eclipse.californium.core.coap.Request 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 Request

use of org.eclipse.californium.core.coap.Request 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 Request

use of org.eclipse.californium.core.coap.Request 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 4 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_read_request.

@Test
public void build_read_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ReadRequest request = new ReadRequest(3, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 5 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_discover_request.

@Test
public void build_discover_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    DiscoverRequest request = new DiscoverRequest(3, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals(MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapRequest.getOptions().getAccept());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Test(org.junit.Test)

Aggregations

Request (org.eclipse.californium.core.coap.Request)26 Registration (org.eclipse.leshan.server.registration.Registration)14 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)13 Test (org.junit.Test)13 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)12 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)12 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)12 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)12 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)12 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)12 Response (org.eclipse.californium.core.coap.Response)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)6 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)5 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)3 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)3 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)3 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)3 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)3 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)3