Example usage for io.netty.util.concurrent Future getNow

List of usage examples for io.netty.util.concurrent Future getNow

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future getNow.

Prototype

V getNow();

Source Link

Document

Return the result without blocking.

Usage

From source file:com.xx_dev.apn.socks.remote.SocksServerConnectHandler.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
    Promise<Channel> promise = ctx.executor().newPromise();
    promise.addListener(new GenericFutureListener<Future<Channel>>() {
        @Override//w ww. j a  v  a2  s  .  c om
        public void operationComplete(final Future<Channel> future) throws Exception {
            final Channel outboundChannel = future.getNow();
            if (future.isSuccess()) {
                restLogger.info(request.host() + ":" + request.port() + "," + "T");
                ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType()))
                        .addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture channelFuture) {
                                ctx.pipeline().remove(SocksServerConnectHandler.this);
                                outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
                                ctx.pipeline().addLast(new RelayHandler(outboundChannel));
                            }
                        });
            } else {
                restLogger.info(request.host() + ":" + request.port() + "," + "F");
                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                SocksServerUtils.closeOnFlush(ctx.channel());
            }
        }
    });

    final Channel inboundChannel = ctx.channel();
    b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true)
            .handler(new DirectClientHandler(promise));

    b.connect(request.host(), request.port()).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // Connection established use handler provided results
            } else {
                // Close the connection if the connection attempt has failed.
                restLogger.info(request.host() + ":" + request.port() + "," + "F");
                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                SocksServerUtils.closeOnFlush(ctx.channel());
            }
        }
    });
}

From source file:io.airlift.drift.transport.netty.client.ConnectionPool.java

License:Apache License

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address) {
    ConnectionKey key = new ConnectionKey(connectionParameters, address);

    while (true) {
        synchronized (this) {
            if (closed) {
                return group.next().newFailedFuture(new TTransportException("Connection pool is closed"));
            }/* w  w w.  j av  a  2s  .c  o  m*/

            Future<Channel> future;
            try {
                future = cachedConnections.get(key, () -> createConnection(key));
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }

            // connection is still opening
            if (!future.isDone()) {
                return future;
            }

            // check if connection is failed or closed
            Channel channel = future.getNow();
            // channel can be null if the future was canceled
            if (channel != null && channel.isOpen()) {
                return future;
            }

            // remove dead connection from cache
            cachedConnections.asMap().remove(key, future);
        }
    }
}

From source file:io.airlift.drift.transport.netty.client.ConnectionPool.java

License:Apache License

private Future<Channel> createConnection(ConnectionKey key) {
    Future<Channel> future = connectionFactory.getConnection(key.getConnectionParameters(), key.getAddress());

    // remove connection from cache when it is closed
    future.addListener(channelFuture -> {
        if (future.isSuccess()) {
            future.getNow().closeFuture()
                    .addListener(closeFuture -> cachedConnections.asMap().remove(key, future));
        }/*from   w  ww  . jav a  2s  .  co m*/
    });

    return future;
}

From source file:io.airlift.drift.transport.netty.client.ConnectionPool.java

License:Apache License

private static void closeConnection(Future<Channel> future) {
    future.addListener(ignored -> {//from w  w  w. jav a2s  . co m
        if (future.isSuccess()) {
            Channel channel = future.getNow();
            channel.close();
        }
    });
}

From source file:io.airlift.drift.transport.netty.client.TestConnectionPool.java

License:Apache License

private static <T> T futureGet(Future<T> future) {
    assertTrue(future.isSuccess());
    return future.getNow();
}

From source file:io.aos.netty5.socksproxy.SocksServerConnectHandler.java

License:Apache License

