* Code clean up

* Codec embedded now supports multiple handler combinations
This commit is contained in:
Trustin Lee 2008-12-03 08:28:50 +00:00
parent 2bf7467726
commit e659482215
3 changed files with 21 additions and 11 deletions

View File

@ -47,22 +47,32 @@ import org.jboss.netty.channel.MessageEvent;
*/ */
abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> { abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> {
private static final String NAME = "__embedded__";
private final Channel channel; private final Channel channel;
private final ChannelPipeline pipeline; private final ChannelPipeline pipeline;
private final EmbeddedChannelSink sink = new EmbeddedChannelSink(); private final EmbeddedChannelSink sink = new EmbeddedChannelSink();
final Queue<Object> productQueue = new LinkedList<Object>(); final Queue<Object> productQueue = new LinkedList<Object>();
AbstractCodecEmbedder(ChannelHandler handler) { AbstractCodecEmbedder(ChannelHandler... handlers) {
if (handler == null) { if (handlers == null) {
throw new NullPointerException("handler"); throw new NullPointerException("handlers");
}
if (handlers.length == 0) {
throw new IllegalArgumentException(
"handlers should contain at least one " +
ChannelHandler.class.getSimpleName() + '.');
} }
pipeline = Channels.pipeline(); pipeline = Channels.pipeline();
pipeline.addLast(NAME, handler); for (int i = 0; i < handlers.length; i ++) {
pipeline.addLast("LAST", sink); ChannelHandler h = handlers[i];
if (h == null) {
throw new NullPointerException("handlers[" + i + "]");
}
pipeline.addLast(String.valueOf(i), handlers[i]);
}
pipeline.addLast("SINK", sink);
channel = new EmbeddedChannel(pipeline, sink); channel = new EmbeddedChannel(pipeline, sink);
// Fire the typical initial events. // Fire the typical initial events.

View File

@ -33,8 +33,8 @@ import org.jboss.netty.channel.ChannelUpstreamHandler;
*/ */
public class DecoderEmbedder<T> extends AbstractCodecEmbedder<T> { public class DecoderEmbedder<T> extends AbstractCodecEmbedder<T> {
public DecoderEmbedder(ChannelUpstreamHandler handler) { public DecoderEmbedder(ChannelUpstreamHandler... handlers) {
super(handler); super(handlers);
} }
public boolean offer(Object input) { public boolean offer(Object input) {

View File

@ -33,8 +33,8 @@ import org.jboss.netty.channel.ChannelDownstreamHandler;
*/ */
public class EncoderEmbedder<T> extends AbstractCodecEmbedder<T> { public class EncoderEmbedder<T> extends AbstractCodecEmbedder<T> {
public EncoderEmbedder(ChannelDownstreamHandler handler) { public EncoderEmbedder(ChannelDownstreamHandler... handlers) {
super(handler); super(handlers);
} }
public boolean offer(Object input) { public boolean offer(Object input) {