All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.internal.connection.AsynchronousSocketChannelStream Maven / Gradle / Ivy

Go to download

The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson

There is a newer version: 3.12.14
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.internal.connection;

import com.mongodb.MongoSocketException;
import com.mongodb.MongoSocketOpenException;
import com.mongodb.ServerAddress;
import com.mongodb.connection.AsyncCompletionHandler;
import com.mongodb.connection.BufferProvider;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.Stream;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static com.mongodb.assertions.Assertions.isTrue;

public final class AsynchronousSocketChannelStream extends AsynchronousChannelStream implements Stream {
    private final ServerAddress serverAddress;
    private final SocketSettings settings;
    private final AsynchronousChannelGroup group;

    public AsynchronousSocketChannelStream(final ServerAddress serverAddress, final SocketSettings settings,
                                    final BufferProvider bufferProvider, final AsynchronousChannelGroup group) {
        super(serverAddress, settings, bufferProvider);
        this.serverAddress = serverAddress;
        this.settings = settings;
        this.group = group;
    }

    @Override
    public void openAsync(final AsyncCompletionHandler handler) {
        isTrue("unopened", getChannel() == null);
        initializeSocketChannel(handler, new LinkedList(serverAddress.getSocketAddresses()));
    }

    @SuppressWarnings("deprecation")
    private void initializeSocketChannel(final AsyncCompletionHandler handler, final Queue 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, settings.isKeepAlive());
                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);
            }
        }
    }

    public AsynchronousChannelGroup getGroup() {
        return group;
    }

    private class OpenCompletionHandler implements CompletionHandler  {
        private AtomicReference> handlerReference;
        private final Queue socketAddressQueue;
        private final AsynchronousSocketChannel attemptConnectionChannel;

        OpenCompletionHandler(final AsyncCompletionHandler handler, final Queue socketAddressQueue,
                              final AsynchronousSocketChannel attemptConnectionChannel) {
            this.handlerReference = new AtomicReference>(handler);
            this.socketAddressQueue = socketAddressQueue;
            this.attemptConnectionChannel = attemptConnectionChannel;
        }

        @Override
        public void completed(final Void result, final Object attachment) {
            setChannel(new AsynchronousSocketChannelAdapter(attemptConnectionChannel));
            handlerReference.getAndSet(null).completed(null);
        }

        @Override
        public void failed(final Throwable exc, final Object attachment) {
            AsyncCompletionHandler localHandler = handlerReference.getAndSet(null);

            if (socketAddressQueue.isEmpty()) {
                if (exc instanceof IOException) {
                    localHandler.failed(new MongoSocketOpenException("Exception opening socket", getAddress(), exc));
                } else {
                    localHandler.failed(exc);
                }
            } else {
                initializeSocketChannel(localHandler, socketAddressQueue);
            }
        }
    }

    private static final class AsynchronousSocketChannelAdapter implements ExtendedAsynchronousByteChannel {
        private final AsynchronousSocketChannel channel;

        private AsynchronousSocketChannelAdapter(final AsynchronousSocketChannel channel) {
            this.channel = channel;
        }

        @Override
        public  void read(final ByteBuffer dst, final long timeout, final TimeUnit unit, final A attach,
                             final CompletionHandler handler) {
            channel.read(dst, timeout, unit, attach, handler);
        }

        @Override
        public  void read(final ByteBuffer[] dsts, final int offset, final int length, final long timeout, final TimeUnit unit,
                             final A attach, final CompletionHandler handler) {
            channel.read(dsts, offset, length, timeout, unit, attach, handler);
        }

        @Override
        public  void write(final ByteBuffer src, final long timeout, final TimeUnit unit, final A attach,
                              final CompletionHandler handler) {
            channel.write(src, timeout, unit, attach, handler);
        }

        @Override
        public  void write(final ByteBuffer[] srcs, final int offset, final int length, final long timeout, final TimeUnit unit,
                              final A attach, final CompletionHandler handler) {
            channel.write(srcs, offset, length, timeout, unit, attach, handler);
        }

        @Override
        public  void read(final ByteBuffer dst, final A attachment, final CompletionHandler handler) {
            channel.read(dst, attachment, handler);
        }

        @Override
        public Future read(final ByteBuffer dst) {
            return channel.read(dst);
        }

        @Override
        public  void write(final ByteBuffer src, final A attachment, final CompletionHandler handler) {
            channel.write(src, attachment, handler);
        }

        @Override
        public Future write(final ByteBuffer src) {
            return channel.write(src);
        }

        @Override
        public boolean isOpen() {
            return channel.isOpen();
        }

        @Override
        public void close() throws IOException {
            channel.close();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy