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);
}
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);
}
Aggregations