Skip to content

Commit

Permalink
Add support of multiple endpoint provider at client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Oct 18, 2023
1 parent 3c4651d commit 73b6c86
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public CoapServer getCoapServer() {

@Override
public LwM2mClientEndpoint getEndpoint(LwM2mServer server) {
if (currentServer.equals(server)) {
if (currentServer != null && currentServer.equals(server)) {
return endpoint;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.leshan.client.bootstrap.BootstrapConsistencyChecker;
import org.eclipse.leshan.client.bootstrap.DefaultBootstrapConsistencyChecker;
import org.eclipse.leshan.client.endpoint.DefaultCompositeClientEndpointsProvider;
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpointsProvider;
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory;
import org.eclipse.leshan.client.engine.RegistrationEngine;
Expand Down Expand Up @@ -266,10 +268,28 @@ public LeshanClientBuilder setSharedExecutor(ScheduledExecutorService executor)
}

/**
* @return the builder for fluent client creation.
* By default LeshanClient doesn't support any protocol. Users need to provide 1 or several
* {@link LwM2mClientEndpointsProvider} implementation.
* <p>
* Leshan project provides {@code coap} and {@code coaps} support based on Californium/Scandium in
* <strong>leshan-client-cf</strong>.
*/
public LeshanClientBuilder setEndpointsProviders(LwM2mClientEndpointsProvider... endpointsProvider) {
return setEndpointsProviders(Arrays.asList(endpointsProvider));
}

/**
* @see #setEndpointsProviders(LwM2mClientEndpointsProvider...)
*/
public LeshanClientBuilder setEndpointsProvider(LwM2mClientEndpointsProvider endpointsProvider) {
this.endpointsProvider = endpointsProvider;
public LeshanClientBuilder setEndpointsProviders(Collection<LwM2mClientEndpointsProvider> providers) {
if (providers == null || providers.isEmpty()) {
throw new IllegalStateException("At least one endpoint provider should be set");
}
if (providers.size() == 1) {
endpointsProvider = providers.iterator().next();
} else {
endpointsProvider = new DefaultCompositeClientEndpointsProvider(providers);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2023 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.client.endpoint;

import java.util.Collection;

/**
* A {@link LwM2mClientEndpointsProvider} composed of several internal {@link LwM2mClientEndpointsProvider}.
* <p>
* Implementation should allow to use several {@link LwM2mClientEndpointsProvider}.
*
* @see DefaultCompositeClientEndpointsProvider
*/
public interface CompositeClientEndpointsProvider extends LwM2mClientEndpointsProvider {
Collection<LwM2mClientEndpointsProvider> getProviders();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*******************************************************************************
* Copyright (c) 2023 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.client.endpoint;

import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.leshan.client.request.DownlinkRequestReceiver;
import org.eclipse.leshan.client.resource.LwM2mObjectTree;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.client.servers.ServerInfo;

/**
* Default implementation of {@link CompositeClientEndpointsProvider}.
* <p>
* It allows to use several {@link LwM2mClientEndpointsProvider} on same Leshan server.
*/
public class DefaultCompositeClientEndpointsProvider implements CompositeClientEndpointsProvider {

private final List<LwM2mClientEndpointsProvider> providers;

public DefaultCompositeClientEndpointsProvider(LwM2mClientEndpointsProvider... providers) {
this(Arrays.asList(providers));
}

public DefaultCompositeClientEndpointsProvider(Collection<LwM2mClientEndpointsProvider> providers) {
this.providers = Collections.unmodifiableList(new ArrayList<>(providers));
}

@Override
public void init(LwM2mObjectTree objectTree, DownlinkRequestReceiver requestReceiver,
ClientEndpointToolbox toolbox) {
for (LwM2mClientEndpointsProvider provider : providers) {
provider.init(objectTree, requestReceiver, toolbox);
}
}

@Override
public LwM2mServer createEndpoint(ServerInfo serverInfo, boolean clientInitiatedOnly, List<Certificate> trustStore,
ClientEndpointToolbox toolbox) {
for (LwM2mClientEndpointsProvider provider : providers) {
LwM2mServer server = provider.createEndpoint(serverInfo, clientInitiatedOnly, trustStore, toolbox);
if (server != null) {
return server;
}
}
return null;
}

@Override
public Collection<LwM2mServer> createEndpoints(Collection<? extends ServerInfo> serverInfo,
boolean clientInitiatedOnly, List<Certificate> trustStore, ClientEndpointToolbox toolbox) {
// not implemented yet ...
return null;
}

@Override
public void destroyEndpoints() {
for (LwM2mClientEndpointsProvider provider : providers) {
provider.destroyEndpoints();
}
}

@Override
public void start() {
for (LwM2mClientEndpointsProvider provider : providers) {
provider.start();
}
}

@Override
public List<LwM2mClientEndpoint> getEndpoints() {
List<LwM2mClientEndpoint> endpoints = new ArrayList<>();
for (LwM2mClientEndpointsProvider provider : providers) {
endpoints.addAll(provider.getEndpoints());
}
return endpoints;
}

@Override
public LwM2mClientEndpoint getEndpoint(LwM2mServer server) {
for (LwM2mClientEndpointsProvider provider : providers) {
LwM2mClientEndpoint endpoint = provider.getEndpoint(server);
if (endpoint != null) {
return endpoint;
}
}
return null;
}

@Override
public void stop() {
for (LwM2mClientEndpointsProvider provider : providers) {
provider.stop();
}

}

@Override
public void destroy() {
for (LwM2mClientEndpointsProvider provider : providers) {
provider.destroy();
}
}

@Override
public Collection<LwM2mClientEndpointsProvider> getProviders() {
return providers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ protected DtlsConnectorConfig.Builder createRootDtlsConnectorConfigBuilder(
// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(cli.main.endpoint);
builder.setObjects(enablers);
builder.setEndpointsProvider(endpointsBuilder.build());
builder.setEndpointsProviders(endpointsBuilder.build());
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 @@ -147,7 +147,7 @@ protected Builder createEffectiveDtlsConnectorConfigBuilder(InetSocketAddress ad
};
CaliforniumClientEndpointsProvider endpointsProvider = new CaliforniumClientEndpointsProvider.Builder(
coapsProtocolProvider).build();
givenClient.setEndpointsProvider(endpointsProvider);
givenClient.setEndpointsProviders(endpointsProvider);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,17 @@ public static LeshanTestClientBuilder givenClient() {
}

public LeshanTestClientBuilder with(LwM2mClientEndpointsProvider endpointsProvider) {
setEndpointsProvider(endpointsProvider);
setEndpointsProviders(endpointsProvider);
return this;
}

public LeshanTestClientBuilder with(String endpointProvider) {
if (endpointProvider.equals("Californium")) {
setEndpointsProvider(
setEndpointsProviders(
new CaliforniumClientEndpointsProvider.Builder(getCaliforniumProtocolProvider(protocolToUse))
.build());
} else if (endpointProvider.equals("Californium-OSCORE")) {
setEndpointsProvider(new CaliforniumClientEndpointsProvider.Builder(
setEndpointsProviders(new CaliforniumClientEndpointsProvider.Builder(
getCaliforniumProtocolProviderSupportingOscore(protocolToUse)).build());
}
return this;
Expand Down

0 comments on commit 73b6c86

Please sign in to comment.