@Override
public void messageReceived(final ChannelHandlerContext ctx, final SocksRequest message) throws Exception {
    if (message instanceof Socks4CmdRequest) {
        final Socks4CmdRequest request = (Socks4CmdRequest) message;
        Promise<Channel> promise = ctx.executor().newPromise();
        promise.addListener(new GenericFutureListener<Future<Channel>>() {
            @Override/*  w ww  . j av  a2s  .co m*/
            public void operationComplete(final Future<Channel> future) throws Exception {
                final Channel outboundChannel = future.getNow();
                if (future.isSuccess()) {
                    ctx.channel().writeAndFlush(new Socks4CmdResponse(Socks4CmdStatus.SUCCESS))
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture channelFuture) {
                                    ctx.pipeline().remove(SocksServerConnectHandler.this);
                                    outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
                                    ctx.pipeline().addLast(new RelayHandler(outboundChannel));
                                }
                            });
                } else {
                    ctx.channel().writeAndFlush(new Socks4CmdResponse(Socks4CmdStatus.REJECTED_OR_FAILED));
                    SocksServerUtils.closeOnFlush(ctx.channel());
                }
            }
        });

        final Channel inboundChannel = ctx.channel();
        b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new DirectClientHandler(promise));

        b.connect(request.host(), request.port()).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    // Connection established use handler provided results
                } else {
                    // Close the connection if the connection attempt has failed.
                    ctx.channel().writeAndFlush(new Socks4CmdResponse(Socks4CmdStatus.REJECTED_OR_FAILED));
                    SocksServerUtils.closeOnFlush(ctx.channel());
                }
            }
        });
    } else if (message instanceof Socks5CmdRequest) {
        final Socks5CmdRequest request = (Socks5CmdRequest) message;
        Promise<Channel> promise = ctx.executor().newPromise();
        promise.addListener(new GenericFutureListener<Future<Channel>>() {
            @Override
            public void operationComplete(final Future<Channel> future) throws Exception {
                final Channel outboundChannel = future.getNow();
                if (future.isSuccess()) {
                    ctx.channel()
                            .writeAndFlush(
                                    new Socks5CmdResponse(Socks5CmdStatus.SUCCESS, request.addressType()))
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture channelFuture) {
                                    ctx.pipeline().remove(SocksServerConnectHandler.this);
                                    outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
                                    ctx.pipeline().addLast(new RelayHandler(outboundChannel));
                                }
                            });
                } else {
                    ctx.channel().writeAndFlush(
                            new Socks5CmdResponse(Socks5CmdStatus.FAILURE, request.addressType()));
                    SocksServerUtils.closeOnFlush(ctx.channel());
                }
            }
        });

        final Channel inboundChannel = ctx.channel();
        b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new DirectClientHandler(promise));

        b.connect(request.host(), request.port()).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    // Connection established use handler provided results
                } else {
                    // Close the connection if the connection attempt has failed.
                    ctx.channel().writeAndFlush(
                            new Socks5CmdResponse(Socks5CmdStatus.FAILURE, request.addressType()));
                    SocksServerUtils.closeOnFlush(ctx.channel());
                }
            }
        });
    } else {
        ctx.close();
    }
}

From source file:io.codis.nedis.NedisClientPoolImpl.java

License:Apache License

private Future<NedisClient> newClient() {
    Future<NedisClientImpl> f = NedisClientBuilder.create().group(group).channel(channelClass)
            .timeoutMs(timeoutMs).belongTo(this).connect(remoteAddress);

    final Promise<NedisClient> promise = getEventExecutor(f).newPromise();
    f.addListener(new FutureListener<NedisClientImpl>() {

        @Override/*from  w  ww .j av a  2  s  . c om*/
        public void operationComplete(Future<NedisClientImpl> future) throws Exception {
            if (future.isSuccess()) {
                initialize(promise, future.getNow(), State.AUTH);
            } else {
                promise.tryFailure(future.cause());
            }
        }

    });
    return promise;
}

From source file:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<List<byte[]>> toList(EventExecutor executor) {
    return new PromiseConverter<List<byte[]>>(executor) {

        @Override//from   w w  w .j  a  va2  s.c  o  m
        public FutureListener<Object> newListener(final Promise<List<byte[]>> promise) {
            return new FutureListener<Object>() {

                @SuppressWarnings("unchecked")
                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else if (resp == RedisResponseDecoder.NULL_REPLY) {
                            promise.trySuccess(null);
                        } else {
                            promise.trySuccess((List<byte[]>) resp);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

From source file:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<Boolean> toBoolean(EventExecutor executor) {
    return new PromiseConverter<Boolean>(executor) {

        @Override/*from  w  w  w  . j  a  v  a2  s  . co m*/
        public FutureListener<Object> newListener(final Promise<Boolean> promise) {
            return new FutureListener<Object>() {

                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else if (resp == RedisResponseDecoder.NULL_REPLY) {
                            promise.trySuccess(false);
                        } else if (resp instanceof String) {
                            promise.trySuccess(true);
                        } else {
                            promise.trySuccess(((Long) resp).intValue() != 0);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

From source file:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<byte[]> toBytes(EventExecutor executor) {
    return new PromiseConverter<byte[]>(executor) {

        @Override/*  w  ww .j a  v a  2 s . c om*/
        public FutureListener<Object> newListener(final Promise<byte[]> promise) {
            return new FutureListener<Object>() {

                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else if (resp == RedisResponseDecoder.NULL_REPLY) {
                            promise.trySuccess(null);
                        } else {
                            promise.trySuccess((byte[]) resp);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}