diff --git a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpoint.java b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpoint.java index e7b4e8ae2e..43337b565e 100644 --- a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpoint.java +++ b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpoint.java @@ -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; @@ -112,7 +114,21 @@ public JavaCoapClientEndpoint(URI endpointUri, CoapClient client, public void send(ServerIdentity server, UplinkRequest request, ResponseCallback responseCallback, ErrorCallback errorCallback, long timeoutInMs) { + final CoapRequest coapRequest = translator.createCoapRequest(server, request, toolbox, model); + + + + CompletableFuture cfcoapResponse = null; + CoapResponse coapResponse = null; + cfcoapResponse = client.send(coapRequest); + try { + coapResponse = cfcoapResponse.get(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } } } diff --git a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpointsProvider.java b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpointsProvider.java index 5b82c26d0a..aa665a5e8d 100644 --- a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpointsProvider.java +++ b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/endpoint/JavaCoapClientEndpointsProvider.java @@ -85,6 +85,7 @@ public void init(LwM2mObjectTree objectTree, DownlinkRequestReceiver requestRece instancepath="/"+enabler.getAvailableInstanceIds().get(i); List availableResources1 = enabler.getAvailableResourceIds(i); + for (Integer availableResource : enabler.getAvailableResourceIds(i)) { resourcepath="/"+availableResource; @@ -92,10 +93,10 @@ public void init(LwM2mObjectTree objectTree, DownlinkRequestReceiver requestRece 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(); diff --git a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/JavaCoapRequestBuilder.java b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/JavaCoapRequestBuilder.java index 103b5097a5..4e7964dfbd 100644 --- a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/JavaCoapRequestBuilder.java +++ b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/JavaCoapRequestBuilder.java @@ -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, @@ -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) { diff --git a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/RandomTokenGenerator.java b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/RandomTokenGenerator.java new file mode 100644 index 0000000000..6c1bb52e8a --- /dev/null +++ b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/request/RandomTokenGenerator.java @@ -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); + } +} diff --git a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/resource/ObjectResource.java b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/resource/ObjectResource.java index 8d54ac3adb..76ef139ba6 100644 --- a/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/resource/ObjectResource.java +++ b/leshan-tl-javacoap-client/src/main/java/org/eclipse/leshan/transport/javacoap/resource/ObjectResource.java @@ -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; @@ -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; @@ -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; } @@ -80,17 +68,15 @@ public CompletableFuture 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); @@ -104,24 +90,18 @@ public CompletableFuture 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) {