Add back Channel(Inbound|Outbound)(Message|Stream)HandlerAdapter

- they are useful when creating an anonymous class
- Also added back CombinedChannelHandler with extra constraints
This commit is contained in:
Trustin Lee 2012-06-07 17:49:45 +09:00
parent aab71ccd8a
commit 8701e24b9a
19 changed files with 313 additions and 86 deletions

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.CombinedChannelHandler;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.util.internal.QueueFactory;

View File

@ -15,6 +15,10 @@
*/
package io.netty.handler.codec.http;
import io.netty.channel.CombinedChannelHandler;
import io.netty.channel.CombinedChannelHandler;
/**
* A combination of {@link HttpRequestDecoder} and {@link HttpResponseEncoder}

View File

@ -15,6 +15,10 @@
*/
package io.netty.handler.codec.spdy;
import io.netty.channel.CombinedChannelHandler;
import io.netty.channel.CombinedChannelHandler;
/**
* A combination of {@link SpdyFrameDecoder} and {@link SpdyFrameEncoder}.

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.spdy;
import io.netty.channel.CombinedChannelHandler;
/**
* A combination of {@link SpdyHttpDecoder} and {@link SpdyHttpEncoder}

View File

@ -15,21 +15,12 @@
*/
package io.netty.handler.codec;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import java.util.Queue;
public abstract class MessageToMessageDecoder<I, O> extends ChannelStateHandlerAdapter implements ChannelInboundHandler<I> {
@Override
public ChannelBufferHolder<I> newInboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.messageBuffer();
}
public abstract class MessageToMessageDecoder<I, O> extends ChannelInboundMessageHandlerAdapter<I> {
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx)

View File

