Skip to content

Commit

Permalink
Read function - some bugs correction, Send added
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawLegierski committed Jan 27, 2023
1 parent c535563 commit 3d49df0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.eclipse.leshan.transport.javacoap.endpoint;

import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox;
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint;
Expand Down Expand Up @@ -112,7 +114,21 @@ public JavaCoapClientEndpoint(URI endpointUri, CoapClient client,
public <T extends LwM2mResponse> void send(ServerIdentity server, UplinkRequest<T> request,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback, long timeoutInMs) {

final CoapRequest coapRequest = translator.createCoapRequest(server, request, toolbox, model);



CompletableFuture<CoapResponse> cfcoapResponse = null;
CoapResponse coapResponse = null;

cfcoapResponse = client.send(coapRequest);
try {
coapResponse = cfcoapResponse.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,18 @@ public void init(LwM2mObjectTree objectTree, DownlinkRequestReceiver requestRece
instancepath="/"+enabler.getAvailableInstanceIds().get(i);
List<Integer> availableResources1 = enabler.getAvailableResourceIds(i);


for (Integer availableResource : enabler.getAvailableResourceIds(i)) {
resourcepath="/"+availableResource;

finalpath=objectpath+instancepath+resourcepath;
resorcesbuilder.add(finalpath, new ObjectResource(requestReceiver, finalpath,toolbox,url, shortserverid));
}

resorcesbuilder.add(objectpath+instancepath, new ObjectResource(requestReceiver, objectpath+instancepath,toolbox,url, shortserverid));
}
}


ResourcesService resources = resorcesbuilder.build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class JavaCoapRequestBuilder implements UplinkRequestVisitor {
protected final LwM2mEncoder encoder;
protected final LwM2mModel model;
protected final LinkSerializer linkSerializer;
private final RandomTokenGenerator tokenGenerator = new RandomTokenGenerator(8);


public JavaCoapRequestBuilder(Identity server, LwM2mEncoder encoder, LwM2mModel model,
Expand Down Expand Up @@ -149,7 +150,8 @@ public JavaCoapRequestBuilder(Identity server, LwM2mEncoder encoder, LwM2mModel

coapRequest = coapRequest.payload(Opaque.of(payload), (short) ContentFormat.LINK.getCode());
// todo there is no token generator in java-coap
coapRequest=coapRequest.token(123456);

coapRequest=coapRequest.token(tokenGenerator.createToken());
}
}
@Override public void visit(UpdateRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.eclipse.leshan.transport.javacoap.request;

import java.security.SecureRandom;

import com.mbed.coap.packet.Opaque;

public class RandomTokenGenerator {

private final int tokenSize;
private final SecureRandom random;

public RandomTokenGenerator(int tokenSize) {
// TODO check size is between 1 and 8;
random = new SecureRandom();
this.tokenSize = tokenSize;
}

public Opaque createToken() {
byte[] token = new byte[tokenSize];
random.nextBytes(token);
return Opaque.of(token);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
package org.eclipse.leshan.transport.javacoap.resource;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import org.eclipse.leshan.client.californium.LwM2mClientCoapResource;
import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox;
import org.eclipse.leshan.client.request.DownlinkRequestReceiver;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.listener.ObjectListener;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.ServerInfo;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.request.ReadRequest;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.eclipse.leshan.core.response.ReadResponse;
import org.eclipse.leshan.transport.javacoap.request.ResponseCodeUtil;
import org.slf4j.Logger;
Expand All @@ -36,7 +26,6 @@ public class ObjectResource extends LwM2mCoapResource {

private static final Logger LOG = LoggerFactory.getLogger(ObjectResource.class);
private static final ServerIdentity identity = null;
private static String URI;
private static String serverurl;
protected DownlinkRequestReceiver requestReceiver;
protected ClientEndpointToolbox toolbox;
Expand All @@ -47,7 +36,6 @@ public ObjectResource(DownlinkRequestReceiver requestReceiver, String uri,Clien
super(uri);
this.requestReceiver = requestReceiver;
this.toolbox = toolbox;
this.URI=uri;
this.serverurl=serverurl;
this.shortserverid= shortserverid;
}
Expand Down Expand Up @@ -80,17 +68,15 @@ public CompletableFuture<CoapResponse> handleGET(CoapRequest coapRequest) {

ReadRequest readRequest = new ReadRequest(requestedContentFormat, coapRequest.options().getUriPath(), coapRequest);



Identity identity = getForeignPeerIdentity(coapRequest);

; //todo - find better solution for Server Identity!!!!
ServerIdentity serveridentity = new ServerIdentity(identity, (long) shortserverid, ServerIdentity.Role.LWM2M_SERVER, java.net.URI.create(serverurl));

CompletableFuture coapResponsecompletablefuture = new CompletableFuture();

ReadResponse response = requestReceiver.requestReceived(serveridentity, readRequest).getResponse();
if (response.getCode() == org.eclipse.leshan.core.ResponseCode.CONTENT) {
LwM2mPath path = getPath(URI);
LwM2mPath path = readRequest.getPath();
LwM2mNode content = response.getContent();
ContentFormat format = getContentFormat(readRequest, requestedContentFormat);

Expand All @@ -104,24 +90,18 @@ public CompletableFuture<CoapResponse> handleGET(CoapRequest coapRequest) {
coapResponse.options().setContentFormat(contentformat);

coapResponse = coapResponse.payload( Opaque.of(toolbox.getEncoder().encode(content, format, path, toolbox.getModel())));
CompletableFuture coapResponsecompletablefuture = new CompletableFuture();

coapResponsecompletablefuture.complete(coapResponse);

return coapResponsecompletablefuture;
} else {

return null;
CoapResponse coapResponse = CoapResponse.of(Code.C404_NOT_FOUND);
coapResponsecompletablefuture.complete(coapResponse);
return coapResponsecompletablefuture;
}


}
protected LwM2mPath getPath(String URI) throws InvalidRequestException {
try {
return new LwM2mPath(URI);
} catch (InvalidLwM2mPathException e) {
throw new InvalidRequestException(e, "Invalid path : %s", e.getMessage());
}
}

protected ContentFormat getContentFormat(DownlinkRequest<?> request, ContentFormat requestedContentFormat) {
if (requestedContentFormat != null) {
Expand Down

0 comments on commit 3d49df0

Please sign in to comment.