2008-08-08 00:37:18 +00:00
|
|
|
/*
|
2011-12-09 14:18:34 +09:00
|
|
|
* Copyright 2011 The Netty Project
|
2008-08-08 00:37:18 +00:00
|
|
|
*
|
2011-12-09 14:18:34 +09: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 00:37:18 +00:00
|
|
|
*
|
2011-12-09 14:18:34 +09:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 01:27:24 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00: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 14:18:34 +09:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 07:15:49 +00:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 00:37:18 +00:00
|
|
|
*/
|
2011-12-09 12:38:59 +09:00
|
|
|
package io.netty.channel;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
import io.netty.buffer.ChannelBuffer;
|
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
|
|
|
|
|
|
|
import java.net.SocketAddress;
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.ArrayList;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.HashMap;
|
2012-06-01 17:51:19 -07:00
|
|
|
import java.util.IdentityHashMap;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.List;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.NoSuchElementException;
|
2012-05-29 12:09:29 -07:00
|
|
|
import java.util.Queue;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2008-09-02 07:13:20 +00:00
|
|
|
/**
|
2012-05-01 17:19:41 +09:00
|
|
|
* The default {@link ChannelPipeline} implementation. It is usually created
|
|
|
|
* by a {@link Channel} implementation when the {@link Channel} is created.
|
2008-09-02 07:13:20 +00:00
|
|
|
*/
|
2008-08-08 00:37:18 +00:00
|
|
|
public class DefaultChannelPipeline implements ChannelPipeline {
|
|
|
|
|
2008-08-09 15:05:53 +00:00
|
|
|
static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelPipeline.class);
|
2009-03-11 10:45:55 +00:00
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
final Channel channel;
|
2012-06-01 18:34:19 -07:00
|
|
|
private final Channel.Unsafe unsafe;
|
|
|
|
private final ChannelBufferHolder<Object> directOutbound;
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
private volatile DefaultChannelHandlerContext head;
|
|
|
|
private volatile DefaultChannelHandlerContext tail;
|
|
|
|
private final Map<String, DefaultChannelHandlerContext> name2ctx =
|
|
|
|
new HashMap<String, DefaultChannelHandlerContext>(4);
|
2012-05-30 11:32:39 -07:00
|
|
|
private boolean firedChannelActive;
|
|
|
|
private boolean fireInboundBufferUpdatedOnActivation;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
final Map<EventExecutor, EventExecutor> childExecutors =
|
|
|
|
new IdentityHashMap<EventExecutor, EventExecutor>();
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
public DefaultChannelPipeline(Channel channel) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
}
|
|
|
|
this.channel = channel;
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe = channel.unsafe();
|
|
|
|
directOutbound = unsafe.directOutbound();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public Channel channel() {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addFirst(String name, ChannelHandler handler) {
|
|
|
|
return addFirst(null, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized ChannelPipeline addFirst(EventExecutor executor, String name, ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name2ctx.isEmpty()) {
|
2012-06-01 17:51:19 -07:00
|
|
|
init(executor, name, handler);
|
2008-08-08 00:37:18 +00:00
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext oldHead = head;
|
2012-06-01 17:51:19 -07:00
|
|
|
DefaultChannelHandlerContext newHead =
|
|
|
|
new DefaultChannelHandlerContext(this, executor, null, oldHead, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newHead);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
oldHead.prev = newHead;
|
|
|
|
head = newHead;
|
|
|
|
name2ctx.put(name, newHead);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newHead);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-15 14:08:42 +09:00
|
|
|
|
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addLast(String name, ChannelHandler handler) {
|
|
|
|
return addLast(null, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized ChannelPipeline addLast(EventExecutor executor, String name, ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name2ctx.isEmpty()) {
|
2012-06-01 17:51:19 -07:00
|
|
|
init(executor, name, handler);
|
2008-08-08 00:37:18 +00:00
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext oldTail = tail;
|
2012-06-01 17:51:19 -07:00
|
|
|
DefaultChannelHandlerContext newTail =
|
|
|
|
new DefaultChannelHandlerContext(this, executor, oldTail, null, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newTail);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
oldTail.next = newTail;
|
|
|
|
tail = newTail;
|
|
|
|
name2ctx.put(name, newTail);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newTail);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-15 14:08:42 +09:00
|
|
|
|
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler) {
|
|
|
|
return addBefore(null, baseName, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized ChannelPipeline addBefore(EventExecutor executor, String baseName, String name, ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
|
|
|
if (ctx == head) {
|
|
|
|
addFirst(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
2012-06-01 17:51:19 -07:00
|
|
|
DefaultChannelHandlerContext newCtx =
|
|
|
|
new DefaultChannelHandlerContext(this, executor, ctx.prev, ctx, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
ctx.prev.next = newCtx;
|
|
|
|
ctx.prev = newCtx;
|
|
|
|
name2ctx.put(name, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-15 14:08:42 +09:00
|
|
|
|
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) {
|
|
|
|
return addAfter(null, baseName, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized ChannelPipeline addAfter(EventExecutor executor, String baseName, String name, ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
|
|
|
if (ctx == tail) {
|
|
|
|
addLast(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
2012-06-01 17:51:19 -07:00
|
|
|
DefaultChannelHandlerContext newCtx =
|
|
|
|
new DefaultChannelHandlerContext(this, executor, ctx, ctx.next, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
ctx.next.prev = newCtx;
|
|
|
|
ctx.next = newCtx;
|
|
|
|
name2ctx.put(name, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-15 14:08:42 +09:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline addFirst(ChannelHandler... handlers) {
|
2012-06-01 17:51:19 -07:00
|
|
|
return addFirst(null, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline addFirst(EventExecutor executor, ChannelHandler... handlers) {
|
2012-05-15 14:08:42 +09:00
|
|
|
if (handlers == null) {
|
|
|
|
throw new NullPointerException("handlers");
|
|
|
|
}
|
2012-05-15 14:49:23 +09:00
|
|
|
if (handlers.length == 0 || handlers[0] == null) {
|
2012-05-15 14:08:42 +09:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size;
|
|
|
|
for (size = 1; size < handlers.length; size ++) {
|
|
|
|
if (handlers[size] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = size - 1; i >= 0; i --) {
|
|
|
|
ChannelHandler h = handlers[i];
|
2012-06-01 17:51:19 -07:00
|
|
|
addFirst(executor, generateName(h), h);
|
2012-05-15 14:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline addLast(ChannelHandler... handlers) {
|
2012-06-01 17:51:19 -07:00
|
|
|
return addLast(null, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline addLast(EventExecutor executor, ChannelHandler... handlers) {
|
2012-05-15 14:08:42 +09:00
|
|
|
if (handlers == null) {
|
|
|
|
throw new NullPointerException("handlers");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ChannelHandler h: handlers) {
|
|
|
|
if (h == null) {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
addLast(executor, generateName(h), h);
|
2012-05-15 14:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:41:26 -07:00
|
|
|
private static String generateName(ChannelHandler handler) {
|
2012-05-15 14:08:42 +09:00
|
|
|
String type = handler.getClass().getSimpleName();
|
|
|
|
StringBuilder buf = new StringBuilder(type.length() + 10);
|
|
|
|
buf.append(type);
|
|
|
|
buf.append("-0");
|
|
|
|
buf.append(Long.toHexString(System.identityHashCode(handler) & 0xFFFFFFFFL | 0x100000000L));
|
|
|
|
buf.setCharAt(buf.length() - 9, 'x');
|
|
|
|
return buf.toString();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void remove(ChannelHandler handler) {
|
|
|
|
remove(getContextOrDie(handler));
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler remove(String name) {
|
2012-05-01 17:19:41 +09:00
|
|
|
return remove(getContextOrDie(name)).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized <T extends ChannelHandler> T remove(Class<T> handlerType) {
|
2012-05-01 17:19:41 +09:00
|
|
|
return (T) remove(getContextOrDie(handlerType)).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext remove(DefaultChannelHandlerContext ctx) {
|
|
|
|
if (head == tail) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else if (ctx == head) {
|
|
|
|
removeFirst();
|
|
|
|
} else if (ctx == tail) {
|
|
|
|
removeLast();
|
|
|
|
} else {
|
2008-12-01 10:07:54 +00:00
|
|
|
callBeforeRemove(ctx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
|
|
|
prev.next = next;
|
|
|
|
next.prev = prev;
|
2012-05-01 17:19:41 +09:00
|
|
|
name2ctx.remove(ctx.name());
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterRemove(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler removeFirst() {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext oldHead = head;
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldHead == null) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldHead);
|
|
|
|
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldHead.next == null) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else {
|
|
|
|
oldHead.next.prev = null;
|
|
|
|
head = oldHead.next;
|
2012-05-01 17:19:41 +09:00
|
|
|
name2ctx.remove(oldHead.name());
|
2008-08-18 02:38:54 +00:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterRemove(oldHead);
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
return oldHead.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler removeLast() {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext oldTail = tail;
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldTail == null) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldTail);
|
|
|
|
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldTail.prev == null) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else {
|
|
|
|
oldTail.prev.next = null;
|
|
|
|
tail = oldTail.prev;
|
2012-05-01 17:19:41 +09:00
|
|
|
name2ctx.remove(oldTail.name());
|
2008-08-18 02:38:54 +00:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldTail);
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
return oldTail.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler) {
|
|
|
|
replace(getContextOrDie(oldHandler), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler) {
|
|
|
|
return replace(getContextOrDie(oldName), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public synchronized <T extends ChannelHandler> T replace(
|
|
|
|
Class<T> oldHandlerType, String newName, ChannelHandler newHandler) {
|
|
|
|
return (T) replace(getContextOrDie(oldHandlerType), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ChannelHandler replace(DefaultChannelHandlerContext ctx, String newName, ChannelHandler newHandler) {
|
|
|
|
if (ctx == head) {
|
|
|
|
removeFirst();
|
|
|
|
addFirst(newName, newHandler);
|
|
|
|
} else if (ctx == tail) {
|
|
|
|
removeLast();
|
|
|
|
addLast(newName, newHandler);
|
|
|
|
} else {
|
2012-05-01 17:19:41 +09:00
|
|
|
boolean sameName = ctx.name().equals(newName);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (!sameName) {
|
|
|
|
checkDuplicateName(newName);
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
2012-06-01 17:51:19 -07:00
|
|
|
DefaultChannelHandlerContext newCtx =
|
|
|
|
new DefaultChannelHandlerContext(this, ctx.executor, prev, next, newName, newHandler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(ctx);
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
prev.next = newCtx;
|
|
|
|
next.prev = newCtx;
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
if (!sameName) {
|
2012-05-01 17:19:41 +09:00
|
|
|
name2ctx.remove(ctx.name());
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2011-12-11 09:21:29 +01:00
|
|
|
name2ctx.put(newName, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
ChannelHandlerLifeCycleException removeException = null;
|
|
|
|
ChannelHandlerLifeCycleException addException = null;
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
|
|
|
callAfterRemove(ctx);
|
|
|
|
removed = true;
|
|
|
|
} catch (ChannelHandlerLifeCycleException e) {
|
|
|
|
removeException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean added = false;
|
|
|
|
try {
|
|
|
|
callAfterAdd(newCtx);
|
|
|
|
added = true;
|
|
|
|
} catch (ChannelHandlerLifeCycleException e) {
|
|
|
|
addException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!removed && !added) {
|
|
|
|
logger.warn(removeException.getMessage(), removeException);
|
|
|
|
logger.warn(addException.getMessage(), addException);
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-01 17:19:41 +09:00
|
|
|
"Both " + ctx.handler().getClass().getName() +
|
|
|
|
".afterRemove() and " + newCtx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() failed; see logs.");
|
|
|
|
} else if (!removed) {
|
|
|
|
throw removeException;
|
|
|
|
} else if (!added) {
|
|
|
|
throw addException;
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
return ctx.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private static void callBeforeAdd(ChannelHandlerContext ctx) {
|
2012-05-31 14:54:48 -07:00
|
|
|
ChannelHandler handler = ctx.handler();
|
|
|
|
if (handler instanceof AbstractChannelHandler) {
|
|
|
|
AbstractChannelHandler h = (AbstractChannelHandler) handler;
|
|
|
|
if (!h.isSharable() && h.added) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
|
|
|
"Only a @Sharable handler can be added or removed multiple times.");
|
|
|
|
}
|
|
|
|
h.added = true;
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2012-05-31 14:54:48 -07:00
|
|
|
handler.beforeAdd(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-31 14:54:48 -07:00
|
|
|
handler.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeAdd() has thrown an exception; not adding.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callAfterAdd(ChannelHandlerContext ctx) {
|
|
|
|
try {
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().afterAdd(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
|
|
|
remove((DefaultChannelHandlerContext) ctx);
|
|
|
|
removed = true;
|
|
|
|
} catch (Throwable t2) {
|
2012-02-17 10:37:41 +01:00
|
|
|
if (logger.isWarnEnabled()) {
|
2012-05-01 17:19:41 +09:00
|
|
|
logger.warn("Failed to remove a handler: " + ctx.name(), t2);
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; removed.", t);
|
|
|
|
} else {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; also failed to remove.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private static void callBeforeRemove(ChannelHandlerContext ctx) {
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().beforeRemove(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeRemove() has thrown an exception; not removing.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private static void callAfterRemove(ChannelHandlerContext ctx) {
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().afterRemove(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterRemove() has thrown an exception.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public synchronized ChannelHandler first() {
|
2008-08-18 02:27:11 +00:00
|
|
|
DefaultChannelHandlerContext head = this.head;
|
|
|
|
if (head == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
return head.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public synchronized ChannelHandler last() {
|
2008-08-18 02:27:11 +00:00
|
|
|
DefaultChannelHandlerContext tail = this.tail;
|
|
|
|
if (tail == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
return tail.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler get(String name) {
|
|
|
|
DefaultChannelHandlerContext ctx = name2ctx.get(name);
|
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2012-05-01 17:19:41 +09:00
|
|
|
return ctx.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized <T extends ChannelHandler> T get(Class<T> handlerType) {
|
2012-05-01 17:19:41 +09:00
|
|
|
ChannelHandlerContext ctx = context(handlerType);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2012-05-01 17:19:41 +09:00
|
|
|
return (T) ctx.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public synchronized ChannelHandlerContext context(String name) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name == null) {
|
|
|
|
throw new NullPointerException("name");
|
|
|
|
}
|
|
|
|
return name2ctx.get(name);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public synchronized ChannelHandlerContext context(ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (handler == null) {
|
|
|
|
throw new NullPointerException("handler");
|
|
|
|
}
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx.handler() == handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public synchronized ChannelHandlerContext context(
|
2008-08-08 00:37:18 +00:00
|
|
|
Class<? extends ChannelHandler> handlerType) {
|
2009-12-17 10:57:57 +00:00
|
|
|
if (handlerType == null) {
|
|
|
|
throw new NullPointerException("handlerType");
|
|
|
|
}
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (handlerType.isAssignableFrom(ctx.handler().getClass())) {
|
2008-08-08 00:37:18 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:43:10 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public List<String> names() {
|
2011-08-02 08:43:10 +09:00
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
2012-05-01 17:19:41 +09:00
|
|
|
list.add(ctx.name());
|
2011-08-02 08:43:10 +09:00
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public Map<String, ChannelHandler> toMap() {
|
|
|
|
Map<String, ChannelHandler> map = new LinkedHashMap<String, ChannelHandler>();
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
2012-05-01 17:19:41 +09:00
|
|
|
map.put(ctx.name(), ctx.handler());
|
2008-08-08 00:37:18 +00:00
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the {@link String} representation of this pipeline.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
buf.append(getClass().getSimpleName());
|
|
|
|
buf.append('{');
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
buf.append('(');
|
2012-05-01 17:19:41 +09:00
|
|
|
buf.append(ctx.name());
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append(" = ");
|
2012-05-01 17:19:41 +09:00
|
|
|
buf.append(ctx.handler().getClass().getName());
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append(')');
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
|
|
|
buf.append('}');
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public Queue<Object> inboundMessageBuffer() {
|
2012-06-02 01:58:15 -07:00
|
|
|
if (channel.type() != ChannelType.MESSAGE) {
|
|
|
|
throw new NoSuchBufferException(
|
|
|
|
"The first inbound buffer of this channel must be a message buffer.");
|
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
return nextInboundMessageBuffer(head);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelBuffer inboundByteBuffer() {
|
2012-06-02 01:58:15 -07:00
|
|
|
if (channel.type() != ChannelType.STREAM) {
|
|
|
|
throw new NoSuchBufferException(
|
|
|
|
"The first inbound buffer of this channel must be a byte buffer.");
|
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
return nextInboundByteBuffer(head);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Queue<Object> outboundMessageBuffer() {
|
|
|
|
return nextOutboundMessageBuffer(tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelBuffer outboundByteBuffer() {
|
|
|
|
return nextOutboundByteBuffer(tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChannelBuffer nextInboundByteBuffer(DefaultChannelHandlerContext ctx) {
|
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
2012-06-03 04:10:32 -07:00
|
|
|
throw new NoSuchBufferException();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
ChannelBufferHolder<Object> in = ctx.in;
|
|
|
|
if (in != null && !in.isBypass() && in.hasByteBuffer()) {
|
|
|
|
return in.byteBuffer();
|
|
|
|
}
|
|
|
|
ctx = ctx.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Queue<Object> nextInboundMessageBuffer(DefaultChannelHandlerContext ctx) {
|
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
2012-06-03 04:10:32 -07:00
|
|
|
throw new NoSuchBufferException();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
ChannelBufferHolder<Object> in = ctx.inbound();
|
|
|
|
if (in != null && !in.isBypass() && in.hasMessageBuffer()) {
|
|
|
|
return in.messageBuffer();
|
|
|
|
}
|
|
|
|
ctx = ctx.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelBuffer nextOutboundByteBuffer(DefaultChannelHandlerContext ctx) {
|
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
2012-06-01 18:34:19 -07:00
|
|
|
if (directOutbound.hasByteBuffer()) {
|
|
|
|
return directOutbound.byteBuffer();
|
2012-06-01 17:51:19 -07:00
|
|
|
} else {
|
2012-06-03 04:10:32 -07:00
|
|
|
throw new NoSuchBufferException();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelBufferHolder<Object> out = ctx.outbound();
|
|
|
|
if (out != null && !out.isBypass() && out.hasByteBuffer()) {
|
|
|
|
return out.byteBuffer();
|
|
|
|
}
|
|
|
|
ctx = ctx.prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Queue<Object> nextOutboundMessageBuffer(DefaultChannelHandlerContext ctx) {
|
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
2012-06-01 18:34:19 -07:00
|
|
|
if (directOutbound.hasMessageBuffer()) {
|
|
|
|
return directOutbound.messageBuffer();
|
2012-06-01 17:51:19 -07:00
|
|
|
} else {
|
2012-06-03 04:10:32 -07:00
|
|
|
throw new NoSuchBufferException();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelBufferHolder<Object> out = ctx.outbound();
|
|
|
|
if (out != null && !out.isBypass() && out.hasMessageBuffer()) {
|
|
|
|
return out.messageBuffer();
|
|
|
|
}
|
|
|
|
ctx = ctx.prev;
|
2012-05-16 23:02:06 +09:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelRegistered() {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
|
|
|
fireChannelRegistered(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void fireChannelRegistered(DefaultChannelHandlerContext ctx) {
|
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
ctx.fireChannelRegisteredTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(ctx.fireChannelRegisteredTask);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelUnregistered() {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
|
|
|
fireChannelUnregistered(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void fireChannelUnregistered(DefaultChannelHandlerContext ctx) {
|
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
ctx.fireChannelUnregisteredTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(ctx.fireChannelUnregisteredTask);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelActive() {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
2012-05-30 11:32:39 -07:00
|
|
|
firedChannelActive = true;
|
2012-05-01 17:19:41 +09:00
|
|
|
fireChannelActive(ctx);
|
2012-05-30 11:32:39 -07:00
|
|
|
if (fireInboundBufferUpdatedOnActivation) {
|
|
|
|
fireInboundBufferUpdatedOnActivation = false;
|
|
|
|
fireInboundBufferUpdated(ctx);
|
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void fireChannelActive(DefaultChannelHandlerContext ctx) {
|
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
ctx.fireChannelActiveTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(ctx.fireChannelActiveTask);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
|
|
|
public void fireChannelInactive() {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
2012-05-31 01:53:58 -07:00
|
|
|
// Some implementations such as EmbeddedChannel can trigger inboundBufferUpdated()
|
|
|
|
// after deactivation, so it's safe not to revert the firedChannelActive flag here.
|
|
|
|
// Also, all known transports never get re-activated.
|
|
|
|
//firedChannelActive = false;
|
2012-05-01 17:19:41 +09:00
|
|
|
fireChannelInactive(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void fireChannelInactive(DefaultChannelHandlerContext ctx) {
|
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
ctx.fireChannelInactiveTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(ctx.fireChannelInactiveTask);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireExceptionCaught(Throwable cause) {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
|
|
|
fireExceptionCaught(ctx, cause);
|
2012-05-30 11:48:04 -07:00
|
|
|
} else {
|
|
|
|
logTerminalException(cause);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void logTerminalException(Throwable cause) {
|
2012-05-30 11:48:04 -07:00
|
|
|
logger.warn(
|
|
|
|
"An exceptionCaught() event was fired, and it reached at the end of the " +
|
|
|
|
"pipeline. It usually means the last inbound handler in the pipeline did not " +
|
|
|
|
"handle the exception.", cause);
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
void fireExceptionCaught(final DefaultChannelHandlerContext ctx, final Throwable cause) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (cause == null) {
|
|
|
|
throw new NullPointerException("cause");
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelInboundHandler<Object>) ctx.handler()).exceptionCaught(ctx, cause);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by a user handler's " +
|
|
|
|
"exceptionCaught() method while handling the following exception:", cause);
|
|
|
|
}
|
|
|
|
notifyHandlerException(t);
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
fireExceptionCaught(ctx, cause);
|
|
|
|
}
|
|
|
|
});
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
|
|
|
public void fireUserEventTriggered(Object event) {
|
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
|
|
|
fireUserEventTriggered(ctx, event);
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
void fireUserEventTriggered(final DefaultChannelHandlerContext ctx, final Object event) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (event == null) {
|
|
|
|
throw new NullPointerException("event");
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelInboundHandler<Object>) ctx.handler()).userEventTriggered(ctx, event);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
fireUserEventTriggered(ctx, event);
|
|
|
|
}
|
|
|
|
});
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public void fireInboundBufferUpdated() {
|
2012-05-30 11:32:39 -07:00
|
|
|
if (!firedChannelActive) {
|
|
|
|
fireInboundBufferUpdatedOnActivation = true;
|
|
|
|
return;
|
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = firstInboundContext();
|
|
|
|
if (ctx != null) {
|
|
|
|
fireInboundBufferUpdated(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static void fireInboundBufferUpdated(DefaultChannelHandlerContext ctx) {
|
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
ctx.fireInboundBufferUpdatedTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(ctx.fireInboundBufferUpdatedTask);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture bind(SocketAddress localAddress) {
|
2012-05-11 00:57:42 +09:00
|
|
|
return bind(localAddress, channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress) {
|
2012-05-11 00:57:42 +09:00
|
|
|
return connect(remoteAddress, channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
|
2012-05-11 00:57:42 +09:00
|
|
|
return connect(remoteAddress, localAddress, channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture disconnect() {
|
2012-05-11 00:57:42 +09:00
|
|
|
return disconnect(channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture close() {
|
2012-05-11 00:57:42 +09:00
|
|
|
return close(channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture deregister() {
|
2012-05-11 00:57:42 +09:00
|
|
|
return deregister(channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture flush() {
|
2012-05-11 00:57:42 +09:00
|
|
|
return flush(channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture write(Object message) {
|
2012-05-11 00:57:42 +09:00
|
|
|
return write(message, channel().newFuture());
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture bind(SocketAddress localAddress, ChannelFuture future) {
|
|
|
|
return bind(firstOutboundContext(), localAddress, future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture bind(final DefaultChannelHandlerContext ctx, final SocketAddress localAddress, final ChannelFuture future) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (localAddress == null) {
|
|
|
|
throw new NullPointerException("localAddress");
|
|
|
|
}
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).bind(ctx, localAddress, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
bind(ctx, localAddress, future);
|
|
|
|
}
|
|
|
|
});
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.bind(localAddress, future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-05-09 22:09:06 +09:00
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future) {
|
|
|
|
return connect(remoteAddress, null, future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) {
|
|
|
|
return connect(firstOutboundContext(), remoteAddress, localAddress, future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture connect(final DefaultChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelFuture future) {
|
2012-05-01 17:19:41 +09:00
|
|
|
if (remoteAddress == null) {
|
|
|
|
throw new NullPointerException("remoteAddress");
|
|
|
|
}
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
|
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).connect(ctx, remoteAddress, localAddress, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
connect(ctx, remoteAddress, localAddress, future);
|
|
|
|
}
|
|
|
|
});
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.connect(remoteAddress, localAddress, future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-05-09 22:09:06 +09:00
|
|
|
|
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture disconnect(ChannelFuture future) {
|
|
|
|
return disconnect(firstOutboundContext(), future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture disconnect(final DefaultChannelHandlerContext ctx, final ChannelFuture future) {
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).disconnect(ctx, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
disconnect(ctx, future);
|
|
|
|
}
|
|
|
|
});
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.disconnect(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-05-09 22:09:06 +09:00
|
|
|
|
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture close(ChannelFuture future) {
|
|
|
|
return close(firstOutboundContext(), future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture close(final DefaultChannelHandlerContext ctx, final ChannelFuture future) {
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).close(ctx, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
close(ctx, future);
|
|
|
|
}
|
|
|
|
});
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.close(future);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-09 22:09:06 +09:00
|
|
|
|
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture deregister(final ChannelFuture future) {
|
|
|
|
return deregister(firstOutboundContext(), future);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture deregister(final DefaultChannelHandlerContext ctx, final ChannelFuture future) {
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).deregister(ctx, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
deregister(ctx, future);
|
|
|
|
}
|
|
|
|
});
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.deregister(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-05-09 22:09:06 +09:00
|
|
|
|
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture flush(ChannelFuture future) {
|
|
|
|
return flush(firstOutboundContext(), future);
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
ChannelFuture flush(final DefaultChannelHandlerContext ctx, final ChannelFuture future) {
|
2012-05-11 09:00:35 +09:00
|
|
|
validateFuture(future);
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx != null) {
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor = ctx.executor();
|
|
|
|
if (executor.inEventLoop()) {
|
2012-06-01 18:34:19 -07:00
|
|
|
flush0(ctx, future);
|
2012-06-01 17:51:19 -07:00
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
flush(ctx, future);
|
|
|
|
}
|
|
|
|
});
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
} else {
|
2012-06-01 18:34:19 -07:00
|
|
|
unsafe.flush(future);
|
2011-08-01 04:16:02 +09:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
|
2012-05-09 22:09:06 +09:00
|
|
|
return future;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 18:34:19 -07:00
|
|
|
private void flush0(final DefaultChannelHandlerContext ctx, ChannelFuture future) {
|
|
|
|
try {
|
|
|
|
((ChannelOutboundHandler<Object>) ctx.handler()).flush(ctx, future);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
notifyHandlerException(t);
|
|
|
|
} finally {
|
|
|
|
ChannelBufferHolder<Object> outbound = ctx.outbound();
|
|
|
|
if (!outbound.isBypass() && outbound.isEmpty() && outbound.hasByteBuffer()) {
|
|
|
|
outbound.byteBuffer().discardReadBytes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture write(Object message, ChannelFuture future) {
|
2012-06-01 17:51:19 -07:00
|
|
|
return write(firstOutboundContext(), message, future);
|
|
|
|
}
|
|
|
|
|
2012-06-03 04:10:32 -07:00
|
|
|
ChannelFuture write(DefaultChannelHandlerContext ctx, final Object message, final ChannelFuture future) {
|
2012-05-11 09:00:35 +09:00
|
|
|
if (message == null) {
|
|
|
|
throw new NullPointerException("message");
|
|
|
|
}
|
|
|
|
validateFuture(future);
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
EventExecutor executor;
|
|
|
|
ChannelBufferHolder<Object> out;
|
2012-06-03 04:10:32 -07:00
|
|
|
boolean msgBuf = false;
|
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
|
|
|
executor = channel.eventLoop();
|
|
|
|
out = directOutbound;
|
|
|
|
if (out.hasByteBuffer()) {
|
|
|
|
if(!(message instanceof ChannelBuffer)) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"cannot write a message whose type is not " +
|
|
|
|
ChannelBuffer.class.getSimpleName() + ": " + message.getClass().getName());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msgBuf = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
out = ctx.outbound();
|
2012-06-03 04:10:32 -07:00
|
|
|
if (out.hasMessageBuffer()) {
|
|
|
|
msgBuf = true;
|
|
|
|
executor = ctx.executor();
|
|
|
|
break;
|
|
|
|
} else if (message instanceof ChannelBuffer) {
|
|
|
|
executor = ctx.executor();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.prev;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-05-16 23:02:06 +09:00
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
if (executor.inEventLoop()) {
|
2012-06-03 04:10:32 -07:00
|
|
|
if (msgBuf) {
|
2012-06-01 17:51:19 -07:00
|
|
|
out.messageBuffer().add(message);
|
|
|
|
} else {
|
2012-06-03 04:10:32 -07:00
|
|
|
ChannelBuffer buf = (ChannelBuffer) message;
|
|
|
|
out.byteBuffer().writeBytes(buf, buf.readerIndex(), buf.readableBytes());
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
2012-06-01 18:34:19 -07:00
|
|
|
if (ctx != null) {
|
|
|
|
flush0(ctx, future);
|
|
|
|
} else {
|
|
|
|
unsafe.flush(future);
|
|
|
|
}
|
|
|
|
return future;
|
2012-06-01 17:51:19 -07:00
|
|
|
} else {
|
2012-06-03 04:10:32 -07:00
|
|
|
final DefaultChannelHandlerContext ctx0 = ctx;
|
2012-06-01 17:51:19 -07:00
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2012-06-03 04:10:32 -07:00
|
|
|
write(ctx0, message, future);
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return future;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2012-05-11 09:00:35 +09:00
|
|
|
private void validateFuture(ChannelFuture future) {
|
|
|
|
if (future == null) {
|
|
|
|
throw new NullPointerException("future");
|
|
|
|
}
|
|
|
|
if (future.channel() != channel()) {
|
|
|
|
throw new IllegalArgumentException(String.format(
|
|
|
|
"future.channel does not match: %s (expected: %s)", future.channel(), channel()));
|
|
|
|
}
|
2012-05-13 01:35:43 +09:00
|
|
|
if (future.isDone()) {
|
|
|
|
throw new IllegalArgumentException("future already done");
|
|
|
|
}
|
|
|
|
if (future instanceof ChannelFuture.Unsafe) {
|
|
|
|
throw new IllegalArgumentException("internal use only future not allowed");
|
|
|
|
}
|
2012-05-11 09:00:35 +09:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private DefaultChannelHandlerContext firstInboundContext() {
|
|
|
|
return nextInboundContext(head);
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext firstOutboundContext() {
|
|
|
|
return nextOutboundContext(tail);
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static DefaultChannelHandlerContext nextInboundContext(DefaultChannelHandlerContext ctx) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext realCtx = ctx;
|
2012-05-01 17:19:41 +09:00
|
|
|
while (!realCtx.canHandleInbound()) {
|
2008-08-08 00:37:18 +00:00
|
|
|
realCtx = realCtx.next;
|
|
|
|
if (realCtx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return realCtx;
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
static DefaultChannelHandlerContext nextOutboundContext(DefaultChannelHandlerContext ctx) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext realCtx = ctx;
|
2012-05-01 17:19:41 +09:00
|
|
|
while (!realCtx.canHandleOutbound()) {
|
2008-08-08 00:37:18 +00:00
|
|
|
realCtx = realCtx.prev;
|
|
|
|
if (realCtx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return realCtx;
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
protected void notifyHandlerException(Throwable cause) {
|
|
|
|
if (!(cause instanceof ChannelPipelineException)) {
|
|
|
|
cause = new ChannelPipelineException(cause);
|
|
|
|
}
|
2012-02-29 13:53:26 -08:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
if (inExceptionCaught(cause)) {
|
2012-02-17 10:37:41 +01:00
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by a user handler " +
|
2012-05-01 17:19:41 +09:00
|
|
|
"while handling an exceptionCaught event", cause);
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
2008-08-18 11:11:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
fireExceptionCaught(cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean inExceptionCaught(Throwable cause) {
|
|
|
|
if (cause == null) {
|
|
|
|
return false;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
StackTraceElement[] trace = cause.getStackTrace();
|
|
|
|
if (trace != null) {
|
|
|
|
for (StackTraceElement t: trace) {
|
|
|
|
if ("exceptionCaught".equals(t.getMethodName())) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
|
|
|
|
return inExceptionCaught(cause.getCause());
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
private void init(EventExecutor executor, String name, ChannelHandler handler) {
|
|
|
|
DefaultChannelHandlerContext ctx =
|
|
|
|
new DefaultChannelHandlerContext(this, executor, null, null, name, handler);
|
2009-12-17 09:05:46 +00:00
|
|
|
callBeforeAdd(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
head = tail = ctx;
|
|
|
|
name2ctx.clear();
|
|
|
|
name2ctx.put(name, ctx);
|
2009-12-17 09:05:46 +00:00
|
|
|
callAfterAdd(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDuplicateName(String name) {
|
|
|
|
if (name2ctx.containsKey(name)) {
|
2011-11-23 14:07:26 +09:00
|
|
|
throw new IllegalArgumentException("Duplicate handler name: " + name);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(String name) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(name);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(name);
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(ChannelHandler handler) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(handler);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(handler.getClass().getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(Class<? extends ChannelHandler> handlerType) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(handlerType);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(handlerType.getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
2009-08-28 07:15:49 +00:00
|
|
|
}
|