Search in sources :

Example 1 with DtlsClusterHealthLogger

use of org.eclipse.californium.scandium.DtlsClusterHealthLogger in project californium by eclipse.

the class ExtendedTestServer method addClusterEndpoint.

private void addClusterEndpoint(ScheduledExecutorService secondaryExecutor, InetSocketAddress dtlsInterface, int nodeId, DtlsClusterConnectorConfig clusterConfiguration, ClusterNodesProvider nodesProvider, ClusterNodesDiscover nodesDiscoverer, BaseConfig cliConfig) {
    if (nodesDiscoverer == null ^ nodesProvider != null) {
        throw new IllegalArgumentException("either nodes-provider or -dicoverer is required!");
    }
    InterfaceType interfaceType = dtlsInterface.getAddress().isLoopbackAddress() ? InterfaceType.LOCAL : InterfaceType.EXTERNAL;
    Configuration configuration = getConfig(Protocol.DTLS, interfaceType);
    String tag = "dtls:node-" + nodeId + ":" + StringUtil.toString(dtlsInterface);
    int handshakeResultDelayMillis = configuration.getTimeAsInt(DTLS_HANDSHAKE_RESULT_DELAY, TimeUnit.MILLISECONDS);
    long healthStatusIntervalMillis = configuration.get(SystemConfig.HEALTH_STATUS_INTERVAL, TimeUnit.MILLISECONDS);
    Integer cidLength = configuration.get(DtlsConfig.DTLS_CONNECTION_ID_LENGTH);
    if (cidLength == null || cidLength < 6) {
        throw new IllegalArgumentException("cid length must be at least 6 for cluster!");
    }
    initCredentials();
    DtlsConnectorConfig.Builder dtlsConfigBuilder = DtlsConnectorConfig.builder(configuration);
    if (cliConfig.clientAuth != null) {
        dtlsConfigBuilder.set(DtlsConfig.DTLS_CLIENT_AUTHENTICATION_MODE, cliConfig.clientAuth);
    }
    // set node-id in dtls-config-builder's Configuration clone
    dtlsConfigBuilder.set(DtlsConfig.DTLS_CONNECTION_ID_NODE_ID, nodeId);
    AsyncAdvancedPskStore asyncPskStore = new AsyncAdvancedPskStore(new PlugPskStore());
    asyncPskStore.setDelay(handshakeResultDelayMillis);
    dtlsConfigBuilder.setAdvancedPskStore(asyncPskStore);
    dtlsConfigBuilder.setAddress(dtlsInterface);
    X509KeyManager keyManager = SslContextUtil.getX509KeyManager(serverCredentials);
    AsyncKeyManagerCertificateProvider certificateProvider = new AsyncKeyManagerCertificateProvider(keyManager, CertificateType.RAW_PUBLIC_KEY, CertificateType.X_509);
    certificateProvider.setDelay(handshakeResultDelayMillis);
    dtlsConfigBuilder.setCertificateIdentityProvider(certificateProvider);
    AsyncNewAdvancedCertificateVerifier.Builder verifierBuilder = AsyncNewAdvancedCertificateVerifier.builder();
    if (cliConfig.trustall) {
        verifierBuilder.setTrustAllCertificates();
    } else {
        verifierBuilder.setTrustedCertificates(trustedCertificates);
    }
    verifierBuilder.setTrustAllRPKs();
    AsyncNewAdvancedCertificateVerifier verifier = verifierBuilder.build();
    verifier.setDelay(handshakeResultDelayMillis);
    dtlsConfigBuilder.setAdvancedCertificateVerifier(verifier);
    dtlsConfigBuilder.setConnectionListener(new MdcConnectionListener());
    dtlsConfigBuilder.setLoggingTag(tag);
    if (healthStatusIntervalMillis > 0) {
        DtlsClusterHealthLogger health = new DtlsClusterHealthLogger(tag);
        dtlsConfigBuilder.setHealthHandler(health);
        add(health);
        // reset to prevent active logger
        dtlsConfigBuilder.set(SystemConfig.HEALTH_STATUS_INTERVAL, 0, TimeUnit.MILLISECONDS);
    }
    DtlsConnectorConfig dtlsConnectorConfig = dtlsConfigBuilder.build();
    CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
    EndpointObserver endpointObserver = null;
    if (nodesDiscoverer != null) {
        DtlsManagedClusterConnector connector = new DtlsManagedClusterConnector(dtlsConnectorConfig, clusterConfiguration);
        final DtlsClusterManager manager = new DtlsClusterManager(connector, dtlsConnectorConfig.getConfiguration(), nodesDiscoverer, secondaryExecutor);
        builder.setConnector(connector);
        endpointObserver = new EndpointObserver() {

            @Override
            public void stopped(Endpoint endpoint) {
                manager.stop();
            }

            @Override
            public void started(Endpoint endpoint) {
                manager.start();
            }

            @Override
            public void destroyed(Endpoint endpoint) {
                manager.stop();
            }
        };
        components.add(manager);
    } else if (nodesProvider != null) {
        builder.setConnector(new DtlsClusterConnector(dtlsConnectorConfig, clusterConfiguration, nodesProvider));
    }
    // use dtls-config-builder's Configuration clone with the set node-id
    builder.setConfiguration(dtlsConnectorConfig.getConfiguration());
    CoapEndpoint endpoint = builder.build();
    if (healthStatusIntervalMillis > 0) {
        HealthStatisticLogger healthLogger = new HealthStatisticLogger(CoAP.COAP_SECURE_URI_SCHEME + "-" + nodeId, true);
        if (healthLogger.isEnabled()) {
            endpoint.addPostProcessInterceptor(healthLogger);
            add(healthLogger);
        }
    }
    if (endpointObserver != null) {
        endpoint.addObserver(endpointObserver);
    }
    addEndpoint(endpoint);
    print(endpoint, interfaceType);
}
Also used : AsyncAdvancedPskStore(org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore) AsyncKeyManagerCertificateProvider(org.eclipse.californium.scandium.dtls.x509.AsyncKeyManagerCertificateProvider) Configuration(org.eclipse.californium.elements.config.Configuration) DtlsManagedClusterConnector(org.eclipse.californium.scandium.DtlsManagedClusterConnector) DtlsClusterManager(org.eclipse.californium.cluster.DtlsClusterManager) HealthStatisticLogger(org.eclipse.californium.core.network.interceptors.HealthStatisticLogger) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Endpoint(org.eclipse.californium.core.network.Endpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) DtlsClusterConnector(org.eclipse.californium.scandium.DtlsClusterConnector) X509KeyManager(javax.net.ssl.X509KeyManager) AsyncNewAdvancedCertificateVerifier(org.eclipse.californium.scandium.dtls.x509.AsyncNewAdvancedCertificateVerifier) MdcConnectionListener(org.eclipse.californium.scandium.MdcConnectionListener) DtlsClusterHealthLogger(org.eclipse.californium.scandium.DtlsClusterHealthLogger) Endpoint(org.eclipse.californium.core.network.Endpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) EndpointObserver(org.eclipse.californium.core.network.EndpointObserver)

