2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 03:27:24 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.channel;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-11-07 04:00:19 +01:00
|
|
|
import static java.util.concurrent.TimeUnit.*;
|
2012-05-25 15:16:25 +02:00
|
|
|
import io.netty.channel.AbstractChannel.FlushCheckpoint;
|
2012-05-01 11:31:17 +02:00
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
2008-11-07 04:00:19 +01:00
|
|
|
|
2012-05-01 11:31:17 +02:00
|
|
|
import java.nio.channels.Channels;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.util.ArrayList;
|
2010-02-19 10:58:38 +01:00
|
|
|
import java.util.Collection;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2008-08-11 09:33:19 +02:00
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* The default {@link ChannelFuture} implementation. It is recommended to
|
|
|
|
* use {@link Channels#future(Channel)} and {@link Channels#future(Channel, boolean)}
|
|
|
|
* to create a new {@link ChannelFuture} rather than calling the constructor
|
|
|
|
* explicitly.
|
2008-08-11 09:33:19 +02:00
|
|
|
*/
|
2012-05-25 15:16:25 +02:00
|
|
|
public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFuture {
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-08-09 16:52:19 +02:00
|
|
|
private static final InternalLogger logger =
|
2008-08-09 17:05:53 +02:00
|
|
|
InternalLoggerFactory.getInstance(DefaultChannelFuture.class);
|
2008-08-11 09:33:19 +02:00
|
|
|
|
2012-05-28 14:05:49 +02:00
|
|
|
private static final int MAX_LISTENER_STACK_DEPTH = 8;
|
|
|
|
private static final ThreadLocal<Integer> LISTENER_STACK_DEPTH = new ThreadLocal<Integer>() {
|
|
|
|
@Override
|
|
|
|
protected Integer initialValue() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
private static final Throwable CANCELLED = new Throwable();
|
|
|
|
|
|
|
|
private final Channel channel;
|
|
|
|
private final boolean cancellable;
|
|
|
|
|
2010-02-19 09:23:48 +01:00
|
|
|
private ChannelFutureListener firstListener;
|
|
|
|
private List<ChannelFutureListener> otherListeners;
|
2010-02-23 06:45:53 +01:00
|
|
|
private List<ChannelFutureProgressListener> progressListeners;
|
2008-08-08 02:37:18 +02:00
|
|
|
private boolean done;
|
|
|
|
private Throwable cause;
|
|
|
|
private int waiters;
|
|
|
|
|
2012-05-25 15:16:25 +02:00
|
|
|
/**
|
|
|
|
* Opportunistically extending FlushCheckpoint to reduce GC.
|
|
|
|
* Only used for flush() operation. See AbstractChannel.DefaultUnsafe.flush() */
|
|
|
|
private long flushCheckpoint;
|
|
|
|
|
2008-08-11 09:33:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
* the {@link Channel} associated with this future
|
|
|
|
* @param cancellable
|
|
|
|
* {@code true} if and only if this future can be canceled
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public DefaultChannelFuture(Channel channel, boolean cancellable) {
|
|
|
|
this.channel = channel;
|
|
|
|
this.cancellable = cancellable;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-05-01 10:19:41 +02:00
|
|
|
public Channel channel() {
|
2008-08-08 02:37:18 +02:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public synchronized boolean isDone() {
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public synchronized boolean isSuccess() {
|
2009-02-26 10:28:37 +01:00
|
|
|
return done && cause == null;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-05-01 10:19:41 +02:00
|
|
|
public synchronized Throwable cause() {
|
2008-08-08 02:37:18 +02:00
|
|
|
if (cause != CANCELLED) {
|
|
|
|
return cause;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public synchronized boolean isCancelled() {
|
|
|
|
return cause == CANCELLED;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-05-14 16:57:23 +02:00
|
|
|
public ChannelFuture addListener(final ChannelFutureListener listener) {
|
2008-08-08 02:37:18 +02:00
|
|
|
if (listener == null) {
|
|
|
|
throw new NullPointerException("listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean notifyNow = false;
|
|
|
|
synchronized (this) {
|
|
|
|
if (done) {
|
|
|
|
notifyNow = true;
|
|
|
|
} else {
|
|
|
|
if (firstListener == null) {
|
|
|
|
firstListener = listener;
|
|
|
|
} else {
|
|
|
|
if (otherListeners == null) {
|
|
|
|
otherListeners = new ArrayList<ChannelFutureListener>(1);
|
|
|
|
}
|
|
|
|
otherListeners.add(listener);
|
|
|
|
}
|
2010-02-19 10:58:38 +01:00
|
|
|
|
|
|
|
if (listener instanceof ChannelFutureProgressListener) {
|
|
|
|
if (progressListeners == null) {
|
|
|
|
progressListeners = new ArrayList<ChannelFutureProgressListener>(1);
|
|
|
|
}
|
|
|
|
progressListeners.add((ChannelFutureProgressListener) listener);
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notifyNow) {
|
2012-05-28 14:05:49 +02:00
|
|
|
notifyListener(this, listener);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
return this;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-05-14 16:57:23 +02:00
|
|
|
public ChannelFuture removeListener(ChannelFutureListener listener) {
|
2008-08-08 02:37:18 +02:00
|
|
|
if (listener == null) {
|
|
|
|
throw new NullPointerException("listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
if (!done) {
|
|
|
|
if (listener == firstListener) {
|
|
|
|
if (otherListeners != null && !otherListeners.isEmpty()) {
|
|
|
|
firstListener = otherListeners.remove(0);
|
|
|
|
} else {
|
|
|
|
firstListener = null;
|
|
|
|
}
|
|
|
|
} else if (otherListeners != null) {
|
|
|
|
otherListeners.remove(listener);
|
|
|
|
}
|
2010-02-19 10:58:38 +01:00
|
|
|
|
|
|
|
if (listener instanceof ChannelFutureProgressListener) {
|
|
|
|
progressListeners.remove(listener);
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
return this;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2012-01-19 05:33:37 +01:00
|
|
|
@Override
|
2012-05-14 16:57:23 +02:00
|
|
|
public ChannelFuture sync() throws InterruptedException {
|
|
|
|
await();
|
|
|
|
rethrowIfFailed();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture syncUninterruptibly() {
|
|
|
|
awaitUninterruptibly();
|
|
|
|
rethrowIfFailed();
|
|
|
|
return this;
|
|
|
|
}
|
2012-05-01 11:31:17 +02:00
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
private void rethrowIfFailed() {
|
2012-05-01 10:19:41 +02:00
|
|
|
Throwable cause = cause();
|
2012-01-19 05:33:37 +01:00
|
|
|
if (cause == null) {
|
2012-05-14 16:57:23 +02:00
|
|
|
return;
|
2012-01-19 05:33:37 +01:00
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
if (cause instanceof RuntimeException) {
|
|
|
|
throw (RuntimeException) cause;
|
2012-01-19 05:33:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cause instanceof Error) {
|
|
|
|
throw (Error) cause;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
throw new ChannelException(cause);
|
2012-01-19 05:33:37 +01:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelFuture await() throws InterruptedException {
|
2009-09-04 06:21:56 +02:00
|
|
|
if (Thread.interrupted()) {
|
|
|
|
throw new InterruptedException();
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
while (!done) {
|
2009-04-06 09:09:11 +02:00
|
|
|
checkDeadLock();
|
2008-08-08 02:37:18 +02:00
|
|
|
waiters++;
|
|
|
|
try {
|
2008-08-19 12:21:04 +02:00
|
|
|
this.wait();
|
2008-08-08 02:37:18 +02:00
|
|
|
} finally {
|
|
|
|
waiters--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public boolean await(long timeout, TimeUnit unit)
|
|
|
|
throws InterruptedException {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(unit.toNanos(timeout), true);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public boolean await(long timeoutMillis) throws InterruptedException {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(MILLISECONDS.toNanos(timeoutMillis), true);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelFuture awaitUninterruptibly() {
|
2009-09-04 06:21:56 +02:00
|
|
|
boolean interrupted = false;
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
while (!done) {
|
2009-04-06 09:09:11 +02:00
|
|
|
checkDeadLock();
|
2008-08-08 02:37:18 +02:00
|
|
|
waiters++;
|
|
|
|
try {
|
2008-08-19 12:21:04 +02:00
|
|
|
this.wait();
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (InterruptedException e) {
|
2009-09-04 06:21:56 +02:00
|
|
|
interrupted = true;
|
2008-08-08 02:37:18 +02:00
|
|
|
} finally {
|
|
|
|
waiters--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 06:21:56 +02:00
|
|
|
if (interrupted) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
|
2008-11-07 04:00:19 +01:00
|
|
|
try {
|
|
|
|
return await0(unit.toNanos(timeout), false);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public boolean awaitUninterruptibly(long timeoutMillis) {
|
|
|
|
try {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(MILLISECONDS.toNanos(timeoutMillis), false);
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 04:00:19 +01:00
|
|
|
private boolean await0(long timeoutNanos, boolean interruptable) throws InterruptedException {
|
2009-09-04 06:21:56 +02:00
|
|
|
if (interruptable && Thread.interrupted()) {
|
|
|
|
throw new InterruptedException();
|
|
|
|
}
|
|
|
|
|
2008-11-07 04:00:19 +01:00
|
|
|
long startTime = timeoutNanos <= 0 ? 0 : System.nanoTime();
|
|
|
|
long waitTime = timeoutNanos;
|
2009-09-04 06:21:56 +02:00
|
|
|
boolean interrupted = false;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2009-09-04 06:21:56 +02:00
|
|
|
try {
|
|
|
|
synchronized (this) {
|
|
|
|
if (done) {
|
|
|
|
return done;
|
|
|
|
} else if (waitTime <= 0) {
|
|
|
|
return done;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2009-09-04 06:21:56 +02:00
|
|
|
checkDeadLock();
|
|
|
|
waiters++;
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
this.wait(waitTime / 1000000, (int) (waitTime % 1000000));
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
if (interruptable) {
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
interrupted = true;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2009-09-04 06:21:56 +02:00
|
|
|
if (done) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
waitTime = timeoutNanos - (System.nanoTime() - startTime);
|
|
|
|
if (waitTime <= 0) {
|
|
|
|
return done;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-04 06:21:56 +02:00
|
|
|
} finally {
|
|
|
|
waiters--;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
2009-09-04 06:21:56 +02:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (interrupted) {
|
|
|
|
Thread.currentThread().interrupt();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 01:47:00 +02:00
|
|
|
private void checkDeadLock() {
|
2012-06-01 01:59:54 +02:00
|
|
|
if (channel().isRegistered() && channel().eventLoop().inEventLoop()) {
|
2012-06-01 01:47:00 +02:00
|
|
|
throw new BlockingOperationException();
|
2009-04-06 09:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-11-26 10:21:00 +01:00
|
|
|
public boolean setSuccess() {
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
2008-11-26 10:21:00 +01:00
|
|
|
return false;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
2008-11-26 10:21:00 +01:00
|
|
|
return true;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-11-26 10:21:00 +01:00
|
|
|
public boolean setFailure(Throwable cause) {
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
2008-11-26 10:21:00 +01:00
|
|
|
return false;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.cause = cause;
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
2008-11-26 10:21:00 +01:00
|
|
|
return true;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public boolean cancel() {
|
|
|
|
if (!cancellable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cause = CANCELLED;
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyListeners() {
|
2010-02-19 09:23:48 +01:00
|
|
|
// This method doesn't need synchronization because:
|
|
|
|
// 1) This method is always called after synchronized (this) block.
|
|
|
|
// Hence any listener list modification happens-before this method.
|
2010-02-19 10:18:10 +01:00
|
|
|
// 2) This method is called only when 'done' is true. Once 'done'
|
|
|
|
// becomes true, the listener list is never modified - see add/removeListener()
|
2012-05-01 11:31:17 +02:00
|
|
|
|
|
|
|
if (firstListener == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel().eventLoop().inEventLoop()) {
|
2012-05-28 14:05:49 +02:00
|
|
|
notifyListener0(this, firstListener);
|
2008-08-08 02:37:18 +02:00
|
|
|
firstListener = null;
|
|
|
|
|
|
|
|
if (otherListeners != null) {
|
|
|
|
for (ChannelFutureListener l: otherListeners) {
|
2012-05-28 14:05:49 +02:00
|
|
|
notifyListener0(this, l);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
otherListeners = null;
|
|
|
|
}
|
2012-05-01 11:31:17 +02:00
|
|
|
} else {
|
|
|
|
final ChannelFutureListener firstListener = this.firstListener;
|
|
|
|
final List<ChannelFutureListener> otherListeners = this.otherListeners;
|
|
|
|
this.firstListener = null;
|
|
|
|
this.otherListeners = null;
|
|
|
|
channel().eventLoop().execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-05-28 14:05:49 +02:00
|
|
|
notifyListener0(DefaultChannelFuture.this, firstListener);
|
2012-05-01 11:31:17 +02:00
|
|
|
if (otherListeners != null) {
|
|
|
|
for (ChannelFutureListener l: otherListeners) {
|
2012-05-28 14:05:49 +02:00
|
|
|
notifyListener0(DefaultChannelFuture.this, l);
|
2012-05-01 11:31:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:05:49 +02:00
|
|
|
static void notifyListener(final ChannelFuture f, final ChannelFutureListener l) {
|
|
|
|
EventLoop loop = f.channel().eventLoop();
|
|
|
|
if (loop.inEventLoop()) {
|
|
|
|
final Integer stackDepth = LISTENER_STACK_DEPTH.get();
|
|
|
|
if (stackDepth < MAX_LISTENER_STACK_DEPTH) {
|
|
|
|
LISTENER_STACK_DEPTH.set(stackDepth + 1);
|
|
|
|
try {
|
|
|
|
notifyListener0(f, l);
|
|
|
|
} finally {
|
|
|
|
LISTENER_STACK_DEPTH.set(stackDepth);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
notifyListener(f, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void notifyListener0(ChannelFuture f, ChannelFutureListener l) {
|
2008-08-08 02:37:18 +02:00
|
|
|
try {
|
2012-05-28 14:05:49 +02:00
|
|
|
l.operationComplete(f);
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (Throwable t) {
|
2012-02-17 10:37:41 +01:00
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by " +
|
|
|
|
ChannelFutureListener.class.getSimpleName() + ".", t);
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-19 10:58:38 +01:00
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-02-19 10:58:38 +01:00
|
|
|
public boolean setProgress(long amount, long current, long total) {
|
|
|
|
ChannelFutureProgressListener[] plisteners;
|
|
|
|
synchronized (this) {
|
|
|
|
// Do not generate progress event after completion.
|
|
|
|
if (done) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-23 02:12:36 +01:00
|
|
|
Collection<ChannelFutureProgressListener> progressListeners =
|
|
|
|
this.progressListeners;
|
|
|
|
if (progressListeners == null || progressListeners.isEmpty()) {
|
2010-02-19 10:58:38 +01:00
|
|
|
// Nothing to notify - no need to create an empty array.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
plisteners = progressListeners.toArray(
|
|
|
|
new ChannelFutureProgressListener[progressListeners.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ChannelFutureProgressListener pl: plisteners) {
|
|
|
|
notifyProgressListener(pl, amount, current, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyProgressListener(
|
|
|
|
ChannelFutureProgressListener l,
|
|
|
|
long amount, long current, long total) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
l.operationProgressed(this, amount, current, total);
|
|
|
|
} catch (Throwable t) {
|
2012-02-17 10:37:41 +01:00
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by " +
|
|
|
|
ChannelFutureProgressListener.class.getSimpleName() + ".", t);
|
|
|
|
}
|
2010-02-19 10:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 15:16:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
long flushCheckpoint() {
|
|
|
|
return flushCheckpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void flushCheckpoint(long checkpoint) {
|
|
|
|
flushCheckpoint = checkpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
ChannelFuture future() {
|
|
|
|
return this;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|