Search in sources :

Example 1 with SocketInterceptor

use of com.hazelcast.nio.SocketInterceptor in project hazelcast by hazelcast.

the class TcpClientConnectionManager method createSocketConnection.

@SuppressWarnings("unchecked")
protected TcpClientConnection createSocketConnection(Address target) {
    CandidateClusterContext currentClusterContext = clusterDiscoveryService.current();
    SocketChannel socketChannel = null;
    try {
        socketChannel = SocketChannel.open();
        Socket socket = socketChannel.socket();
        bindSocketToPort(socket);
        Channel channel = networking.register(currentClusterContext.getChannelInitializer(), socketChannel, true);
        channel.attributeMap().put(Address.class, target);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(target.getInetAddress(), target.getPort());
        channel.connect(inetSocketAddress, connectionTimeoutMillis);
        TcpClientConnection connection = new TcpClientConnection(client, connectionIdGen.incrementAndGet(), channel);
        socketChannel.configureBlocking(true);
        SocketInterceptor socketInterceptor = currentClusterContext.getSocketInterceptor();
        if (socketInterceptor != null) {
            socketInterceptor.onConnect(socket);
        }
        channel.start();
        return connection;
    } catch (Exception e) {
        closeResource(socketChannel);
        logger.finest(e);
        throw rethrow(e);
    }
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel) SocketInterceptor(com.hazelcast.nio.SocketInterceptor) Socket(java.net.Socket) HazelcastException(com.hazelcast.core.HazelcastException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) EOFException(java.io.EOFException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 2 with SocketInterceptor

use of com.hazelcast.nio.SocketInterceptor in project hazelcast by hazelcast.

the class ClusterDiscoveryServiceBuilder method build.

public ClusterDiscoveryService build() {
    ArrayList<CandidateClusterContext> contexts = new ArrayList<>();
    for (ClientConfig config : configs) {
        ClientNetworkConfig networkConfig = config.getNetworkConfig();
        SocketInterceptor interceptor = initSocketInterceptor(networkConfig.getSocketInterceptorConfig());
        ICredentialsFactory credentialsFactory = initCredentialsFactory(config);
        if (credentialsFactory == null) {
            credentialsFactory = new StaticCredentialsFactory(new UsernamePasswordCredentials(null, null));
        }
        credentialsFactory.configure(new ClientCallbackHandler(config));
        DiscoveryService discoveryService = initDiscoveryService(config);
        AddressProvider provider;
        if (externalAddressProvider != null) {
            provider = externalAddressProvider;
        } else {
            provider = createAddressProvider(config, discoveryService);
        }
        final SSLConfig sslConfig = networkConfig.getSSLConfig();
        final SocketOptions socketOptions = networkConfig.getSocketOptions();
        contexts.add(new CandidateClusterContext(config.getClusterName(), provider, discoveryService, credentialsFactory, interceptor, clientExtension.createChannelInitializer(sslConfig, socketOptions)));
    }
    return new ClusterDiscoveryService(unmodifiableList(contexts), configsTryCount, lifecycleService);
}
Also used : SSLConfig(com.hazelcast.config.SSLConfig) SocketOptions(com.hazelcast.client.config.SocketOptions) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(com.hazelcast.security.UsernamePasswordCredentials) DefaultAddressProvider(com.hazelcast.client.impl.spi.impl.DefaultAddressProvider) AddressProvider(com.hazelcast.client.impl.connection.AddressProvider) RemoteAddressProvider(com.hazelcast.client.impl.spi.impl.discovery.RemoteAddressProvider) ICredentialsFactory(com.hazelcast.security.ICredentialsFactory) SocketInterceptor(com.hazelcast.nio.SocketInterceptor) StaticCredentialsFactory(com.hazelcast.config.security.StaticCredentialsFactory) ClientConfig(com.hazelcast.client.config.ClientConfig) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig)

Aggregations

SocketInterceptor (com.hazelcast.nio.SocketInterceptor)2 AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)1 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)1 HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)1 SocketOptions (com.hazelcast.client.config.SocketOptions)1 CandidateClusterContext (com.hazelcast.client.impl.clientside.CandidateClusterContext)1 AddressProvider (com.hazelcast.client.impl.connection.AddressProvider)1 DefaultAddressProvider (com.hazelcast.client.impl.spi.impl.DefaultAddressProvider)1 RemoteAddressProvider (com.hazelcast.client.impl.spi.impl.discovery.RemoteAddressProvider)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 SSLConfig (com.hazelcast.config.SSLConfig)1 StaticCredentialsFactory (com.hazelcast.config.security.StaticCredentialsFactory)1 HazelcastException (com.hazelcast.core.HazelcastException)1 Channel (com.hazelcast.internal.networking.Channel)1 ICredentialsFactory (com.hazelcast.security.ICredentialsFactory)1 UsernamePasswordCredentials (com.hazelcast.security.UsernamePasswordCredentials)1 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)1