Search in sources :

Example 11 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(final Registration destination, final DownlinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // 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();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, 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(obs);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.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) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Example 12 with Request

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

the class CaliforniumLwM2mRequestSender method cancelPendingRequests.

@Override
public void cancelPendingRequests(Registration registration) {
    Validate.notNull(registration);
    String registrationId = registration.getId();
    SortedMap<String, Request> requests = pendingRequests.subMap(getFloorKey(registrationId), getCeilingKey(registrationId));
    for (Request coapRequest : requests.values()) {
        coapRequest.cancel();
    }
    requests.clear();
}
Also used : Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest)

Example 13 with Request

use of org.eclipse.californium.core.coap.Request 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 14 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> void send(final String endpointName, final Identity destination, final DownlinkRequest<T> request, final long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
    request.accept(coapClientRequestBuilder);
    final Request coapRequest = coapClientRequestBuilder.getRequest();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, 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(obs);
    // Send CoAP request asynchronously
    if (destination.isSecure())
        secureEndpoint.sendRequest(coapRequest);
    else
        nonSecureEndpoint.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) Registration(org.eclipse.leshan.server.registration.Registration) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Example 15 with Request

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

the class BootstrapResource method handlePOST.

@Override
public void handlePOST(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();
    LOG.debug("POST received : {}", request);
    // messages
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    // which endpoint?
    String endpoint = null;
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
            endpoint = param.substring(QUERY_PARAM_ENDPOINT.length());
            break;
        }
    }
    // Extract client identity
    Identity clientIdentity = EndpointContextUtil.extractIdentity(request.getSourceContext());
    // handle bootstrap request
    BootstrapResponse response = bootstrapHandler.bootstrap(clientIdentity, new BootstrapRequest(endpoint));
    if (response.isSuccess()) {
        exchange.respond(toCoapResponseCode(response.getCode()));
    } else {
        exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) Request(org.eclipse.californium.core.coap.Request) BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) BootstrapResponse(org.eclipse.leshan.core.response.BootstrapResponse) Identity(org.eclipse.leshan.core.request.Identity)

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