Example usage for io.netty.channel ChannelFuture isSuccess

List of usage examples for io.netty.channel ChannelFuture isSuccess

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture isSuccess.

Prototype

boolean isSuccess();

Source Link

Document

Returns true if and only if the I/O operation was completed successfully.

Usage

From source file:com.spotify.heroic.consumer.collectd.Server.java

License:Apache License

public static AsyncFuture<Server> setup(final AsyncFramework async, final CollectdChannelHandler handler,
        final InetAddress host, final int port) {
    final EventLoopGroup group = new NioEventLoopGroup();
    final Bootstrap b = new Bootstrap();

    b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(handler);

    final ResolvableFuture<Server> future = async.future();

    b.bind(host, port).addListener(new ChannelFutureListener() {
        @Override/*from  www  . ja va  2  s  .c o  m*/
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (f.isSuccess()) {
                future.resolve(new Server(async, f.channel()));
            } else {
                future.fail(f.cause() != null ? f.cause() : new RuntimeException("Failed to bind"));
            }
        }
    });

    return future;
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private <R> ChannelFutureListener handleConnect(final NativeRpcRequest request,
        final ResolvableFuture<R> future, final AtomicReference<Timeout> heartbeatTimeout,
        final Timeout requestTimeout) {
    return new ChannelFutureListener() {
        @Override//from   w ww  .ja  v  a  2 s  . co m
        public void operationComplete(ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                future.fail(f.cause());
                return;
            }

            f.channel().writeAndFlush(request)
                    .addListener(handleRequestSent(future, heartbeatTimeout, requestTimeout));
        }
    };
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private <R> ChannelFutureListener handleRequestSent(final ResolvableFuture<R> future,
        final AtomicReference<Timeout> heartbeatTimeout, final Timeout requestTimeout) {
    return new ChannelFutureListener() {
        @Override/*w ww  .  j  a va  2s .c om*/
        public void operationComplete(ChannelFuture f) throws Exception {
            requestTimeout.cancel();

            if (!f.isSuccess()) {
                future.fail(f.cause());
                return;
            }

            final Timeout timeout = timer.newTimeout(heartbeatTimeout(f.channel(), future), heartbeatInterval,
                    TimeUnit.MILLISECONDS);

            heartbeatTimeout.set(timeout);
        }
    };
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcProtocolServer.java

License:Apache License

private AsyncFuture<Void> start() {
    final ServerBootstrap s = new ServerBootstrap();
    s.channel(NioServerSocketChannel.class);
    s.group(bossGroup, workerGroup);//from   w w w .ja v  a  2 s  . c o  m
    s.childHandler(new NativeRpcServerSession(timer, mapper, container, maxFrameSize, encoding));

    final ChannelFuture bind = s.bind(address);

    bind.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                bindFuture.fail(f.cause());
                return;
            }

            serverChannel.set(f.channel());
            final InetSocketAddress address = (InetSocketAddress) f.channel().localAddress();
            bindFuture.resolve(address);
        }
    });

    return bindFuture.directTransform(a -> null);
}

From source file:com.supermy.im.netty.TCPServer.java

License:Apache License

/**
 * 3??// w w  w  . ja  v  a  2  s. c  o  m
 */
protected void doBind() {
    if (closed) {
        return;
    }

    serverBootstrap.bind(tcpPort).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            if (f.isSuccess()) {
                System.out.println("Started Tcp Server: " + tcpPort);
            } else {
                System.out.println("Started Tcp Server Failed: " + tcpPort);

                // f.channel().eventLoop().schedule(() -> doBind(), 3, TimeUnit.SECONDS);

                f.channel().eventLoop().schedule(new Runnable() {
                    @Override
                    public void run() {
                        doBind();
                    }
                }, 3, TimeUnit.SECONDS);
            }
        }
    });
}

From source file:com.tc.websocket.server.handler.ProxyBackendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    ByteBuf buf = (ByteBuf) msg;//ww w .  ja  v  a 2s  . c o m
    String data = new String(ByteBufUtil.getBytes(buf));
    ByteBuf bufData = buf;

    if (Config.getInstance().isEncrypted() && data.contains(StringCache.HTTP)) {
        data = data.replace(StringCache.HTTP, StringCache.HTTPS);
        bufData = Unpooled.wrappedBuffer(data.getBytes());
    }

    //ProxyFrontendHandler.writeToFile("backend", ByteBufUtil.getBytes(bufData));

    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ctx.channel().read();

            } else {
                future.channel().close();
            }
        }
    });

}

