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:SecureChatServerHandler.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/*from   ww  w. j  av a2  s . c om*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");

                    channels.add(ctx.channel());
                }
            });
}

From source file:at.yawk.dbus.protocol.DbusChannelImpl.java

private static <V> CompletableFuture<V> nettyFutureToStage(Future<V> future) {
    CompletableFuture<V> stage = new CompletableFuture<>();
    future.addListener(new GenericFutureListener<Future<V>>() {
        @Override/*from  ww w  .  j  av a2 s.c  o m*/
        public void operationComplete(Future<V> future) throws Exception {
            try {
                stage.complete(future.get());
            } catch (ExecutionException e) {
                stage.completeExceptionally(e.getCause());
            }
        }
    });
    return stage;
}

From source file:blazingcache.network.netty.NettyChannel.java

License:Apache License

@Override
public void sendOneWayMessage(Message message, SendResultCallback callback) {
    if (message.getMessageId() == null) {
        message.setMessageId(UUID.randomUUID().toString());
    }/*from  ww w  . j a v a2 s . com*/
    SocketChannel _socket = this.socket;
    if (_socket == null || !_socket.isOpen()) {
        callback.messageSent(message, new Exception(this + " connection is closed"));
        return;
    }
    _socket.writeAndFlush(message).addListener(new GenericFutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                callback.messageSent(message, null);
            } else {
                LOGGER.log(Level.SEVERE, this + ": error " + future.cause(), future.cause());
                callback.messageSent(message, future.cause());
                close();
            }
        }

    });
}

From source file:books.netty.protocol.http.xml.server.HttpXmlServerHandler.java

License:Apache License

public void messageReceived(final ChannelHandlerContext ctx, HttpXmlRequest xmlRequest) {
    HttpRequest request = xmlRequest.getRequest();
    Order order = (Order) xmlRequest.getBody();
    System.out.println("Http server receive request : " + order);
    dobusiness(order);//from   ww w.  ja  va  2s .c  om
    ChannelFuture future = ctx.writeAndFlush(new HttpXmlResponse(null, order));
    if (!isKeepAlive(request)) {
        future.addListener(new GenericFutureListener<Future<? super Void>>() {
            public void operationComplete(Future future) {
                ctx.close();
            }
        });
    }
}

From source file:books.netty.ssl.SecureChatServerHandler.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 ww .j  a  va 2 s . c  o  m*/
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");

                    channels.add(ctx.channel());
                }
            });
}

From source file:c5db.eventLogging.EventLogListener.java

License:Apache License

@Override
protected void doStart() {
    nioEventLoopGroup.next().execute(() -> {

        Bootstrap bootstrap = new Bootstrap();
        try {// w w w .j  ava2  s .co m
            bootstrap.group(nioEventLoopGroup).channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true)
                    .handler(new ChannelInitializer<DatagramChannel>() {
                        @Override
                        protected void initChannel(DatagramChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("protostuffDecoder",
                                    new UdpProtostuffDecoder<>(EventLogEntry.getSchema(), false));
                            p.addLast("logger", new MsgHandler());
                        }
                    });

            bootstrap.bind(port).addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    channel = future.channel();
                }
            });

            notifyStarted();
        } catch (Throwable t) {
            notifyFailed(t);
        }
    });
}

From source file:c5db.eventLogging.EventLogService.java

License:Apache License

@Override
protected void doStart() {
    nioEventLoopGroup.next().execute(() -> {
        Bootstrap bootstrap = new Bootstrap();
        try {/*from  w  w w.  ja v a 2s . c  o m*/
            bootstrap.group(nioEventLoopGroup).channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true)
                    .handler(new ChannelInitializer<DatagramChannel>() {
                        @Override
                        protected void initChannel(DatagramChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("protostuffEncoder",
                                    new UdpProtostuffEncoder<>(EventLogEntry.getSchema(), false));
                        }
                    });

            bootstrap.bind(port).addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    broadcastChannel = future.channel();
                }
            });

            eventLogChannel.subscribe(fiber, msg -> {
                if (broadcastChannel == null) {
                    LOG.debug("Broadcast channel isn't read yet, dropped message");
                    return;
                }

                LOG.trace("Sending event {}", msg);
                broadcastChannel
                        .writeAndFlush(new UdpProtostuffEncoder.UdpProtostuffMessage<>(sendAddress, msg));
            });

            fiber.start();

            notifyStarted();
        } catch (Throwable t) {
            fiber.dispose();

            notifyFailed(t);
        }
    });
}

From source file:ca.lambtoncollege.netty.chat.SecureChatServerHandler.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//from  w ww. j a v a  2 s . c  om
                public void operationComplete(Future<Channel> future) throws Exception {
                    ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName()
                            + " secure chat service!\n");
                    ctx.writeAndFlush("Your session is protected by "
                            + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite()
                            + " cipher suite.\n");
                    channels.add(ctx.channel());
                }
            });
}

From source file:cc.ly.mc.client.netty.SocketClient.java

License:Apache License

public void write(Message message) {
    channel.writeAndFlush(message).addListener(new GenericFutureListener<Future<Void>>() {
        @Override//  w ww.j a va2 s.co  m
        public void operationComplete(Future future) throws Exception {
            if (!future.isSuccess()) {
                System.out.println("failed to send message");
            }
        }
    });
}

From source file:cn.david.socks.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/*from   w  w  w  . j a  v  a  2 s. c o  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(SocksServerConnectHandler.this);
                                outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
                                ctx.pipeline().addLast(new RelayHandler(outboundChannel));
                            }
                        });
            } else {
                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.
                ctx.channel()
                        .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                SocksServerUtils.closeOnFlush(ctx.channel());
            }
        }
    });
}