Search in sources :

Example 56 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class Bzip2Encoder method close.

@Override
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
    ChannelFuture f = finishEncode(ctx, ctx.newPromise());
    f.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            ctx.close(promise);
        }
    });
    if (!f.isDone()) {
        // Ensure the channel is closed even if the write operation completes in time.
        ctx.executor().schedule(new Runnable() {

            @Override
            public void run() {
                ctx.close(promise);
            }
        }, 10, // FIXME: Magic number
        TimeUnit.SECONDS);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 57 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class JZlibEncoder method close.

@Override
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) {
    ChannelFuture f = finishEncode(ctx, ctx.newPromise());
    f.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            ctx.close(promise);
        }
    });
    if (!f.isDone()) {
        // Ensure the channel is closed even if the write operation completes in time.
        ctx.executor().schedule(new Runnable() {

            @Override
            public void run() {
                ctx.close(promise);
            }
        }, 10, // FIXME: Magic number
        TimeUnit.SECONDS);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 58 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class UniqueIpFilter method accept.

@Override
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception {
    final InetAddress remoteIp = remoteAddress.getAddress();
    if (connected.contains(remoteIp)) {
        return false;
    } else {
        connected.add(remoteIp);
        ctx.channel().closeFuture().addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                connected.remove(remoteIp);
            }
        });
        return true;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetAddress(java.net.InetAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 59 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class BootstrapTest method testLateRegisterSuccess.

@Test
public void testLateRegisterSuccess() throws Exception {
    TestEventLoopGroup group = new TestEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group);
        bootstrap.channel(LocalServerChannel.class);
        bootstrap.childHandler(new DummyHandler());
        bootstrap.localAddress(new LocalAddress("1"));
        ChannelFuture future = bootstrap.bind();
        assertFalse(future.isDone());
        group.promise.setSuccess();
        final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
        future.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
                queue.add(future.isSuccess());
            }
        });
        assertTrue(queue.take());
        assertTrue(queue.take());
    } finally {
        group.shutdownGracefully();
        group.terminationFuture().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 60 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class SimpleChannelPool method acquireHealthyFromPoolOrNew.

/**
     * Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
     * @param promise the promise to provide acquire result.
     * @return future for acquiring a channel.
     */
private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise) {
    try {
        final Channel ch = pollChannel();
        if (ch == null) {
            // No Channel left in the pool bootstrap a new Channel
            Bootstrap bs = bootstrap.clone();
            bs.attr(POOL_KEY, this);
            ChannelFuture f = connectChannel(bs);
            if (f.isDone()) {
                notifyConnect(f, promise);
            } else {
                f.addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        notifyConnect(future, promise);
                    }
                });
            }
            return promise;
        }
        EventLoop loop = ch.eventLoop();
        if (loop.inEventLoop()) {
            doHealthCheck(ch, promise);
        } else {
            loop.execute(new Runnable() {

                @Override
                public void run() {
                    doHealthCheck(ch, promise);
                }
            });
        }
    } catch (Throwable cause) {
        promise.tryFailure(cause);
    }
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)92 ChannelFuture (io.netty.channel.ChannelFuture)87 Channel (io.netty.channel.Channel)29 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)24 Bootstrap (io.netty.bootstrap.Bootstrap)20 Test (org.junit.Test)19 ByteBuf (io.netty.buffer.ByteBuf)18 ClosedChannelException (java.nio.channels.ClosedChannelException)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)15 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 IOException (java.io.IOException)12 ChannelPromise (io.netty.channel.ChannelPromise)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 ConnectException (java.net.ConnectException)11 InetSocketAddress (java.net.InetSocketAddress)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AbstractChannel (io.netty.channel.AbstractChannel)6 EventLoopGroup (io.netty.channel.EventLoopGroup)6 ChannelPipeline (io.netty.channel.ChannelPipeline)5