Skip to content

Commit

Permalink
#485: add support of CoAP request for CaliforniumLwM2mRequestSender
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 7, 2018
1 parent 3397939 commit e16d8f3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2018 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.californium;

import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.leshan.core.californium.CoapResponseCallback;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.server.registration.Registration;

public interface CoapRequestSender {

/**
* Sends a CoAP request synchronously. Will block until a response is received from the remote client.
*
* @param destination the remote client
* @param request the request to send to the client
* @param timeout the request timeout in millisecond
* @return the response or <code>null</code> if the timeout expires (given parameter or CoAP timeout).
*
* @throws CodecException if request payload can not be encoded.
* @throws InterruptedException if the thread was interrupted.
*/
Response sendCoapRequest(final Registration destination, final Request coapRequest, long timeout)
throws InterruptedException;

/**
* Sends a CoAP request asynchronously.
*
* @param destination the remote client
* @param request the request to send to the client
* @param timeout the request timeout in millisecond
* @param responseCallback a callback called when a response is received (successful or error response)
* @param errorCallback a callback called when an error or exception occurred when response is received
*
* @throws CodecException if request payload can not be encoded.
*/
void sendCoapRequest(final Registration destination, final Request coapRequest, long timeout,
CoapResponseCallback responseCallback, ErrorCallback errorCallback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.leshan.core.californium.AsyncRequestObserver;
import org.eclipse.leshan.core.californium.CoapAsyncRequestObserver;
import org.eclipse.leshan.core.californium.CoapResponseCallback;
import org.eclipse.leshan.core.californium.CoapSyncRequestObserver;
import org.eclipse.leshan.core.californium.EndpointContextUtil;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
Expand All @@ -37,12 +42,13 @@
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.server.californium.CoapRequestSender;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.request.LwM2mRequestSender;
import org.eclipse.leshan.util.Validate;

public class CaliforniumLwM2mRequestSender implements LwM2mRequestSender {
public class CaliforniumLwM2mRequestSender implements LwM2mRequestSender, CoapRequestSender {

private final Set<Endpoint> endpoints;
private final ObservationServiceImpl observationService;
Expand Down Expand Up @@ -140,6 +146,49 @@ public T buildResponse(Response coapResponse) {
endpoint.sendRequest(coapRequest);
}

@Override
public Response sendCoapRequest(final Registration destination, final Request coapRequest, long timeout)
throws InterruptedException {

// Define destination
EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
coapRequest.setDestinationContext(context);

// Send CoAP request synchronously
CoapSyncRequestObserver syncMessageObserver = new CoapSyncRequestObserver(coapRequest, timeout);
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.waitForCoapResponse();
}

@Override
public void sendCoapRequest(final Registration destination, final Request coapRequest, long timeout,
CoapResponseCallback responseCallback, ErrorCallback errorCallback) {

// Define destination
EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
coapRequest.setDestinationContext(context);

// Add CoAP request callback
MessageObserver obs = new CoapAsyncRequestObserver(coapRequest, responseCallback, errorCallback, timeout);
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);
}

@Override
public void cancelPendingRequests(Registration registration) {
Validate.notNull(registration);
Expand Down

0 comments on commit e16d8f3

Please sign in to comment.