Skip to content

Commit

Permalink
First draft about adding coap layer for server using java-coap
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Dec 22, 2022
1 parent 639e801 commit a45f310
Show file tree
Hide file tree
Showing 7 changed files with 883 additions and 0 deletions.
35 changes: 35 additions & 0 deletions leshan-tl-javacoap-server/logback-leshan-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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
-->
<configuration>
<!--
This file will only be used by maven by default.
If you want to use it in your IDE, just :
- use -Dlogback.configurationFile=logback-test-.xml argument
or
- put a logback-test.xml file in your classpath (it will be ignore by git)
-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %p %C{1} [%t] %m%n</pattern>
</encoder>
</appender>

<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>

77 changes: 77 additions & 0 deletions leshan-tl-javacoap-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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-server</artifactId>
<packaging>bundle</packaging>
<name>leshan - server java-coap</name>
<description>A transport implementation for leshan server 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>
</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,93 @@
/*******************************************************************************
* 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.core.endpoint.Protocol;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.request.DownlinkRequest;
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.endpoint.LwM2mServerEndpoint;
import org.eclipse.leshan.server.profile.ClientProfile;
import org.eclipse.leshan.server.request.LowerLayerConfig;

import com.mbed.coap.server.CoapServer;

public class JavaCoapServerEndpoint implements LwM2mServerEndpoint {

private final URI endpointUri;
private final CoapServer coapServer;

public JavaCoapServerEndpoint(URI endpointUri, CoapServer coapServer) {
this.endpointUri = endpointUri;
this.coapServer = coapServer;
}

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

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

@Override
public <T extends LwM2mResponse> T send(ClientProfile destination, DownlinkRequest<T> request,
LowerLayerConfig lowerLayerConfig, long timeoutInMs) throws InterruptedException {

// TODO send request using code like this ?
// from
// https://github.com/open-coap/java-coap/blob/42032086dca3bf0482d3a4461d0431c9502fcf98/example-client/src/main/java/com/mbed/coap/cli/CoapCli.java#L112-L139

// InetSocketAddress destination = new InetSocketAddress(uri.getHost(), uri.getPort());
// CoapClient cli = CoapClientBuilder.clientFor(destination, cliServer);
//
// Thread.sleep(200);
//
// String uriPath = uri.getPath().isEmpty() ? CoapConstants.WELL_KNOWN_CORE : uri.getPath();
// try {
// CoapResponse resp = cli.sendSync(CoapRequest.of(destination, Method.valueOf(method), uriPath)
// .query(uri.getQuery() == null ? "" : uri.getQuery())
// .token(System.currentTimeMillis() % 0xFFFF)
// .proxy(proxyUri)
// .blockSize(blockSize)
// .payload(payload)
// );
return null;
}

@Override
public <T extends LwM2mResponse> void send(ClientProfile destination, DownlinkRequest<T> request,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback, LowerLayerConfig lowerLayerConfig,
long timeoutInMs) {
// TODO not implemented yet
}

@Override
public void cancelRequests(String sessionID) {
// TODO not implemented yet
}

@Override
public void cancelObservation(Observation observation) {
// TODO not implemented yet
}
}
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.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
import org.eclipse.leshan.server.LeshanServer;
import org.eclipse.leshan.server.endpoint.LwM2mServerEndpoint;
import org.eclipse.leshan.server.endpoint.LwM2mServerEndpointsProvider;
import org.eclipse.leshan.server.endpoint.ServerEndpointToolbox;
import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver;
import org.eclipse.leshan.server.request.UplinkRequestReceiver;
import org.eclipse.leshan.server.security.ServerSecurityInfo;
import org.eclipse.leshan.transport.javacoap.resource.RegistrationResource;
import org.eclipse.leshan.transport.javacoap.resource.ResourcesService;

import com.mbed.coap.packet.CoapRequest;
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.server.CoapServer;
import com.mbed.coap.utils.Service;

public class JavaCoapServerEndpointsProvider implements LwM2mServerEndpointsProvider {

private CoapServer coapServer;
private final int coapPort;

private JavaCoapServerEndpoint lwm2mEndpoint;

public JavaCoapServerEndpointsProvider(int coapPort) {
this.coapPort = coapPort;
}

@Override
public void createEndpoints(UplinkRequestReceiver requestReceiver, LwM2mNotificationReceiver observationService,
ServerEndpointToolbox toolbox, ServerSecurityInfo serverSecurityInfo, LeshanServer server) {

// TODO we should get endpoint used URI dynamically in Resources
URI endpointURI = EndpointUriUtil.createUri("coap", "0.0.0.0", coapPort);

// create Resources / Routes
ResourcesService resources = ResourcesService.builder() //
.add("/rd/*", new RegistrationResource(requestReceiver, toolbox.getLinkParser(), endpointURI)).build();
coapServer = CoapServer.builder().transport(5683).route(resources).build();

lwm2mEndpoint = new JavaCoapServerEndpoint(endpointURI, coapServer);
}

protected Service<CoapRequest, CoapResponse> createRequestsHandler() {
return new Service<CoapRequest, CoapResponse>() {
@Override
public CompletableFuture<CoapResponse> apply(CoapRequest t) {
// TODO Auto-generated method stub
return null;
}
};
}

@Override
public List<LwM2mServerEndpoint> getEndpoints() {
// We support only one endpoint for now
// TODO I don't know if 1 java-coap server can support several endpoints ?
if (lwm2mEndpoint == null) {
return Collections.emptyList();
} else {
return Arrays.asList(lwm2mEndpoint);
}
}

@Override
public LwM2mServerEndpoint getEndpoint(URI uri) {
// We support only one endpoint for now
// TODO I don't know if 1 java-coap server can support several endpoints ?
if (lwm2mEndpoint != null && lwm2mEndpoint.getURI().equals(uri))
return lwm2mEndpoint;
else
return null;
}

@Override
public void start() {
try {
coapServer.start();
} catch (IllegalStateException | IOException e) {
// TODO handle this correctly
e.printStackTrace();
}
}

@Override
public void stop() {
// TODO in Leshan stop means "we can restart after a stop", so we should check what means stop for java-coap
coapServer.stop();
}

@Override
public void destroy() {
// TODO there is no destroy, so we just stop ?
coapServer.stop();
}
}
Loading

0 comments on commit a45f310

Please sign in to comment.