@ -15,23 +15,13 @@
*/
package io.netty.handler.codec;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOperationHandlerAdapter;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelOutboundMessageHandlerAdapter;
import java.util.Queue;
public abstract class MessageToMessageEncoder<I, O>
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler<I> {
@Override
public ChannelBufferHolder<I> newOutboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.messageBuffer();
}
public abstract class MessageToMessageEncoder<I, O> extends ChannelOutboundMessageHandlerAdapter<I> {
@Override
public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception {

View File

@ -16,23 +16,13 @@
package io.netty.handler.codec;
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOperationHandlerAdapter;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelOutboundMessageHandlerAdapter;
import java.util.Queue;
public abstract class MessageToStreamEncoder<I>
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler<I> {
@Override
public ChannelBufferHolder<I> newOutboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.messageBuffer();
}
public abstract class MessageToStreamEncoder<I> extends ChannelOutboundMessageHandlerAdapter<I> {
@Override
public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception {

View File

@ -16,23 +16,19 @@
package io.netty.handler.codec;
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundStreamHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandlerAdapter;
public abstract class StreamToMessageDecoder<O>
extends ChannelStateHandlerAdapter implements ChannelInboundHandler<Byte> {
public abstract class StreamToMessageDecoder<O> extends ChannelInboundStreamHandlerAdapter {
private ChannelHandlerContext ctx;
@Override
public ChannelBufferHolder<Byte> newInboundBuffer(
ChannelHandlerContext ctx) throws Exception {
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
return ChannelBufferHolders.byteBuffer();
super.beforeAdd(ctx);
}
@Override

View File

@ -16,20 +16,10 @@
package io.netty.handler.codec;
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.ChannelInboundStreamHandlerAdapter;
public abstract class StreamToStreamDecoder
extends ChannelStateHandlerAdapter implements ChannelInboundHandler<Byte> {
@Override
public ChannelBufferHolder<Byte> newInboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.byteBuffer();
}
public abstract class StreamToStreamDecoder extends ChannelInboundStreamHandlerAdapter {
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {

View File

@ -16,21 +16,11 @@
package io.netty.handler.codec;
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOperationHandlerAdapter;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelOutboundStreamHandlerAdapter;
public abstract class StreamToStreamEncoder
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler<Byte> {
@Override
public ChannelBufferHolder<Byte> newOutboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.byteBuffer();
}
public abstract class StreamToStreamEncoder extends ChannelOutboundStreamHandlerAdapter {
@Override
public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception {

View File

@ -21,9 +21,8 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.util.internal.QueueFactory;
import java.io.IOException;
@ -70,8 +69,7 @@ import java.util.concurrent.TimeUnit;
*
* @param <E> the type of the received messages
*/
public class BlockingReadHandler<E>
extends ChannelStateHandlerAdapter implements ChannelInboundHandler<Object> {
public class BlockingReadHandler<E> extends ChannelInboundMessageHandlerAdapter<Object> {
private static final Object INACTIVE = new Object();

View File

@ -16,18 +16,15 @@
package io.netty.bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.EventLoop;
import io.netty.channel.ServerChannel;
import io.netty.logging.InternalLogger;
@ -231,13 +228,7 @@ public class ServerBootstrap {
validate();
}
private class Acceptor extends ChannelStateHandlerAdapter implements ChannelInboundHandler<Channel> {
@Override
public ChannelBufferHolder<Channel> newInboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.messageBuffer();
}
private class Acceptor extends ChannelInboundMessageHandlerAdapter<Channel> {
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) {

View File

@ -0,0 +1,22 @@
/*
* 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;
public abstract class ChannelInboundHandlerAdapter<I> extends ChannelStateHandlerAdapter
implements ChannelInboundHandler<I> {
// Useful when you have to create an anonymous class
}

View File

@ -17,8 +17,7 @@ package io.netty.channel;
import java.util.Queue;
public class ChannelInboundMessageHandlerAdapter<I>
extends ChannelStateHandlerAdapter implements ChannelInboundHandler<I> {
public class ChannelInboundMessageHandlerAdapter<I> extends ChannelInboundHandlerAdapter<I> {
@Override
public ChannelBufferHolder<I> newInboundBuffer(ChannelHandlerContext ctx) throws Exception {

View File

@ -18,8 +18,7 @@ package io.netty.channel;
import io.netty.buffer.ChannelBuffer;
public class ChannelInboundStreamHandlerAdapter
extends ChannelStateHandlerAdapter implements ChannelInboundHandler<Byte> {
public class ChannelInboundStreamHandlerAdapter extends ChannelInboundHandlerAdapter<Byte> {
@Override
public ChannelBufferHolder<Byte> newInboundBuffer(ChannelHandlerContext ctx) throws Exception {

View File

@ -0,0 +1,22 @@
/*
* 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;
public abstract class ChannelOutboundHandlerAdapter<O>
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler<O> {
// Useful when you have to create an anonymous class
}

View File

@ -0,0 +1,24 @@
/*
* 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;
public class ChannelOutboundMessageHandlerAdapter<I> extends ChannelOutboundHandlerAdapter<I> {
@Override
public ChannelBufferHolder<I> newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.messageBuffer();
}
}

View File

@ -0,0 +1,23 @@
/*
* 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;
public class ChannelOutboundStreamHandlerAdapter extends ChannelOutboundHandlerAdapter<Byte> {
@Override
public ChannelBufferHolder<Byte> newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ChannelBufferHolders.byteBuffer();
}
}

View File

@ -0,0 +1,191 @@
/*
* 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;
import java.net.SocketAddress;
public class CombinedChannelHandler
extends ChannelHandlerAdapter
implements ChannelInboundHandler<Object>, ChannelOutboundHandler<Object> {
private ChannelOutboundHandler<Object> out;
private ChannelInboundHandler<Object> in;
protected CombinedChannelHandler() {
// User will call init in the subclass constructor.
}
public CombinedChannelHandler(
ChannelInboundHandler<?> inboundHandler, ChannelOutboundHandler<?> outboundHandler) {
init(inboundHandler, outboundHandler);
}
@SuppressWarnings("unchecked")
protected void init(ChannelInboundHandler<?> inboundHandler,
ChannelOutboundHandler<?> outboundHandler) {
if (inboundHandler == null) {
throw new NullPointerException("inboundHandler");
}
if (outboundHandler == null) {
throw new NullPointerException("outboundHandler");
}
if (inboundHandler instanceof ChannelOperationHandler) {
throw new IllegalArgumentException(
"inboundHandler must not implement " +
ChannelOperationHandler.class.getSimpleName() + " to get combined.");
}
if (outboundHandler instanceof ChannelStateHandler) {
throw new IllegalArgumentException(
"outboundHandler must not implement " +
ChannelStateHandler.class.getSimpleName() + " to get combined.");
}
if (in != null) {
throw new IllegalStateException("init() cannot be called more than once.");
}
in = (ChannelInboundHandler<Object>) inboundHandler;
out = (ChannelOutboundHandler<Object>) outboundHandler;
}
@Override
public ChannelBufferHolder<Object> newInboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return in.newInboundBuffer(ctx);
}
@Override
public ChannelBufferHolder<Object> newOutboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return out.newOutboundBuffer(ctx);
}
@Override
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
if (in == null) {
throw new IllegalStateException(
"not initialized yet - call init() in the constructor of the subclass");
}
try {
in.beforeAdd(ctx);
} finally {
out.beforeAdd(ctx);
}
}
@Override
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
try {
in.afterAdd(ctx);
} finally {
out.afterAdd(ctx);
}
}
@Override
public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
try {
in.beforeRemove(ctx);
} finally {
out.beforeRemove(ctx);
}
}
@Override
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
try {
in.afterRemove(ctx);
} finally {
out.afterRemove(ctx);
}
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
in.channelRegistered(ctx);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
in.channelUnregistered(ctx);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
in.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
in.channelInactive(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
in.exceptionCaught(ctx, cause);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
in.userEventTriggered(ctx, evt);
}
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
in.inboundBufferUpdated(ctx);
}
@Override
public void bind(
ChannelHandlerContext ctx,
SocketAddress localAddress, ChannelFuture future) throws Exception {
out.bind(ctx, localAddress, future);
}
@Override
public void connect(
ChannelHandlerContext ctx,
SocketAddress remoteAddress, SocketAddress localAddress,
ChannelFuture future) throws Exception {
out.connect(ctx, remoteAddress, localAddress, future);
}
@Override
public void disconnect(
ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
out.disconnect(ctx, future);
}
@Override
public void close(
ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
out.close(ctx, future);
}
@Override
public void deregister(
ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
out.deregister(ctx, future);
}
@Override
public void flush(
ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
out.flush(ctx, future);
}
}