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.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

@Override
public boolean joinGroup() {
    if (!isActive())
        return false;

    ChannelFuture cf = getChannel().joinGroup(getMulticastAddress(), getNetworkInterface());

    CountDownLatch latch = new CountDownLatch(1);
    cf.addListener(getJoinGroupListener(getMulticastAddress(), latch));

    await(latch, "Multicast channel join group timed out");

    return cf.isSuccess();
}

From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

@Override
public boolean leaveGroup() {
    if (!isActive())
        return false;

    ChannelFuture cf = getChannel().leaveGroup(getMulticastAddress(), getNetworkInterface());

    CountDownLatch latch = new CountDownLatch(1);
    cf.addListener(getLeaveGroupListener(getMulticastAddress(), latch));

    await(latch, "Multicast channel leave group timed out");

    return cf.isSuccess();
}

From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

@Override
public boolean block(InetAddress sourceToBlock) {
    if (!isActive())
        return false;

    ChannelFuture cf = getChannel().block(getMulticastAddress().getAddress(), getNetworkInterface(),
            sourceToBlock);//  w  w  w .  ja va2 s.  c  o m

    CountDownLatch latch = new CountDownLatch(1);
    cf.addListener(getBlockListener(getMulticastAddress(), latch, sourceToBlock));

    await(latch, "Multicast block timed out");

    return cf.isSuccess();
}

From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

private GenericFutureListener<ChannelFuture> getJoinGroupListener(final InetSocketAddress multicast,
        final CountDownLatch latch) {
    return new GenericFutureListener<ChannelFuture>() {

        @Override//from  w ww  .ja va2s  . c om
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    log.debug("Multicast channel joined group {}", multicast);
                } else {
                    Throwable cause = future.cause();
                    if (cause == null) {
                        log.error("Could not join multicast group for {}", multicast);
                    } else {
                        log.error("Could not join multicast group for {}", multicast, cause);
                    }
                }
            } finally {
                latch.countDown();
            }
        }
    };
}

From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

private GenericFutureListener<ChannelFuture> getLeaveGroupListener(final InetSocketAddress multicast,
        final CountDownLatch latch) {
    return new GenericFutureListener<ChannelFuture>() {

        @Override/*  w  w w .  j  ava2  s.  co m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    log.debug("Multicast channel left group {}", multicast);
                } else {
                    Throwable cause = future.cause();
                    if (cause == null) {
                        log.error("Could not leave multicast group for {}", multicast);
                    } else {
                        log.error("Could not leave multicast group for {}", multicast, cause);
                    }
                }
            } finally {
                latch.countDown();
            }
        }
    };
}

From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyMulticastChannel.java

License:Open Source License

private GenericFutureListener<ChannelFuture> getBlockListener(final InetSocketAddress multicast,
        final CountDownLatch latch, final InetAddress sourceToBlock) {
    return new GenericFutureListener<ChannelFuture>() {

        @Override//from w w  w  .  ja  va 2s  . co  m
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    log.debug("Multicast channel {} now blocking {}", multicast, sourceToBlock);
                } else {
                    Throwable cause = future.cause();
                    if (cause == null) {
                        log.error("Could not block {} from {}", sourceToBlock, multicast);
                    } else {
                        log.error("Could not block {} from {}", sourceToBlock, multicast, cause);
                    }
                }
            } finally {
                latch.countDown();
            }
        }
    };
}

From source file:com.github.mrstampy.kitchensync.test.stream.ByteArrayStreamerTester.java

License:Open Source License

/**
 * Stream.// w  w  w . j  ava2 s  . c o m
 *
 * @throws Exception
 *           the exception
 */
protected void stream() throws Exception {
    for (String file : files) {
        byte[] b = getBytes(file);
        ChannelFuture future = streamer.stream(b);
        future.awaitUninterruptibly();

        log.info("Success? {}", future.isSuccess());
        BigDecimal packetLoss = (BigDecimal.ONE.subtract(new BigDecimal(received.get())
                .divide(new BigDecimal(streamer.sent()), 6, RoundingMode.HALF_UP)))
                        .multiply(new BigDecimal(100));
        log.info("Sent: {}, Received: {}, Packet loss: {} %, Concurrent threads: {}", streamer.sent(),
                received.get(), packetLoss.toPlainString(), streamer.getConcurrentThreads());

        received.set(0);
    }

    streamer.cancel();
}

From source file:com.github.mrstampy.kitchensync.test.stream.StreamerTester.java

License:Open Source License

/**
 * Stream.//from  w  w  w  .j a va2s .  com
 *
 * @throws InterruptedException
 *           the interrupted exception
 */
protected void stream() throws InterruptedException {
    for (int i = 0; i < 3; i++) {
        ChannelFuture future = streamer.stream();
        future.awaitUninterruptibly();

        log.info("Success? {}", future.isSuccess());
        BigDecimal packetLoss = (BigDecimal.ONE.subtract(new BigDecimal(received.get())
                .divide(new BigDecimal(streamer.size()), 6, RoundingMode.HALF_UP)))
                        .multiply(new BigDecimal(100));
        log.info("Sent: {}, Received: {}, Packet loss: {} %, Concurrent threads: {}", streamer.size(),
                received.get(), packetLoss.toPlainString(), streamer.getConcurrentThreads());
        streamer.reset();
        received.set(0);

        KiSyUtils.snooze(100);
    }

    Thread.sleep(50);
}

From source file:com.github.rmannibucau.featuredmock.http.DefaultFeaturedHttpServer.java

License:Apache License

@Override
public FeaturedHttpServer start() {
    workerGroup = new NioEventLoopGroup(threads, new FeaturedThreadFactory());

    try {/*from   w ww  . j  a  v  a 2s .co m*/
        final ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_SNDBUF, 1024)
                .option(ChannelOption.TCP_NODELAY, true).group(workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new FeaturedChannelInitializer(mappers, engine)).bind(host, port)
                .addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(final ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            LOGGER.severe("Can't start HTTP server");
                        } else {
                            LOGGER.info(String.format("Server started on http://%s:%s", host, port));
                        }
                    }
                }).sync();
    } catch (final InterruptedException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    }

    return this;
}

