From 4e77bacdf793dc647552a3c21b75da04a2eba20b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 30 Dec 2012 17:40:24 +0100 Subject: [PATCH] [#873] [#868] Split ChannelFuture into ChannelFuture and ChannelPromise --- .../websocketx/WebSocketClientHandshaker.java | 9 +- .../WebSocketClientHandshaker00.java | 9 +- .../WebSocketClientHandshaker07.java | 9 +- .../WebSocketClientHandshaker08.java | 9 +- .../WebSocketClientHandshaker13.java | 9 +- .../websocketx/WebSocketServerHandshaker.java | 17 +- .../WebSocketServerHandshaker00.java | 13 +- .../WebSocketServerHandshaker07.java | 11 +- .../WebSocketServerHandshaker08.java | 15 +- .../WebSocketServerHandshaker13.java | 15 +- .../codec/spdy/SpdySessionHandler.java | 23 +- .../WebSocketServerProtocolHandlerTest.java | 3 +- .../netty/handler/codec/ByteToByteCodec.java | 6 +- .../handler/codec/ByteToByteEncoder.java | 6 +- .../handler/codec/ByteToMessageCodec.java | 6 +- .../handler/codec/MessageToByteEncoder.java | 6 +- .../handler/codec/MessageToMessageCodec.java | 4 +- .../codec/MessageToMessageEncoder.java | 6 +- .../codec/compression/JZlibEncoder.java | 17 +- .../codec/compression/JdkZlibEncoder.java | 23 +- .../codec/compression/ZlibEncoder.java | 3 +- .../sctp/SctpOutboundByteStreamHandler.java | 6 +- .../client/WebSocketClientHandler.java | 5 +- .../handler/logging/ByteLoggingHandler.java | 6 +- .../netty/handler/logging/LoggingHandler.java | 26 +-- .../logging/MessageLoggingHandler.java | 6 +- .../java/io/netty/handler/ssl/SslHandler.java | 117 +++++----- .../handler/stream/ChunkedWriteHandler.java | 13 +- .../handler/timeout/IdleStateHandler.java | 34 +-- .../handler/timeout/WriteTimeoutHandler.java | 17 +- .../AbstractTrafficShapingHandler.java | 10 +- .../io/netty/bootstrap/AbstractBootstrap.java | 7 +- .../java/io/netty/bootstrap/Bootstrap.java | 7 +- .../io/netty/bootstrap/ServerBootstrap.java | 3 +- .../io/netty/channel/AbstractChannel.java | 190 +++++++++-------- .../netty/channel/AbstractServerChannel.java | 12 +- .../main/java/io/netty/channel/Channel.java | 36 ++-- ....java => ChannelFlushPromiseNotifier.java} | 28 +-- .../java/io/netty/channel/ChannelFuture.java | 54 +---- .../netty/channel/ChannelHandlerAdapter.java | 28 +-- .../channel/ChannelOperationHandler.java | 28 +-- .../ChannelOperationHandlerAdapter.java | 42 ++-- .../netty/channel/ChannelOutboundInvoker.java | 52 ++--- .../java/io/netty/channel/ChannelPromise.java | 102 +++++++++ ...tor.java => ChannelPromiseAggregator.java} | 41 ++-- ...ifier.java => ChannelPromiseNotifier.java} | 41 ++-- .../netty/channel/ChannelPropertyAccess.java | 4 +- .../netty/channel/CombinedChannelHandler.java | 28 +-- .../netty/channel/CompleteChannelFuture.java | 34 +-- .../netty/channel/CompleteChannelPromise.java | 84 ++++++++ .../channel/DefaultChannelHandlerContext.java | 62 +++--- .../netty/channel/DefaultChannelPipeline.java | 180 ++++++++-------- ...Future.java => DefaultChannelPromise.java} | 201 ++++++++---------- .../java/io/netty/channel/EventLoopGroup.java | 2 +- .../io/netty/channel/FailedChannelFuture.java | 13 -- .../channel/MultithreadEventLoopGroup.java | 4 +- .../netty/channel/SingleThreadEventLoop.java | 14 +- .../netty/channel/SucceededChannelFuture.java | 13 -- ...nelFuture.java => VoidChannelPromise.java} | 53 ++--- .../embedded/AbstractEmbeddedChannel.java | 6 +- .../channel/embedded/EmbeddedEventLoop.java | 9 +- .../io/netty/channel/local/LocalChannel.java | 22 +- .../netty/channel/socket/DatagramChannel.java | 17 +- .../io/netty/channel/socket/SctpChannel.java | 5 +- .../netty/channel/socket/SocketChannel.java | 5 +- .../socket/aio/AbstractAioChannel.java | 34 +-- .../channel/socket/aio/AioEventLoop.java | 3 +- .../socket/aio/AioServerSocketChannel.java | 6 +- .../channel/socket/aio/AioSocketChannel.java | 41 ++-- .../socket/nio/AbstractNioByteChannel.java | 26 +-- .../socket/nio/AbstractNioChannel.java | 30 +-- .../socket/nio/NioDatagramChannel.java | 71 ++++--- .../channel/socket/nio/NioSctpChannel.java | 25 +-- .../channel/socket/nio/NioSocketChannel.java | 13 +- .../socket/oio/AbstractOioChannel.java | 12 +- .../socket/oio/OioDatagramChannel.java | 65 +++--- .../channel/socket/oio/OioEventLoop.java | 5 +- .../channel/socket/oio/OioEventLoopGroup.java | 13 +- .../channel/socket/oio/OioSctpChannel.java | 25 +-- .../channel/socket/oio/OioSocketChannel.java | 11 +- .../channel/CompleteChannelFutureTest.java | 15 -- .../channel/SingleThreadEventLoopTest.java | 2 +- .../local/LocalTransportThreadModelTest.java | 12 +- 83 files changed, 1194 insertions(+), 1101 deletions(-) rename transport/src/main/java/io/netty/channel/{ChannelFlushFutureNotifier.java => ChannelFlushPromiseNotifier.java} (89%) create mode 100644 transport/src/main/java/io/netty/channel/ChannelPromise.java rename transport/src/main/java/io/netty/channel/{ChannelFutureAggregator.java => ChannelPromiseAggregator.java} (52%) rename transport/src/main/java/io/netty/channel/{ChannelFutureNotifier.java => ChannelPromiseNotifier.java} (56%) create mode 100644 transport/src/main/java/io/netty/channel/CompleteChannelPromise.java rename transport/src/main/java/io/netty/channel/{DefaultChannelFuture.java => DefaultChannelPromise.java} (79%) rename transport/src/main/java/io/netty/channel/{VoidChannelFuture.java => VoidChannelPromise.java} (69%) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java index 0a95662a99..c42610a0e3 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java @@ -17,6 +17,7 @@ package io.netty.handler.codec.http.websocketx; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.HttpResponse; import java.net.URI; @@ -126,7 +127,7 @@ public abstract class WebSocketClientHandshaker { if (channel == null) { throw new NullPointerException("channel"); } - return handshake(channel, channel.newFuture()); + return handshake(channel, channel.newPromise()); } /** @@ -134,10 +135,10 @@ public abstract class WebSocketClientHandshaker { * * @param channel * Channel - * @param future - * the {@link ChannelFuture} to be notified when the opening handshake is sent + * @param promise + * the {@link ChannelPromise} to be notified when the opening handshake is sent */ - public abstract ChannelFuture handshake(Channel channel, ChannelFuture future); + public abstract ChannelFuture handshake(Channel channel, ChannelPromise promise); /** * Validates and finishes the opening handshake initiated by {@link #handshake}}. diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index 736e04dd5d..9749863253 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -20,6 +20,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; @@ -91,7 +92,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { * Channel into which we can write our request */ @Override - public ChannelFuture handshake(Channel channel, final ChannelFuture handshakeFuture) { + public ChannelFuture handshake(Channel channel, final ChannelPromise promise) { // Make keys int spaces1 = WebSocketUtil.randomNumber(1, 12); int spaces2 = WebSocketUtil.randomNumber(1, 12); @@ -183,14 +184,14 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { "ws-encoder", new WebSocket00FrameEncoder()); if (future.isSuccess()) { - handshakeFuture.setSuccess(); + promise.setSuccess(); } else { - handshakeFuture.setFailure(future.cause()); + promise.setFailure(future.cause()); } } }); - return handshakeFuture; + return promise; } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java index 46512cdda1..2cf9ef0f48 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; @@ -97,7 +98,7 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { * Channel into which we can write our request */ @Override - public ChannelFuture handshake(Channel channel, final ChannelFuture handshakeFuture) { + public ChannelFuture handshake(Channel channel, final ChannelPromise promise) { // Get path URI wsURL = getWebSocketUrl(); String path = wsURL.getPath(); @@ -161,14 +162,14 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { "ws-encoder", new WebSocket07FrameEncoder(true)); if (future.isSuccess()) { - handshakeFuture.setSuccess(); + promise.setSuccess(); } else { - handshakeFuture.setFailure(future.cause()); + promise.setFailure(future.cause()); } } }); - return handshakeFuture; + return promise; } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java index efa9f3ba02..98ddcf1e2d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; @@ -97,7 +98,7 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { * Channel into which we can write our request */ @Override - public ChannelFuture handshake(Channel channel, final ChannelFuture handshakeFuture) { + public ChannelFuture handshake(Channel channel, final ChannelPromise promise) { // Get path URI wsURL = getWebSocketUrl(); String path = wsURL.getPath(); @@ -161,14 +162,14 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { "ws-encoder", new WebSocket08FrameEncoder(true)); if (future.isSuccess()) { - handshakeFuture.setSuccess(); + promise.setSuccess(); } else { - handshakeFuture.setFailure(future.cause()); + promise.setFailure(future.cause()); } } }); - return handshakeFuture; + return promise; } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java index ce3336f25e..2371cda1d4 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; @@ -97,7 +98,7 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { * Channel into which we can write our request */ @Override - public ChannelFuture handshake(Channel channel, final ChannelFuture handshakeFuture) { + public ChannelFuture handshake(Channel channel, final ChannelPromise promise) { // Get path URI wsURL = getWebSocketUrl(); String path = wsURL.getPath(); @@ -161,13 +162,13 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { "ws-encoder", new WebSocket13FrameEncoder(true)); if (future.isSuccess()) { - handshakeFuture.setSuccess(); + promise.setSuccess(); } else { - handshakeFuture.setFailure(future.cause()); + promise.setFailure(future.cause()); } } }); - return handshakeFuture; + return promise; } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java index 5f69258a24..6b89cbefe0 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java @@ -17,6 +17,7 @@ package io.netty.handler.codec.http.websocketx; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.HttpRequest; import io.netty.util.internal.StringUtil; @@ -115,7 +116,7 @@ public abstract class WebSocketServerHandshaker { if (channel == null) { throw new NullPointerException("channel"); } - return handshake(channel, req, channel.newFuture()); + return handshake(channel, req, channel.newPromise()); } /** @@ -125,10 +126,10 @@ public abstract class WebSocketServerHandshaker { * Channel * @param req * HTTP Request - * @param future - * the {@link ChannelFuture} to be notified when the opening handshake is done + * @param promise + * the {@link ChannelPromise} to be notified when the opening handshake is done */ - public abstract ChannelFuture handshake(Channel channel, HttpRequest req, ChannelFuture future); + public abstract ChannelFuture handshake(Channel channel, HttpRequest req, ChannelPromise promise); /** * Performs the closing handshake @@ -142,7 +143,7 @@ public abstract class WebSocketServerHandshaker { if (channel == null) { throw new NullPointerException("channel"); } - return close(channel, frame, channel.newFuture()); + return close(channel, frame, channel.newPromise()); } /** @@ -152,10 +153,10 @@ public abstract class WebSocketServerHandshaker { * Channel * @param frame * Closing Frame that was received - * @param future - * the {@link ChannelFuture} to be notified when the closing handshake is done + * @param promise + * the {@link ChannelPromise} to be notified when the closing handshake is done */ - public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelFuture future); + public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise); /** * Selects the first matching supported sub protocol diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index 5cf11f9f22..0836082a7f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -21,6 +21,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunkAggregator; import io.netty.handler.codec.http.HttpHeaders.Names; @@ -118,7 +119,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { * HTTP request */ @Override - public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelFuture future) { + public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelPromise promise) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 00 server handshake", channel.id())); @@ -180,8 +181,8 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { } // Upgrade the connection and send the handshake response. - channel.write(res, future); - future.addListener(new ChannelFutureListener() { + channel.write(res, promise); + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); @@ -195,7 +196,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { } }); - return future; + return promise; } /** @@ -207,7 +208,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { * Web Socket frame that was received */ @Override - public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelFuture future) { - return channel.write(frame, future); + public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) { + return channel.write(frame, promise); } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java index 33a20fab36..ab7126ba72 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunkAggregator; import io.netty.handler.codec.http.HttpHeaders.Names; @@ -108,7 +109,7 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { * HTTP request */ @Override - public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelFuture future) { + public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelPromise promise) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 7 server handshake", channel.id())); @@ -143,10 +144,10 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { } } - channel.write(res, future); + channel.write(res, promise); // Upgrade the connection and send the handshake response. - future.addListener(new ChannelFutureListener() { + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); @@ -160,7 +161,7 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { } }); - return future; + return promise; } /** @@ -172,7 +173,7 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { * Web Socket frame that was received */ @Override - public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelFuture future) { + public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise future) { future.addListener(ChannelFutureListener.CLOSE); return channel.write(frame, future); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java index 76d21d70b6..3c8059ce15 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunkAggregator; import io.netty.handler.codec.http.HttpHeaders.Names; @@ -109,7 +110,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { * HTTP request */ @Override - public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelFuture future) { + public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelPromise promise) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 8 server handshake", channel.id())); @@ -144,10 +145,10 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { } } - channel.write(res, future); + channel.write(res, promise); // Upgrade the connection and send the handshake response. - future.addListener(new ChannelFutureListener() { + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); @@ -161,7 +162,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { } }); - return future; + return promise; } /** @@ -173,8 +174,8 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { * Web Socket frame that was received */ @Override - public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelFuture future) { - future.addListener(ChannelFutureListener.CLOSE); - return channel.write(frame, future); + public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) { + promise.addListener(ChannelFutureListener.CLOSE); + return channel.write(frame, promise); } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java index e36b1dd18a..5946857371 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java @@ -19,6 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunkAggregator; import io.netty.handler.codec.http.HttpHeaders.Names; @@ -108,7 +109,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { * HTTP request */ @Override - public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelFuture future) { + public ChannelFuture handshake(Channel channel, HttpRequest req, ChannelPromise promise) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.id())); @@ -144,10 +145,10 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { } } - channel.write(res, future); + channel.write(res, promise); // Upgrade the connection and send the handshake response. - future.addListener(new ChannelFutureListener() { + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); @@ -161,7 +162,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { } }); - return future; + return promise; } /** @@ -173,8 +174,8 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { * Web Socket frame that was received */ @Override - public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelFuture future) { - future.addListener(ChannelFutureListener.CLOSE); - return channel.write(frame, future); + public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) { + promise.addListener(ChannelFutureListener.CLOSE); + return channel.write(frame, promise); } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java index c5852c0cdf..e1a2bbaa94 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandler; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelPromise; import java.util.concurrent.atomic.AtomicInteger; @@ -62,7 +63,7 @@ public class SpdySessionHandler private volatile boolean sentGoAwayFrame; private volatile boolean receivedGoAwayFrame; - private volatile ChannelFuture closeSessionFuture; + private volatile ChannelPromise closeSessionFuture; private final boolean server; private final boolean flowControl; @@ -438,12 +439,12 @@ public class SpdySessionHandler } @Override - public void close(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - sendGoAwayFrame(ctx, future); + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + sendGoAwayFrame(ctx, promise); } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { MessageBuf in = ctx.outboundMessageBuffer(); for (;;) { Object msg = in.poll(); @@ -464,7 +465,7 @@ public class SpdySessionHandler ctx.nextOutboundMessageBuffer().add(msg); } } - ctx.flush(future); + ctx.flush(promise); } private void handleOutboundMessage(ChannelHandlerContext ctx, Object msg) @@ -876,7 +877,7 @@ public class SpdySessionHandler } } - private void sendGoAwayFrame(ChannelHandlerContext ctx, ChannelFuture future) { + private void sendGoAwayFrame(ChannelHandlerContext ctx, ChannelPromise future) { // Avoid NotYetConnectedException if (!ctx.channel().isActive()) { ctx.close(future); @@ -888,7 +889,7 @@ public class SpdySessionHandler if (spdySession.noActiveStreams()) { f.addListener(new ClosingChannelFutureListener(ctx, future)); } else { - closeSessionFuture = ctx.newFuture(); + closeSessionFuture = ctx.newPromise(); closeSessionFuture.addListener(new ClosingChannelFutureListener(ctx, future)); } // FIXME: Close the connection forcibly after timeout. @@ -905,16 +906,16 @@ public class SpdySessionHandler private static final class ClosingChannelFutureListener implements ChannelFutureListener { private final ChannelHandlerContext ctx; - private final ChannelFuture future; + private final ChannelPromise promise; - ClosingChannelFutureListener(ChannelHandlerContext ctx, ChannelFuture future) { + ClosingChannelFutureListener(ChannelHandlerContext ctx, ChannelPromise promise) { this.ctx = ctx; - this.future = future; + this.promise = promise; } @Override public void operationComplete(ChannelFuture sentGoAwayFuture) throws Exception { - ctx.close(future); + ctx.close(promise); } } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java index d0010a99f7..62bf3b0fc3 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelOutboundMessageHandlerAdapter; +import io.netty.channel.ChannelPromise; import io.netty.channel.embedded.EmbeddedMessageChannel; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpMethod; @@ -138,7 +139,7 @@ public class WebSocketServerProtocolHandlerTest { private static class MockOutboundHandler extends ChannelOutboundMessageHandlerAdapter { @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { //NoOp } } diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteCodec.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteCodec.java index 646a19b249..bc8631d7b3 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteCodec.java @@ -17,11 +17,11 @@ package io.netty.handler.codec; import io.netty.buffer.Buf; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundByteHandler; +import io.netty.channel.ChannelPromise; /** * A Codec for on-the-fly encoding/decoding of bytes. @@ -92,8 +92,8 @@ public abstract class ByteToByteCodec @Override public void flush( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - encoder.flush(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + encoder.flush(ctx, promise); } @Override diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java index 48e569bc28..5055e5d9a3 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java @@ -16,9 +16,9 @@ package io.netty.handler.codec; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundByteHandlerAdapter; +import io.netty.channel.ChannelPromise; /** * {@link ChannelOutboundByteHandlerAdapter} which encodes bytes in a stream-like fashion from one {@link ByteBuf} to an @@ -46,7 +46,7 @@ import io.netty.channel.ChannelOutboundByteHandlerAdapter; public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapter { @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { ByteBuf in = ctx.outboundByteBuffer(); ByteBuf out = ctx.nextOutboundByteBuffer(); @@ -67,7 +67,7 @@ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapte } in.discardSomeReadBytes(); - ctx.flush(future); + ctx.flush(promise); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java index 8252882a40..b8e0237af3 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java @@ -18,11 +18,11 @@ package io.netty.handler.codec; import io.netty.buffer.Buf; import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelPromise; public abstract class ByteToMessageCodec extends ChannelHandlerAdapter @@ -66,8 +66,8 @@ public abstract class ByteToMessageCodec @Override public void flush( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - encoder.flush(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + encoder.flush(ctx, promise); } @Override diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java index da71dadf96..fc1b545bd2 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java @@ -17,12 +17,12 @@ package io.netty.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundMessageHandler; import io.netty.channel.ChannelOutboundMessageHandlerAdapter; import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelHandlerUtil; +import io.netty.channel.ChannelPromise; /** @@ -55,7 +55,7 @@ public abstract class MessageToByteEncoder extends ChannelOutboundMessageHand } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { MessageBuf in = ctx.outboundMessageBuffer(); ByteBuf out = ctx.nextOutboundByteBuffer(); @@ -83,7 +83,7 @@ public abstract class MessageToByteEncoder extends ChannelOutboundMessageHand } } - ctx.flush(future); + ctx.flush(promise); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java index 5d2a5c791b..e29d93b07c 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java @@ -17,12 +17,12 @@ package io.netty.handler.codec; import io.netty.buffer.Buf; import io.netty.buffer.MessageBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerUtil; import io.netty.channel.ChannelInboundMessageHandler; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelPromise; /** * A Codec for on-the-fly encoding/decoding of message. @@ -120,7 +120,7 @@ public abstract class MessageToMessageCodec extends ChannelOutboundMessa } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { MessageBuf in = ctx.outboundMessageBuffer(); for (;;) { try { @@ -89,7 +89,7 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundMessa } } - ctx.flush(future); + ctx.flush(promise); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/compression/JZlibEncoder.java b/codec/src/main/java/io/netty/handler/codec/compression/JZlibEncoder.java index 19b58c6de3..159edfe482 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/JZlibEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/JZlibEncoder.java @@ -20,6 +20,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; import io.netty.util.internal.jzlib.JZlib; import io.netty.util.internal.jzlib.ZStream; @@ -243,12 +244,12 @@ public class JZlibEncoder extends ZlibEncoder { @Override public ChannelFuture close() { - return close(ctx().channel().newFuture()); + return close(ctx().channel().newPromise()); } @Override - public ChannelFuture close(ChannelFuture future) { - return finishEncode(ctx(), future); + public ChannelFuture close(ChannelPromise promise) { + return finishEncode(ctx(), promise); } private ChannelHandlerContext ctx() { @@ -338,12 +339,12 @@ public class JZlibEncoder extends ZlibEncoder { @Override public void close( final ChannelHandlerContext ctx, - final ChannelFuture future) throws Exception { - ChannelFuture f = finishEncode(ctx, ctx.newFuture()); + 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(future); + ctx.close(promise); } }); @@ -352,13 +353,13 @@ public class JZlibEncoder extends ZlibEncoder { ctx.executor().schedule(new Runnable() { @Override public void run() { - ctx.close(future); + ctx.close(promise); } }, 10, TimeUnit.SECONDS); // FIXME: Magic number } } - private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelFuture future) { + private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise future) { if (!finished.compareAndSet(false, true)) { future.setSuccess(); return future; diff --git a/codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java b/codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java index cb11401df5..a4bd6c344a 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java @@ -20,6 +20,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -155,11 +156,11 @@ public class JdkZlibEncoder extends ZlibEncoder { @Override public ChannelFuture close() { - return close(ctx().newFuture()); + return close(ctx().newPromise()); } @Override - public ChannelFuture close(ChannelFuture future) { + public ChannelFuture close(ChannelPromise future) { return finishEncode(ctx(), future); } @@ -209,12 +210,12 @@ public class JdkZlibEncoder extends ZlibEncoder { } @Override - public void close(final ChannelHandlerContext ctx, final ChannelFuture future) throws Exception { - ChannelFuture f = finishEncode(ctx, ctx.newFuture()); + 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(future); + ctx.close(promise); } }); @@ -223,16 +224,16 @@ public class JdkZlibEncoder extends ZlibEncoder { ctx.executor().schedule(new Runnable() { @Override public void run() { - ctx.close(future); + ctx.close(promise); } }, 10, TimeUnit.SECONDS); // FIXME: Magic number } } - private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelFuture future) { + private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) { if (!finished.compareAndSet(false, true)) { - future.setSuccess(); - return future; + promise.setSuccess(); + return promise; } ByteBuf footer = Unpooled.buffer(); @@ -258,9 +259,9 @@ public class JdkZlibEncoder extends ZlibEncoder { } ctx.nextOutboundByteBuffer().writeBytes(footer); - ctx.flush(future); + ctx.flush(promise); - return future; + return promise; } @Override diff --git a/codec/src/main/java/io/netty/handler/codec/compression/ZlibEncoder.java b/codec/src/main/java/io/netty/handler/codec/compression/ZlibEncoder.java index 1a188566c4..6122416889 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/ZlibEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/ZlibEncoder.java @@ -17,6 +17,7 @@ package io.netty.handler.codec.compression; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.ByteToByteEncoder; /** @@ -45,6 +46,6 @@ public abstract class ZlibEncoder extends ByteToByteEncoder { * The given {@link ChannelFuture} will be notified once the operation * completes and will also be returned. */ - public abstract ChannelFuture close(ChannelFuture future); + public abstract ChannelFuture close(ChannelPromise promise); } diff --git a/codec/src/main/java/io/netty/handler/codec/sctp/SctpOutboundByteStreamHandler.java b/codec/src/main/java/io/netty/handler/codec/sctp/SctpOutboundByteStreamHandler.java index cb44c03b9c..88e0459724 100644 --- a/codec/src/main/java/io/netty/handler/codec/sctp/SctpOutboundByteStreamHandler.java +++ b/codec/src/main/java/io/netty/handler/codec/sctp/SctpOutboundByteStreamHandler.java @@ -18,9 +18,9 @@ package io.netty.handler.codec.sctp; import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundByteHandlerAdapter; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.SctpMessage; import io.netty.handler.codec.EncoderException; @@ -43,7 +43,7 @@ public class SctpOutboundByteStreamHandler extends ChannelOutboundByteHandlerAda } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { ByteBuf in = ctx.outboundByteBuffer(); try { @@ -56,6 +56,6 @@ public class SctpOutboundByteStreamHandler extends ChannelOutboundByteHandlerAda ctx.fireExceptionCaught(new EncoderException(t)); } - ctx.flush(future); + ctx.flush(promise); } } diff --git a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java index 8902029a6c..84874d9406 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java @@ -41,6 +41,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; @@ -52,7 +53,7 @@ import io.netty.util.CharsetUtil; public class WebSocketClientHandler extends ChannelInboundMessageHandlerAdapter { private final WebSocketClientHandshaker handshaker; - private ChannelFuture handshakeFuture; + private ChannelPromise handshakeFuture; public WebSocketClientHandler(WebSocketClientHandshaker handshaker) { this.handshaker = handshaker; @@ -64,7 +65,7 @@ public class WebSocketClientHandler extends ChannelInboundMessageHandlerAdapter< @Override public void beforeAdd(ChannelHandlerContext ctx) throws Exception { - handshakeFuture = ctx.newFuture(); + handshakeFuture = ctx.newPromise(); } @Override diff --git a/handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java b/handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java index 4090e5791f..a206735256 100644 --- a/handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java +++ b/handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java @@ -17,10 +17,10 @@ package io.netty.handler.logging; import io.netty.buffer.Buf; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundByteHandler; +import io.netty.channel.ChannelPromise; public class ByteLoggingHandler extends LoggingHandler implements ChannelInboundByteHandler, ChannelOutboundByteHandler { @@ -138,14 +138,14 @@ public class ByteLoggingHandler } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { ByteBuf buf = ctx.outboundByteBuffer(); if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, formatBuffer("WRITE", buf))); } ctx.nextOutboundByteBuffer().writeBytes(buf); - ctx.flush(future); + ctx.flush(promise); } protected String formatBuffer(String message, ByteBuf buf) { diff --git a/handler/src/main/java/io/netty/handler/logging/LoggingHandler.java b/handler/src/main/java/io/netty/handler/logging/LoggingHandler.java index 57ef11a1a3..dc3256efa3 100644 --- a/handler/src/main/java/io/netty/handler/logging/LoggingHandler.java +++ b/handler/src/main/java/io/netty/handler/logging/LoggingHandler.java @@ -15,11 +15,11 @@ */ package io.netty.handler.logging; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; import io.netty.logging.InternalLogLevel; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; @@ -186,48 +186,48 @@ public class LoggingHandler extends ChannelHandlerAdapter { @Override public void bind(ChannelHandlerContext ctx, - SocketAddress localAddress, ChannelFuture future) throws Exception { + SocketAddress localAddress, ChannelPromise promise) throws Exception { if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, "BIND(" + localAddress + ')')); } - super.bind(ctx, localAddress, future); + super.bind(ctx, localAddress, promise); } @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, - ChannelFuture future) throws Exception { + ChannelPromise promise) throws Exception { if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, "CONNECT(" + remoteAddress + ", " + localAddress + ')')); } - super.connect(ctx, remoteAddress, localAddress, future); + super.connect(ctx, remoteAddress, localAddress, promise); } @Override public void disconnect(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise promise) throws Exception { if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, "DISCONNECT()")); } - super.disconnect(ctx, future); + super.disconnect(ctx, promise); } @Override public void close(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise promise) throws Exception { if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, "CLOSE()")); } - super.close(ctx, future); + super.close(ctx, promise); } @Override public void deregister(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise promise) throws Exception { if (logger.isEnabled(internalLevel)) { logger.log(internalLevel, format(ctx, "DEREGISTER()")); } - super.deregister(ctx, future); + super.deregister(ctx, promise); } @Override @@ -236,9 +236,9 @@ public class LoggingHandler extends ChannelHandlerAdapter { } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - ctx.flush(future); + ctx.flush(promise); } @Override diff --git a/handler/src/main/java/io/netty/handler/logging/MessageLoggingHandler.java b/handler/src/main/java/io/netty/handler/logging/MessageLoggingHandler.java index c71524cf41..e95bf7b13d 100644 --- a/handler/src/main/java/io/netty/handler/logging/MessageLoggingHandler.java +++ b/handler/src/main/java/io/netty/handler/logging/MessageLoggingHandler.java @@ -18,10 +18,10 @@ package io.netty.handler.logging; import io.netty.buffer.Buf; import io.netty.buffer.MessageBuf; import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandler; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelPromise; public class MessageLoggingHandler extends LoggingHandler @@ -88,7 +88,7 @@ public class MessageLoggingHandler } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { MessageBuf buf = ctx.outboundMessageBuffer(); if (logger.isEnabled(internalLevel)) { @@ -103,7 +103,7 @@ public class MessageLoggingHandler } out.add(o); } - ctx.flush(future); + ctx.flush(promise); } protected String formatBuffer(String message, MessageBuf buf) { diff --git a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java index f1d3fd7322..3132feb7f8 100644 --- a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java +++ b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java @@ -19,7 +19,7 @@ import io.netty.buffer.Buf; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.channel.Channel; -import io.netty.channel.ChannelFlushFutureNotifier; +import io.netty.channel.ChannelFlushPromiseNotifier; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerAdapter; @@ -27,7 +27,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundByteHandler; import io.netty.channel.ChannelPipeline; -import io.netty.channel.DefaultChannelFuture; +import io.netty.channel.ChannelPromise; +import io.netty.channel.DefaultChannelPromise; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; import io.netty.util.internal.DetectionUtil; @@ -157,12 +158,12 @@ public class SslHandler private volatile ChannelHandlerContext ctx; private final SSLEngine engine; private final Executor delegatedTaskExecutor; - private final ChannelFlushFutureNotifier flushFutureNotifier = new ChannelFlushFutureNotifier(); + private final ChannelFlushPromiseNotifier flushFutureNotifier = new ChannelFlushPromiseNotifier(); private final boolean startTls; private boolean sentFirstMessage; - private final Queue handshakeFutures = new ArrayDeque(); + private final Queue handshakePromises = new ArrayDeque(); private final SSLEngineInboundCloseFuture sslCloseFuture = new SSLEngineInboundCloseFuture(); private volatile long handshakeTimeoutMillis = 10000; @@ -277,16 +278,16 @@ public class SslHandler * get notified once the handshake completes. */ public ChannelFuture handshake() { - return handshake(ctx.newFuture()); + return handshake(ctx.newPromise()); } /** * Starts an SSL / TLS handshake for the specified channel. * - * @return a {@link ChannelFuture} which is notified when the handshake + * @return a {@link ChannelPromise} which is notified when the handshake * succeeds or fails. */ - public ChannelFuture handshake(final ChannelFuture future) { + public ChannelFuture handshake(final ChannelPromise promise) { final ChannelHandlerContext ctx = this.ctx; final ScheduledFuture timeoutFuture; @@ -294,12 +295,12 @@ public class SslHandler timeoutFuture = ctx.executor().schedule(new Runnable() { @Override public void run() { - if (future.isDone()) { + if (promise.isDone()) { return; } SSLException e = new SSLException("handshake timed out"); - if (future.setFailure(e)) { + if (promise.tryFailure(e)) { ctx.fireExceptionCaught(e); ctx.close(); } @@ -317,10 +318,10 @@ public class SslHandler timeoutFuture.cancel(false); } engine.beginHandshake(); - handshakeFutures.add(future); - flush(ctx, ctx.newFuture()); + handshakePromises.add(promise); + flush(ctx, ctx.newPromise()); } catch (Exception e) { - if (future.setFailure(e)) { + if (promise.tryFailure(e)) { ctx.fireExceptionCaught(e); ctx.close(); } @@ -328,7 +329,7 @@ public class SslHandler } }); - return future; + return promise; } /** @@ -336,14 +337,14 @@ public class SslHandler * destroys the underlying {@link SSLEngine}. */ public ChannelFuture close() { - return close(ctx.newFuture()); + return close(ctx.newPromise()); } /** * See {@link #close()} */ - public ChannelFuture close(final ChannelFuture future) { + public ChannelFuture close(final ChannelPromise future) { final ChannelHandlerContext ctx = this.ctx; ctx.executor().execute(new Runnable() { @Override @@ -390,14 +391,14 @@ public class SslHandler @Override public void disconnect(final ChannelHandlerContext ctx, - final ChannelFuture future) throws Exception { - closeOutboundAndChannel(ctx, future, true); + final ChannelPromise promise) throws Exception { + closeOutboundAndChannel(ctx, promise, true); } @Override public void close(final ChannelHandlerContext ctx, - final ChannelFuture future) throws Exception { - closeOutboundAndChannel(ctx, future, false); + final ChannelPromise promise) throws Exception { + closeOutboundAndChannel(ctx, promise, false); } @Override @@ -406,7 +407,7 @@ public class SslHandler } @Override - public void flush(final ChannelHandlerContext ctx, ChannelFuture future) throws Exception { + public void flush(final ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { final ByteBuf in = ctx.outboundByteBuffer(); final ByteBuf out = ctx.nextOutboundByteBuffer(); @@ -417,15 +418,15 @@ public class SslHandler if (startTls && !sentFirstMessage) { sentFirstMessage = true; out.writeBytes(in); - ctx.flush(future); + ctx.flush(promise); return; } if (ctx.executor() == ctx.channel().eventLoop()) { - flushFutureNotifier.addFlushFuture(future, in.readableBytes()); + flushFutureNotifier.addFlushFuture(promise, in.readableBytes()); } else { synchronized (flushFutureNotifier) { - flushFutureNotifier.addFlushFuture(future, in.readableBytes()); + flushFutureNotifier.addFlushFuture(promise, in.readableBytes()); } } @@ -441,7 +442,7 @@ public class SslHandler if (in.readable()) { in.clear(); SSLException e = new SSLException("SSLEngine already closed"); - future.setFailure(e); + promise.setFailure(e); ctx.fireExceptionCaught(e); flush0(ctx, bytesConsumed, e); bytesConsumed = 0; @@ -488,7 +489,7 @@ public class SslHandler } private void flush0(final ChannelHandlerContext ctx, final int bytesConsumed) { - ctx.flush(ctx.newFuture().addListener(new ChannelFutureListener() { + ctx.flush(ctx.newPromise().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (ctx.executor() == ctx.channel().eventLoop()) { @@ -512,7 +513,7 @@ public class SslHandler } private void flush0(final ChannelHandlerContext ctx, final int bytesConsumed, final Throwable cause) { - ChannelFuture flushFuture = ctx.flush(ctx.newFuture().addListener(new ChannelFutureListener() { + ChannelFuture flushFuture = ctx.flush(ctx.newPromise().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (ctx.executor() == ctx.channel().eventLoop()) { @@ -534,7 +535,7 @@ public class SslHandler } })); - safeClose(ctx, flushFuture, ctx.newFuture()); + safeClose(ctx, flushFuture, ctx.newPromise()); } private static SSLEngineResult wrap(SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { @@ -823,7 +824,7 @@ public class SslHandler } if (wrapLater) { - flush(ctx, ctx.newFuture()); + flush(ctx, ctx.newPromise()); } } catch (SSLException e) { setHandshakeFailure(e); @@ -877,11 +878,11 @@ public class SslHandler */ private void setHandshakeSuccess() { for (;;) { - ChannelFuture f = handshakeFutures.poll(); - if (f == null) { + ChannelPromise p = handshakePromises.poll(); + if (p == null) { break; } - f.setSuccess(); + p.setSuccess(); } } @@ -907,32 +908,32 @@ public class SslHandler } for (;;) { - ChannelFuture f = handshakeFutures.poll(); - if (f == null) { + ChannelPromise p = handshakePromises.poll(); + if (p == null) { break; } - f.setFailure(cause); + p.setFailure(cause); } flush0(ctx, 0, cause); } private void closeOutboundAndChannel( - final ChannelHandlerContext ctx, final ChannelFuture future, boolean disconnect) throws Exception { + final ChannelHandlerContext ctx, final ChannelPromise promise, boolean disconnect) throws Exception { if (!ctx.channel().isActive()) { if (disconnect) { - ctx.disconnect(future); + ctx.disconnect(promise); } else { - ctx.close(future); + ctx.close(promise); } return; } engine.closeOutbound(); - ChannelFuture closeNotifyFuture = ctx.newFuture(); + ChannelPromise closeNotifyFuture = ctx.newPromise(); flush(ctx, closeNotifyFuture); - safeClose(ctx, closeNotifyFuture, future); + safeClose(ctx, closeNotifyFuture, promise); } @Override @@ -976,9 +977,9 @@ public class SslHandler private void safeClose( final ChannelHandlerContext ctx, ChannelFuture flushFuture, - final ChannelFuture closeFuture) { + final ChannelPromise promise) { if (!ctx.channel().isActive()) { - ctx.close(closeFuture); + ctx.close(promise); return; } @@ -991,7 +992,7 @@ public class SslHandler logger.warn( ctx.channel() + " last write attempt timed out." + " Force-closing the connection."); - ctx.close(closeFuture); + ctx.close(promise); } }, closeNotifyTimeoutMillis, TimeUnit.MILLISECONDS); } else { @@ -1007,19 +1008,19 @@ public class SslHandler timeoutFuture.cancel(false); } if (ctx.channel().isActive()) { - ctx.close(closeFuture); + ctx.close(promise); } } }); } - private final class SSLEngineInboundCloseFuture extends DefaultChannelFuture { + private final class SSLEngineInboundCloseFuture extends DefaultChannelPromise { public SSLEngineInboundCloseFuture() { - super(null, true); + super(null); } void setClosed() { - super.setSuccess(); + super.trySuccess(); } @Override @@ -1033,12 +1034,32 @@ public class SslHandler } @Override - public boolean setSuccess() { + public boolean trySuccess() { return false; } @Override - public boolean setFailure(Throwable cause) { + public boolean tryFailure(Throwable cause) { + return false; + } + + @Override + public void setSuccess() { + throw new IllegalStateException(); + } + + @Override + public void setFailure(Throwable cause) { + throw new IllegalStateException(); + } + + @Override + public void setProgress(long amount, long current, long total) { + throw new IllegalStateException(); + } + + @Override + public boolean tryProgress(long amount, long current, long total) { return false; } } diff --git a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java index 2aeea7d677..d88d2305e3 100644 --- a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java +++ b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java @@ -27,6 +27,7 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundMessageHandler; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; @@ -145,8 +146,8 @@ public class ChunkedWriteHandler } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - queue.add(future); + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + queue.add(promise); if (isWritable() || !ctx.channel().isActive()) { doFlush(ctx); } @@ -186,8 +187,8 @@ public class ChunkedWriteHandler logger.warn(ChunkedInput.class.getSimpleName() + ".isEndOfInput() failed", e); } closeInput(in); - } else if (currentEvent instanceof ChannelFuture) { - ChannelFuture f = (ChannelFuture) currentEvent; + } else if (currentEvent instanceof ChannelPromise) { + ChannelPromise f = (ChannelPromise) currentEvent; if (!success) { fireExceptionCaught = true; if (cause == null) { @@ -221,9 +222,9 @@ public class ChunkedWriteHandler } final Object currentEvent = this.currentEvent; - if (currentEvent instanceof ChannelFuture) { + if (currentEvent instanceof ChannelPromise) { this.currentEvent = null; - ctx.flush((ChannelFuture) currentEvent); + ctx.flush((ChannelPromise) currentEvent); } else if (currentEvent instanceof ChunkedInput) { final ChunkedInput chunks = (ChunkedInput) currentEvent; boolean read; diff --git a/handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java b/handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java index bf61593e65..95fbc46cde 100644 --- a/handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java +++ b/handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java @@ -23,6 +23,7 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOperationHandler; +import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelStateHandlerAdapter; import io.netty.channel.EventExecutor; import io.netty.channel.FileRegion; @@ -267,8 +268,9 @@ public class IdleStateHandler extends ChannelStateHandlerAdapter implements Chan } @Override - public void flush(final ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - future.addListener(new ChannelFutureListener() { + + public void flush(final ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { lastWriteTime = System.currentTimeMillis(); @@ -276,45 +278,45 @@ public class IdleStateHandler extends ChannelStateHandlerAdapter implements Chan } }); - ctx.flush(future); + ctx.flush(promise); } @Override - public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelFuture future) throws Exception { - ctx.bind(localAddress, future); + public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { + ctx.bind(localAddress, promise); } @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, - ChannelFuture future) throws Exception { - ctx.connect(remoteAddress, localAddress, future); + ChannelPromise promise) throws Exception { + ctx.connect(remoteAddress, localAddress, promise); } @Override - public void disconnect(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - ctx.disconnect(future); + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + ctx.disconnect(promise); } @Override - public void close(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - ctx.close(future); + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + ctx.close(promise); } @Override - public void deregister(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - ctx.deregister(future); + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + ctx.deregister(promise); } @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { - future.addListener(new ChannelFutureListener() { + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception { + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { lastWriteTime = System.currentTimeMillis(); writerIdleCount = allIdleCount = 0; } }); - ctx.sendFile(region, future); + ctx.sendFile(region, promise); } private void initialize(ChannelHandlerContext ctx) { diff --git a/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java b/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java index 71d05535a7..6ef6b3e75b 100644 --- a/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java +++ b/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java @@ -23,6 +23,7 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOperationHandlerAdapter; +import io.netty.channel.ChannelPromise; import io.netty.channel.FileRegion; import java.util.concurrent.ScheduledFuture; @@ -104,26 +105,26 @@ public class WriteTimeoutHandler extends ChannelOperationHandlerAdapter { } @Override - public void flush(final ChannelHandlerContext ctx, final ChannelFuture future) throws Exception { - scheduleTimeout(ctx, future); + public void flush(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception { + scheduleTimeout(ctx, promise); - super.flush(ctx, future); + super.flush(ctx, promise); } @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { - scheduleTimeout(ctx, future); + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception { + scheduleTimeout(ctx, promise); - super.sendFile(ctx, region, future); + super.sendFile(ctx, region, promise); } - private void scheduleTimeout(final ChannelHandlerContext ctx, final ChannelFuture future) { + private void scheduleTimeout(final ChannelHandlerContext ctx, final ChannelPromise future) { if (timeoutMillis > 0) { // Schedule a timeout. final ScheduledFuture sf = ctx.executor().schedule(new Runnable() { @Override public void run() { - if (future.setFailure(WriteTimeoutException.INSTANCE)) { + if (future.tryFailure(WriteTimeoutException.INSTANCE)) { // If succeeded to mark as failure, notify the pipeline, too. try { writeTimedOut(ctx); diff --git a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java index 2d63f12a8c..3c8002a6d1 100644 --- a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java @@ -17,11 +17,11 @@ package io.netty.handler.traffic; import io.netty.buffer.Buf; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundByteHandler; +import io.netty.channel.ChannelPromise; import io.netty.util.Attribute; import io.netty.util.AttributeKey; @@ -302,14 +302,14 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte } @Override - public void flush(final ChannelHandlerContext ctx, final ChannelFuture future) throws Exception { + public void flush(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception { long curtime = System.currentTimeMillis(); long size = ctx.outboundByteBuffer().readableBytes(); if (trafficCounter != null) { trafficCounter.bytesWriteFlowControl(size); if (writeLimit == 0) { - ctx.flush(future); + ctx.flush(promise); return; } // compute the number of ms to wait before continue with the @@ -321,13 +321,13 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte ctx.executor().schedule(new Runnable() { @Override public void run() { - ctx.flush(future); + ctx.flush(promise); } }, wait, TimeUnit.MILLISECONDS); return; } } - ctx.flush(future); + ctx.flush(promise); } /** diff --git a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java index 13494e5cba..e0bc7c32c3 100644 --- a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoopGroup; import io.netty.util.AttributeKey; @@ -195,7 +196,7 @@ public abstract class AbstractBootstrap> { public ChannelFuture bind() { validate(); Channel channel = factory().newChannel(); - return bind(channel.newFuture()); + return bind(channel.newPromise()); } /** @@ -210,7 +211,7 @@ public abstract class AbstractBootstrap> { return (B) this; } - protected static boolean ensureOpen(ChannelFuture future) { + protected static boolean ensureOpen(ChannelPromise future) { if (!future.channel().isOpen()) { // Registration was successful but the channel was closed due to some failure in // handler. @@ -223,7 +224,7 @@ public abstract class AbstractBootstrap> { /** * Bind the {@link Channel} of the given {@link ChannelFactory}. */ - public abstract ChannelFuture bind(ChannelFuture future); + public abstract ChannelFuture bind(ChannelPromise future); protected final SocketAddress localAddress() { return localAddress; diff --git a/transport/src/main/java/io/netty/bootstrap/Bootstrap.java b/transport/src/main/java/io/netty/bootstrap/Bootstrap.java index 385dd154ef..41dd72660c 100644 --- a/transport/src/main/java/io/netty/bootstrap/Bootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/Bootstrap.java @@ -20,6 +20,7 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; import io.netty.util.AttributeKey; @@ -66,7 +67,7 @@ public class Bootstrap extends AbstractBootstrap { } @Override - public ChannelFuture bind(ChannelFuture future) { + public ChannelFuture bind(ChannelPromise future) { validate(future); if (localAddress() == null) { throw new IllegalStateException("localAddress not set"); @@ -92,13 +93,13 @@ public class Bootstrap extends AbstractBootstrap { public ChannelFuture connect() { validate(); Channel channel = factory().newChannel(); - return connect(channel.newFuture()); + return connect(channel.newPromise()); } /** * @see {@link #connect()} */ - public ChannelFuture connect(ChannelFuture future) { + public ChannelFuture connect(ChannelPromise future) { validate(future); if (remoteAddress == null) { throw new IllegalStateException("remoteAddress not set"); diff --git a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java index beb327673c..72d59c2689 100644 --- a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -27,6 +27,7 @@ import io.netty.channel.ChannelInboundMessageHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoopGroup; import io.netty.channel.ServerChannel; import io.netty.channel.socket.SocketChannel; @@ -144,7 +145,7 @@ public class ServerBootstrap extends AbstractBootstrap { } @Override - public ChannelFuture bind(ChannelFuture future) { + public ChannelFuture bind(ChannelPromise future) { validate(future); Channel channel = future.channel(); if (channel.isActive()) { diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index 3b8f6d76be..3bbda12391 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -79,10 +79,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha private final Unsafe unsafe; private final DefaultChannelPipeline pipeline; private final ChannelFuture succeededFuture = new SucceededChannelFuture(this); - private final ChannelFuture voidFuture = new VoidChannelFuture(this); + private final VoidChannelPromise voidPromise = new VoidChannelPromise(this); private final CloseFuture closeFuture = new CloseFuture(this); - protected final ChannelFlushFutureNotifier flushFutureNotifier = new ChannelFlushFutureNotifier(); + protected final ChannelFlushPromiseNotifier flushFutureNotifier = new ChannelFlushPromiseNotifier(); private volatile SocketAddress localAddress; private volatile SocketAddress remoteAddress; @@ -248,33 +248,33 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public ChannelFuture bind(SocketAddress localAddress, ChannelFuture future) { - return pipeline.bind(localAddress, future); + public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) { + return pipeline.bind(localAddress, promise); } @Override - public ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future) { - return pipeline.connect(remoteAddress, future); + public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) { + return pipeline.connect(remoteAddress, promise); } @Override - public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) { - return pipeline.connect(remoteAddress, localAddress, future); + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { + return pipeline.connect(remoteAddress, localAddress, promise); } @Override - public ChannelFuture disconnect(ChannelFuture future) { - return pipeline.disconnect(future); + public ChannelFuture disconnect(ChannelPromise promise) { + return pipeline.disconnect(promise); } @Override - public ChannelFuture close(ChannelFuture future) { - return pipeline.close(future); + public ChannelFuture close(ChannelPromise promise) { + return pipeline.close(promise); } @Override - public ChannelFuture deregister(ChannelFuture future) { - return pipeline.deregister(future); + public ChannelFuture deregister(ChannelPromise promise) { + return pipeline.deregister(promise); } @Override @@ -294,18 +294,18 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public ChannelFuture flush(ChannelFuture future) { - return pipeline.flush(future); + public ChannelFuture flush(ChannelPromise promise) { + return pipeline.flush(promise); } @Override - public ChannelFuture write(Object message, ChannelFuture future) { - return pipeline.write(message, future); + public ChannelFuture write(Object message, ChannelPromise promise) { + return pipeline.write(message, promise); } @Override - public ChannelFuture newFuture() { - return new DefaultChannelFuture(this, false); + public ChannelPromise newPromise() { + return new DefaultChannelPromise(this); } @Override @@ -334,8 +334,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public ChannelFuture sendFile(FileRegion region, ChannelFuture future) { - return pipeline.sendFile(region, future); + public ChannelFuture sendFile(FileRegion region, ChannelPromise promise) { + return pipeline.sendFile(region, promise); } /** @@ -411,13 +411,13 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha private final class FlushTask { final FileRegion region; - final ChannelFuture future; + final ChannelPromise promise; FlushTask next; - FlushTask(FileRegion region, ChannelFuture future) { + FlushTask(FileRegion region, ChannelPromise promise) { this.region = region; - this.future = future; - future.addListener(new ChannelFutureListener() { + this.promise = promise; + promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { flushTaskInProgress = next; @@ -427,13 +427,13 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha if (region == null) { // no region present means the next flush task was to directly flush // the outbound buffer - flushNotifierAndFlush(next.future); + flushNotifierAndFlush(next.promise); } else { // flush the region now - doFlushFileRegion(region, next.future); + doFlushFileRegion(region, next.promise); } } catch (Throwable cause) { - next.future.setFailure(cause); + next.promise.setFailure(cause); } } else { // notify the flush futures @@ -460,39 +460,39 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha }; @Override - public final void sendFile(final FileRegion region, final ChannelFuture future) { + public final void sendFile(final FileRegion region, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { if (outboundBufSize() > 0) { - flushNotifier(newFuture()).addListener(new ChannelFutureListener() { + flushNotifier(newPromise()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture cf) throws Exception { - sendFile0(region, future); + sendFile0(region, promise); } }); } else { // nothing pending try to send the fileRegion now! - sendFile0(region, future); + sendFile0(region, promise); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - sendFile(region, future); + sendFile(region, promise); } }); } } - private void sendFile0(FileRegion region, ChannelFuture future) { + private void sendFile0(FileRegion region, ChannelPromise promise) { if (flushTaskInProgress == null) { - flushTaskInProgress = new FlushTask(region, future); + flushTaskInProgress = new FlushTask(region, promise); try { // the first FileRegion to flush so trigger it now! - doFlushFileRegion(region, future); + doFlushFileRegion(region, promise); } catch (Throwable cause) { region.close(); - future.setFailure(cause); + promise.setFailure(cause); } return; } @@ -506,7 +506,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha task = next; } // there is something that needs to get flushed first so add it as next in the chain - task.next = new FlushTask(region, future); + task.next = new FlushTask(region, promise); } @Override @@ -515,8 +515,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public final ChannelFuture voidFuture() { - return voidFuture; + public final ChannelPromise voidFuture() { + return voidPromise; } @Override @@ -530,7 +530,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public final void register(EventLoop eventLoop, final ChannelFuture future) { + public final void register(EventLoop eventLoop, final ChannelPromise promise) { if (eventLoop == null) { throw new NullPointerException("eventLoop"); } @@ -545,7 +545,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha assert eventLoop().inEventLoop(); - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } @@ -555,22 +555,22 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha // // See https://github.com/netty/netty/issues/654 if (eventLoop.inEventLoop()) { - register0(future); + register0(promise); } else { eventLoop.execute(new Runnable() { @Override public void run() { - register0(future); + register0(promise); } }); } } - private void register0(ChannelFuture future) { + private void register0(ChannelPromise promise) { try { Runnable postRegisterTask = doRegister(); registered = true; - future.setSuccess(); + promise.setSuccess(); pipeline.fireChannelRegistered(); if (postRegisterTask != null) { postRegisterTask.run(); @@ -586,15 +586,15 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha logger.warn("Failed to close a channel", t2); } - future.setFailure(t); + promise.setFailure(t); closeFuture.setClosed(); } } @Override - public final void bind(final SocketAddress localAddress, final ChannelFuture future) { + public final void bind(final SocketAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } @@ -615,58 +615,58 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } doBind(localAddress); - future.setSuccess(); + promise.setSuccess(); if (!wasActive && isActive()) { pipeline.fireChannelActive(); } } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); closeIfClosed(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - bind(localAddress, future); + bind(localAddress, promise); } }); } } @Override - public final void disconnect(final ChannelFuture future) { + public final void disconnect(final ChannelPromise promise) { if (eventLoop().inEventLoop()) { try { boolean wasActive = isActive(); doDisconnect(); - future.setSuccess(); + promise.setSuccess(); if (wasActive && !isActive()) { pipeline.fireChannelInactive(); } } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); closeIfClosed(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - disconnect(future); + disconnect(promise); } }); } } @Override - public final void close(final ChannelFuture future) { + public final void close(final ChannelPromise promise) { if (eventLoop().inEventLoop()) { boolean wasActive = isActive(); if (closeFuture.setClosed()) { try { doClose(); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } if (closedChannelException != null) { @@ -682,13 +682,13 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha deregister(voidFuture()); } else { // Closed already. - future.setSuccess(); + promise.setSuccess(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - close(future); + close(promise); } }); } @@ -704,10 +704,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public final void deregister(final ChannelFuture future) { + public final void deregister(final ChannelPromise promise) { if (eventLoop().inEventLoop()) { if (!registered) { - future.setSuccess(); + promise.setSuccess(); return; } @@ -718,20 +718,20 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } finally { if (registered) { registered = false; - future.setSuccess(); + promise.setSuccess(); pipeline.fireChannelUnregistered(); } else { // Some transports like local and AIO does not allow the deregistration of // an open channel. Their doDeregister() calls close(). Consequently, // close() calls deregister() again - no need to fire channelUnregistered. - future.setSuccess(); + promise.setSuccess(); } } } else { eventLoop().execute(new Runnable() { @Override public void run() { - deregister(future); + deregister(promise); } }); } @@ -752,7 +752,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } @Override - public void flush(final ChannelFuture future) { + public void flush(final ChannelPromise promise) { if (eventLoop().inEventLoop()) { if (flushTaskInProgress != null) { @@ -766,23 +766,23 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } task = t.next; } - task.next = new FlushTask(null, future); + task.next = new FlushTask(null, promise); return; } - flushNotifierAndFlush(future); + flushNotifierAndFlush(promise); } else { eventLoop().execute(new Runnable() { @Override public void run() { - flush(future); + flush(promise); } }); } } - private void flushNotifierAndFlush(ChannelFuture future) { - flushNotifier(future); + private void flushNotifierAndFlush(ChannelPromise promise) { + flushNotifier(promise); flush0(); } @@ -797,12 +797,12 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha return bufSize; } - private ChannelFuture flushNotifier(ChannelFuture future) { + private ChannelFuture flushNotifier(ChannelPromise promise) { // Append flush future to the notification list. - if (future != voidFuture) { - flushFutureNotifier.addFlushFuture(future, outboundBufSize()); + if (promise != voidPromise) { + flushFutureNotifier.addFlushFuture(promise, outboundBufSize()); } - return future; + return promise; } private void flush0() { @@ -882,13 +882,13 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } } - protected final boolean ensureOpen(ChannelFuture future) { + protected final boolean ensureOpen(ChannelPromise promise) { if (isOpen()) { return true; } Exception e = new ClosedChannelException(); - future.setFailure(e); + promise.setFailure(e); return false; } @@ -985,7 +985,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha * * Sub-classes may override this as this implementation will just thrown an {@link UnsupportedOperationException} */ - protected void doFlushFileRegion(FileRegion region, ChannelFuture future) throws Exception { + protected void doFlushFileRegion(FileRegion region, ChannelPromise promise) throws Exception { throw new UnsupportedOperationException(); } @@ -1002,19 +1002,39 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha */ protected abstract boolean isFlushPending(); - private final class CloseFuture extends DefaultChannelFuture implements ChannelFuture.Unsafe { + private final class CloseFuture extends DefaultChannelPromise implements ChannelFuture.Unsafe { CloseFuture(AbstractChannel ch) { - super(ch, false); + super(ch); } @Override - public boolean setSuccess() { + public void setSuccess() { throw new IllegalStateException(); } @Override - public boolean setFailure(Throwable cause) { + public void setFailure(Throwable cause) { + throw new IllegalStateException(); + } + + @Override + public void setProgress(long amount, long current, long total) { + throw new IllegalStateException(); + } + + @Override + public boolean tryProgress(long amount, long current, long total) { + throw new IllegalStateException(); + } + + @Override + public boolean trySuccess() { + throw new IllegalStateException(); + } + + @Override + public boolean tryFailure(Throwable cause) { throw new IllegalStateException(); } @@ -1024,7 +1044,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha } catch (Exception e) { logger.warn("doPreClose() raised an exception.", e); } - return super.setSuccess(); + return super.trySuccess(); } } } diff --git a/transport/src/main/java/io/netty/channel/AbstractServerChannel.java b/transport/src/main/java/io/netty/channel/AbstractServerChannel.java index 3f857fda4c..c5f7b98844 100755 --- a/transport/src/main/java/io/netty/channel/AbstractServerChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractServerChannel.java @@ -25,9 +25,9 @@ import java.net.SocketAddress; * A skeletal server-side {@link Channel} implementation. A server-side * {@link Channel} does not allow the following operations: *
    - *
  • {@link #connect(SocketAddress, ChannelFuture)}
  • - *
  • {@link #disconnect(ChannelFuture)}
  • - *
  • {@link #flush(ChannelFuture)}
  • + *
  • {@link #connect(SocketAddress, ChannelPromise)}
  • + *
  • {@link #disconnect(ChannelPromise)}
  • + *
  • {@link #flush(ChannelPromise)}
  • *
  • and the shortcut methods which calls the methods mentioned above *
*/ @@ -86,7 +86,7 @@ public abstract class AbstractServerChannel extends AbstractChannel implements S private final class DefaultServerUnsafe extends AbstractUnsafe { @Override - public void flush(final ChannelFuture future) { + public void flush(final ChannelPromise future) { if (eventLoop().inEventLoop()) { reject(future); } else { @@ -102,7 +102,7 @@ public abstract class AbstractServerChannel extends AbstractChannel implements S @Override public void connect( final SocketAddress remoteAddress, final SocketAddress localAddress, - final ChannelFuture future) { + final ChannelPromise future) { if (eventLoop().inEventLoop()) { reject(future); } else { @@ -115,7 +115,7 @@ public abstract class AbstractServerChannel extends AbstractChannel implements S } } - private void reject(ChannelFuture future) { + private void reject(ChannelPromise future) { Exception cause = new UnsupportedOperationException(); future.setFailure(cause); pipeline().fireExceptionCaught(cause); diff --git a/transport/src/main/java/io/netty/channel/Channel.java b/transport/src/main/java/io/netty/channel/Channel.java index ad713f9b32..d38d8a8bec 100755 --- a/transport/src/main/java/io/netty/channel/Channel.java +++ b/transport/src/main/java/io/netty/channel/Channel.java @@ -185,9 +185,9 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr ChannelHandlerContext directOutboundContext(); /** - * Return a {@link VoidChannelFuture}. This method always return the same instance. + * Return a {@link VoidChannelPromise}. This method always return the same instance. */ - ChannelFuture voidFuture(); + ChannelPromise voidFuture(); /** * Return the {@link SocketAddress} to which is bound local or @@ -202,37 +202,37 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr SocketAddress remoteAddress(); /** - * Register the {@link Channel} of the {@link ChannelFuture} with the {@link EventLoop} and notify + * Register the {@link Channel} of the {@link ChannelPromise} with the {@link EventLoop} and notify * the {@link ChannelFuture} once the registration was complete. */ - void register(EventLoop eventLoop, ChannelFuture future); + void register(EventLoop eventLoop, ChannelPromise promise); /** - * Bind the {@link SocketAddress} to the {@link Channel} of the {@link ChannelFuture} and notify + * Bind the {@link SocketAddress} to the {@link Channel} of the {@link ChannelPromise} and notify * it once its done. */ - void bind(SocketAddress localAddress, ChannelFuture future); + void bind(SocketAddress localAddress, ChannelPromise promise); /** * Connect the {@link Channel} of the given {@link ChannelFuture} with the given remote {@link SocketAddress}. * If a specific local {@link SocketAddress} should be used it need to be given as argument. Otherwise just * pass {@code null} to it. * - * The {@link ChannelFuture} will get notified once the connect operation was complete. + * The {@link ChannelPromise} will get notified once the connect operation was complete. */ - void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future); + void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise); /** - * Disconnect the {@link Channel} of the {@link ChannelFuture} and notify the {@link ChannelFuture} once the + * Disconnect the {@link Channel} of the {@link ChannelFuture} and notify the {@link ChannelPromise} once the * operation was complete. */ - void disconnect(ChannelFuture future); + void disconnect(ChannelPromise promise); /** - * Close the {@link Channel} of the {@link ChannelFuture} and notify the {@link ChannelFuture} once the + * Close the {@link Channel} of the {@link ChannelPromise} and notify the {@link ChannelPromise} once the * operation was complete. */ - void close(ChannelFuture future); + void close(ChannelPromise promise); /** * Closes the {@link Channel} immediately without firing any events. Probably only useful @@ -241,10 +241,10 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr void closeForcibly(); /** - * Deregister the {@link Channel} of the {@link ChannelFuture} from {@link EventLoop} and notify the - * {@link ChannelFuture} once the operation was complete. + * Deregister the {@link Channel} of the {@link ChannelPromise} from {@link EventLoop} and notify the + * {@link ChannelPromise} once the operation was complete. */ - void deregister(ChannelFuture future); + void deregister(ChannelPromise promise); /** * Schedules a read operation that fills the inbound buffer of the first {@link ChannelInboundHandler} in the @@ -256,7 +256,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr * Flush out all data that was buffered in the buffer of the {@link #directOutboundContext()} and was not * flushed out yet. After that is done the {@link ChannelFuture} will get notified */ - void flush(ChannelFuture future); + void flush(ChannelPromise promise); /** * Flush out all data now. @@ -264,10 +264,10 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr void flushNow(); /** - * Send a {@link FileRegion} to the remote peer and notify the {@link ChannelFuture} once it completes + * Send a {@link FileRegion} to the remote peer and notify the {@link ChannelPromise} once it completes * or an error was detected. Once the {@link FileRegion} was transfered or an error was thrown it will * automaticly closed via {@link FileRegion#close()}. */ - void sendFile(FileRegion region, ChannelFuture future); + void sendFile(FileRegion region, ChannelPromise promise); } } diff --git a/transport/src/main/java/io/netty/channel/ChannelFlushFutureNotifier.java b/transport/src/main/java/io/netty/channel/ChannelFlushPromiseNotifier.java similarity index 89% rename from transport/src/main/java/io/netty/channel/ChannelFlushFutureNotifier.java rename to transport/src/main/java/io/netty/channel/ChannelFlushPromiseNotifier.java index 3a2c4dafaa..02474d97a2 100644 --- a/transport/src/main/java/io/netty/channel/ChannelFlushFutureNotifier.java +++ b/transport/src/main/java/io/netty/channel/ChannelFlushPromiseNotifier.java @@ -22,16 +22,16 @@ import java.util.Queue; * This implementation allows to register {@link ChannelFuture} instances which will get notified once some amount of * data was written and so a checkpoint was reached. */ -public final class ChannelFlushFutureNotifier { +public final class ChannelFlushPromiseNotifier { private long writeCounter; private final Queue flushCheckpoints = new ArrayDeque(); /** - * Add a {@link ChannelFuture} to this {@link ChannelFlushFutureNotifier} which will be notified after the given + * Add a {@link ChannelPromise} to this {@link ChannelFlushPromiseNotifier} which will be notified after the given * pendingDataSize was reached. */ - public void addFlushFuture(ChannelFuture future, int pendingDataSize) { + public void addFlushFuture(ChannelPromise future, int pendingDataSize) { if (future == null) { throw new NullPointerException("future"); } @@ -59,17 +59,17 @@ public final class ChannelFlushFutureNotifier { } /** - * Return the current write counter of this {@link ChannelFlushFutureNotifier} + * Return the current write counter of this {@link ChannelFlushPromiseNotifier} */ public long writeCounter() { return writeCounter; } /** - * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelFuture, int)} and + * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelPromise, int)} and * their pendingDatasize is smaller after the the current writeCounter returned by {@link #writeCounter()}. * - * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushFutureNotifier} and + * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushPromiseNotifier} and * so not receive anymore notificiation. */ public void notifyFlushFutures() { @@ -77,10 +77,10 @@ public final class ChannelFlushFutureNotifier { } /** - * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelFuture, int)} and + * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelPromise, int)} and * their pendingDatasize isis smaller then the current writeCounter returned by {@link #writeCounter()}. * - * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushFutureNotifier} and + * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushPromiseNotifier} and * so not receive anymore notificiation. * * The rest of the remaining {@link ChannelFuture}s will be failed with the given {@link Throwable}. @@ -99,11 +99,11 @@ public final class ChannelFlushFutureNotifier { } /** - * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelFuture, int)} and + * Notify all {@link ChannelFuture}s that were registered with {@link #addFlushFuture(ChannelPromise, int)} and * their pendingDatasize is smaller then the current writeCounter returned by {@link #writeCounter()} using * the given cause1. * - * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushFutureNotifier} and + * After a {@link ChannelFuture} was notified it will be removed from this {@link ChannelFlushPromiseNotifier} and * so not receive anymore notificiation. * * The rest of the remaining {@link ChannelFuture}s will be failed with the given {@link Throwable}. @@ -171,14 +171,14 @@ public final class ChannelFlushFutureNotifier { abstract static class FlushCheckpoint { abstract long flushCheckpoint(); abstract void flushCheckpoint(long checkpoint); - abstract ChannelFuture future(); + abstract ChannelPromise future(); } private static class DefaultFlushCheckpoint extends FlushCheckpoint { private long checkpoint; - private final ChannelFuture future; + private final ChannelPromise future; - DefaultFlushCheckpoint(long checkpoint, ChannelFuture future) { + DefaultFlushCheckpoint(long checkpoint, ChannelPromise future) { this.checkpoint = checkpoint; this.future = future; } @@ -194,7 +194,7 @@ public final class ChannelFlushFutureNotifier { } @Override - ChannelFuture future() { + ChannelPromise future() { return future; } } diff --git a/transport/src/main/java/io/netty/channel/ChannelFuture.java b/transport/src/main/java/io/netty/channel/ChannelFuture.java index 8251a928c9..049d6b7b69 100644 --- a/transport/src/main/java/io/netty/channel/ChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/ChannelFuture.java @@ -15,7 +15,6 @@ */ package io.netty.channel; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import io.netty.bootstrap.Bootstrap; @@ -162,7 +161,7 @@ import io.netty.bootstrap.Bootstrap; * @apiviz.landmark * @apiviz.owns io.netty.channel.ChannelFutureListener - - notifies */ -public interface ChannelFuture extends Future { +public interface ChannelFuture { /** * Returns a channel where the I/O operation associated with this @@ -172,19 +171,10 @@ public interface ChannelFuture extends Future { /** * Returns {@code true} if and only if this future is - * complete, regardless of whether the operation was successful, failed, - * or cancelled. + * complete, regardless of whether the operation was successful or failed. */ - @Override boolean isDone(); - /** - * Returns {@code true} if and only if this future was - * cancelled by a {@link #cancel()} method. - */ - @Override - boolean isCancelled(); - /** * Returns {@code true} if and only if the I/O operation was completed * successfully. @@ -201,46 +191,6 @@ public interface ChannelFuture extends Future { */ Throwable cause(); - /** - * Cancels the I/O operation associated with this future - * and notifies all listeners if canceled successfully. - * - * @return {@code true} if and only if the operation has been canceled. - * {@code false} if the operation can't be canceled or is already - * completed. - */ - boolean cancel(); - - /** - * Marks this future as a success and notifies all - * listeners. - * - * @return {@code true} if and only if successfully marked this future as - * a success. Otherwise {@code false} because this future is - * already marked as either a success or a failure. - */ - boolean setSuccess(); - - /** - * Marks this future as a failure and notifies all - * listeners. - * - * @return {@code true} if and only if successfully marked this future as - * a failure. Otherwise {@code false} because this future is - * already marked as either a success or a failure. - */ - boolean setFailure(Throwable cause); - - /** - * Notifies the progress of the operation to the listeners that implements - * {@link ChannelFutureProgressListener}. Please note that this method will - * not do anything and return {@code false} if this future is complete - * already. - * - * @return {@code true} if and only if notification was made. - */ - boolean setProgress(long amount, long current, long total); - /** * Adds the specified listener to this future. The * specified listener is notified when this future is diff --git a/transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java index c2649f184d..d515db640f 100644 --- a/transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java +++ b/transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java @@ -27,61 +27,61 @@ import java.net.SocketAddress; public abstract class ChannelHandlerAdapter extends ChannelStateHandlerAdapter implements ChannelOperationHandler { /** - * Calls {@link ChannelHandlerContext#bind(SocketAddress, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#bind(SocketAddress, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { ctx.bind(localAddress, future); } /** - * Calls {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, - SocketAddress localAddress, ChannelFuture future) throws Exception { + SocketAddress localAddress, ChannelPromise future) throws Exception { ctx.connect(remoteAddress, localAddress, future); } /** - * Calls {@link ChannelHandlerContext#disconnect(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#disconnect(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void disconnect(ChannelHandlerContext ctx, ChannelFuture future) + public void disconnect(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { ctx.disconnect(future); } /** - * Calls {@link ChannelHandlerContext#close(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#close(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void close(ChannelHandlerContext ctx, ChannelFuture future) + public void close(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { ctx.close(future); } /** - * Calls {@link ChannelHandlerContext#close(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#close(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void deregister(ChannelHandlerContext ctx, ChannelFuture future) + public void deregister(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { ctx.deregister(future); } @@ -92,7 +92,7 @@ public abstract class ChannelHandlerAdapter extends ChannelStateHandlerAdapter i } /** - * Calls {@link ChannelHandlerContext#flush(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#flush(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. @@ -101,7 +101,7 @@ public abstract class ChannelHandlerAdapter extends ChannelStateHandlerAdapter i * method and provide some proper implementation. Fail to do so, will result in an {@link IllegalStateException}! */ @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) + public void flush(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { if (this instanceof ChannelOutboundHandler) { throw new IllegalStateException( @@ -112,13 +112,13 @@ public abstract class ChannelHandlerAdapter extends ChannelStateHandlerAdapter i } /** - * Calls {@link ChannelHandlerContext#sendFile(FileRegion, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#sendFile(FileRegion, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise future) throws Exception { ctx.sendFile(region, future); } } diff --git a/transport/src/main/java/io/netty/channel/ChannelOperationHandler.java b/transport/src/main/java/io/netty/channel/ChannelOperationHandler.java index f81a13e7f9..3ff70f5ce3 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOperationHandler.java +++ b/transport/src/main/java/io/netty/channel/ChannelOperationHandler.java @@ -26,10 +26,10 @@ public interface ChannelOperationHandler extends ChannelHandler { * * @param ctx the {@link ChannelHandlerContext} for which the bind operation is made * @param localAddress the {@link SocketAddress} to which it should bound - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelFuture future) throws Exception; + void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception; /** * Called once a connect operation is made. @@ -37,39 +37,39 @@ public interface ChannelOperationHandler extends ChannelHandler { * @param ctx the {@link ChannelHandlerContext} for which the connect operation is made * @param remoteAddress the {@link SocketAddress} to which it should connect * @param localAddress the {@link SocketAddress} which is used as source on connect - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ void connect( ChannelHandlerContext ctx, SocketAddress remoteAddress, - SocketAddress localAddress, ChannelFuture future) throws Exception; + SocketAddress localAddress, ChannelPromise promise) throws Exception; /** * Called once a disconnect operation is made. * * @param ctx the {@link ChannelHandlerContext} for which the disconnect operation is made - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void disconnect(ChannelHandlerContext ctx, ChannelFuture future) throws Exception; + void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; /** * Called once a close operation is made. * * @param ctx the {@link ChannelHandlerContext} for which the close operation is made - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void close(ChannelHandlerContext ctx, ChannelFuture future) throws Exception; + void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; /** * Called once a deregister operation is made from the current registered {@link EventLoop}. * * @param ctx the {@link ChannelHandlerContext} for which the close operation is made - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void deregister(ChannelHandlerContext ctx, ChannelFuture future) throws Exception; + void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; /** * Intercepts {@link ChannelHandlerContext#read()}. @@ -80,19 +80,19 @@ public interface ChannelOperationHandler extends ChannelHandler { * Called once a flush operation is made and so the outbound data should be written. * * @param ctx the {@link ChannelHandlerContext} for which the flush operation is made - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception; + void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception; /** * Called once a sendFile operation is made and so the {@link FileRegion} should be transfered. * * @param ctx the {@link ChannelHandlerContext} for which the flush operation is made * @param region the {@link FileRegion} to transfer - * @param future the {@link ChannelFuture} to notify once the operation completes + * @param promise the {@link ChannelPromise} to notify once the operation completes * @throws Exception thrown if an error accour */ - void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception; + void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception; } diff --git a/transport/src/main/java/io/netty/channel/ChannelOperationHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelOperationHandlerAdapter.java index c6d1af15e5..0093925f79 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOperationHandlerAdapter.java +++ b/transport/src/main/java/io/netty/channel/ChannelOperationHandlerAdapter.java @@ -76,63 +76,63 @@ public abstract class ChannelOperationHandlerAdapter implements ChannelOperation } /** - * Calls {@link ChannelHandlerContext#bind(SocketAddress, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#bind(SocketAddress, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, - ChannelFuture future) throws Exception { - ctx.bind(localAddress, future); + ChannelPromise promise) throws Exception { + ctx.bind(localAddress, promise); } /** - * Calls {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, - SocketAddress localAddress, ChannelFuture future) throws Exception { - ctx.connect(remoteAddress, localAddress, future); + SocketAddress localAddress, ChannelPromise promise) throws Exception { + ctx.connect(remoteAddress, localAddress, promise); } /** - * Calls {@link ChannelHandlerContext#disconnect(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#disconnect(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void disconnect(ChannelHandlerContext ctx, ChannelFuture future) + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - ctx.disconnect(future); + ctx.disconnect(promise); } /** - * Calls {@link ChannelHandlerContext#close(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#close(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void close(ChannelHandlerContext ctx, ChannelFuture future) + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - ctx.close(future); + ctx.close(promise); } /** - * Calls {@link ChannelHandlerContext#close(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#close(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void deregister(ChannelHandlerContext ctx, ChannelFuture future) + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - ctx.deregister(future); + ctx.deregister(promise); } @Override @@ -141,7 +141,7 @@ public abstract class ChannelOperationHandlerAdapter implements ChannelOperation } /** - * Calls {@link ChannelHandlerContext#flush(ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#flush(ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. @@ -150,24 +150,24 @@ public abstract class ChannelOperationHandlerAdapter implements ChannelOperation * method! */ @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { if (this instanceof ChannelOutboundHandler) { throw new IllegalStateException( "flush(...) must be overridden by " + getClass().getName() + ", which implements " + ChannelOutboundHandler.class.getSimpleName()); } - ctx.flush(future); + ctx.flush(promise); } /** - * Calls {@link ChannelHandlerContext#sendFile(FileRegion, ChannelFuture)} to forward + * Calls {@link ChannelHandlerContext#sendFile(FileRegion, ChannelPromise)} to forward * to the next {@link ChannelOperationHandler} in the {@link ChannelPipeline}. * * Sub-classes may override this method to change behavior. */ @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { - ctx.sendFile(region, future); + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception { + ctx.sendFile(region, promise); } } diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java b/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java index 2ee4e81eb7..bedaa17d3e 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java +++ b/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java @@ -87,58 +87,58 @@ public interface ChannelOutboundInvoker { ChannelFuture sendFile(FileRegion region); /** - * Bind to the given {@link SocketAddress} and notify the {@link ChannelFuture} once the operation completes, + * Bind to the given {@link SocketAddress} and notify the {@link ChannelPromise} once the operation completes, * either because the operation was successful or because of an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture bind(SocketAddress localAddress, ChannelFuture future); + ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise); /** - * Connect to the given {@link SocketAddress} and notify the {@link ChannelFuture} once the operation completes, + * Connect to the given {@link SocketAddress} and notify the {@link ChannelPromise} once the operation completes, * either because the operation was successful or because of * an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelFuture} will be notified. */ - ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future); + ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise); /** - * Connect to the given {@link SocketAddress} while bind to the localAddress and notify the {@link ChannelFuture} + * Connect to the given {@link SocketAddress} while bind to the localAddress and notify the {@link ChannelPromise} * once the operation completes, either because the operation was successful or because of * an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified and also returned. */ - ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future); + ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise); /** - * Discconect from the remote peer and notify the {@link ChannelFuture} once the operation completes, + * Discconect from the remote peer and notify the {@link ChannelPromise} once the operation completes, * either because the operation was successful or because of * an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture disconnect(ChannelFuture future); + ChannelFuture disconnect(ChannelPromise promise); /** - * Close this ChannelOutboundInvoker and notify the {@link ChannelFuture} once the operation completes, + * Close this ChannelOutboundInvoker and notify the {@link ChannelPromise} once the operation completes, * either because the operation was successful or because of * an error. * * After it is closed it is not possible to reuse it again. - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture close(ChannelFuture future); + ChannelFuture close(ChannelPromise promise); /** * Deregister this ChannelOutboundInvoker from the previous assigned {@link EventExecutor} and notify the * {@link ChannelFuture} once the operation completes, either because the operation was successful or because of * an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture deregister(ChannelFuture future); + ChannelFuture deregister(ChannelPromise promise); /** * Reads data from the {@link Channel} into the first inbound buffer, triggers an @@ -150,27 +150,27 @@ public interface ChannelOutboundInvoker { void read(); /** - * Flush all pending data which belongs to this ChannelOutboundInvoker and notify the {@link ChannelFuture} + * Flush all pending data which belongs to this ChannelOutboundInvoker and notify the {@link ChannelPromise} * once the operation completes, either because the operation was successful or because of an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture flush(ChannelFuture future); + ChannelFuture flush(ChannelPromise promise); /** - * Write a message via this ChannelOutboundInvoker and notify the {@link ChannelFuture} + * Write a message via this ChannelOutboundInvoker and notify the {@link ChannelPromise} * once the operation completes, either because the operation was successful or because of an error. * * If you want to write a {@link FileRegion} use {@link #sendFile(FileRegion)} - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified and also returned. */ - ChannelFuture write(Object message, ChannelFuture future); + ChannelFuture write(Object message, ChannelPromise promise); /** - * Send a {@link FileRegion} via this ChannelOutboundInvoker and notify the {@link ChannelFuture} + * Send a {@link FileRegion} via this ChannelOutboundInvoker and notify the {@link ChannelPromise} * once the operation completes, either because the operation was successful or because of an error. * - * The given {@link ChannelFuture} will be notified and also returned. + * The given {@link ChannelPromise} will be notified. */ - ChannelFuture sendFile(FileRegion region, ChannelFuture future); + ChannelFuture sendFile(FileRegion region, ChannelPromise promise); } diff --git a/transport/src/main/java/io/netty/channel/ChannelPromise.java b/transport/src/main/java/io/netty/channel/ChannelPromise.java new file mode 100644 index 0000000000..b4bbe610cc --- /dev/null +++ b/transport/src/main/java/io/netty/channel/ChannelPromise.java @@ -0,0 +1,102 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel; + +/** + * Special {@link ChannelFuture} which is writable. + */ +public interface ChannelPromise extends ChannelFuture { + + /** + * Marks this future as a success and notifies all + * listeners. + * + * If it is success or failed already it will throw an {@link IllegalStateException}. + */ + void setSuccess(); + + /** + * Marks this future as a success and notifies all + * listeners. + * + * @return {@code true} if and only if successfully marked this future as + * a success. Otherwise {@code false} because this future is + * already marked as either a success or a failure. + */ + boolean trySuccess(); + + /** + * Marks this future as a failure and notifies all + * listeners. + * + * If it is success or failed already it will throw an {@link IllegalStateException}. + */ + void setFailure(Throwable cause); + + /** + * Marks this future as a failure and notifies all + * listeners. + * + * @return {@code true} if and only if successfully marked this future as + * a failure. Otherwise {@code false} because this future is + * already marked as either a success or a failure. + */ + boolean tryFailure(Throwable cause); + + /** + * Notifies the progress of the operation to the listeners that implements + * {@link ChannelFutureProgressListener}. Please note that this method will + * not do anything and return {@code false} if this future is complete + * already. + * + * If it is success or failed already it will throw an {@link IllegalStateException}. + */ + void setProgress(long amount, long current, long total); + + /** + * Notifies the progress of the operation to the listeners that implements + * {@link ChannelFutureProgressListener}. Please note that this method will + * not do anything and return {@code false} if this future is complete + * already. + * + * @return {@code true} if and only if notification was made. + */ + boolean tryProgress(long amount, long current, long total); + + @Override + ChannelPromise addListener(ChannelFutureListener listener); + + @Override + ChannelPromise addListeners(ChannelFutureListener... listeners); + + @Override + ChannelPromise removeListener(ChannelFutureListener listener); + + @Override + ChannelPromise removeListeners(ChannelFutureListener... listeners); + + @Override + ChannelPromise sync() throws InterruptedException; + + @Override + ChannelPromise syncUninterruptibly(); + + @Override + ChannelPromise await() throws InterruptedException; + + @Override + ChannelPromise awaitUninterruptibly(); +} diff --git a/transport/src/main/java/io/netty/channel/ChannelFutureAggregator.java b/transport/src/main/java/io/netty/channel/ChannelPromiseAggregator.java similarity index 52% rename from transport/src/main/java/io/netty/channel/ChannelFutureAggregator.java rename to transport/src/main/java/io/netty/channel/ChannelPromiseAggregator.java index 0f2e2c704d..4fe4fb2bdf 100644 --- a/transport/src/main/java/io/netty/channel/ChannelFutureAggregator.java +++ b/transport/src/main/java/io/netty/channel/ChannelPromiseAggregator.java @@ -25,48 +25,43 @@ import java.util.Set; * listening to the individual futures and producing an aggregated result * (success/failure) when all futures have completed. */ -public final class ChannelFutureAggregator implements ChannelFutureListener { +public final class ChannelPromiseAggregator implements ChannelFutureListener { - private final ChannelFuture aggregateFuture; + private final ChannelPromise aggregatePromise; - private Set pendingFutures; + private Set pendingPromises; - public ChannelFutureAggregator(ChannelFuture aggregateFuture) { - this.aggregateFuture = aggregateFuture; + public ChannelPromiseAggregator(ChannelPromise aggregatePromise) { + this.aggregatePromise = aggregatePromise; } - public void addFuture(ChannelFuture future) { + public void addFuture(ChannelPromise promise) { synchronized (this) { - if (pendingFutures == null) { - pendingFutures = new HashSet(); + if (pendingPromises == null) { + pendingPromises = new HashSet(); } - pendingFutures.add(future); + pendingPromises.add(promise); } - future.addListener(this); + promise.addListener(this); } @Override public void operationComplete(ChannelFuture future) throws Exception { - if (future.isCancelled()) { - // TODO: what should the correct behaviour be when a fragment is cancelled? - // cancel all outstanding fragments and cancel the aggregate? - return; - } synchronized (this) { - if (pendingFutures == null) { - aggregateFuture.setSuccess(); + if (pendingPromises == null) { + aggregatePromise.setSuccess(); } else { - pendingFutures.remove(future); + pendingPromises.remove(future); if (!future.isSuccess()) { - aggregateFuture.setFailure(future.cause()); - for (ChannelFuture pendingFuture: pendingFutures) { - pendingFuture.cancel(); + aggregatePromise.setFailure(future.cause()); + for (ChannelPromise pendingFuture: pendingPromises) { + pendingFuture.setFailure(future.cause()); } } else { - if (pendingFutures.isEmpty()) { - aggregateFuture.setSuccess(); + if (pendingPromises.isEmpty()) { + aggregatePromise.setSuccess(); } } } diff --git a/transport/src/main/java/io/netty/channel/ChannelFutureNotifier.java b/transport/src/main/java/io/netty/channel/ChannelPromiseNotifier.java similarity index 56% rename from transport/src/main/java/io/netty/channel/ChannelFutureNotifier.java rename to transport/src/main/java/io/netty/channel/ChannelPromiseNotifier.java index 9a6ba2f7a8..0f9cf8c57e 100644 --- a/transport/src/main/java/io/netty/channel/ChannelFutureNotifier.java +++ b/transport/src/main/java/io/netty/channel/ChannelPromiseNotifier.java @@ -18,45 +18,34 @@ package io.netty.channel; /** * ChannelFutureListener implementation which takes other {@link ChannelFuture}(s) and notifies them on completion. */ -public final class ChannelFutureNotifier implements ChannelFutureListener { +public final class ChannelPromiseNotifier implements ChannelFutureListener { - private final ChannelFuture[] futures; + private final ChannelPromise[] promises; - public ChannelFutureNotifier(ChannelFuture... futures) { - if (futures == null) { - throw new NullPointerException("futures"); + public ChannelPromiseNotifier(ChannelPromise... promises) { + if (promises == null) { + throw new NullPointerException("promises"); } - this.futures = futures.clone(); + for (ChannelPromise promise: promises) { + if (promise == null) { + throw new IllegalArgumentException("promises contains null ChannelPromise"); + } + } + this.promises = promises.clone(); } @Override public void operationComplete(ChannelFuture cf) throws Exception { if (cf.isSuccess()) { - for (ChannelFuture f: futures) { - if (f == null) { - break; - } - f.setSuccess(); - } - return; - } - - if (cf.isCancelled()) { - for (ChannelFuture f: futures) { - if (f == null) { - break; - } - f.cancel(); + for (ChannelPromise p: promises) { + p.setSuccess(); } return; } Throwable cause = cf.cause(); - for (ChannelFuture f: futures) { - if (f == null) { - break; - } - f.setFailure(cause); + for (ChannelPromise p: promises) { + p.setFailure(cause); } } } diff --git a/transport/src/main/java/io/netty/channel/ChannelPropertyAccess.java b/transport/src/main/java/io/netty/channel/ChannelPropertyAccess.java index 439d1c21ac..23be2bb806 100644 --- a/transport/src/main/java/io/netty/channel/ChannelPropertyAccess.java +++ b/transport/src/main/java/io/netty/channel/ChannelPropertyAccess.java @@ -34,9 +34,9 @@ interface ChannelPropertyAccess { ByteBufAllocator alloc(); /** - * Create a new {@link ChannelFuture} + * Create a new {@link ChannelPromise} */ - ChannelFuture newFuture(); + ChannelPromise newPromise(); /** * Create a new {@link ChannelFuture} which is marked as successes already. So {@link ChannelFuture#isSuccess()} diff --git a/transport/src/main/java/io/netty/channel/CombinedChannelHandler.java b/transport/src/main/java/io/netty/channel/CombinedChannelHandler.java index 4ff912f970..f3d9d23878 100644 --- a/transport/src/main/java/io/netty/channel/CombinedChannelHandler.java +++ b/transport/src/main/java/io/netty/channel/CombinedChannelHandler.java @@ -174,34 +174,34 @@ public class CombinedChannelHandler extends ChannelStateHandlerAdapter implement @Override public void bind( ChannelHandlerContext ctx, - SocketAddress localAddress, ChannelFuture future) throws Exception { - out.bind(ctx, localAddress, future); + SocketAddress localAddress, ChannelPromise promise) throws Exception { + out.bind(ctx, localAddress, promise); } @Override public void connect( ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, - ChannelFuture future) throws Exception { - out.connect(ctx, remoteAddress, localAddress, future); + ChannelPromise promise) throws Exception { + out.connect(ctx, remoteAddress, localAddress, promise); } @Override public void disconnect( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - out.disconnect(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + out.disconnect(ctx, promise); } @Override public void close( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - out.close(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + out.close(ctx, promise); } @Override public void deregister( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - out.deregister(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + out.deregister(ctx, promise); } @Override @@ -211,12 +211,12 @@ public class CombinedChannelHandler extends ChannelStateHandlerAdapter implement @Override public void flush( - ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - out.flush(ctx, future); + ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + out.flush(ctx, promise); } @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { - out.sendFile(ctx, region, future); + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception { + out.sendFile(ctx, region, promise); } } diff --git a/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java b/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java index 446d8c4d97..2256825528 100644 --- a/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java @@ -42,7 +42,7 @@ abstract class CompleteChannelFuture implements ChannelFuture { if (listener == null) { throw new NullPointerException("listener"); } - DefaultChannelFuture.notifyListener(this, listener); + DefaultChannelPromise.notifyListener(this, listener); return this; } @@ -55,7 +55,7 @@ abstract class CompleteChannelFuture implements ChannelFuture { if (l == null) { break; } - DefaultChannelFuture.notifyListener(this, l); + DefaultChannelPromise.notifyListener(this, l); } return this; } @@ -120,34 +120,4 @@ abstract class CompleteChannelFuture implements ChannelFuture { public boolean isDone() { return true; } - - @Override - public boolean setProgress(long amount, long current, long total) { - return false; - } - - @Override - public boolean setFailure(Throwable cause) { - return false; - } - - @Override - public boolean setSuccess() { - return false; - } - - @Override - public boolean cancel() { - return false; - } - - @Override - public boolean cancel(boolean mayInterruptIfRunning) { - return false; - } - - @Override - public boolean isCancelled() { - return false; - } } diff --git a/transport/src/main/java/io/netty/channel/CompleteChannelPromise.java b/transport/src/main/java/io/netty/channel/CompleteChannelPromise.java new file mode 100644 index 0000000000..2fb63c6572 --- /dev/null +++ b/transport/src/main/java/io/netty/channel/CompleteChannelPromise.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel; + + +abstract class CompleteChannelPromise extends CompleteChannelFuture implements ChannelPromise { + + protected CompleteChannelPromise(Channel channel) { + super(channel); + } + + @Override + public void setProgress(long amount, long current, long total) { + throw new IllegalStateException(); + } + + @Override + public boolean tryProgress(long amount, long current, long total) { + return false; + } + + @Override + public void setFailure(Throwable cause) { + throw new IllegalStateException(); + } + + @Override + public boolean tryFailure(Throwable cause) { + return false; + } + + @Override + public void setSuccess() { + throw new IllegalStateException(); + } + + @Override + public boolean trySuccess() { + return false; + } + + @Override + public ChannelPromise addListener(final ChannelFutureListener listener) { + return (ChannelPromise) super.addListener(listener); + } + + @Override + public ChannelPromise addListeners(ChannelFutureListener... listeners) { + return (ChannelPromise) super.addListeners(listeners); + } + + @Override + public ChannelPromise removeListener(ChannelFutureListener listener) { + return (ChannelPromise) super.removeListener(listener); + } + + @Override + public ChannelPromise removeListeners(ChannelFutureListener... listeners) { + return (ChannelPromise) super.removeListeners(listeners); + } + + @Override + public ChannelPromise await() throws InterruptedException { + return (ChannelPromise) super.await(); + } + + @Override + public ChannelPromise awaitUninterruptibly() { + return (ChannelPromise) super.awaitUninterruptibly(); + } +} diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java index e59dffa39d..c540378789 100755 --- a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java @@ -1096,72 +1096,72 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements @Override public ChannelFuture bind(SocketAddress localAddress) { - return bind(localAddress, newFuture()); + return bind(localAddress, newPromise()); } @Override public ChannelFuture connect(SocketAddress remoteAddress) { - return connect(remoteAddress, newFuture()); + return connect(remoteAddress, newPromise()); } @Override public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { - return connect(remoteAddress, localAddress, newFuture()); + return connect(remoteAddress, localAddress, newPromise()); } @Override public ChannelFuture disconnect() { - return disconnect(newFuture()); + return disconnect(newPromise()); } @Override public ChannelFuture close() { - return close(newFuture()); + return close(newPromise()); } @Override public ChannelFuture deregister() { - return deregister(newFuture()); + return deregister(newPromise()); } @Override public ChannelFuture flush() { - return flush(newFuture()); + return flush(newPromise()); } @Override public ChannelFuture write(Object message) { - return write(message, newFuture()); + return write(message, newPromise()); } @Override - public ChannelFuture bind(SocketAddress localAddress, ChannelFuture future) { - return pipeline.bind(prevContext(prev, FLAG_OPERATION_HANDLER), localAddress, future); + public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) { + return pipeline.bind(prevContext(prev, FLAG_OPERATION_HANDLER), localAddress, promise); } @Override - public ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future) { - return connect(remoteAddress, null, future); + public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) { + return connect(remoteAddress, null, promise); } @Override - public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) { - return pipeline.connect(prevContext(prev, FLAG_OPERATION_HANDLER), remoteAddress, localAddress, future); + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { + return pipeline.connect(prevContext(prev, FLAG_OPERATION_HANDLER), remoteAddress, localAddress, promise); } @Override - public ChannelFuture disconnect(ChannelFuture future) { - return pipeline.disconnect(prevContext(prev, FLAG_OPERATION_HANDLER), future); + public ChannelFuture disconnect(ChannelPromise promise) { + return pipeline.disconnect(prevContext(prev, FLAG_OPERATION_HANDLER), promise); } @Override - public ChannelFuture close(ChannelFuture future) { - return pipeline.close(prevContext(prev, FLAG_OPERATION_HANDLER), future); + public ChannelFuture close(ChannelPromise promise) { + return pipeline.close(prevContext(prev, FLAG_OPERATION_HANDLER), promise); } @Override - public ChannelFuture deregister(ChannelFuture future) { - return pipeline.deregister(prevContext(prev, FLAG_OPERATION_HANDLER), future); + public ChannelFuture deregister(ChannelPromise promise) { + return pipeline.deregister(prevContext(prev, FLAG_OPERATION_HANDLER), promise); } @Override @@ -1170,27 +1170,27 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements } @Override - public ChannelFuture flush(final ChannelFuture future) { + public ChannelFuture flush(final ChannelPromise promise) { EventExecutor executor = executor(); if (executor.inEventLoop()) { DefaultChannelHandlerContext prev = prevContext(this.prev, FLAG_OPERATION_HANDLER); prev.fillBridge(); - pipeline.flush(prev, future); + pipeline.flush(prev, promise); } else { executor.execute(new Runnable() { @Override public void run() { - flush(future); + flush(promise); } }); } - return future; + return promise; } @Override - public ChannelFuture write(Object message, ChannelFuture future) { - return pipeline.write(prev, message, future); + public ChannelFuture write(Object message, ChannelPromise promise) { + return pipeline.write(prev, message, promise); } void callFreeInboundBuffer() { @@ -1213,8 +1213,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements } @Override - public ChannelFuture newFuture() { - return channel.newFuture(); + public ChannelPromise newPromise() { + return channel.newPromise(); } @Override @@ -1308,11 +1308,11 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements @Override public ChannelFuture sendFile(FileRegion region) { - return pipeline.sendFile(prevContext(prev, FLAG_OPERATION_HANDLER), region, newFuture()); + return pipeline.sendFile(prevContext(prev, FLAG_OPERATION_HANDLER), region, newPromise()); } @Override - public ChannelFuture sendFile(FileRegion region, ChannelFuture future) { - return pipeline.sendFile(prevContext(prev, FLAG_OPERATION_HANDLER), region, future); + public ChannelFuture sendFile(FileRegion region, ChannelPromise promise) { + return pipeline.sendFile(prevContext(prev, FLAG_OPERATION_HANDLER), region, promise); } } diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index dea990330a..d428e6fd2d 100755 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -992,60 +992,60 @@ final class DefaultChannelPipeline implements ChannelPipeline { @Override public ChannelFuture bind(SocketAddress localAddress) { - return bind(localAddress, channel.newFuture()); + return bind(localAddress, channel.newPromise()); } @Override public ChannelFuture connect(SocketAddress remoteAddress) { - return connect(remoteAddress, channel.newFuture()); + return connect(remoteAddress, channel.newPromise()); } @Override public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { - return connect(remoteAddress, localAddress, channel.newFuture()); + return connect(remoteAddress, localAddress, channel.newPromise()); } @Override public ChannelFuture disconnect() { - return disconnect(channel.newFuture()); + return disconnect(channel.newPromise()); } @Override public ChannelFuture close() { - return close(channel.newFuture()); + return close(channel.newPromise()); } @Override public ChannelFuture deregister() { - return deregister(channel.newFuture()); + return deregister(channel.newPromise()); } @Override public ChannelFuture flush() { - return flush(channel.newFuture()); + return flush(channel.newPromise()); } @Override public ChannelFuture write(Object message) { - return write(message, channel.newFuture()); + return write(message, channel.newPromise()); } @Override - public ChannelFuture bind(SocketAddress localAddress, ChannelFuture future) { - return bind(lastContext(FLAG_OPERATION_HANDLER), localAddress, future); + public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) { + return bind(lastContext(FLAG_OPERATION_HANDLER), localAddress, promise); } ChannelFuture bind( - final DefaultChannelHandlerContext ctx, final SocketAddress localAddress, final ChannelFuture future) { + final DefaultChannelHandlerContext ctx, final SocketAddress localAddress, final ChannelPromise promise) { if (localAddress == null) { throw new NullPointerException("localAddress"); } - validateFuture(future); + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { - ((ChannelOperationHandler) ctx.handler()).bind(ctx, localAddress, future); + ((ChannelOperationHandler) ctx.handler()).bind(ctx, localAddress, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1053,35 +1053,35 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - bind(ctx, localAddress, future); + bind(ctx, localAddress, promise); } }); } - return future; + return promise; } @Override - public ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future) { - return connect(remoteAddress, null, future); + public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) { + return connect(remoteAddress, null, promise); } @Override - public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) { - return connect(lastContext(FLAG_OPERATION_HANDLER), remoteAddress, localAddress, future); + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { + return connect(lastContext(FLAG_OPERATION_HANDLER), remoteAddress, localAddress, promise); } ChannelFuture connect( final DefaultChannelHandlerContext ctx, final SocketAddress remoteAddress, - final SocketAddress localAddress, final ChannelFuture future) { + final SocketAddress localAddress, final ChannelPromise promise) { if (remoteAddress == null) { throw new NullPointerException("remoteAddress"); } - validateFuture(future); + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { - ((ChannelOperationHandler) ctx.handler()).connect(ctx, remoteAddress, localAddress, future); + ((ChannelOperationHandler) ctx.handler()).connect(ctx, remoteAddress, localAddress, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1089,31 +1089,31 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - connect(ctx, remoteAddress, localAddress, future); + connect(ctx, remoteAddress, localAddress, promise); } }); } - return future; + return promise; } @Override - public ChannelFuture disconnect(ChannelFuture future) { - return disconnect(lastContext(FLAG_OPERATION_HANDLER), future); + public ChannelFuture disconnect(ChannelPromise promise) { + return disconnect(lastContext(FLAG_OPERATION_HANDLER), promise); } - ChannelFuture disconnect(final DefaultChannelHandlerContext ctx, final ChannelFuture future) { + ChannelFuture disconnect(final DefaultChannelHandlerContext ctx, final ChannelPromise promise) { // Translate disconnect to close if the channel has no notion of disconnect-reconnect. // So far, UDP/IP is the only transport that has such behavior. if (!ctx.channel().metadata().hasDisconnect()) { - return close(ctx, future); + return close(ctx, promise); } - validateFuture(future); + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { - ((ChannelOperationHandler) ctx.handler()).disconnect(ctx, future); + ((ChannelOperationHandler) ctx.handler()).disconnect(ctx, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1121,25 +1121,25 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - disconnect(ctx, future); + disconnect(ctx, promise); } }); } - return future; + return promise; } @Override - public ChannelFuture close(ChannelFuture future) { - return close(lastContext(FLAG_OPERATION_HANDLER), future); + public ChannelFuture close(ChannelPromise promise) { + return close(lastContext(FLAG_OPERATION_HANDLER), promise); } - ChannelFuture close(final DefaultChannelHandlerContext ctx, final ChannelFuture future) { - validateFuture(future); + ChannelFuture close(final DefaultChannelHandlerContext ctx, final ChannelPromise promise) { + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { - ((ChannelOperationHandler) ctx.handler()).close(ctx, future); + ((ChannelOperationHandler) ctx.handler()).close(ctx, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1147,25 +1147,25 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - close(ctx, future); + close(ctx, promise); } }); } - return future; + return promise; } @Override - public ChannelFuture deregister(final ChannelFuture future) { - return deregister(lastContext(FLAG_OPERATION_HANDLER), future); + public ChannelFuture deregister(final ChannelPromise promise) { + return deregister(lastContext(FLAG_OPERATION_HANDLER), promise); } - ChannelFuture deregister(final DefaultChannelHandlerContext ctx, final ChannelFuture future) { - validateFuture(future); + ChannelFuture deregister(final DefaultChannelHandlerContext ctx, final ChannelPromise promise) { + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { - ((ChannelOperationHandler) ctx.handler()).deregister(ctx, future); + ((ChannelOperationHandler) ctx.handler()).deregister(ctx, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1173,36 +1173,36 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - deregister(ctx, future); + deregister(ctx, promise); } }); } - return future; + return promise; } @Override public ChannelFuture sendFile(FileRegion region) { - return sendFile(region, channel().newFuture()); + return sendFile(region, channel().newPromise()); } @Override - public ChannelFuture sendFile(FileRegion region, ChannelFuture future) { - return sendFile(lastContext(FLAG_OPERATION_HANDLER), region, future); + public ChannelFuture sendFile(FileRegion region, ChannelPromise promise) { + return sendFile(lastContext(FLAG_OPERATION_HANDLER), region, promise); } ChannelFuture sendFile(final DefaultChannelHandlerContext ctx, final FileRegion region, - final ChannelFuture future) { + final ChannelPromise promise) { if (region == null) { throw new NullPointerException("region"); } - validateFuture(future); + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { try { ctx.flushBridge(); - ((ChannelOperationHandler) ctx.handler()).sendFile(ctx, region, future); + ((ChannelOperationHandler) ctx.handler()).sendFile(ctx, region, promise); } catch (Throwable t) { notifyHandlerException(t); } @@ -1210,12 +1210,12 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - sendFile(ctx, region, future); + sendFile(ctx, region, promise); } }); } - return future; + return promise; } @Override @@ -1241,36 +1241,36 @@ final class DefaultChannelPipeline implements ChannelPipeline { } @Override - public ChannelFuture flush(ChannelFuture future) { - return flush(lastContext(FLAG_OPERATION_HANDLER), future); + public ChannelFuture flush(ChannelPromise promise) { + return flush(lastContext(FLAG_OPERATION_HANDLER), promise); } - ChannelFuture flush(final DefaultChannelHandlerContext ctx, final ChannelFuture future) { - validateFuture(future); + ChannelFuture flush(final DefaultChannelHandlerContext ctx, final ChannelPromise promise) { + validateFuture(promise); EventExecutor executor = ctx.executor(); if (executor.inEventLoop()) { - flush0(ctx, future); + flush0(ctx, promise); } else { executor.execute(new Runnable() { @Override public void run() { - flush0(ctx, future); + flush0(ctx, promise); } }); } - return future; + return promise; } - private void flush0(final DefaultChannelHandlerContext ctx, ChannelFuture future) { + private void flush0(final DefaultChannelHandlerContext ctx, ChannelPromise promise) { if (!channel.isRegistered() && !channel.isActive()) { - future.setFailure(new ClosedChannelException()); + promise.setFailure(new ClosedChannelException()); return; } try { ctx.flushBridge(); - ((ChannelOperationHandler) ctx.handler()).flush(ctx, future); + ((ChannelOperationHandler) ctx.handler()).flush(ctx, promise); } catch (Throwable t) { notifyHandlerException(t); } finally { @@ -1284,18 +1284,18 @@ final class DefaultChannelPipeline implements ChannelPipeline { } @Override - public ChannelFuture write(Object message, ChannelFuture future) { + public ChannelFuture write(Object message, ChannelPromise promise) { if (message instanceof FileRegion) { - return sendFile((FileRegion) message, future); + return sendFile((FileRegion) message, promise); } - return write(tail, message, future); + return write(tail, message, promise); } - ChannelFuture write(DefaultChannelHandlerContext ctx, final Object message, final ChannelFuture future) { + ChannelFuture write(DefaultChannelHandlerContext ctx, final Object message, final ChannelPromise promise) { if (message == null) { throw new NullPointerException("message"); } - validateFuture(future); + validateFuture(promise); final DefaultChannelHandlerContext initialCtx = ctx; EventExecutor executor; @@ -1333,8 +1333,8 @@ final class DefaultChannelPipeline implements ChannelPipeline { } if (executor.inEventLoop()) { - write0(ctx, message, future, msgBuf); - return future; + write0(ctx, message, promise, msgBuf); + return promise; } final boolean msgBuf0 = msgBuf; @@ -1342,16 +1342,16 @@ final class DefaultChannelPipeline implements ChannelPipeline { executor.execute(new Runnable() { @Override public void run() { - write0(ctx0, message, future, msgBuf0); + write0(ctx0, message, promise, msgBuf0); } }); - return future; + return promise; } - private void write0(DefaultChannelHandlerContext ctx, Object message, ChannelFuture future, boolean msgBuf) { + private void write0(DefaultChannelHandlerContext ctx, Object message, ChannelPromise promise, boolean msgBuf) { if (!channel.isRegistered() && !channel.isActive()) { - future.setFailure(new ClosedChannelException()); + promise.setFailure(new ClosedChannelException()); return; } @@ -1361,7 +1361,7 @@ final class DefaultChannelPipeline implements ChannelPipeline { ByteBuf buf = (ByteBuf) message; ctx.outboundByteBuffer().writeBytes(buf, buf.readerIndex(), buf.readableBytes()); } - flush0(ctx, future); + flush0(ctx, promise); } private void validateFuture(ChannelFuture future) { @@ -1523,32 +1523,32 @@ final class DefaultChannelPipeline implements ChannelPipeline { @Override public void bind( - ChannelHandlerContext ctx, SocketAddress localAddress, ChannelFuture future) + ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { - unsafe.bind(localAddress, future); + unsafe.bind(localAddress, promise); } @Override public void connect( ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, - ChannelFuture future) throws Exception { - unsafe.connect(remoteAddress, localAddress, future); + ChannelPromise promise) throws Exception { + unsafe.connect(remoteAddress, localAddress, promise); } @Override - public void disconnect(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - unsafe.disconnect(future); + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + unsafe.disconnect(promise); } @Override - public void close(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - unsafe.close(future); + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + unsafe.close(promise); } @Override - public void deregister(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - unsafe.deregister(future); + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + unsafe.deregister(promise); } @Override @@ -1557,8 +1557,8 @@ final class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception { - unsafe.flush(future); + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + unsafe.flush(promise); } @Override @@ -1572,8 +1572,8 @@ final class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelFuture future) throws Exception { - unsafe.sendFile(region, future); + public void sendFile(ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception { + unsafe.sendFile(region, promise); } } } diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java b/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java similarity index 79% rename from transport/src/main/java/io/netty/channel/DefaultChannelFuture.java rename to transport/src/main/java/io/netty/channel/DefaultChannelPromise.java index 66f5192e97..dc46d15308 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java @@ -15,28 +15,25 @@ */ package io.netty.channel; -import io.netty.channel.ChannelFlushFutureNotifier.FlushCheckpoint; +import io.netty.channel.ChannelFlushPromiseNotifier.FlushCheckpoint; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; -import java.nio.channels.Channels; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import static java.util.concurrent.TimeUnit.*; /** - * The default {@link ChannelFuture} implementation. It is recommended to use {@link Channel#newFuture()} to create - * a new {@link ChannelFuture} rather than calling the constructor explicitly. + * The default {@link ChannelPromise} implementation. It is recommended to use {@link Channel#newPromise()} to create + * a new {@link ChannelPromise} rather than calling the constructor explicitly. */ -public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFuture { +public class DefaultChannelPromise extends FlushCheckpoint implements ChannelPromise { private static final InternalLogger logger = - InternalLoggerFactory.getInstance(DefaultChannelFuture.class); + InternalLoggerFactory.getInstance(DefaultChannelPromise.class); private static final int MAX_LISTENER_STACK_DEPTH = 8; private static final ThreadLocal LISTENER_STACK_DEPTH = new ThreadLocal() { @@ -46,10 +43,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } }; - private static final Throwable CANCELLED = new Throwable(); - private final Channel channel; - private final boolean cancellable; private ChannelFutureListener firstListener; private List otherListeners; @@ -68,12 +62,9 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu * * @param channel * the {@link Channel} associated with this future - * @param cancellable - * {@code true} if and only if this future can be canceled */ - public DefaultChannelFuture(Channel channel, boolean cancellable) { + public DefaultChannelPromise(Channel channel) { this.channel = channel; - this.cancellable = cancellable; } @Override @@ -93,20 +84,11 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu @Override public synchronized Throwable cause() { - if (cause != CANCELLED) { - return cause; - } else { - return null; - } + return cause; } @Override - public synchronized boolean isCancelled() { - return cause == CANCELLED; - } - - @Override - public ChannelFuture addListener(final ChannelFutureListener listener) { + public ChannelPromise addListener(final ChannelFutureListener listener) { if (listener == null) { throw new NullPointerException("listener"); } @@ -142,7 +124,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture addListeners(ChannelFutureListener... listeners) { + public ChannelPromise addListeners(ChannelFutureListener... listeners) { if (listeners == null) { throw new NullPointerException("listeners"); } @@ -157,7 +139,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture removeListener(ChannelFutureListener listener) { + public ChannelPromise removeListener(ChannelFutureListener listener) { if (listener == null) { throw new NullPointerException("listener"); } @@ -184,7 +166,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture removeListeners(ChannelFutureListener... listeners) { + public ChannelPromise removeListeners(ChannelFutureListener... listeners) { if (listeners == null) { throw new NullPointerException("listeners"); } @@ -199,45 +181,19 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture sync() throws InterruptedException { + public ChannelPromise sync() throws InterruptedException { await(); rethrowIfFailed(); return this; } @Override - public ChannelFuture syncUninterruptibly() { + public ChannelPromise syncUninterruptibly() { awaitUninterruptibly(); rethrowIfFailed(); return this; } - @Override - public Void get() throws InterruptedException, ExecutionException { - await(); - Throwable cause = cause(); - if (cause == null) { - return null; - } else { - throw new ExecutionException(cause); - } - } - - @Override - public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, - TimeoutException { - if (!await(timeout, unit)) { - throw new TimeoutException(); - } - - Throwable cause = cause(); - if (cause == null) { - return null; - } else { - throw new ExecutionException(cause); - } - } - private void rethrowIfFailed() { Throwable cause = cause(); if (cause == null) { @@ -256,7 +212,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture await() throws InterruptedException { + public ChannelPromise await() throws InterruptedException { if (Thread.interrupted()) { throw new InterruptedException(); } @@ -287,7 +243,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public ChannelFuture awaitUninterruptibly() { + public ChannelPromise awaitUninterruptibly() { boolean interrupted = false; synchronized (this) { while (!done) { @@ -384,68 +340,66 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public boolean setSuccess() { - synchronized (this) { - // Allow only once. - if (done) { - return false; - } - - done = true; - if (waiters > 0) { - notifyAll(); - } + public void setSuccess() { + if (success0()) { + notifyListeners(); + return; } - - notifyListeners(); - return true; + throw new IllegalStateException(); } @Override - public boolean setFailure(Throwable cause) { - synchronized (this) { - // Allow only once. - if (done) { - return false; - } - - this.cause = cause; - done = true; - if (waiters > 0) { - notifyAll(); - } + public boolean trySuccess() { + if (success0()) { + notifyListeners(); + return true; } - - notifyListeners(); - return true; + return false; } - @Override - public boolean cancel() { - if (!cancellable) { + private synchronized boolean success0() { + // Allow only once. + if (done) { return false; } - synchronized (this) { - // Allow only once. - if (done) { - return false; - } - - cause = CANCELLED; - done = true; - if (waiters > 0) { - notifyAll(); - } + done = true; + if (waiters > 0) { + notifyAll(); } - - notifyListeners(); return true; } @Override - public boolean cancel(boolean mayInterruptIfRunning) { - return cancel(); + public void setFailure(Throwable cause) { + if (failure0(cause)) { + notifyListeners(); + return; + } + throw new IllegalStateException(); + } + + @Override + public boolean tryFailure(Throwable cause) { + if (failure0(cause)) { + notifyListeners(); + return true; + } + return false; + } + + private synchronized boolean failure0(Throwable cause) { + // Allow only once. + if (done) { + return false; + } + + this.cause = cause; + done = true; + if (waiters > 0) { + notifyAll(); + } + return true; } private void notifyListeners() { @@ -477,10 +431,10 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu channel().eventLoop().execute(new Runnable() { @Override public void run() { - notifyListener0(DefaultChannelFuture.this, firstListener); + notifyListener0(DefaultChannelPromise.this, firstListener); if (otherListeners != null) { for (ChannelFutureListener l: otherListeners) { - notifyListener0(DefaultChannelFuture.this, l); + notifyListener0(DefaultChannelPromise.this, l); } } } @@ -524,7 +478,32 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - public boolean setProgress(long amount, long current, long total) { + public void setProgress(long amount, long current, long total) { + ChannelFutureProgressListener[] plisteners; + synchronized (this) { + // Do not generate progress event after completion. + if (done) { + throw new IllegalStateException(); + } + + Collection progressListeners = + this.progressListeners; + if (progressListeners == null || progressListeners.isEmpty()) { + // Nothing to notify - no need to create an empty array. + return; + } + + plisteners = progressListeners.toArray( + new ChannelFutureProgressListener[progressListeners.size()]); + } + + for (ChannelFutureProgressListener pl: plisteners) { + notifyProgressListener(pl, amount, current, total); + } + } + + @Override + public boolean tryProgress(long amount, long current, long total) { ChannelFutureProgressListener[] plisteners; synchronized (this) { // Do not generate progress event after completion. @@ -533,7 +512,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } Collection progressListeners = - this.progressListeners; + this.progressListeners; if (progressListeners == null || progressListeners.isEmpty()) { // Nothing to notify - no need to create an empty array. return true; @@ -576,7 +555,7 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu } @Override - ChannelFuture future() { + ChannelPromise future() { return this; } } diff --git a/transport/src/main/java/io/netty/channel/EventLoopGroup.java b/transport/src/main/java/io/netty/channel/EventLoopGroup.java index 16cc4e5084..d942e69873 100644 --- a/transport/src/main/java/io/netty/channel/EventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/EventLoopGroup.java @@ -37,5 +37,5 @@ public interface EventLoopGroup extends EventExecutorGroup { * Register a {@link Channel} with this {@link EventLoop}. The passed {@link ChannelFuture} * will get notified once the registration was complete and also will get returned. */ - ChannelFuture register(Channel channel, ChannelFuture future); + ChannelFuture register(Channel channel, ChannelPromise promise); } diff --git a/transport/src/main/java/io/netty/channel/FailedChannelFuture.java b/transport/src/main/java/io/netty/channel/FailedChannelFuture.java index b163fec082..be41f7dfea 100644 --- a/transport/src/main/java/io/netty/channel/FailedChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/FailedChannelFuture.java @@ -15,9 +15,6 @@ */ package io.netty.channel; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - /** * The {@link CompleteChannelFuture} which is failed already. It is * recommended to use {@link Channel#newFailedFuture(Throwable)} @@ -72,14 +69,4 @@ final class FailedChannelFuture extends CompleteChannelFuture { throw new ChannelException(cause); } - - @Override - public Void get() throws ExecutionException { - throw new ExecutionException(cause); - } - - @Override - public Void get(long timeout, TimeUnit unit) throws ExecutionException { - throw new ExecutionException(cause); - } } diff --git a/transport/src/main/java/io/netty/channel/MultithreadEventLoopGroup.java b/transport/src/main/java/io/netty/channel/MultithreadEventLoopGroup.java index 8086666b95..290ddc589d 100644 --- a/transport/src/main/java/io/netty/channel/MultithreadEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/MultithreadEventLoopGroup.java @@ -42,7 +42,7 @@ public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutor } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { - return next().register(channel, future); + public ChannelFuture register(Channel channel, ChannelPromise promise) { + return next().register(channel, promise); } } diff --git a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java index 397a3eee1a..ed79380f81 100644 --- a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java +++ b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java @@ -47,27 +47,27 @@ public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor im if (channel == null) { throw new NullPointerException("channel"); } - return register(channel, channel.newFuture()); + return register(channel, channel.newPromise()); } @Override - public ChannelFuture register(final Channel channel, final ChannelFuture future) { + public ChannelFuture register(final Channel channel, final ChannelPromise promise) { if (isShutdown()) { channel.unsafe().closeForcibly(); - future.setFailure(new EventLoopException("cannot register a channel to a shut down loop")); - return future; + promise.setFailure(new EventLoopException("cannot register a channel to a shut down loop")); + return promise; } if (inEventLoop()) { - channel.unsafe().register(this, future); + channel.unsafe().register(this, promise); } else { execute(new Runnable() { @Override public void run() { - channel.unsafe().register(SingleThreadEventLoop.this, future); + channel.unsafe().register(SingleThreadEventLoop.this, promise); } }); } - return future; + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java b/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java index 6599b9d7a7..71826bb904 100644 --- a/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java @@ -15,8 +15,6 @@ */ package io.netty.channel; -import java.util.concurrent.TimeUnit; - /** * The {@link CompleteChannelFuture} which is succeeded already. It is * recommended to use {@link Channel#newSucceededFuture()} instead of @@ -52,15 +50,4 @@ final class SucceededChannelFuture extends CompleteChannelFuture { public ChannelFuture syncUninterruptibly() { return this; } - - @Override - public Void get() { - return null; - } - - @Override - public Void get(long timeout, TimeUnit unit) { - return null; - } - } diff --git a/transport/src/main/java/io/netty/channel/VoidChannelFuture.java b/transport/src/main/java/io/netty/channel/VoidChannelPromise.java similarity index 69% rename from transport/src/main/java/io/netty/channel/VoidChannelFuture.java rename to transport/src/main/java/io/netty/channel/VoidChannelPromise.java index 05cc49624d..2d76c962ce 100644 --- a/transport/src/main/java/io/netty/channel/VoidChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/VoidChannelPromise.java @@ -17,7 +17,7 @@ package io.netty.channel; import java.util.concurrent.TimeUnit; -final class VoidChannelFuture implements ChannelFuture.Unsafe { +final class VoidChannelPromise implements ChannelFuture.Unsafe, ChannelPromise { private final Channel channel; @@ -26,7 +26,7 @@ final class VoidChannelFuture implements ChannelFuture.Unsafe { * * @param channel the {@link Channel} associated with this future */ - public VoidChannelFuture(Channel channel) { + public VoidChannelPromise(Channel channel) { if (channel == null) { throw new NullPointerException("channel"); } @@ -34,31 +34,31 @@ final class VoidChannelFuture implements ChannelFuture.Unsafe { } @Override - public ChannelFuture addListener(final ChannelFutureListener listener) { + public ChannelPromise addListener(final ChannelFutureListener listener) { fail(); return this; } @Override - public ChannelFuture addListeners(final ChannelFutureListener... listeners) { + public ChannelPromise addListeners(final ChannelFutureListener... listeners) { fail(); return this; } @Override - public ChannelFuture removeListener(ChannelFutureListener listener) { + public ChannelPromise removeListener(ChannelFutureListener listener) { // NOOP return this; } @Override - public ChannelFuture removeListeners(ChannelFutureListener... listeners) { + public ChannelPromise removeListeners(ChannelFutureListener... listeners) { // NOOP return this; } @Override - public ChannelFuture await() throws InterruptedException { + public ChannelPromise await() throws InterruptedException { if (Thread.interrupted()) { throw new InterruptedException(); } @@ -78,7 +78,7 @@ final class VoidChannelFuture implements ChannelFuture.Unsafe { } @Override - public ChannelFuture awaitUninterruptibly() { + public ChannelPromise awaitUninterruptibly() { fail(); return this; } @@ -105,11 +105,6 @@ final class VoidChannelFuture implements ChannelFuture.Unsafe { return false; } - @Override - public boolean isCancelled() { - return false; - } - @Override public boolean isSuccess() { return false; @@ -121,51 +116,41 @@ final class VoidChannelFuture implements ChannelFuture.Unsafe { } @Override - public ChannelFuture sync() { + public ChannelPromise sync() { fail(); return this; } @Override - public ChannelFuture syncUninterruptibly() { + public ChannelPromise syncUninterruptibly() { fail(); return this; } @Override - public Void get() { - fail(); - return null; + public void setProgress(long amount, long current, long total) { } @Override - public Void get(long timeout, TimeUnit unit) { - fail(); - return null; + public void setFailure(Throwable cause) { } @Override - public boolean setProgress(long amount, long current, long total) { + public void setSuccess() { + } + + @Override + public boolean tryProgress(long amount, long current, long total) { return false; } @Override - public boolean setFailure(Throwable cause) { + public boolean tryFailure(Throwable cause) { return false; } @Override - public boolean setSuccess() { - return false; - } - - @Override - public boolean cancel() { - return false; - } - - @Override - public boolean cancel(boolean mayInterruptIfRunning) { + public boolean trySuccess() { return false; } diff --git a/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java b/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java index c8338e122c..04135c36ed 100755 --- a/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java +++ b/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java @@ -23,13 +23,13 @@ import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelException; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultChannelConfig; import io.netty.channel.EventLoop; import io.netty.logging.InternalLogger; @@ -319,8 +319,8 @@ public abstract class AbstractEmbeddedChannel extends AbstractChannel { private class DefaultUnsafe extends AbstractUnsafe { @Override public void connect(SocketAddress remoteAddress, - SocketAddress localAddress, ChannelFuture future) { - future.setSuccess(); + SocketAddress localAddress, ChannelPromise promise) { + promise.setSuccess(); } } diff --git a/transport/src/main/java/io/netty/channel/embedded/EmbeddedEventLoop.java b/transport/src/main/java/io/netty/channel/embedded/EmbeddedEventLoop.java index 9f8e4d4bbc..2ed5a6d213 100644 --- a/transport/src/main/java/io/netty/channel/embedded/EmbeddedEventLoop.java +++ b/transport/src/main/java/io/netty/channel/embedded/EmbeddedEventLoop.java @@ -17,6 +17,7 @@ package io.netty.channel.embedded; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; @@ -105,13 +106,13 @@ final class EmbeddedEventLoop extends AbstractExecutorService implements EventLo @Override public ChannelFuture register(Channel channel) { - return register(channel, channel.newFuture()); + return register(channel, channel.newPromise()); } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { - channel.unsafe().register(this, future); - return future; + public ChannelFuture register(Channel channel, ChannelPromise promise) { + channel.unsafe().register(this, promise); + return promise; } @Override diff --git a/transport/src/main/java/io/netty/channel/local/LocalChannel.java b/transport/src/main/java/io/netty/channel/local/LocalChannel.java index bf5c249440..9b8ac2694b 100755 --- a/transport/src/main/java/io/netty/channel/local/LocalChannel.java +++ b/transport/src/main/java/io/netty/channel/local/LocalChannel.java @@ -21,9 +21,9 @@ import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelException; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultChannelConfig; import io.netty.channel.EventLoop; import io.netty.channel.SingleThreadEventExecutor; @@ -55,7 +55,7 @@ public class LocalChannel extends AbstractChannel { private volatile LocalChannel peer; private volatile LocalAddress localAddress; private volatile LocalAddress remoteAddress; - private volatile ChannelFuture connectFuture; + private volatile ChannelPromise connectPromise; private volatile boolean readInProgress; public LocalChannel() { @@ -149,7 +149,7 @@ public class LocalChannel extends AbstractChannel { peerEventLoop.execute(new Runnable() { @Override public void run() { - peer.connectFuture.setSuccess(); + peer.connectPromise.setSuccess(); peer.pipeline().fireChannelActive(); } }); @@ -272,24 +272,24 @@ public class LocalChannel extends AbstractChannel { @Override public void connect(final SocketAddress remoteAddress, - SocketAddress localAddress, final ChannelFuture future) { + SocketAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } if (state == 2) { Exception cause = new AlreadyConnectedException(); - future.setFailure(cause); + promise.setFailure(cause); pipeline().fireExceptionCaught(cause); return; } - if (connectFuture != null) { + if (connectPromise != null) { throw new ConnectionPendingException(); } - connectFuture = future; + connectPromise = promise; if (state != 1) { // Not bound yet and no localAddress specified - get one. @@ -302,7 +302,7 @@ public class LocalChannel extends AbstractChannel { try { doBind(localAddress); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); pipeline().fireExceptionCaught(t); close(voidFuture()); return; @@ -313,7 +313,7 @@ public class LocalChannel extends AbstractChannel { if (!(boundChannel instanceof LocalServerChannel)) { Exception cause = new ChannelException("connection refused"); - future.setFailure(cause); + promise.setFailure(cause); pipeline().fireExceptionCaught(cause); close(voidFuture()); return; @@ -326,7 +326,7 @@ public class LocalChannel extends AbstractChannel { eventLoop().execute(new Runnable() { @Override public void run() { - connect(remoteAddress, localAddress0, future); + connect(remoteAddress, localAddress0, promise); } }); } diff --git a/transport/src/main/java/io/netty/channel/socket/DatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/DatagramChannel.java index 5eb06a5406..6f575abd67 100644 --- a/transport/src/main/java/io/netty/channel/socket/DatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/DatagramChannel.java @@ -17,6 +17,7 @@ package io.netty.channel.socket; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -50,7 +51,7 @@ public interface DatagramChannel extends Channel { * * The given {@link ChannelFuture} will be notified and also returned. */ - ChannelFuture joinGroup(InetAddress multicastAddress, ChannelFuture future); + ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise future); /** * Joins the specified multicast group at the specified interface and notifies the {@link ChannelFuture} @@ -65,7 +66,7 @@ public interface DatagramChannel extends Channel { * The given {@link ChannelFuture} will be notified and also returned. */ ChannelFuture joinGroup( - InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelFuture future); + InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelPromise future); /** * Joins the specified multicast group at the specified interface and notifies the {@link ChannelFuture} @@ -80,7 +81,7 @@ public interface DatagramChannel extends Channel { * The given {@link ChannelFuture} will be notified and also returned. */ ChannelFuture joinGroup( - InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, ChannelFuture future); + InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, ChannelPromise future); /** * Leaves a multicast group and notifies the {@link ChannelFuture} once the operation completes. @@ -92,7 +93,7 @@ public interface DatagramChannel extends Channel { * * The given {@link ChannelFuture} will be notified and also returned. */ - ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelFuture future); + ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise future); /** * Leaves a multicast group on a specified local interface and notifies the {@link ChannelFuture} once the @@ -107,7 +108,7 @@ public interface DatagramChannel extends Channel { * The given {@link ChannelFuture} will be notified and also returned. */ ChannelFuture leaveGroup( - InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelFuture future); + InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelPromise future); /** * Leave the specified multicast group at the specified interface using the specified source and notifies @@ -125,7 +126,7 @@ public interface DatagramChannel extends Channel { */ ChannelFuture leaveGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, - ChannelFuture future); + ChannelPromise future); /** * Block the given sourceToBlock address for the given multicastAddress on the given networkInterface and notifies @@ -145,7 +146,7 @@ public interface DatagramChannel extends Channel { */ ChannelFuture block( InetAddress multicastAddress, NetworkInterface networkInterface, - InetAddress sourceToBlock, ChannelFuture future); + InetAddress sourceToBlock, ChannelPromise future); /** * Block the given sourceToBlock address for the given multicastAddress and notifies the {@link ChannelFuture} once @@ -162,5 +163,5 @@ public interface DatagramChannel extends Channel { * The given {@link ChannelFuture} will be notified and also returned. */ ChannelFuture block( - InetAddress multicastAddress, InetAddress sourceToBlock, ChannelFuture future); + InetAddress multicastAddress, InetAddress sourceToBlock, ChannelPromise future); } diff --git a/transport/src/main/java/io/netty/channel/socket/SctpChannel.java b/transport/src/main/java/io/netty/channel/socket/SctpChannel.java index e648243d6b..8df521ee25 100644 --- a/transport/src/main/java/io/netty/channel/socket/SctpChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/SctpChannel.java @@ -18,6 +18,7 @@ package io.netty.channel.socket; import com.sun.nio.sctp.Association; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import java.net.InetAddress; import java.net.SocketAddress; @@ -88,7 +89,7 @@ public interface SctpChannel extends Channel { * * The given {@link ChannelFuture} will be notified and also returned. */ - ChannelFuture bindAddress(InetAddress localAddress, ChannelFuture future); + ChannelFuture bindAddress(InetAddress localAddress, ChannelPromise future); /** * Unbind the address from channel's multi-homing address list. @@ -102,5 +103,5 @@ public interface SctpChannel extends Channel { * * The given {@link ChannelFuture} will be notified and also returned. */ - ChannelFuture unbindAddress(InetAddress localAddress, ChannelFuture future); + ChannelFuture unbindAddress(InetAddress localAddress, ChannelPromise future); } diff --git a/transport/src/main/java/io/netty/channel/socket/SocketChannel.java b/transport/src/main/java/io/netty/channel/socket/SocketChannel.java index 5eb6303225..f616ba1614 100644 --- a/transport/src/main/java/io/netty/channel/socket/SocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/SocketChannel.java @@ -17,6 +17,7 @@ package io.netty.channel.socket; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import java.net.InetSocketAddress; import java.net.Socket; @@ -55,7 +56,7 @@ public interface SocketChannel extends Channel { /** * @see Socket#shutdownOutput() * - * Will notify and return the given {@link ChannelFuture} + * Will notify the given {@link ChannelPromise} */ - ChannelFuture shutdownOutput(ChannelFuture future); + ChannelFuture shutdownOutput(ChannelPromise future); } diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AbstractAioChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AbstractAioChannel.java index 7d84799c4b..23c1762196 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AbstractAioChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AbstractAioChannel.java @@ -17,7 +17,7 @@ package io.netty.channel.socket.aio; import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import java.net.ConnectException; @@ -39,7 +39,7 @@ abstract class AbstractAioChannel extends AbstractChannel { * The future of the current connection attempt. If not null, subsequent * connection attempts will fail. */ - protected ChannelFuture connectFuture; + protected ChannelPromise connectPromise; protected ScheduledFuture connectTimeoutFuture; private ConnectException connectTimeoutException; @@ -103,19 +103,19 @@ abstract class AbstractAioChannel extends AbstractChannel { @Override public void connect(final SocketAddress remoteAddress, - final SocketAddress localAddress, final ChannelFuture future) { + final SocketAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } try { - if (connectFuture != null) { + if (connectPromise != null) { throw new IllegalStateException("connection attempt already made"); } - connectFuture = future; + connectPromise = promise; - doConnect(remoteAddress, localAddress, future); + doConnect(remoteAddress, localAddress, promise); // Schedule connect timeout. int connectTimeoutMillis = config().getConnectTimeoutMillis(); @@ -126,9 +126,9 @@ abstract class AbstractAioChannel extends AbstractChannel { if (connectTimeoutException == null) { connectTimeoutException = new ConnectException("connection timed out"); } - ChannelFuture connectFuture = AbstractAioChannel.this.connectFuture; + ChannelPromise connectFuture = connectPromise; if (connectFuture != null && - connectFuture.setFailure(connectTimeoutException)) { + connectFuture.tryFailure(connectTimeoutException)) { pipeline().fireExceptionCaught(connectTimeoutException); close(voidFuture()); } @@ -136,40 +136,40 @@ abstract class AbstractAioChannel extends AbstractChannel { }, connectTimeoutMillis, TimeUnit.MILLISECONDS); } } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); closeIfClosed(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - connect(remoteAddress, localAddress, future); + connect(remoteAddress, localAddress, promise); } }); } } protected void connectFailed(Throwable t) { - connectFuture.setFailure(t); + connectPromise.setFailure(t); pipeline().fireExceptionCaught(t); closeIfClosed(); } protected void connectSuccess() { assert eventLoop().inEventLoop(); - assert connectFuture != null; + assert connectPromise != null; try { boolean wasActive = isActive(); - connectFuture.setSuccess(); + connectPromise.setSuccess(); if (!wasActive && isActive()) { pipeline().fireChannelActive(); } } catch (Throwable t) { - connectFuture.setFailure(t); + connectPromise.setFailure(t); closeIfClosed(); } finally { connectTimeoutFuture.cancel(false); - connectFuture = null; + connectPromise = null; } } } @@ -178,6 +178,6 @@ abstract class AbstractAioChannel extends AbstractChannel { * Connect to the remote peer using the given localAddress if one is specified or {@code null} otherwise. */ protected abstract void doConnect(SocketAddress remoteAddress, - SocketAddress localAddress, ChannelFuture connectFuture); + SocketAddress localAddress, ChannelPromise connectPromise); } diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioEventLoop.java b/transport/src/main/java/io/netty/channel/socket/aio/AioEventLoop.java index 271b06c844..8cec51bbda 100644 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioEventLoop.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioEventLoop.java @@ -18,6 +18,7 @@ package io.netty.channel.socket.aio; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelTaskScheduler; import io.netty.channel.SingleThreadEventLoop; @@ -65,7 +66,7 @@ final class AioEventLoop extends SingleThreadEventLoop { } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { + public ChannelFuture register(Channel channel, ChannelPromise future) { return super.register(channel, future).addListener(registrationListener); } diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java index e97792b803..15fa03fedc 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java @@ -17,8 +17,8 @@ package io.netty.channel.socket.aio; import io.netty.buffer.BufType; import io.netty.channel.ChannelException; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.ServerSocketChannelConfig; @@ -140,8 +140,8 @@ public class AioServerSocketChannel extends AbstractAioChannel implements Server @Override protected void doConnect( - SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) { - future.setFailure(new UnsupportedOperationException()); + SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { + promise.setFailure(new UnsupportedOperationException()); } @Override diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java index 72eeda7ec3..8edb47c8d2 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java @@ -18,13 +18,14 @@ package io.netty.channel.socket.aio; import io.netty.buffer.BufType; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelException; -import io.netty.channel.ChannelFlushFutureNotifier; +import io.netty.channel.ChannelFlushPromiseNotifier; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; +import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoop; import io.netty.channel.FileRegion; -import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.socket.SocketChannel; import java.io.IOException; @@ -125,38 +126,38 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne @Override public ChannelFuture shutdownOutput() { - return shutdownOutput(newFuture()); + return shutdownOutput(newPromise()); } @Override - public ChannelFuture shutdownOutput(final ChannelFuture future) { + public ChannelFuture shutdownOutput(final ChannelPromise promise) { EventLoop loop = eventLoop(); if (loop.inEventLoop()) { try { javaChannel().shutdownOutput(); outputShutdown = true; - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { loop.execute(new Runnable() { @Override public void run() { - shutdownOutput(future); + shutdownOutput(promise); } }); } - return future; + return promise; } @Override - protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress, final ChannelFuture future) { + protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress, final ChannelPromise promise) { if (localAddress != null) { try { javaChannel().bind(localAddress); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); return; } } @@ -304,8 +305,8 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne } @Override - protected void doFlushFileRegion(FileRegion region, ChannelFuture future) throws Exception { - region.transferTo(new WritableByteChannelAdapter(region, future), 0); + protected void doFlushFileRegion(FileRegion region, ChannelPromise promise) throws Exception { + region.transferTo(new WritableByteChannelAdapter(region, promise), 0); } @Override @@ -363,7 +364,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne // Notify flush futures only when the handler is called outside of unsafe().flushNow() // because flushNow() will do that for us. - ChannelFlushFutureNotifier notifier = channel.flushFutureNotifier; + ChannelFlushPromiseNotifier notifier = channel.flushFutureNotifier; notifier.increaseWriteCounter(writtenBytes); notifier.notifyFlushFutures(); @@ -506,12 +507,12 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne private final class WritableByteChannelAdapter implements WritableByteChannel { private final FileRegion region; - private final ChannelFuture future; + private final ChannelPromise promise; private long written; - public WritableByteChannelAdapter(FileRegion region, ChannelFuture future) { + public WritableByteChannelAdapter(FileRegion region, ChannelPromise promise) { this.region = region; - this.future = future; + this.promise = promise; } @Override @@ -527,14 +528,14 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne } if (result == -1) { checkEOF(region, written); - future.setSuccess(); + promise.setSuccess(); return; } written += result; if (written >= region.count()) { region.close(); - future.setSuccess(); + promise.setSuccess(); return; } if (src.hasRemaining()) { @@ -544,14 +545,14 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne } } catch (Throwable cause) { region.close(); - future.setFailure(cause); + promise.setFailure(cause); } } @Override public void failed(Throwable exc, Object attachment) { region.close(); - future.setFailure(exc); + promise.setFailure(exc); } }); return 0; diff --git a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java index f933c5d2c8..d498ce23ec 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java @@ -17,11 +17,11 @@ package io.netty.channel.socket.nio; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; +import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.FileRegion; -import io.netty.channel.socket.ChannelInputShutdownEvent; import java.io.IOException; import java.nio.channels.ClosedChannelException; @@ -151,9 +151,9 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { } @Override - protected void doFlushFileRegion(final FileRegion region, final ChannelFuture future) throws Exception { + protected void doFlushFileRegion(final FileRegion region, final ChannelPromise promise) throws Exception { if (javaChannel() instanceof WritableByteChannel) { - TransferTask transferTask = new TransferTask(region, (WritableByteChannel) javaChannel(), future); + TransferTask transferTask = new TransferTask(region, (WritableByteChannel) javaChannel(), promise); transferTask.transfer(); } else { throw new UnsupportedOperationException("Underlying Channel is not of instance " @@ -165,12 +165,12 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { private long writtenBytes; private final FileRegion region; private final WritableByteChannel wch; - private final ChannelFuture future; + private final ChannelPromise promise; - TransferTask(FileRegion region, WritableByteChannel wch, ChannelFuture future) { + TransferTask(FileRegion region, WritableByteChannel wch, ChannelPromise promise) { this.region = region; this.wch = wch; - this.future = future; + this.promise = promise; } void transfer() { @@ -184,20 +184,20 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { return; } else if (localWrittenBytes == -1) { checkEOF(region, writtenBytes); - future.setSuccess(); + promise.setSuccess(); return; } else { writtenBytes += localWrittenBytes; if (writtenBytes >= region.count()) { region.close(); - future.setSuccess(); + promise.setSuccess(); return; } } } } catch (Throwable cause) { region.close(); - future.setFailure(cause); + promise.setFailure(cause); } } @@ -209,16 +209,16 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { @Override public void channelUnregistered(SelectableChannel ch, Throwable cause) throws Exception { if (cause != null) { - future.setFailure(cause); + promise.setFailure(cause); return; } if (writtenBytes < region.count()) { region.close(); if (!isOpen()) { - future.setFailure(new ClosedChannelException()); + promise.setFailure(new ClosedChannelException()); } else { - future.setFailure(new IllegalStateException( + promise.setFailure(new IllegalStateException( "Channel was unregistered before the region could be fully written")); } } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioChannel.java index 39b6f43cae..5d6eab0336 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioChannel.java @@ -18,7 +18,7 @@ package io.netty.channel.socket.nio; import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelException; -import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; @@ -52,7 +52,7 @@ public abstract class AbstractNioChannel extends AbstractChannel { * The future of the current connection attempt. If not null, subsequent * connection attempts will fail. */ - private ChannelFuture connectFuture; + private ChannelPromise connectPromise; private ScheduledFuture connectTimeoutFuture; private ConnectException connectTimeoutException; @@ -165,25 +165,25 @@ public abstract class AbstractNioChannel extends AbstractChannel { @Override public void connect( - final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelFuture future) { + final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } try { - if (connectFuture != null) { + if (connectPromise != null) { throw new IllegalStateException("connection attempt already made"); } boolean wasActive = isActive(); if (doConnect(remoteAddress, localAddress)) { - future.setSuccess(); + promise.setSuccess(); if (!wasActive && isActive()) { pipeline().fireChannelActive(); } } else { - connectFuture = future; + connectPromise = promise; // Schedule connect timeout. int connectTimeoutMillis = config().getConnectTimeoutMillis(); @@ -194,8 +194,8 @@ public abstract class AbstractNioChannel extends AbstractChannel { if (connectTimeoutException == null) { connectTimeoutException = new ConnectException("connection timed out"); } - ChannelFuture connectFuture = AbstractNioChannel.this.connectFuture; - if (connectFuture != null && connectFuture.setFailure(connectTimeoutException)) { + ChannelPromise connectFuture = AbstractNioChannel.this.connectPromise; + if (connectFuture != null && connectFuture.tryFailure(connectTimeoutException)) { close(voidFuture()); } } @@ -203,14 +203,14 @@ public abstract class AbstractNioChannel extends AbstractChannel { } } } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); closeIfClosed(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - connect(remoteAddress, localAddress, future); + connect(remoteAddress, localAddress, promise); } }); } @@ -219,20 +219,20 @@ public abstract class AbstractNioChannel extends AbstractChannel { @Override public void finishConnect() { assert eventLoop().inEventLoop(); - assert connectFuture != null; + assert connectPromise != null; try { boolean wasActive = isActive(); doFinishConnect(); - connectFuture.setSuccess(); + connectPromise.setSuccess(); if (!wasActive && isActive()) { pipeline().fireChannelActive(); } } catch (Throwable t) { - connectFuture.setFailure(t); + connectPromise.setFailure(t); closeIfClosed(); } finally { connectTimeoutFuture.cancel(false); - connectFuture = null; + connectPromise = null; } } } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index 06034caa7b..4d2ad979cc 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -22,6 +22,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DatagramChannelConfig; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.InternetProtocolFamily; @@ -249,45 +250,45 @@ public final class NioDatagramChannel @Override public ChannelFuture joinGroup(InetAddress multicastAddress) { - return joinGroup(multicastAddress, newFuture()); + return joinGroup(multicastAddress, newPromise()); } @Override - public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelFuture future) { + public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { try { return joinGroup( multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), - null, future); + null, promise); } catch (SocketException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override public ChannelFuture joinGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface) { - return joinGroup(multicastAddress, networkInterface, newFuture()); + return joinGroup(multicastAddress, networkInterface, newPromise()); } @Override public ChannelFuture joinGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface, - ChannelFuture future) { - return joinGroup(multicastAddress.getAddress(), networkInterface, null, future); + ChannelPromise promise) { + return joinGroup(multicastAddress.getAddress(), networkInterface, null, promise); } @Override public ChannelFuture joinGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) { - return joinGroup(multicastAddress, networkInterface, source, newFuture()); + return joinGroup(multicastAddress, networkInterface, source, newPromise()); } @Override public ChannelFuture joinGroup( InetAddress multicastAddress, NetworkInterface networkInterface, - InetAddress source, ChannelFuture future) { + InetAddress source, ChannelPromise promise) { if (DetectionUtil.javaVersion() >= 7) { if (multicastAddress == null) { throw new NullPointerException("multicastAddress"); @@ -314,55 +315,55 @@ public final class NioDatagramChannel keys.add(key); } - future.setSuccess(); + promise.setSuccess(); } catch (Throwable e) { - future.setFailure(e); + promise.setFailure(e); } } else { throw new UnsupportedOperationException(); } - return future; + return promise; } @Override public ChannelFuture leaveGroup(InetAddress multicastAddress) { - return leaveGroup(multicastAddress, newFuture()); + return leaveGroup(multicastAddress, newPromise()); } @Override - public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelFuture future) { + public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise promise) { try { return leaveGroup( - multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), null, future); + multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), null, promise); } catch (SocketException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override public ChannelFuture leaveGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface) { - return leaveGroup(multicastAddress, networkInterface, newFuture()); + return leaveGroup(multicastAddress, networkInterface, newPromise()); } @Override public ChannelFuture leaveGroup( InetSocketAddress multicastAddress, - NetworkInterface networkInterface, ChannelFuture future) { - return leaveGroup(multicastAddress.getAddress(), networkInterface, null, future); + NetworkInterface networkInterface, ChannelPromise promise) { + return leaveGroup(multicastAddress.getAddress(), networkInterface, null, promise); } @Override public ChannelFuture leaveGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) { - return leaveGroup(multicastAddress, networkInterface, source, newFuture()); + return leaveGroup(multicastAddress, networkInterface, source, newPromise()); } @Override public ChannelFuture leaveGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, - ChannelFuture future) { + ChannelPromise promise) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } @@ -396,8 +397,8 @@ public final class NioDatagramChannel } } - future.setSuccess(); - return future; + promise.setSuccess(); + return promise; } /** @@ -407,7 +408,7 @@ public final class NioDatagramChannel public ChannelFuture block( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress sourceToBlock) { - return block(multicastAddress, networkInterface, sourceToBlock, newFuture()); + return block(multicastAddress, networkInterface, sourceToBlock, newPromise()); } /** @@ -416,7 +417,7 @@ public final class NioDatagramChannel @Override public ChannelFuture block( InetAddress multicastAddress, NetworkInterface networkInterface, - InetAddress sourceToBlock, ChannelFuture future) { + InetAddress sourceToBlock, ChannelPromise promise) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { @@ -438,14 +439,14 @@ public final class NioDatagramChannel try { key.block(sourceToBlock); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); } } } } } - future.setSuccess(); - return future; + promise.setSuccess(); + return promise; } } @@ -455,7 +456,7 @@ public final class NioDatagramChannel */ @Override public ChannelFuture block(InetAddress multicastAddress, InetAddress sourceToBlock) { - return block(multicastAddress, sourceToBlock, newFuture()); + return block(multicastAddress, sourceToBlock, newPromise()); } /** @@ -464,15 +465,15 @@ public final class NioDatagramChannel */ @Override public ChannelFuture block( - InetAddress multicastAddress, InetAddress sourceToBlock, ChannelFuture future) { + InetAddress multicastAddress, InetAddress sourceToBlock, ChannelPromise promise) { try { return block( multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), - sourceToBlock, future); + sourceToBlock, promise); } catch (SocketException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java index f931f5ced0..e8747526fe 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java @@ -27,6 +27,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DefaultSctpChannelConfig; import io.netty.channel.socket.SctpChannelConfig; import io.netty.channel.socket.SctpMessage; @@ -306,51 +307,51 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett @Override public ChannelFuture bindAddress(InetAddress localAddress) { - return bindAddress(localAddress, newFuture()); + return bindAddress(localAddress, newPromise()); } @Override - public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelFuture future) { + public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { try { javaChannel().bindAddress(localAddress); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - bindAddress(localAddress, future); + bindAddress(localAddress, promise); } }); } - return future; + return promise; } @Override public ChannelFuture unbindAddress(InetAddress localAddress) { - return unbindAddress(localAddress, newFuture()); + return unbindAddress(localAddress, newPromise()); } @Override - public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelFuture future) { + public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { try { javaChannel().unbindAddress(localAddress); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - unbindAddress(localAddress, future); + unbindAddress(localAddress, promise); } }); } - return future; + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java index 0fd6ffaa05..4df9553aa1 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java @@ -21,6 +21,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.socket.DefaultSocketChannelConfig; import io.netty.channel.socket.SocketChannelConfig; @@ -125,28 +126,28 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty @Override public ChannelFuture shutdownOutput() { - return shutdownOutput(newFuture()); + return shutdownOutput(newPromise()); } @Override - public ChannelFuture shutdownOutput(final ChannelFuture future) { + public ChannelFuture shutdownOutput(final ChannelPromise promise) { EventLoop loop = eventLoop(); if (loop.inEventLoop()) { try { javaChannel().socket().shutdownOutput(); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { loop.execute(new Runnable() { @Override public void run() { - shutdownOutput(future); + shutdownOutput(promise); } }); } - return future; + return promise; } @Override diff --git a/transport/src/main/java/io/netty/channel/socket/oio/AbstractOioChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/AbstractOioChannel.java index 60c44045cd..2ac1816540 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/AbstractOioChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/AbstractOioChannel.java @@ -17,7 +17,7 @@ package io.netty.channel.socket.oio; import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import java.net.InetSocketAddress; @@ -66,28 +66,28 @@ abstract class AbstractOioChannel extends AbstractChannel { @Override public void connect( final SocketAddress remoteAddress, - final SocketAddress localAddress, final ChannelFuture future) { + final SocketAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { - if (!ensureOpen(future)) { + if (!ensureOpen(promise)) { return; } try { boolean wasActive = isActive(); doConnect(remoteAddress, localAddress); - future.setSuccess(); + promise.setSuccess(); if (!wasActive && isActive()) { pipeline().fireChannelActive(); } } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); closeIfClosed(); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - connect(remoteAddress, localAddress, future); + connect(remoteAddress, localAddress, promise); } }); } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java index f2adcd7f3a..a4740ce40c 100755 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java @@ -22,6 +22,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramChannelConfig; import io.netty.channel.socket.DatagramPacket; @@ -227,38 +228,38 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture joinGroup(InetAddress multicastAddress) { - return joinGroup(multicastAddress, newFuture()); + return joinGroup(multicastAddress, newPromise()); } @Override - public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelFuture future) { + public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { ensureBound(); try { socket.joinGroup(multicastAddress); - future.setSuccess(); + promise.setSuccess(); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override public ChannelFuture joinGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface) { - return joinGroup(multicastAddress, networkInterface, newFuture()); + return joinGroup(multicastAddress, networkInterface, newPromise()); } @Override public ChannelFuture joinGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface, - ChannelFuture future) { + ChannelPromise promise) { ensureBound(); try { socket.joinGroup(multicastAddress, networkInterface); - future.setSuccess(); + promise.setSuccess(); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override @@ -270,9 +271,9 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture joinGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, - ChannelFuture future) { - future.setFailure(new UnsupportedOperationException()); - return future; + ChannelPromise promise) { + promise.setFailure(new UnsupportedOperationException()); + return promise; } private void ensureBound() { @@ -285,37 +286,37 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture leaveGroup(InetAddress multicastAddress) { - return leaveGroup(multicastAddress, newFuture()); + return leaveGroup(multicastAddress, newPromise()); } @Override - public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelFuture future) { + public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise promise) { try { socket.leaveGroup(multicastAddress); - future.setSuccess(); + promise.setSuccess(); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override public ChannelFuture leaveGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface) { - return leaveGroup(multicastAddress, networkInterface, newFuture()); + return leaveGroup(multicastAddress, networkInterface, newPromise()); } @Override public ChannelFuture leaveGroup( InetSocketAddress multicastAddress, NetworkInterface networkInterface, - ChannelFuture future) { + ChannelPromise promise) { try { socket.leaveGroup(multicastAddress, networkInterface); - future.setSuccess(); + promise.setSuccess(); } catch (IOException e) { - future.setFailure(e); + promise.setFailure(e); } - return future; + return promise; } @Override @@ -327,9 +328,9 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture leaveGroup( InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, - ChannelFuture future) { - future.setFailure(new UnsupportedOperationException()); - return future; + ChannelPromise promise) { + promise.setFailure(new UnsupportedOperationException()); + return promise; } @Override @@ -341,9 +342,9 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture block(InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress sourceToBlock, - ChannelFuture future) { - future.setFailure(new UnsupportedOperationException()); - return future; + ChannelPromise promise) { + promise.setFailure(new UnsupportedOperationException()); + return promise; } @Override @@ -354,8 +355,8 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override public ChannelFuture block(InetAddress multicastAddress, - InetAddress sourceToBlock, ChannelFuture future) { - future.setFailure(new UnsupportedOperationException()); - return future; + InetAddress sourceToBlock, ChannelPromise promise) { + promise.setFailure(new UnsupportedOperationException()); + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoop.java b/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoop.java index 2c92447b12..3a7d2d153b 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoop.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoop.java @@ -18,6 +18,7 @@ package io.netty.channel.socket.oio; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelPromise; import io.netty.channel.SingleThreadEventLoop; @@ -37,8 +38,8 @@ class OioEventLoop extends SingleThreadEventLoop { } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { - return super.register(channel, future).addListener(new ChannelFutureListener() { + public ChannelFuture register(Channel channel, ChannelPromise promise) { + return super.register(channel, promise).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoopGroup.java b/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoopGroup.java index df195eb701..163045ed1a 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioEventLoopGroup.java @@ -19,6 +19,7 @@ package io.netty.channel.socket.oio; import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelTaskScheduler; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; @@ -60,7 +61,7 @@ public class OioEventLoopGroup implements EventLoopGroup { * @param maxChannels the maximum number of channels to handle with this instance. Once you try to register * a new {@link Channel} and the maximum is exceed it will throw an * {@link ChannelException} on the {@link #register(Channel)} and - * {@link #register(Channel, ChannelFuture)} method. + * {@link #register(Channel, ChannelPromise)} method. * Use {@code 0} to use no limit */ public OioEventLoopGroup(int maxChannels) { @@ -73,7 +74,7 @@ public class OioEventLoopGroup implements EventLoopGroup { * @param maxChannels the maximum number of channels to handle with this instance. Once you try to register * a new {@link Channel} and the maximum is exceed it will throw an * {@link ChannelException} on the {@link #register(Channel)} and - * {@link #register(Channel, ChannelFuture)} method. + * {@link #register(Channel, ChannelPromise)} method. * Use {@code 0} to use no limit * @param threadFactory the {@link ThreadFactory} used to create new {@link Thread} instances that handle the * registered {@link Channel}s @@ -199,14 +200,16 @@ public class OioEventLoopGroup implements EventLoopGroup { } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { + public ChannelFuture register(Channel channel, ChannelPromise promise) { if (channel == null) { throw new NullPointerException("channel"); } try { - return nextChild().register(channel, future); + return nextChild().register(channel, promise); } catch (Throwable t) { - return channel.newFailedFuture(t); + promise.setFailure(t); + + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java index 45598472d8..809cc7eb17 100755 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java @@ -27,6 +27,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DefaultSctpChannelConfig; import io.netty.channel.socket.SctpChannelConfig; import io.netty.channel.socket.SctpMessage; @@ -345,51 +346,51 @@ public class OioSctpChannel extends AbstractOioMessageChannel @Override public ChannelFuture bindAddress(InetAddress localAddress) { - return bindAddress(localAddress, newFuture()); + return bindAddress(localAddress, newPromise()); } @Override - public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelFuture future) { + public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { try { ch.bindAddress(localAddress); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - bindAddress(localAddress, future); + bindAddress(localAddress, promise); } }); } - return future; + return promise; } @Override public ChannelFuture unbindAddress(InetAddress localAddress) { - return unbindAddress(localAddress, newFuture()); + return unbindAddress(localAddress, newPromise()); } @Override - public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelFuture future) { + public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) { if (eventLoop().inEventLoop()) { try { ch.unbindAddress(localAddress); - future.setSuccess(); + promise.setSuccess(); } catch (Throwable t) { - future.setFailure(t); + promise.setFailure(t); } } else { eventLoop().execute(new Runnable() { @Override public void run() { - unbindAddress(localAddress, future); + unbindAddress(localAddress, promise); } }); } - return future; + return promise; } } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java index afac55de15..af9c8b24a6 100755 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java @@ -21,6 +21,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelMetadata; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.FileRegion; import io.netty.channel.socket.DefaultSocketChannelConfig; @@ -138,11 +139,11 @@ public class OioSocketChannel extends AbstractOioByteChannel @Override public ChannelFuture shutdownOutput() { - return shutdownOutput(newFuture()); + return shutdownOutput(newPromise()); } @Override - public ChannelFuture shutdownOutput(final ChannelFuture future) { + public ChannelFuture shutdownOutput(final ChannelPromise future) { EventLoop loop = eventLoop(); if (loop.inEventLoop()) { try { @@ -239,7 +240,7 @@ public class OioSocketChannel extends AbstractOioByteChannel } @Override - protected void doFlushFileRegion(FileRegion region, ChannelFuture future) throws Exception { + protected void doFlushFileRegion(FileRegion region, ChannelPromise promise) throws Exception { OutputStream os = this.os; if (os == null) { throw new NotYetConnectedException(); @@ -254,12 +255,12 @@ public class OioSocketChannel extends AbstractOioByteChannel if (localWritten == -1) { checkEOF(region, written); region.close(); - future.setSuccess(); + promise.setSuccess(); return; } written += localWritten; if (written >= region.count()) { - future.setSuccess(); + promise.setSuccess(); return; } } diff --git a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java index 577b56aa7e..85c9b90d2c 100644 --- a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java +++ b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java @@ -18,9 +18,7 @@ package io.netty.channel; import static org.easymock.EasyMock.*; import static org.junit.Assert.*; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.junit.Before; import org.junit.Test; @@ -53,8 +51,6 @@ public class CompleteChannelFutureTest { public void testConstantProperties() throws InterruptedException { assertSame(channel, future.channel()); assertTrue(future.isDone()); - assertFalse(future.cancel()); - assertFalse(future.isCancelled()); assertSame(future, future.await()); assertTrue(future.await(1)); assertTrue(future.await(1, TimeUnit.NANOSECONDS)); @@ -88,16 +84,5 @@ public class CompleteChannelFutureTest { public ChannelFuture syncUninterruptibly() { throw new Error(); } - - @Override - public Void get() throws InterruptedException, ExecutionException { - throw new Error(); - } - - @Override - public Void get(long timeout, TimeUnit unit) throws InterruptedException, - ExecutionException, TimeoutException { - throw new Error(); - } } } diff --git a/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java b/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java index 012fd995ef..19df18258b 100644 --- a/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java +++ b/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java @@ -277,7 +277,7 @@ public class SingleThreadEventLoopTest { } @Override - public ChannelFuture register(Channel channel, ChannelFuture future) { + public ChannelFuture register(Channel channel, ChannelPromise future) { // Untested return future; } diff --git a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java index c95a65e1c8..85df30b68a 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java +++ b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java @@ -21,7 +21,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandler; @@ -30,6 +29,7 @@ import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOutboundByteHandler; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultEventExecutorGroup; import io.netty.channel.EventExecutorGroup; import io.netty.channel.EventLoopGroup; @@ -373,7 +373,7 @@ public class LocalTransportThreadModelTest { @Override public void flush(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { ctx.outboundMessageBuffer().clear(); outboundThreadNames.add(Thread.currentThread().getName()); ctx.flush(future); @@ -458,7 +458,7 @@ public class LocalTransportThreadModelTest { @Override public void flush(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { Assert.assertSame(t, Thread.currentThread()); // Don't let the write request go to the server-side channel - just swallow. @@ -554,7 +554,7 @@ public class LocalTransportThreadModelTest { @Override public void flush(final ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { Assert.assertSame(t, Thread.currentThread()); MessageBuf in = ctx.outboundMessageBuffer(); @@ -652,7 +652,7 @@ public class LocalTransportThreadModelTest { @Override public void flush(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { Assert.assertSame(t, Thread.currentThread()); MessageBuf in = ctx.outboundMessageBuffer(); @@ -738,7 +738,7 @@ public class LocalTransportThreadModelTest { @Override public void flush(ChannelHandlerContext ctx, - ChannelFuture future) throws Exception { + ChannelPromise future) throws Exception { Assert.assertSame(t, Thread.currentThread()); MessageBuf in = ctx.outboundMessageBuffer();