Skip to content

Commit

Permalink
Update Client Request Sender to be able to support "Send operation"
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 5, 2021
1 parent 80d1ad6 commit bfbb425
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.leshan.client.resource.listener.ObjectsListenerAdapter;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.util.Validate;
Expand Down Expand Up @@ -113,7 +114,7 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
bootstrapHandler = createBoostrapHandler(objectTree);
endpointsManager = createEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore,
endpointFactory);
requestSender = createRequestSender(endpointsManager, sharedExecutor);
requestSender = createRequestSender(endpointsManager, sharedExecutor, encoder, objectTree.getModel());
engine = engineFactory.createRegistratioEngine(endpoint, objectTree, endpointsManager, requestSender,
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes, sharedExecutor);

Expand Down Expand Up @@ -218,8 +219,8 @@ protected CaliforniumEndpointsManager createEndpointsManager(InetSocketAddress l
}

protected CaliforniumLwM2mRequestSender createRequestSender(CaliforniumEndpointsManager endpointsManager,
ScheduledExecutorService executor) {
return new CaliforniumLwM2mRequestSender(endpointsManager, executor);
ScheduledExecutorService executor, LwM2mNodeEncoder encoder, LwM2mModel model) {
return new CaliforniumLwM2mRequestSender(endpointsManager, executor, encoder, model);
}

protected RegistrationUpdateHandler createRegistrationUpdateHandler(RegistrationEngine engine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.californium.AsyncRequestObserver;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.UplinkRequest;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
Expand All @@ -45,9 +47,11 @@ public class CaliforniumLwM2mRequestSender implements LwM2mRequestSender {
private final ScheduledExecutorService executor;
private final boolean attached;
private final CaliforniumEndpointsManager endpointsManager;
private final LwM2mNodeEncoder encoder;
private final LwM2mModel model;

public CaliforniumLwM2mRequestSender(CaliforniumEndpointsManager endpointsManager,
ScheduledExecutorService sharedExecutor) {
ScheduledExecutorService sharedExecutor, LwM2mNodeEncoder encoder, LwM2mModel model) {
this.endpointsManager = endpointsManager;
if (sharedExecutor == null) {
this.executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Leshan Async Request timeout"));
Expand All @@ -56,13 +60,15 @@ public CaliforniumLwM2mRequestSender(CaliforniumEndpointsManager endpointsManage
this.executor = sharedExecutor;
this.attached = false;
}
this.model = model;
this.encoder = encoder;
}

@Override
public <T extends LwM2mResponse> T send(ServerIdentity server, final UplinkRequest<T> request, long timeout)
throws InterruptedException {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(server.getIdentity());
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(server.getIdentity(), encoder, model);
request.accept(coapClientRequestBuilder);
Request coapRequest = coapClientRequestBuilder.getRequest();

Expand All @@ -89,7 +95,7 @@ public T buildResponse(Response coapResponse) {
public <T extends LwM2mResponse> void send(ServerIdentity server, final UplinkRequest<T> request, long timeout,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(server.getIdentity());
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(server.getIdentity(), encoder, model);
request.accept(coapClientRequestBuilder);
Request coapRequest = coapClientRequestBuilder.getRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.leshan.core.Link;
import org.eclipse.leshan.core.californium.EndpointContextUtil;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.ContentFormat;
Expand All @@ -43,9 +45,13 @@ public class CoapRequestBuilder implements UplinkRequestVisitor {

protected Request coapRequest;
protected final Identity server;
protected final LwM2mNodeEncoder encoder;
protected final LwM2mModel model;

public CoapRequestBuilder(Identity server) {
public CoapRequestBuilder(Identity server, LwM2mNodeEncoder encoder, LwM2mModel model) {
this.server = server;
this.encoder = encoder;
this.model = model;
}

@Override
Expand Down Expand Up @@ -145,8 +151,14 @@ public void visit(DeregisterRequest request) {
}

@Override
public void visit(SendRequest sendRequest) {
throw new UnsupportedOperationException("Not implemented");
public void visit(SendRequest request) {
coapRequest = Request.newPost();
buildRequestSettings();
coapRequest.getOptions().setUriPath("/dp");

ContentFormat format = request.getFormat();
coapRequest.getOptions().setContentFormat(format.getCode());
coapRequest.setPayload(encoder.encodeNodes(request.getNodes(), format, model));
}

public Request getRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.leshan.core.response.DeregisterResponse;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.RegisterResponse;
import org.eclipse.leshan.core.response.SendResponse;
import org.eclipse.leshan.core.response.UpdateResponse;

/**
Expand Down Expand Up @@ -95,8 +96,18 @@ public void visit(UpdateRequest request) {
}

@Override
public void visit(SendRequest sendRequest) {
throw new UnsupportedOperationException("Not implemented");
public void visit(SendRequest request) {
if (coapResponse.isError()) {
// handle error response:
lwM2mresponse = new SendResponse(toLwM2mResponseCode(coapResponse.getCode()),
coapResponse.getPayloadString());
} else if (coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CHANGED) {
// handle success response:
lwM2mresponse = SendResponse.success();
} else {
// handle unexpected response:
handleUnexpectedResponseCode(request, coapResponse);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.integration.tests.lockstep;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Random;

import org.eclipse.californium.core.coap.Message;
Expand All @@ -25,6 +26,12 @@
import org.eclipse.californium.core.test.lockstep.LockstepEndpoint;
import org.eclipse.californium.elements.RawData;
import org.eclipse.leshan.client.californium.request.CoapRequestBuilder;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.request.UplinkRequest;
import org.eclipse.leshan.core.response.LwM2mResponse;
Expand All @@ -33,15 +40,20 @@ public class LockStepLwM2mClient extends LockstepEndpoint {

private static final Random r = new Random();
private InetSocketAddress destination;
private final LwM2mNodeEncoder encoder;
private final LwM2mModel model;

public LockStepLwM2mClient(final InetSocketAddress destination) {
super(destination);
this.destination = destination;
this.encoder = new DefaultLwM2mNodeEncoder();
List<ObjectModel> models = ObjectLoader.loadDefault();
this.model = new StaticModel(models);
}

public Request createCoapRequest(UplinkRequest<? extends LwM2mResponse> lwm2mReq) {
// create CoAP request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(Identity.unsecure(destination));
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(Identity.unsecure(destination), encoder, model);
lwm2mReq.accept(coapRequestBuilder);
Request coapReq = coapRequestBuilder.getRequest();
byte[] token = new byte[8];
Expand Down

0 comments on commit bfbb425

Please sign in to comment.