Example usage for io.netty.util.concurrent GenericFutureListener GenericFutureListener

List of usage examples for io.netty.util.concurrent GenericFutureListener GenericFutureListener

Introduction

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

Prototype

GenericFutureListener

Source Link

Usage

From source file:p2p_server.AppHandler.java

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override//from  w ww.j  ava 2 s. com
                public void operationComplete(Future<Channel> future) throws Exception {
                    ChannelFuture flag = ctx.writeAndFlush("Welcome to "
                            + InetAddress.getLocalHost().getHostName() + " secure chat service!\n");
                    /*
                    if (!flag.isSuccess()) {
                    System.out.println(" app ssl error " + flag.cause());
                            
                    } else {
                    System.out.println("connected ssl");
                            
                    }
                    */

                }
            });
}

From source file:p2p_server.DeviceHandler.java

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override/*from  w ww . j a  v a 2s .com*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ChannelFuture flag = ctx.writeAndFlush("Welcome to "
                            + InetAddress.getLocalHost().getHostName() + " secure chat service!\n");
                    /*
                    if (!flag.isSuccess()) {
                    System.out.println(" dev ssl error " + flag.cause());
                            
                    } else {
                    System.out.println("connected ssl");
                            
                    }
                     */

                }
            });
}

From source file:pers.zlf.sslocal.handler.shadowsocks.ShadowsocksServerConnectHandler.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
    final Option option = ShadowsocksClient.getShadowsocksOption(ctx.channel());
    Promise<Channel> promise = ctx.executor().newPromise();
    promise.addListener(new GenericFutureListener<Future<Channel>>() {
        @Override/*from w ww.j  a va  2  s  .co m*/
        public void operationComplete(final Future<Channel> future) throws Exception {
            final Channel outboundChannel = future.getNow();
            if (future.isSuccess()) {
                ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType()))
                        .addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture channelFuture) {
                                ctx.pipeline().remove(ShadowsocksServerConnectHandler.this);
                                outboundChannel.pipeline().addLast(
                                        new ShadowsocksMessageCodec(CryptoFactory
                                                .createCrypto(option.getMethod(), option.getPassword())),
                                        new RelayHandler(ctx.channel()));
                                ctx.pipeline().addLast(new RelayHandler(outboundChannel));

                                outboundChannel.writeAndFlush(request);
                            }
                        });
            } else {
                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                ChannelUtils.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(option.getRemoteHost(), option.getRemotePort()).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.
                if (logger.isErrorEnabled()) {
                    logger.error("Failed to connect shadowsocks server", future.cause());
                }

                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                ChannelUtils.closeOnFlush(ctx.channel());
            }
        }
    });
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpServer.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public Promise<Void> doShutdown() {

    final Promise<Void> d = Promises.prepare();

    final AtomicInteger groupsToShutdown = new AtomicInteger(2);
    GenericFutureListener listener = new GenericFutureListener() {

        @Override//w  ww. j a v  a  2 s. c  om
        public void operationComplete(Future future) throws Exception {
            if (groupsToShutdown.decrementAndGet() == 0) {
                d.onComplete();
            }
        }
    };
    selectorGroup.shutdownGracefully().addListener(listener);
    if (null == nettyOptions || null == nettyOptions.eventLoopGroup()) {
        ioGroup.shutdownGracefully().addListener(listener);
    }

    return d;
}

From source file:reactor.io.net.impl.netty.udp.NettyDatagramServer.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
protected Promise<Void> doShutdown() {
    final Promise<Void> d = Promises.prepare();

    ChannelFuture future = channel.close();
    final GenericFutureListener listener = new GenericFutureListener() {
        @Override//from w w  w  .  j  ava  2  s. c  o m
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                d.onComplete();
            } else {
                d.onError(future.cause());
            }
        }
    };

    future.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                d.onError(future.cause());
                return;
            }

            if (null == nettyOptions || null == nettyOptions.eventLoopGroup()) {
                ioGroup.shutdownGracefully().addListener(listener);
            }
        }
    });

    return d;
}

From source file:reactor.io.net.netty.tcp.NettyTcpServer.java

License:Apache License

@Override
public Promise<Boolean> shutdown() {
    final Promise<Boolean> d = Promises.ready(getEnvironment(), getReactor().getDispatcher());
    getReactor().schedule(new Consumer<Void>() {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override//from   w  w w. j a v  a2s  .co  m
        public void accept(Void v) {
            final AtomicInteger groupsToShutdown = new AtomicInteger(2);
            GenericFutureListener listener = new GenericFutureListener() {

                @Override
                public void operationComplete(Future future) throws Exception {
                    if (groupsToShutdown.decrementAndGet() == 0) {
                        notifyShutdown();
                        d.onNext(true);
                    }
                }
            };
            selectorGroup.shutdownGracefully().addListener(listener);
            if (null == nettyOptions || null == nettyOptions.eventLoopGroup()) {
                ioGroup.shutdownGracefully().addListener(listener);
            }
        }
    }, null);

    return d;
}

