Search in sources :

Example 16 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(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)

Example 17 with Request

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

the class RegisterResource method handlePOST.

@Override
public void handlePOST(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();
    LOG.debug("POST received : {}", request);
    // The LWM2M spec (section 8.2) mandates the usage of confirmable messages
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    List<String> uri = exchange.getRequestOptions().getUriPath();
    if (uri == null || uri.size() == 0 || !RESOURCE_NAME.equals(uri.get(0))) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    if (uri.size() == 1) {
        handleRegister(exchange, request);
        return;
    } else if (uri.size() == 2) {
        handleUpdate(exchange, request, uri.get(1));
        return;
    } else {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Request(org.eclipse.californium.core.coap.Request)

Example 18 with Request

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

the class RegisterResource method handlePUT.

// Since the V1_0-20150615-D version of specification, the registration update should be a CoAP POST.
// To keep compatibility with older version, we still accept CoAP PUT.
// TODO remove this backward compatibility when the version 1.0.0 of the spec will be released.
@Override
public void handlePUT(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();
    LOG.debug("UPDATE received : {}", request);
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    List<String> uri = exchange.getRequestOptions().getUriPath();
    if (uri == null || uri.size() != 2 || !RESOURCE_NAME.equals(uri.get(0))) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    LOG.debug("Warning a client made a registration update using a CoAP PUT, a POST must be used since version V1_0-20150615-D of the specification. Request: {}", request);
    handleUpdate(exchange, request, uri.get(1));
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Request(org.eclipse.californium.core.coap.Request)

Example 19 with Request

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

the class CoapRequestBuilderTest method build_write_request.

@Test
public void build_write_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    WriteRequest request = new WriteRequest(Mode.UPDATE, 3, 0, LwM2mSingleResource.newStringResource(4, "value"));
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of resources TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.RESOURCE_VALUE, tlvs[0].getType());
    assertEquals("value", TlvDecoder.decodeString(tlvs[0].getValue()));
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) 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) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 20 with Request

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

the class CoapRequestBuilderTest method build_write_attribute_request.

@Test
public void build_write_attribute_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    WriteAttributesRequest request = new WriteAttributesRequest(3, 0, 14, new ObserveSpec.Builder().minPeriod(10).maxPeriod(100).build());
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.PUT, 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/14?pmin=10&pmax=100", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) Builder(org.eclipse.leshan.server.registration.Registration.Builder) 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) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) 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