Search in sources :

Example 1 with EndpointObserver

use of org.eclipse.californium.core.network.EndpointObserver in project californium by eclipse.

the class CoapServer method addEndpoint.

/**
 * Adds an Endpoint to the server.
 *
 * WARNING: It automatically configures the default executor of the server.
 * Endpoints that should use their own executor (e.g., to prioritize or
 * balance request handling) either set it afterwards before starting the
 * server or override the setExecutor() method of the special Endpoint.
 *
 * @param endpoint the endpoint to add
 */
@Override
public void addEndpoint(final Endpoint endpoint) {
    endpoint.setMessageDeliverer(deliverer);
    if (executor != null && secondaryExecutor != null) {
        endpoint.setExecutors(executor, secondaryExecutor);
    }
    for (EndpointObserver observer : defaultObservers) {
        endpoint.addObserver(observer);
    }
    endpoints.add(endpoint);
}
Also used : EndpointObserver(org.eclipse.californium.core.network.EndpointObserver)

Example 2 with EndpointObserver

use of org.eclipse.californium.core.network.EndpointObserver 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)

Aggregations

EndpointObserver (org.eclipse.californium.core.network.EndpointObserver)2 X509KeyManager (javax.net.ssl.X509KeyManager)1 DtlsClusterManager (org.eclipse.californium.cluster.DtlsClusterManager)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 HealthStatisticLogger (org.eclipse.californium.core.network.interceptors.HealthStatisticLogger)1 Configuration (org.eclipse.californium.elements.config.Configuration)1 DtlsClusterConnector (org.eclipse.californium.scandium.DtlsClusterConnector)1 DtlsClusterHealthLogger (org.eclipse.californium.scandium.DtlsClusterHealthLogger)1 DtlsManagedClusterConnector (org.eclipse.californium.scandium.DtlsManagedClusterConnector)1 MdcConnectionListener (org.eclipse.californium.scandium.MdcConnectionListener)1 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)1 AsyncAdvancedPskStore (org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore)1 AsyncKeyManagerCertificateProvider (org.eclipse.californium.scandium.dtls.x509.AsyncKeyManagerCertificateProvider)1 AsyncNewAdvancedCertificateVerifier (org.eclipse.californium.scandium.dtls.x509.AsyncNewAdvancedCertificateVerifier)1