Search in sources :

Example 1 with Server

use of org.eclipse.leshan.client.object.Server in project leshan by eclipse.

the class LeshanClientDemo method createAndStartClient.

public static void createAndStartClient(String endpoint, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, boolean needBootstrap, String serverURI, byte[] pskIdentity, byte[] pskKey, Float latitude, Float longitude, float scaleFactor) {
    locationInstance = new MyLocation(latitude, longitude, scaleFactor);
    // Initialize model
    List<ObjectModel> models = ObjectLoader.loadDefault();
    models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
    // Initialize object list
    ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
    if (needBootstrap) {
        if (pskIdentity == null)
            initializer.setInstancesForObject(SECURITY, noSecBootstap(serverURI));
        else
            initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskIdentity, pskKey));
    } else {
        if (pskIdentity == null) {
            initializer.setInstancesForObject(SECURITY, noSec(serverURI, 123));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        } else {
            initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskIdentity, pskKey));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        }
    }
    initializer.setClassForObject(DEVICE, MyDevice.class);
    initializer.setInstancesForObject(LOCATION, locationInstance);
    initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
    List<LwM2mObjectEnabler> enablers = initializer.create(SECURITY, SERVER, DEVICE, LOCATION, OBJECT_ID_TEMPERATURE_SENSOR);
    // Create CoAP Config
    NetworkConfig coapConfig;
    File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
    if (configFile.isFile()) {
        coapConfig = new NetworkConfig();
        coapConfig.load(configFile);
    } else {
        coapConfig = LeshanClientBuilder.createDefaultNetworkConfig();
        coapConfig.store(configFile);
    }
    // Create client
    LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
    builder.setLocalAddress(localAddress, localPort);
    builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
    builder.setObjects(enablers);
    builder.setCoapConfig(coapConfig);
    // so we can disable the other one.
    if (!needBootstrap) {
        if (pskIdentity == null)
            builder.disableSecuredEndpoint();
        else
            builder.disableUnsecuredEndpoint();
    }
    final LeshanClient client = builder.build();
    LOG.info("Press 'w','a','s','d' to change reported Location ({},{}).", locationInstance.getLatitude(), locationInstance.getLongitude());
    // Start the client
    client.start();
    // De-register on shutdown and stop client.
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // send de-registration request before destroy
            client.destroy(true);
        }
    });
    // Change the location through the Console
    try (Scanner scanner = new Scanner(System.in)) {
        while (scanner.hasNext()) {
            String nextMove = scanner.next();
            locationInstance.moveLocation(nextMove);
        }
    }
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Scanner(java.util.Scanner) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) File(java.io.File) LeshanClient(org.eclipse.leshan.client.californium.LeshanClient)

Example 2 with Server

use of org.eclipse.leshan.client.object.Server in project leshan by eclipse.

the class SecureIntegrationTestHelper method createPSKClient.

public void createPSKClient() {
    ObjectsInitializer initializer = new ObjectsInitializer();
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.psk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, GOOD_PSK_ID.getBytes(StandardCharsets.UTF_8), GOOD_PSK_KEY));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
    List<LwM2mObjectEnabler> objects = initializer.createMandatory();
    objects.add(initializer.create(2));
    InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
    LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
    builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
    builder.setObjects(objects);
    client = builder.build();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) CoapServer(org.eclipse.californium.core.CoapServer) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) Device(org.eclipse.leshan.client.object.Device) InetSocketAddress(java.net.InetSocketAddress) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder)

Example 3 with Server

use of org.eclipse.leshan.client.object.Server 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)

Example 4 with Server

use of org.eclipse.leshan.client.object.Server in project leshan by eclipse.

the class QueueModeIntegrationTestHelper method createClient.

@Override
public void createClient() {
    // Create objects Enabler
    ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(), 12345));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.UQ, false));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "UQ") {

        @Override
        public ExecuteResponse execute(int resourceid, String params) {
            if (resourceid == 4) {
                return ExecuteResponse.success();
            } else {
                return super.execute(resourceid, params);
            }
        }
    });
    List<LwM2mObjectEnabler> objects = initializer.createMandatory();
    objects.addAll(initializer.create(2, 2000));
    // Build Client
    LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
    builder.setObjects(objects);
    client = builder.build();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) Device(org.eclipse.leshan.client.object.Device) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Example 5 with Server

use of org.eclipse.leshan.client.object.Server in project leshan by eclipse.

the class IntegrationTestHelper method createClient.

public void createClient() {
    // Create objects Enabler
    ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(), 12345));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U") {

        @Override
        public ExecuteResponse execute(int resourceid, String params) {
            if (resourceid == 4) {
                return ExecuteResponse.success();
            } else {
                return super.execute(resourceid, params);
            }
        }
    });
    List<LwM2mObjectEnabler> objects = initializer.createMandatory();
    objects.addAll(initializer.create(2, 2000));
    // Build Client
    LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
    builder.setObjects(objects);
    client = builder.build();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Server(org.eclipse.leshan.client.object.Server) LeshanServer(org.eclipse.leshan.server.californium.impl.LeshanServer) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) Device(org.eclipse.leshan.client.object.Device) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Aggregations

Server (org.eclipse.leshan.client.object.Server)7 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)7 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)7 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)6 Device (org.eclipse.leshan.client.object.Device)6 InetSocketAddress (java.net.InetSocketAddress)4 CoapServer (org.eclipse.californium.core.CoapServer)3 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)3 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)3 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)3 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)3 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)3 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)2 File (java.io.File)1 Scanner (java.util.Scanner)1 Builder (org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)1 LeshanClient (org.eclipse.leshan.client.californium.LeshanClient)1 SecurityObjectPskStore (org.eclipse.leshan.client.californium.impl.SecurityObjectPskStore)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 LeshanServer (org.eclipse.leshan.server.californium.impl.LeshanServer)1