From source file:com.github.sinsinpub.pero.backend.ConnectBackendHandler.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
    final Channel inboundChannel = ctx.channel();
    final Promise<Channel> outboundPromise = newOutboundPromise(ctx, request);
    final int maxProxies = AppProps.PROPS.getInteger("upstream.socks5.count", 1);
    final AtomicBoolean isFinalSuccess = new AtomicBoolean(false);
    final AtomicBoolean isRetryOccured = new AtomicBoolean(false);
    for (int i = 1; i <= maxProxies; i++) {
        final boolean isFirstOne = (i == 1);
        final boolean isFinalOne = (i == maxProxies);
        if (!isFirstOne) {
            isRetryOccured.set(true);/* ww  w.ja va 2  s.  com*/
        }
        try {
            final ProxyHandler upstreamProxyHandler = newUpstreamProxyHandler(i);
            final Bootstrap b = newConnectionBootstrap(inboundChannel, outboundPromise, upstreamProxyHandler);
            logger.info(String.format("Connecting backend %s:%s via %s", request.host(), request.port(),
                    upstreamProxyHandler != null ? upstreamProxyHandler.proxyAddress() : "direct"));
            ChannelFuture connFuture = b.connect(request.host(), request.port());
            connFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        // Connection established use handler provided results
                        if (upstreamProxyHandler == null) {
                            logger.info("Backend connected directly");
                        } else {
                            logger.info("Backend connected via: " + upstreamProxyHandler.proxyAddress());
                        }
                    } else {
                        // Close the connection if the connection attempt has failed.
                        if (isFinalOne) {
                            logger.error("Backend connection failed: " + future.cause(), future.cause());
                            ctx.channel().writeAndFlush(
                                    new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
                            NettyChannelUtils.closeOnFlush(ctx.channel());
                        } else {
                            logger.warn("Backend connection failed: {}", future.cause().toString());
                        }
                    }
                }
            });
            connFuture.await(getConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
            if (connFuture.isSuccess()) {
                isFinalSuccess.set(true);
                break;
            }
        } catch (Exception e) {
            logger.error("Connecting exception {} caught:", e);
        }
    }
    if (isFinalSuccess.get() && isRetryOccured.get()) {
        logger.warn("Protected from Error-Neko: {} times", nekoKilled.incrementAndGet());
    }
}