Skip to content

Commit

Permalink
Leshan client with java-coap stack - initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawLegierski committed Jan 4, 2023
1 parent 425d062 commit 7f48980
Show file tree
Hide file tree
Showing 11 changed files with 1,414 additions and 1 deletion.
6 changes: 6 additions & 0 deletions leshan-client-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Contributors:
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core-demo</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-tl-javacoap-client</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder;
import org.eclipse.leshan.transport.javacoap.endpoint.JavaCoapClientEndpointsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -256,6 +257,10 @@ protected DtlsConnectorConfig.Builder createDtlsConnectorConfigBuilder(
CaliforniumClientEndpointsProvider.Builder endpointsBuilder = new CaliforniumClientEndpointsProvider.Builder(
new CoapOscoreProtocolProvider(), customCoapsProtocolProvider);

// test java-coap EndpointProvider
JavaCoapClientEndpointsProvider endpointBuilder = new JavaCoapClientEndpointsProvider(5683);


// Create Californium Configuration
Configuration clientCoapConfig = endpointsBuilder.createDefaultConfiguration();

Expand Down Expand Up @@ -285,7 +290,9 @@ protected DtlsConnectorConfig.Builder createDtlsConnectorConfigBuilder(
// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(cli.main.endpoint);
builder.setObjects(enablers);
builder.setEndpointsProvider(endpointsBuilder.build());
//builder.setEndpointsProvider(endpointsBuilder.build());
// test java-coap
builder.setEndpointsProvider(endpointBuilder);
builder.setDataSenders(new ManualDataSender());
if (cli.identity.isx509())
builder.setTrustStore(cli.identity.getX509().trustStore);
Expand Down
89 changes: 89 additions & 0 deletions leshan-tl-javacoap-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2013-2015 Sierra Wireless and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v20.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
Bosch Software Innovations GmbH - OSGi support
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.leshan</groupId>
<artifactId>lib-build-config</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../build-config/lib-build-config/pom.xml</relativePath>
</parent>
<artifactId>leshan-tl-javacoap-client</artifactId>
<packaging>bundle</packaging>
<name>leshan - client java-coap</name>
<description>A transport implementation for leshan client based on Java CoAP</description>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-core</artifactId>
</dependency>

<dependency>
<groupId>com.github.open-coap.java-coap</groupId>
<artifactId>coap-core</artifactId>
<version>6.5.0</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-client-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core-cf</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-client-cf</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- We need to launch each tests in its own JVM to be able to check number
of active threads in LeshanBootstrapServerTest and LeshanServerTest -->
<reuseForks>false</reuseForks>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*******************************************************************************
* Copyright (c) 2022 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.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.transport.javacoap.endpoint;

import java.net.URI;

import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox;
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.request.UplinkRequest;
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.transport.javacoap.request.JavaCoapClientCoapMessageTranslator;

import com.mbed.coap.client.CoapClient;
import com.mbed.coap.exception.CoapException;
import com.mbed.coap.packet.CoapRequest;
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.server.CoapServer;

public class JavaCoapClientEndpoint implements LwM2mClientEndpoint {

private final URI endpointUri;
private final CoapClient client;
private final JavaCoapClientCoapMessageTranslator translator;
private final ClientEndpointToolbox toolbox;
private final LwM2mModel model;



public JavaCoapClientEndpoint(URI endpointUri, CoapServer coapServer, CoapClient client,
JavaCoapClientCoapMessageTranslator translator, ClientEndpointToolbox toolbox, LwM2mModel model)
{
this.endpointUri = endpointUri;
this.client = client;
this.translator = translator;
this.toolbox = toolbox;
this.model = model;

}

@Override public Protocol getProtocol() {
return Protocol.COAP;
}

@Override public URI getURI() {
return endpointUri;
}

@Override public void forceReconnection(ServerIdentity server, boolean resume) {

}

@Override public long getMaxCommunicationPeriodFor(long lifetimeInMs){
// See https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/283 to better understand.
// TODO For DTLS, worst Handshake scenario should be taking into account too.

int floor = 30000; // value from which we stop to adjust communication period using COAP EXCHANGE LIFETIME.

// To be sure registration doesn't expired, update request should be send considering all CoAP retransmissions
// and registration lifetime.
// See https://tools.ietf.org/html/rfc7252#section-4.8.2
//long exchange_lifetime = lwm2mendpoint.getConfig().get(CoapConfig.EXCHANGE_LIFETIME, TimeUnit.MILLISECONDS);

long exchange_lifetime = 247000;
if (lifetimeInMs - exchange_lifetime >= floor) {
return lifetimeInMs - exchange_lifetime;
} else {
// LOG.warn("Too small lifetime : we advice to not use a lifetime < (COAP EXCHANGE LIFETIME + 30s)");
// lifetime value is too short, so we do a compromise and we don't remove COAP EXCHANGE LIFETIME completely
// We distribute the remaining lifetime range [0, exchange_lifetime + floor] on the remaining range
// [1,floor]s.
return lifetimeInMs * (floor - 1000) / (exchange_lifetime + floor) + 1000;
}
}

@Override public <T extends LwM2mResponse> T send(ServerIdentity server, UplinkRequest<T> request, long timeoutInMs)
throws InterruptedException {

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

CoapResponse coapResponse = null;

// Send CoAP request synchronously
try {
coapResponse = client.sendSync(coapRequest);
} catch (CoapException e) {
e.printStackTrace();
}


return translator.createLwM2mResponse(server, request, coapRequest, coapResponse, toolbox);

}

@Override
public <T extends LwM2mResponse> void send(ServerIdentity server, UplinkRequest<T> request,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback, long timeoutInMs) {



}
}
Loading

0 comments on commit 7f48980

Please sign in to comment.