@Override public void completed(final Void t) { if (iter.hasNext()) { pipeOneBuffer(byteChannel, iter.next(), this); } else { handler.completed(null); } }
@Override public void failed(final Throwable t) { handler.failed(t); } });
@Override public void completed(final Integer result, final Object attachment) { AsyncCompletionHandler<Void> localHandler = getHandlerAndClear(); localHandler.completed(null); }
@Override public void failed(final Throwable t) { outerHandler.failed(t); } });
@Override public void operationComplete(final ChannelFuture future) { if (future.isSuccess()) { channel = channelFuture.channel(); channel.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future) { handleReadResponse(null, new IOException("The connection to the server was closed")); } }); handler.completed(null); } else { if (socketAddressQueue.isEmpty()) { handler.failed(new MongoSocketOpenException("Exception opening socket", getAddress(), future.cause())); } else { initializeChannel(handler, socketAddressQueue); } } } }
@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<Void> localHandler = getHandlerAndClear(); localHandler.failed(exc); } }
@Override public void completed(final Integer result, final Void attachment) { AsyncCompletionHandler<ByteBuf> localHandler = getHandlerAndClear(); ByteBuf localByteBuf = byteBufReference.getAndSet(null); if (result == -1) { localByteBuf.release(); localHandler.failed(new MongoSocketReadException("Prematurely reached end of stream", serverAddress)); } else if (!localByteBuf.hasRemaining()) { localByteBuf.flip(); localHandler.completed(localByteBuf); } else { channel.read(localByteBuf.asNIO(), settings.getReadTimeout(MILLISECONDS), MILLISECONDS, null, new BasicCompletionHandler(localByteBuf, localHandler)); } }
@Override public void completed(final Void t) { if (byteBuffer.hasRemaining()) { byteChannel.write(byteBuffer.asNIO(), this); } else { outerHandler.completed(null); } }
@Override public void failed(final Throwable exc, final Object attachment) { AsyncCompletionHandler<Void> 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); } } }
@Override public void openAsync(final AsyncCompletionHandler<Void> handler) { // nothing to do here handler.completed(null); }
@Override public void failed(final Throwable t, final Void attachment) { AsyncCompletionHandler<ByteBuf> localHandler = getHandlerAndClear(); ByteBuf localByteBuf = byteBufReference.getAndSet(null); localByteBuf.release(); if (t instanceof InterruptedByTimeoutException) { localHandler.failed(new MongoSocketReadTimeoutException("Timeout while receiving message", serverAddress, t)); } else { localHandler.failed(t); } } }
@Override public void run() { try { if (!socketChannel.finishConnect()) { throw new MongoSocketOpenException("Failed to finish connect", getServerAddress()); } SSLEngine sslEngine = getSslContext().createSSLEngine(getServerAddress().getHost(), getServerAddress().getPort()); sslEngine.setUseClientMode(true); SSLParameters sslParameters = sslEngine.getSSLParameters(); enableSni(getServerAddress().getHost(), sslParameters); if (!sslSettings.isInvalidHostNameAllowed()) { enableHostNameVerification(sslParameters); } sslEngine.setSSLParameters(sslParameters); BufferAllocator bufferAllocator = new BufferProviderAllocator(); TlsChannel tlsChannel = ClientTlsChannel.newBuilder(socketChannel, sslEngine) .withEncryptedBufferAllocator(bufferAllocator) .withPlainBufferAllocator(bufferAllocator) .build(); // build asynchronous channel, based in the TLS channel and associated with the global group. setChannel(new AsynchronousTlsChannel(group, tlsChannel, socketChannel)); handler.completed(null); } catch (IOException e) { handler.failed(new MongoSocketOpenException("Exception opening socket", getServerAddress(), e)); } catch (Throwable t) { handler.failed(t); } } });
@Override public void completed(final Void t) { if (iter.hasNext()) { pipeOneBuffer(byteChannel, iter.next(), this); } else { handler.completed(null); } }
@SuppressWarnings("deprecation") private void initializeChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) { if (socketAddressQueue.isEmpty()) { handler.failed(new MongoSocketException("Exception opening socket", getAddress())); } else { SocketAddress nextAddress = socketAddressQueue.poll();
@Override public void completed(final Integer result, final Object attachment) { AsyncCompletionHandler<Void> localHandler = getHandlerAndClear(); localHandler.completed(null); }
@SuppressWarnings("deprecation") 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, 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); } } }