Search in sources :

Example 1 with SecurityObjectPskStore

use of org.eclipse.leshan.client.californium.impl.SecurityObjectPskStore in project leshan by eclipse.

the class LeshanClientBuilder method build.

/**
 * 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.createMandatory();
    }
    if (coapConfig == null) {
        coapConfig = createDefaultNetworkConfig();
    }
    // handle dtlsConfig
    DtlsConnectorConfig dtlsConfig = null;
    if (dtlsConfigBuilder == null) {
        dtlsConfigBuilder = new DtlsConnectorConfig.Builder();
    }
    DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig();
    // Handle PSK Store
    LwM2mObjectEnabler securityEnabler = this.objectEnablers.get(LwM2mId.SECURITY);
    if (securityEnabler == null) {
        throw new IllegalArgumentException("Security object is mandatory");
    }
    if (incompleteConfig.getPskStore() == null) {
        dtlsConfigBuilder.setPskStore(new SecurityObjectPskStore(securityEnabler));
    } else {
        LOG.warn("PskStore should be automatically set by Leshan. Using a custom implementation is not advised.");
    }
    // Handle secure address
    if (incompleteConfig.getAddress() == null) {
        if (localSecureAddress == null) {
            localSecureAddress = new InetSocketAddress(0);
        }
        dtlsConfigBuilder.setAddress(localSecureAddress);
    } else if (localSecureAddress != null && !localSecureAddress.equals(incompleteConfig.getAddress())) {
        throw new IllegalStateException(String.format("Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for secure address: %s != %s", localSecureAddress, 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);
    }
    dtlsConfig = dtlsConfigBuilder.build();
    // create endpoints
    CoapEndpoint unsecuredEndpoint = null;
    if (!noUnsecuredEndpoint) {
        if (endpointFactory != null) {
            unsecuredEndpoint = endpointFactory.createUnsecuredEndpoint(localAddress, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setInetSocketAddress(localAddress);
            builder.setNetworkConfig(coapConfig);
            unsecuredEndpoint = builder.build();
        }
    }
    CoapEndpoint securedEndpoint = null;
    if (!noSecuredEndpoint) {
        if (endpointFactory != null) {
            securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setConnector(new DTLSConnector(dtlsConfig));
            builder.setNetworkConfig(coapConfig);
            securedEndpoint = builder.build();
        }
    }
    if (securedEndpoint == null && unsecuredEndpoint == null) {
        throw new IllegalStateException("All CoAP enpoints are deactivated, at least one endpoint should be activated");
    }
    return new LeshanClient(endpoint, unsecuredEndpoint, securedEndpoint, objectEnablers, coapConfig, additionalAttributes);
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) InetSocketAddress(java.net.InetSocketAddress) Device(org.eclipse.leshan.client.object.Device) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) SecurityObjectPskStore(org.eclipse.leshan.client.californium.impl.SecurityObjectPskStore) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)1 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)1 Builder (org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)1 SecurityObjectPskStore (org.eclipse.leshan.client.californium.impl.SecurityObjectPskStore)1 Device (org.eclipse.leshan.client.object.Device)1 Server (org.eclipse.leshan.client.object.Server)1 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)1 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)1