From source file:com.tc.websocket.server.handler.ProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    this.handler = new ProxyBackendHandler(inboundChannel);

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()).handler(this.handler)
            .option(ChannelOption.AUTO_READ, false);

    ChannelFuture f = b.connect(remoteHost, remotePort);
    outboundChannel = f.channel();//w  w w . j a v  a  2s  .c o  m

    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();

            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });

}

From source file:com.tc.websocket.server.handler.ProxyFrontendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {

    ByteBuf buf = (ByteBuf) msg;//from  w  w  w  . j  a v a  2 s . co  m
    String data = new String(ByteBufUtil.getBytes(buf));

    if (data.contains(Const.UPGRADE_WEBSOCKET) || data.contains(Const.GET_WEBSOCKET)) {
        proxy.set(false);

    } else if (data.contains(StringCache.HTTP_1_1)) {
        proxy.set(true);
    }

    if (proxy.get() == false) {
        writeToFile("frontend." + ctx.channel().id(), ByteBufUtil.getBytes(buf));
    }

    if (Config.getInstance().isCertAuth()) {
        SslHandler sslhandler = (SslHandler) ctx.channel().pipeline().get("ssl");
        try {
            X509Certificate cert = sslhandler.engine().getSession().getPeerCertificateChain()[0];
            Principal p = cert.getSubjectDN();

            /* Added by Miguel */
            LdapName ldapDN = new LdapName(p.getName());
            String username = "";
            String thumbprint = getThumbPrint(cert.getEncoded());

            for (Rdn rdn : ldapDN.getRdns()) {
                //System.out.println(rdn.getType() + " -> " + rdn.getValue());
                if (rdn.getType().equals("CN")) {
                    username = rdn.getValue().toString();
                    break;
                }
            }
            /* End Added by Miguel*/

            String sessionId = parseSessionID(data);

            if (sessionId != null) {
                String current = System.getProperty("user.dir");
                //System.out.println("Current working directory in Java : " + current);

                //File sessionFile = new File("c:/sessions/" + sessionId + ".txt");
                File sessionFile = new File(current + "/data/sessions/" + sessionId + ".txt");

                //only write the file if it hasn't been written yet.
                if (sessionFile.createNewFile()) {
                    FileUtils.write(sessionFile, p.getName().replaceAll("\"", "").replaceAll("\\+", ",") + "\n"
                            + username + "\n" + thumbprint);
                }
            }

        } catch (Exception e) {
            LOG.log(Level.SEVERE, null, e);
        }
    }

    if (proxy.get()) {
        ctx.channel().config().setAutoRead(false);
        if (outboundChannel.isActive()) {
            outboundChannel.writeAndFlush(buf).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) {
                    if (future.isSuccess()) {
                        // was able to flush out data, start to read the next chunk
                        ctx.channel().read();
                    } else {
                        future.channel().close();
                    }
                }
            });
        }
    } else {
        //make sure the backend handler knows its a websocket connection.
        this.handler.setWebsocket(true);

        //get handle on the pipeline.
        ChannelPipeline pipeline = ctx.pipeline();

        //apply the websocket handlers
        builder.apply(pipeline);

        //remove this handler.
        pipeline.remove(this);

        //fire the event to move on to the next handler.
        ctx.fireChannelRead(msg);
    }

}

From source file:com.tesora.dve.parlb.MysqlClientHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class)
            .handler(new LoadBalancerServerHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = b.connect(peServerAddress);
    outboundChannel = f.channel();//  w  w w.j a va  2  s .com
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();
            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });
}

From source file:com.test.AbstractBootstrap.java

License:Apache License

private static void doBind0(final ChannelFuture regFuture, final Channel channel,
        final SocketAddress localAddress, final ChannelPromise promise) {

    // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
    // the pipeline in its channelRegistered() implementation.
    channel.eventLoop().execute(new Runnable() {
        @Override/*from   w  ww  .ja  v  a2s.  c o m*/
        public void run() {
            if (regFuture.isSuccess()) {
                channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            } else {
                promise.setFailure(regFuture.cause());
            }
        }
    });
}