Search in sources :

Example 1 with MongoSocketOpenException

use of com.mongodb.MongoSocketOpenException in project mongo-java-driver by mongodb.

the class SocketStream method open.

@Override
public void open() throws IOException {
    try {
        socket = socketFactory.createSocket();
        SocketStreamHelper.initialize(socket, address, settings, sslSettings);
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
    } catch (IOException e) {
        close();
        throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
    }
}
Also used : IOException(java.io.IOException) MongoSocketOpenException(com.mongodb.MongoSocketOpenException)

Example 2 with MongoSocketOpenException

use of com.mongodb.MongoSocketOpenException in project mongo-java-driver by mongodb.

the class SocketStream method open.

@Override
public void open() {
    try {
        socket = initializeSocket();
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
    } catch (IOException e) {
        close();
        throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
    }
}
Also used : IOException(java.io.IOException) MongoSocketOpenException(com.mongodb.MongoSocketOpenException)

Example 3 with MongoSocketOpenException

use of com.mongodb.MongoSocketOpenException in project mongo-java-driver by mongodb.

the class NettyStream method openAsync.

@Override
public void openAsync(final AsyncCompletionHandler<Void> handler) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(workerGroup);
    bootstrap.channel(socketChannelClass);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, settings.getConnectTimeout(MILLISECONDS));
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, settings.isKeepAlive());
    if (settings.getReceiveBufferSize() > 0) {
        bootstrap.option(ChannelOption.SO_RCVBUF, settings.getReceiveBufferSize());
    }
    if (settings.getSendBufferSize() > 0) {
        bootstrap.option(ChannelOption.SO_SNDBUF, settings.getSendBufferSize());
    }
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(final SocketChannel ch) throws Exception {
            if (sslSettings.isEnabled()) {
                SSLEngine engine = SSLContext.getDefault().createSSLEngine(address.getHost(), address.getPort());
                engine.setUseClientMode(true);
                SSLParameters sslParameters = engine.getSSLParameters();
                enableSni(address, sslParameters);
                if (!sslSettings.isInvalidHostNameAllowed()) {
                    enableHostNameVerification(sslParameters);
                }
                engine.setSSLParameters(sslParameters);
                ch.pipeline().addFirst("ssl", new SslHandler(engine, false));
            }
            int readTimeout = settings.getReadTimeout(MILLISECONDS);
            if (readTimeout > 0) {
                ch.pipeline().addLast(READ_HANDLER_NAME, new ReadTimeoutHandler(readTimeout));
            }
            ch.pipeline().addLast(new InboundBufferHandler());
        }
    });
    final ChannelFuture channelFuture = bootstrap.connect(address.getHost(), address.getPort());
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                channel = channelFuture.channel();
                channel.closeFuture().addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(final ChannelFuture f2) throws Exception {
                        handleReadResponse(null, new IOException("The connection to the server was closed"));
                    }
                });
                handler.completed(null);
            } else {
                handler.failed(new MongoSocketOpenException("Exception opening socket", getAddress(), future.cause()));
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) SSLEngine(javax.net.ssl.SSLEngine) IOException(java.io.IOException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MongoInternalException(com.mongodb.MongoInternalException) MongoSocketOpenException(com.mongodb.MongoSocketOpenException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException) SslHandler(io.netty.handler.ssl.SslHandler) MongoSocketOpenException(com.mongodb.MongoSocketOpenException) SSLParameters(javax.net.ssl.SSLParameters) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 4 with MongoSocketOpenException

use of com.mongodb.MongoSocketOpenException in project mongo-java-driver by mongodb.

the class SocketChannelStream method open.

@Override
public void open() throws IOException {
    try {
        socketChannel = SocketChannel.open();
        SocketStreamHelper.initialize(socketChannel.socket(), address, settings, sslSettings);
    } catch (IOException e) {
        close();
        throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
    }
}
Also used : IOException(java.io.IOException) MongoSocketOpenException(com.mongodb.MongoSocketOpenException)

Example 5 with MongoSocketOpenException

use of com.mongodb.MongoSocketOpenException in project mongo-java-driver by mongodb.

the class AsynchronousSocketChannelStream method initializeSocketChannel.

private void initializeSocketChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) {
    if (socketAddressQueue.isEmpty()) {
        handler.failed(new MongoSocketException("Exception opening socket", serverAddress));
    } else {
        SocketAddress socketAddress = socketAddressQueue.poll();
        try {
            AsynchronousSocketChannel attemptConnectionChannel = AsynchronousSocketChannel.open(group);
            attemptConnectionChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
            attemptConnectionChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
            if (settings.getReceiveBufferSize() > 0) {
                attemptConnectionChannel.setOption(StandardSocketOptions.SO_RCVBUF, settings.getReceiveBufferSize());
            }
            if (settings.getSendBufferSize() > 0) {
                attemptConnectionChannel.setOption(StandardSocketOptions.SO_SNDBUF, settings.getSendBufferSize());
            }
            attemptConnectionChannel.connect(socketAddress, null, new OpenCompletionHandler(handler, socketAddressQueue, attemptConnectionChannel));
        } catch (IOException e) {
            handler.failed(new MongoSocketOpenException("Exception opening socket", serverAddress, e));
        } catch (Throwable t) {
            handler.failed(t);
        }
    }
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) MongoSocketException(com.mongodb.MongoSocketException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) MongoSocketOpenException(com.mongodb.MongoSocketOpenException)

Aggregations

MongoSocketOpenException (com.mongodb.MongoSocketOpenException)5 IOException (java.io.IOException)5 MongoException (com.mongodb.MongoException)1 MongoInternalException (com.mongodb.MongoInternalException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoSocketException (com.mongodb.MongoSocketException)1 MongoSocketReadTimeoutException (com.mongodb.MongoSocketReadTimeoutException)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 SslHandler (io.netty.handler.ssl.SslHandler)1 ReadTimeoutException (io.netty.handler.timeout.ReadTimeoutException)1 SocketAddress (java.net.SocketAddress)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLParameters (javax.net.ssl.SSLParameters)1