From source file:reactor.io.net.netty.udp.NettyDatagramServer.java

License:Apache License

@Override
public Promise<Boolean> shutdown() {
    final Promise<Boolean> d = Promises.ready(getEnvironment(), getReactor().getDispatcher());

    getReactor().schedule(new Consumer<Void>() {
        @SuppressWarnings("unchecked")
        @Override/*  ww w .  jav  a 2  s  .  c  om*/
        public void accept(Void v) {
            GenericFutureListener listener = new GenericFutureListener() {
                @Override
                public void operationComplete(Future future) throws Exception {
                    if (future.isSuccess()) {
                        d.onNext(true);
                    } else {
                        d.onError(future.cause());
                    }
                }
            };
            if (null == nettyOptions || null == nettyOptions.eventLoopGroup()) {
                ioGroup.shutdownGracefully().addListener(listener);
            }
        }
    }, null);
    notifyShutdown();

    return d;
}

From source file:reactor.tcp.netty.NettyTcpClient.java

License:Open Source License

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doClose(final Deferred<Void, Promise<Void>> d) {
    try {//from   ww  w. j av a2  s .  co  m
        this.ioGroup.shutdownGracefully().await().addListener(new GenericFutureListener() {
            @Override
            public void operationComplete(Future future) throws Exception {
                // Sleep for 1 second to allow Netty's GlobalEventExecutor thread to die
                // TODO We need a better way of being sure that all of Netty's threads have died
                Thread.sleep(1000);
                d.accept((Void) null);
            }
        });
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        d.accept(e);
    }
}

From source file:se.sics.gvod.net.NettyNetwork.java

License:Open Source License

private void closeSocket(final InetSocketAddress addr, final Transport protocol,
        final CloseConnectionResponse response) {
    Bootstrap bootstrap;/*w w  w.  java 2  s.co  m*/
    switch (protocol) {
    case TCP:
        bootstrap = tcpSocketsToBootstraps.get(addr);
        break;
    case UDP:
        bootstrap = udpSocketsToBootstraps.get(addr);
        break;
    case UDT:
        bootstrap = udtSocketsToBootstraps.get(addr);
        break;
    default:
        throw new Error("Transport type not supported");
    }

    // Has been removed before
    if (bootstrap == null) {
        return;
    }

    Future future = bootstrap.group().shutdownGracefully();
    future.addListener(new GenericFutureListener<Future<?>>() {
        @Override
        public void operationComplete(Future<?> future) throws Exception {
            if (response != null) {
                // if a response is requested, send it
                trigger(response, netControl);
            }
        }
    });
}

From source file:uidserver.UIDServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // Once session is secured, send a greeting and register the channel to the global channel
    // list so the channel received the messages from others.
    ctx.pipeline().get(SslHandler.class).handshakeFuture()
            .addListener(new GenericFutureListener<Future<Channel>>() {
                @Override//w  w  w.  j a  v a2 s.co  m
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " for unit id service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");

                    channels.add(ctx.channel());
                    System.out.println("New Client " + ctx.channel().remoteAddress().toString() + " connected");
                    if (logFile != null) {
                        System.out.println("file already existing");
                    } else {
                        String remoteAdd = ctx.channel().remoteAddress().toString();
                        remoteAdd = remoteAdd.replace(':', '_');
                        remoteAdd = remoteAdd.replace('.', '_');
                        remoteAdd = remoteAdd.substring(1, remoteAdd.length());

                        logFile = new File(Config.logPath.getAbsolutePath() + "/" + remoteAdd + "/"
                                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("uuuuMMddhhmmss"))
                                + ".log");
                        //                            System.out.println("Create log file: " + logFile.getAbsolutePath());

                        if (!logFile.exists()) {
                            try {
                                if (!logFile.getParentFile().exists()) {
                                    logFile.getParentFile().mkdirs();
                                }
                                if (!logFile.createNewFile()) {
                                    logFile = null;
                                    System.out
                                            .println("Failed to create log file: " + logFile.getAbsolutePath());
                                } else {
                                    System.out.println(
                                            "Succesfully to create log file: " + logFile.getAbsolutePath());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                logFile = null;
                            }
                        }
                    }
                }
            });
}