Merge the attribute map of ChannelHandlerContext into Channel

- Fixes #2136
- Also fixes some generics warnings
This commit is contained in:
Trustin Lee 2014-01-20 15:16:12 +09:00
parent 77d1f97b57
commit 7e60877bc5

View File

@ -15,9 +15,9 @@
*/ */
package io.netty.channel; package io.netty.channel;
import static io.netty.channel.DefaultChannelPipeline.logger;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.util.DefaultAttributeMap; import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.Recycler; import io.netty.util.Recycler;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
@ -26,7 +26,9 @@ import io.netty.util.internal.StringUtil;
import java.net.SocketAddress; import java.net.SocketAddress;
final class DefaultChannelHandlerContext extends DefaultAttributeMap implements ChannelHandlerContext { import static io.netty.channel.DefaultChannelPipeline.*;
final class DefaultChannelHandlerContext implements ChannelHandlerContext {
volatile DefaultChannelHandlerContext next; volatile DefaultChannelHandlerContext next;
volatile DefaultChannelHandlerContext prev; volatile DefaultChannelHandlerContext prev;
@ -143,6 +145,11 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return name; return name;
} }
@Override
public <T> Attribute<T> attr(AttributeKey<T> key) {
return channel.attr(key);
}
@Override @Override
public ChannelHandlerContext fireChannelRegistered() { public ChannelHandlerContext fireChannelRegistered() {
final DefaultChannelHandlerContext next = findContextInbound(); final DefaultChannelHandlerContext next = findContextInbound();
@ -185,6 +192,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return this; return this;
} }
@SuppressWarnings("deprecation")
private void invokeChannelUnregistered() { private void invokeChannelUnregistered() {
try { try {
((ChannelInboundHandler) handler).channelUnregistered(this); ((ChannelInboundHandler) handler).channelUnregistered(this);
@ -273,6 +281,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return this; return this;
} }
@SuppressWarnings("deprecation")
private void invokeExceptionCaught(final Throwable cause) { private void invokeExceptionCaught(final Throwable cause) {
try { try {
handler.exceptionCaught(this, cause); handler.exceptionCaught(this, cause);
@ -586,6 +595,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return promise; return promise;
} }
@SuppressWarnings("deprecation")
private void invokeDeregister(ChannelPromise promise) { private void invokeDeregister(ChannelPromise promise) {
try { try {
((ChannelOutboundHandler) handler).deregister(this, promise); ((ChannelOutboundHandler) handler).deregister(this, promise);
@ -870,19 +880,19 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
} }
} }
abstract static class AbstractWriteTask implements Runnable { abstract static class AbstractWriteTask<T extends AbstractWriteTask<T>> implements Runnable {
private final Recycler.Handle handle; private final Recycler.Handle<T> handle;
private DefaultChannelHandlerContext ctx; private DefaultChannelHandlerContext ctx;
private Object msg; private Object msg;
private ChannelPromise promise; private ChannelPromise promise;
private int size; private int size;
private AbstractWriteTask(Recycler.Handle handle) { private AbstractWriteTask(Recycler.Handle<T> handle) {
this.handle = handle; this.handle = handle;
} }
protected static void init(AbstractWriteTask task, DefaultChannelHandlerContext ctx, protected static void init(AbstractWriteTask<?> task, DefaultChannelHandlerContext ctx,
Object msg, int size, ChannelPromise promise) { Object msg, int size, ChannelPromise promise) {
task.ctx = ctx; task.ctx = ctx;
task.msg = msg; task.msg = msg;
@ -914,14 +924,15 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
ctx.invokeWrite(msg, promise); ctx.invokeWrite(msg, promise);
} }
protected abstract void recycle(Recycler.Handle handle); protected abstract void recycle(Recycler.Handle<T> handle);
} }
static final class WriteTask extends AbstractWriteTask implements SingleThreadEventLoop.NonWakeupRunnable { static final class WriteTask
extends AbstractWriteTask<WriteTask> implements SingleThreadEventLoop.NonWakeupRunnable {
private static final Recycler<WriteTask> RECYCLER = new Recycler<WriteTask>() { private static final Recycler<WriteTask> RECYCLER = new Recycler<WriteTask>() {
@Override @Override
protected WriteTask newObject(Handle handle) { protected WriteTask newObject(Handle<WriteTask> handle) {
return new WriteTask(handle); return new WriteTask(handle);
} }
}; };
@ -933,21 +944,21 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return task; return task;
} }
private WriteTask(Recycler.Handle handle) { private WriteTask(Recycler.Handle<WriteTask> handle) {
super(handle); super(handle);
} }
@Override @Override
protected void recycle(Recycler.Handle handle) { protected void recycle(Recycler.Handle<WriteTask> handle) {
RECYCLER.recycle(this, handle); RECYCLER.recycle(this, handle);
} }
} }
static final class WriteAndFlushTask extends AbstractWriteTask { static final class WriteAndFlushTask extends AbstractWriteTask<WriteAndFlushTask> {
private static final Recycler<WriteAndFlushTask> RECYCLER = new Recycler<WriteAndFlushTask>() { private static final Recycler<WriteAndFlushTask> RECYCLER = new Recycler<WriteAndFlushTask>() {
@Override @Override
protected WriteAndFlushTask newObject(Handle handle) { protected WriteAndFlushTask newObject(Handle<WriteAndFlushTask> handle) {
return new WriteAndFlushTask(handle); return new WriteAndFlushTask(handle);
} }
}; };
@ -959,7 +970,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return task; return task;
} }
private WriteAndFlushTask(Recycler.Handle handle) { private WriteAndFlushTask(Recycler.Handle<WriteAndFlushTask> handle) {
super(handle); super(handle);
} }
@ -970,7 +981,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
} }
@Override @Override
protected void recycle(Recycler.Handle handle) { protected void recycle(Recycler.Handle<WriteAndFlushTask> handle) {
RECYCLER.recycle(this, handle); RECYCLER.recycle(this, handle);
} }
} }