org.eclipse.leshan.client.californium.LeshanClientBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leshan-all Show documentation
Show all versions of leshan-all Show documentation
A LWM2M client and server based on Californium (CoAP) all in one.
The newest version!
/*******************************************************************************
* Copyright (c) 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 v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.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.californium;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.network.config.NetworkConfig.Keys;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder;
import org.eclipse.leshan.LwM2mId;
import org.eclipse.leshan.client.object.Device;
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.util.Validate;
/**
* Helper class to build and configure a Californium based Leshan Lightweight M2M client.
*/
public class LeshanClientBuilder {
private final String endpoint;
private InetSocketAddress localAddress;
private List extends LwM2mObjectEnabler> objectEnablers;
private NetworkConfig coapConfig;
private Builder dtlsConfigBuilder;
private EndpointFactory endpointFactory;
private Map additionalAttributes;
/**
* Creates a new instance for setting the configuration options for a {@link LeshanClient} instance.
*
* The builder is initialized with the following default values:
*
* - local address: a local address and an ephemeral port (picked up during binding)
* - object enablers:
*
* - Security(0) with one instance (DM server security): uri=coap://leshan.eclipse.org:5683, mode=NoSec
*
* - Server(1) with one instance (DM server): id=12345, lifetime=5minutes
* - Device(3): manufacturer=Eclipse Leshan, modelNumber=model12345, serialNumber=12345
*
*
*
*
* @param endpoint the end-point to identify the client on the server
*/
public LeshanClientBuilder(String endpoint) {
Validate.notEmpty(endpoint);
this.endpoint = endpoint;
}
/**
* Sets the local non-secure end-point address
*/
public LeshanClientBuilder setLocalAddress(String hostname, int port) {
if (hostname == null) {
this.localAddress = new InetSocketAddress(port);
} else {
this.localAddress = new InetSocketAddress(hostname, port);
}
return this;
}
/**
*
* Sets the list of objects enablers
*
* Warning : The Security ObjectEnabler should not contains 2 or more entries with the same identity. This is not a
* LWM2M specification constraint but an implementation limitation.
*/
public LeshanClientBuilder setObjects(List extends LwM2mObjectEnabler> objectEnablers) {
this.objectEnablers = objectEnablers;
return this;
}
/**
* Set the Californium/CoAP {@link NetworkConfig}.
*/
public LeshanClientBuilder setCoapConfig(NetworkConfig config) {
this.coapConfig = config;
return this;
}
/**
* Set the Scandium/DTLS Configuration : {@link DtlsConnectorConfig}.
*/
public LeshanClientBuilder setDtlsConfig(DtlsConnectorConfig.Builder config) {
this.dtlsConfigBuilder = config;
return this;
}
/**
* Used to create custom CoAP endpoint, this is only for advanced users.
* DTLSConnector is expected for secured endpoint.
*/
public LeshanClientBuilder setEndpointFactory(EndpointFactory endpointFactory) {
this.endpointFactory = endpointFactory;
return this;
}
/**
* Set the additionalAttributes for {@link org.eclipse.leshan.core.request.RegisterRequest}.
*/
public LeshanClientBuilder setAdditionalAttributes(Map additionalAttributes) {
this.additionalAttributes = additionalAttributes;
return this;
}
public static NetworkConfig createDefaultNetworkConfig() {
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.set(Keys.MID_TRACKER, "NULL");
networkConfig.set(Keys.MAX_ACTIVE_PEERS, 10);
networkConfig.set(Keys.PROTOCOL_STAGE_THREAD_COUNT, 1);
// Workaround for https://github.com/eclipse/leshan/issues/502
// TODO remove this line when we will integrate Cf 2.0.0-M10
networkConfig.set(Keys.HEALTH_STATUS_INTERVAL, 0);
return networkConfig;
}
/**
* Creates an instance of {@link LeshanClient} based on the properties set on this builder.
*/
public LeshanClient build() {
if (localAddress == null) {
localAddress = new InetSocketAddress(0);
}
if (objectEnablers == null) {
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY,
Security.noSec("coap://leshan.eclipse.org:5683", 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, 5 * 60, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", "model12345", "12345", "U"));
objectEnablers = initializer.createAll();
}
if (coapConfig == null) {
coapConfig = createDefaultNetworkConfig();
}
// handle dtlsConfig
if (dtlsConfigBuilder == null) {
dtlsConfigBuilder = new DtlsConnectorConfig.Builder();
}
DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig();
// Handle secure address
if (incompleteConfig.getAddress() == null) {
if (localAddress == null) {
localAddress = new InetSocketAddress(0);
}
dtlsConfigBuilder.setAddress(localAddress);
} else if (localAddress != null && !localAddress.equals(incompleteConfig.getAddress())) {
throw new IllegalStateException(String.format(
"Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for address: %s != %s",
localAddress, incompleteConfig.getAddress()));
}
// Handle active peers
if (incompleteConfig.getMaxConnections() == null)
dtlsConfigBuilder.setMaxConnections(coapConfig.getInt(Keys.MAX_ACTIVE_PEERS));
if (incompleteConfig.getStaleConnectionThreshold() == null)
dtlsConfigBuilder.setStaleConnectionThreshold(coapConfig.getLong(Keys.MAX_PEER_INACTIVITY_PERIOD));
// Use only 1 thread to handle DTLS connection by default
if (incompleteConfig.getConnectionThreadCount() == null) {
dtlsConfigBuilder.setConnectionThreadCount(1);
}
// Deactivate SNI by default
// TODO should we support SNI ?
if (incompleteConfig.isSniEnabled() == null) {
dtlsConfigBuilder.setSniEnabled(false);
}
return new LeshanClient(endpoint, localAddress, objectEnablers, coapConfig, dtlsConfigBuilder, endpointFactory,
additionalAttributes);
}
}