Skip to content

Commit

Permalink
Add option to use java-coap for COAP protocol with leshan-client-demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Oct 18, 2023
1 parent 8b9656a commit ee2ccdd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions leshan-client-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Contributors:
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-client-cf</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-tl-javacoap-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.californium.elements.config.Configuration;
Expand All @@ -43,11 +44,13 @@
import org.eclipse.leshan.client.LeshanClientBuilder;
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpointFactory;
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpointsProvider;
import org.eclipse.leshan.client.californium.endpoint.ClientProtocolProvider;
import org.eclipse.leshan.client.californium.endpoint.coap.CoapOscoreProtocolProvider;
import org.eclipse.leshan.client.californium.endpoint.coaps.CoapsClientEndpointFactory;
import org.eclipse.leshan.client.californium.endpoint.coaps.CoapsClientProtocolProvider;
import org.eclipse.leshan.client.demo.cli.LeshanClientDemoCLI;
import org.eclipse.leshan.client.demo.cli.interactive.InteractiveCommands;
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpointsProvider;
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory;
import org.eclipse.leshan.client.object.LwM2mTestObject;
import org.eclipse.leshan.client.object.Oscore;
Expand All @@ -65,6 +68,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.client.endpoint.JavaCoapClientEndpointsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -259,8 +263,13 @@ protected DtlsConnectorConfig.Builder createRootDtlsConnectorConfigBuilder(
};

// Create client endpoints Provider
List<ClientProtocolProvider> protocolProvider = new ArrayList<>();
if (!cli.main.useJavaCoap) {
protocolProvider.add(new CoapOscoreProtocolProvider());
}
protocolProvider.add(customCoapsProtocolProvider);
CaliforniumClientEndpointsProvider.Builder endpointsBuilder = new CaliforniumClientEndpointsProvider.Builder(
new CoapOscoreProtocolProvider(), customCoapsProtocolProvider);
protocolProvider.toArray(new ClientProtocolProvider[protocolProvider.size()]));

// Create Californium Configuration
Configuration clientCoapConfig = endpointsBuilder.createDefaultConfiguration();
Expand All @@ -285,13 +294,20 @@ protected DtlsConnectorConfig.Builder createRootDtlsConnectorConfigBuilder(

// Set Californium Configuration
endpointsBuilder.setConfiguration(clientCoapConfig);

endpointsBuilder.setClientAddress(cli.main.localAddress);

// creates EndpointsProvider
List<LwM2mClientEndpointsProvider> endpointsProvider = new ArrayList<>();
endpointsProvider.add(endpointsBuilder.build());
if (cli.main.useJavaCoap) {
endpointsProvider.add(new JavaCoapClientEndpointsProvider());
}

// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(cli.main.endpoint);
builder.setObjects(enablers);
builder.setEndpointsProviders(endpointsBuilder.build());
builder.setEndpointsProviders(
endpointsProvider.toArray(new LwM2mClientEndpointsProvider[endpointsProvider.size()]));
builder.setDataSenders(new ManualDataSender());
if (cli.identity.isx509())
builder.setTrustStore(cli.identity.getX509().trustStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public static class GeneralSection {
"Activate support of old/unofficial content format.", //
"See https://github.com/eclipse/leshan/pull/720" })
public boolean supportOldFormat;

@Option(names = { "-jc", "--use-java-coap" },
description = { //
"Use java-coap for CoAP protocol instead of Californium." })
public boolean useJavaCoap;
}

/* ********************************** Location Section ******************************** */
Expand Down

0 comments on commit ee2ccdd

Please sign in to comment.