Example 2 with DtlsClusterHealthLogger

use of org.eclipse.californium.scandium.DtlsClusterHealthLogger in project californium by eclipse.

the class NatTestHelper method createSecureServer.

void createSecureServer(ConnectionIdGenerator... cidGenerators) throws IOException {
    MyClusterNodesProvider provider = new MyClusterNodesProvider();
    int count = 1;
    for (ConnectionIdGenerator generator : cidGenerators) {
        String tag = "server" + count;
        DtlsClusterHealthLogger health = new DtlsClusterHealthLogger(tag);
        this.serverStatistics.add(health);
        TestUtilPskStore pskStore = new TestUtilPskStore();
        pskStore.set(IDENITITY, KEY.getBytes());
        pskStore.setCatchAll(true);
        DtlsConnectorConfig dtlsConfig = DtlsConnectorConfig.builder(config).set(DtlsConfig.DTLS_ROLE, DtlsRole.SERVER_ONLY).set(DtlsConfig.DTLS_MAX_CONNECTIONS, 10000).set(DtlsConfig.DTLS_STALE_CONNECTION_THRESHOLD, 20, TimeUnit.SECONDS).set(DtlsConfig.DTLS_RECEIVER_THREAD_COUNT, 2).set(DtlsConfig.DTLS_CONNECTOR_THREAD_COUNT, 4).set(DtlsConfig.DTLS_VERIFY_PEERS_ON_RESUMPTION_THRESHOLD, 100).setAddress(TestTools.LOCALHOST_EPHEMERAL).setLoggingTag(tag).setHealthHandler(health).setConnectionIdGenerator(generator).setAdvancedPskStore(pskStore).build();
        DebugConnectionStore serverConnectionStore = ConnectorHelper.createDebugConnectionStore(dtlsConfig);
        this.serverConnections.add(serverConnectionStore);
        CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
        if (generator instanceof NodeConnectionIdGenerator) {
            DtlsClusterConnectorConfig.Builder clusterConfigBuilder = DtlsClusterConnectorConfig.builder();
            clusterConfigBuilder.setAddress(TestTools.LOCALHOST_EPHEMERAL);
            DtlsManagedClusterConnector serverConnector = new MyDtlsClusterConnector(dtlsConfig, clusterConfigBuilder.build(), serverConnectionStore);
            serverConnector.setClusterNodesProvider(provider);
            provider.add(serverConnector);
            serverConnector.setAlertHandler(new MyAlertHandler(dtlsConfig.getLoggingTag()));
            builder.setConnector(serverConnector);
        } else {
            DTLSConnector serverConnector = new MyDtlsConnector(dtlsConfig, serverConnectionStore);
            serverConnector.setAlertHandler(new MyAlertHandler(dtlsConfig.getLoggingTag()));
            builder.setConnector(serverConnector);
        }
        builder.setConfiguration(config);
        CoapEndpoint serverEndpoint = builder.build();
        HealthStatisticLogger healthLogger = new HealthStatisticLogger(tag, true);
        serverCoapStatistics.add(healthLogger);
        serverEndpoint.addPostProcessInterceptor(healthLogger);
        serverEndpoints.add(serverEndpoint);
        CoapServer server = new CoapServer();
        server.addEndpoint(serverEndpoint);
        resource = new MyResource(TARGET);
        server.add(resource);
        server.start();
        servers.add(server);
        ++count;
    }
    createLoadBalancer();
    System.out.println("coap-server " + uri);
}
Also used : DtlsManagedClusterConnector(org.eclipse.californium.scandium.DtlsManagedClusterConnector) CoapServer(org.eclipse.californium.core.CoapServer) DtlsClusterHealthLogger(org.eclipse.californium.scandium.DtlsClusterHealthLogger) HealthStatisticLogger(org.eclipse.californium.core.network.interceptors.HealthStatisticLogger) Endpoint(org.eclipse.californium.core.network.Endpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) NodeConnectionIdGenerator(org.eclipse.californium.scandium.dtls.NodeConnectionIdGenerator) SingleNodeConnectionIdGenerator(org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator) NodeConnectionIdGenerator(org.eclipse.californium.scandium.dtls.NodeConnectionIdGenerator) ConnectionIdGenerator(org.eclipse.californium.scandium.dtls.ConnectionIdGenerator) SingleNodeConnectionIdGenerator(org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator) DebugConnectionStore(org.eclipse.californium.scandium.dtls.DebugConnectionStore) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) DtlsClusterConnectorConfig(org.eclipse.californium.scandium.config.DtlsClusterConnectorConfig)

