Search in sources :

Example 1 with SocketChannelWrapper

use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.

the class ClientConnectionManagerImpl method createSocketConnection.

protected ClientConnection createSocketConnection(final Address address) throws IOException {
    if (!alive) {
        throw new HazelcastException("ConnectionManager is not active!");
    }
    SocketChannel socketChannel = null;
    try {
        socketChannel = SocketChannel.open();
        Socket socket = socketChannel.socket();
        socket.setKeepAlive(socketOptions.isKeepAlive());
        socket.setTcpNoDelay(socketOptions.isTcpNoDelay());
        socket.setReuseAddress(socketOptions.isReuseAddress());
        if (socketOptions.getLingerSeconds() > 0) {
            socket.setSoLinger(true, socketOptions.getLingerSeconds());
        }
        int bufferSize = getBufferSize();
        socket.setSendBufferSize(bufferSize);
        socket.setReceiveBufferSize(bufferSize);
        InetSocketAddress inetSocketAddress = address.getInetSocketAddress();
        socketChannel.socket().connect(inetSocketAddress, connectionTimeout);
        SocketChannelWrapper socketChannelWrapper = socketChannelWrapperFactory.wrapSocketChannel(socketChannel, true);
        final ClientConnection clientConnection = new ClientConnection(client, ioThreadingModel, connectionIdGen.incrementAndGet(), socketChannelWrapper);
        socketChannel.configureBlocking(true);
        if (socketInterceptor != null) {
            socketInterceptor.onConnect(socket);
        }
        socketChannel.configureBlocking(ioThreadingModel.isBlocking());
        socket.setSoTimeout(0);
        clientConnection.start();
        return clientConnection;
    } catch (Exception e) {
        if (socketChannel != null) {
            socketChannel.close();
        }
        throw ExceptionUtil.rethrow(e, IOException.class);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HazelcastException(com.hazelcast.core.HazelcastException) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException)

Example 2 with SocketChannelWrapper

use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.

the class SocketAcceptorThread method acceptSocket.

private void acceptSocket() {
    SocketChannelWrapper socketChannelWrapper = null;
    try {
        final SocketChannel socketChannel = serverSocketChannel.accept();
        if (socketChannel != null) {
            socketChannelWrapper = connectionManager.wrapSocketChannel(socketChannel, false);
        }
    } catch (Exception e) {
        exceptionCount.inc();
        if (e instanceof ClosedChannelException && !connectionManager.isLive()) {
            // ClosedChannelException
            // or AsynchronousCloseException
            // or ClosedByInterruptException
            logger.finest("Terminating socket acceptor thread...", e);
        } else {
            logger.severe("Unexpected error while accepting connection! " + e.getClass().getName() + ": " + e.getMessage());
            try {
                serverSocketChannel.close();
            } catch (Exception ex) {
                logger.finest("Closing server socket failed", ex);
            }
            ioService.onFatalError(e);
        }
    }
    if (socketChannelWrapper != null) {
        final SocketChannelWrapper socketChannel = socketChannelWrapper;
        logger.info("Accepting socket connection from " + socketChannel.socket().getRemoteSocketAddress());
        if (connectionManager.isSocketInterceptorEnabled()) {
            configureAndAssignSocket(socketChannel);
        } else {
            ioService.executeAsync(new Runnable() {

                @Override
                public void run() {
                    configureAndAssignSocket(socketChannel);
                }
            });
        }
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException)

Example 3 with SocketChannelWrapper

use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.

the class TcpIpConnectionManager method stop.

@Override
public synchronized void stop() {
    if (!live) {
        return;
    }
    live = false;
    logger.finest("Stopping ConnectionManager");
    shutdownAcceptorThread();
    for (SocketChannelWrapper socketChannel : acceptedSockets) {
        closeResource(socketChannel);
    }
    for (Connection conn : connectionsMap.values()) {
        destroySilently(conn, "TcpIpConnectionManager is stopping");
    }
    for (TcpIpConnection conn : activeConnections) {
        destroySilently(conn, "TcpIpConnectionManager is stopping");
    }
    ioThreadingModel.shutdown();
    acceptedSockets.clear();
    connectionsInProgress.clear();
    connectionsMap.clear();
    monitors.clear();
    activeConnections.clear();
}
Also used : SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) Connection(com.hazelcast.nio.Connection)

Example 4 with SocketChannelWrapper

use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.

the class TcpIpConnectionManager method wrapSocketChannel.

SocketChannelWrapper wrapSocketChannel(SocketChannel socketChannel, boolean client) throws Exception {
    SocketChannelWrapper wrapper = socketChannelWrapperFactory.wrapSocketChannel(socketChannel, client);
    acceptedSockets.add(wrapper);
    return wrapper;
}
Also used : SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper)

Example 5 with SocketChannelWrapper

use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.

the class DefaultSocketChannelWrapperFactoryTest method wrapSocketChannel.

@Test
public void wrapSocketChannel() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    SocketChannelWrapper wrapper = factory.wrapSocketChannel(socketChannel, false);
    assertInstanceOf(DefaultSocketChannelWrapper.class, wrapper);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SocketChannelWrapper (com.hazelcast.internal.networking.SocketChannelWrapper)7 IOException (java.io.IOException)4 SocketChannel (java.nio.channels.SocketChannel)4 AuthenticationException (com.hazelcast.client.AuthenticationException)1 HazelcastException (com.hazelcast.core.HazelcastException)1 ReadHandler (com.hazelcast.internal.networking.ReadHandler)1 SocketWriter (com.hazelcast.internal.networking.SocketWriter)1 Connection (com.hazelcast.nio.Connection)1 IOService (com.hazelcast.nio.IOService)1 IOUtil.newByteBuffer (com.hazelcast.nio.IOUtil.newByteBuffer)1 TextReadHandler (com.hazelcast.nio.ascii.TextReadHandler)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 StringUtil.bytesToString (com.hazelcast.util.StringUtil.bytesToString)1 EOFException (java.io.EOFException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1