Aggregations

CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)2 Endpoint (org.eclipse.californium.core.network.Endpoint)2 HealthStatisticLogger (org.eclipse.californium.core.network.interceptors.HealthStatisticLogger)2 DtlsClusterHealthLogger (org.eclipse.californium.scandium.DtlsClusterHealthLogger)2 DtlsManagedClusterConnector (org.eclipse.californium.scandium.DtlsManagedClusterConnector)2 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)2 X509KeyManager (javax.net.ssl.X509KeyManager)1 DtlsClusterManager (org.eclipse.californium.cluster.DtlsClusterManager)1 CoapServer (org.eclipse.californium.core.CoapServer)1 EndpointObserver (org.eclipse.californium.core.network.EndpointObserver)1 Configuration (org.eclipse.californium.elements.config.Configuration)1 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)1 DtlsClusterConnector (org.eclipse.californium.scandium.DtlsClusterConnector)1 MdcConnectionListener (org.eclipse.californium.scandium.MdcConnectionListener)1 DtlsClusterConnectorConfig (org.eclipse.californium.scandium.config.DtlsClusterConnectorConfig)1 ConnectionIdGenerator (org.eclipse.californium.scandium.dtls.ConnectionIdGenerator)1 DebugConnectionStore (org.eclipse.californium.scandium.dtls.DebugConnectionStore)1 NodeConnectionIdGenerator (org.eclipse.californium.scandium.dtls.NodeConnectionIdGenerator)1 SingleNodeConnectionIdGenerator (org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator)1 AsyncAdvancedPskStore (org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore)1