Made a serious design mistake - rewrote partially
This commit is contained in:
parent
017dfff527
commit
f04d8ae99a
@ -24,8 +24,18 @@ package org.jboss.netty.handler.codec.embedder;
|
|||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.*;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
|
import org.jboss.netty.channel.ChannelEvent;
|
||||||
import org.jboss.netty.channel.ChannelHandler;
|
import org.jboss.netty.channel.ChannelHandler;
|
||||||
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
|
import org.jboss.netty.channel.ChannelPipelineException;
|
||||||
|
import org.jboss.netty.channel.ChannelSink;
|
||||||
|
import org.jboss.netty.channel.Channels;
|
||||||
|
import org.jboss.netty.channel.ExceptionEvent;
|
||||||
|
import org.jboss.netty.channel.MessageEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
@ -34,37 +44,76 @@ import org.jboss.netty.channel.ChannelHandler;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> {
|
abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> {
|
||||||
|
|
||||||
final EmbeddedChannelHandlerContext context;
|
private static final String NAME = "__embedded__";
|
||||||
|
|
||||||
|
private final Channel channel;
|
||||||
|
private final ChannelPipeline pipeline;
|
||||||
|
final Queue<Object> productQueue = new LinkedList<Object>();
|
||||||
|
|
||||||
AbstractCodecEmbedder(ChannelHandler handler) {
|
AbstractCodecEmbedder(ChannelHandler handler) {
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
throw new NullPointerException("handler");
|
throw new NullPointerException("handler");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.context = new EmbeddedChannelHandlerContext(handler);
|
pipeline = Channels.pipeline();
|
||||||
|
pipeline.addLast(NAME, handler);
|
||||||
|
channel = new EmbeddedChannel(pipeline, new EmbeddedChannelSink());
|
||||||
|
|
||||||
// Fire the typical initial events.
|
// Fire the typical initial events.
|
||||||
Channel channel = context.getChannel();
|
fireChannelOpen(channel);
|
||||||
fireChannelOpen(context, channel);
|
fireChannelBound(channel, channel.getLocalAddress());
|
||||||
fireChannelBound(context, channel, channel.getLocalAddress());
|
fireChannelConnected(channel, channel.getRemoteAddress());
|
||||||
fireChannelConnected(context, channel, channel.getRemoteAddress());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean finish() {
|
public boolean finish() {
|
||||||
Channel channel = context.getChannel();
|
fireChannelDisconnected(channel);
|
||||||
fireChannelDisconnected(context, channel);
|
fireChannelUnbound(channel);
|
||||||
fireChannelUnbound(context, channel);
|
fireChannelClosed(channel);
|
||||||
fireChannelClosed(context, channel);
|
return !productQueue.isEmpty();
|
||||||
return !context.productQueue.isEmpty();
|
}
|
||||||
|
|
||||||
|
protected final Channel getChannel() {
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final boolean isEmpty() {
|
||||||
|
return productQueue.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T poll() {
|
public final T poll() {
|
||||||
return (T) context.productQueue.poll();
|
return (T) productQueue.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T peek() {
|
public final T peek() {
|
||||||
return (T) context.productQueue.peek();
|
return (T) productQueue.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class EmbeddedChannelSink implements ChannelSink {
|
||||||
|
EmbeddedChannelSink() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) {
|
||||||
|
if (e instanceof MessageEvent) {
|
||||||
|
productQueue.offer(((MessageEvent) e).getMessage());
|
||||||
|
} else if (e instanceof ExceptionEvent) {
|
||||||
|
throw new CodecEmbedderException(((ExceptionEvent) e).getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swallow otherwise.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exceptionCaught(
|
||||||
|
ChannelPipeline pipeline, ChannelEvent e,
|
||||||
|
ChannelPipelineException cause) throws Exception {
|
||||||
|
Throwable actualCause = cause.getCause();
|
||||||
|
if (actualCause == null) {
|
||||||
|
actualCause = cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CodecEmbedderException(actualCause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class DecoderEmbedder<T> extends AbstractCodecEmbedder<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean offer(Object input) {
|
public boolean offer(Object input) {
|
||||||
fireMessageReceived(context, context.getChannel(), input);
|
fireMessageReceived(getChannel(), input);
|
||||||
return !context.productQueue.isEmpty();
|
return !super.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import java.net.SocketAddress;
|
|||||||
import org.jboss.netty.channel.AbstractChannel;
|
import org.jboss.netty.channel.AbstractChannel;
|
||||||
import org.jboss.netty.channel.ChannelConfig;
|
import org.jboss.netty.channel.ChannelConfig;
|
||||||
import org.jboss.netty.channel.ChannelPipeline;
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
|
import org.jboss.netty.channel.ChannelSink;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
@ -38,9 +39,8 @@ class EmbeddedChannel extends AbstractChannel {
|
|||||||
private final SocketAddress localAddress = new EmbeddedSocketAddress();
|
private final SocketAddress localAddress = new EmbeddedSocketAddress();
|
||||||
private final SocketAddress remoteAddress = new EmbeddedSocketAddress();
|
private final SocketAddress remoteAddress = new EmbeddedSocketAddress();
|
||||||
|
|
||||||
EmbeddedChannel(
|
EmbeddedChannel(ChannelPipeline pipeline, ChannelSink sink) {
|
||||||
EmbeddedChannelHandlerContext context, ChannelPipeline pipeline) {
|
super(null, EmbeddedChannelFactory.INSTANCE, pipeline, sink);
|
||||||
super(null, EmbeddedChannelFactory.INSTANCE, pipeline, context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelConfig getConfig() {
|
public ChannelConfig getConfig() {
|
||||||
|
@ -1,118 +0,0 @@
|
|||||||
/*
|
|
||||||
* JBoss, Home of Professional Open Source
|
|
||||||
*
|
|
||||||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
||||||
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
||||||
* full listing of individual contributors.
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU Lesser General Public License as
|
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This software is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with this software; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
||||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
||||||
*/
|
|
||||||
package org.jboss.netty.handler.codec.embedder;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Queue;
|
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
|
||||||
import org.jboss.netty.channel.ChannelDownstreamHandler;
|
|
||||||
import org.jboss.netty.channel.ChannelEvent;
|
|
||||||
import org.jboss.netty.channel.ChannelHandler;
|
|
||||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
|
||||||
import org.jboss.netty.channel.ChannelPipeline;
|
|
||||||
import org.jboss.netty.channel.ChannelPipelineException;
|
|
||||||
import org.jboss.netty.channel.ChannelSink;
|
|
||||||
import org.jboss.netty.channel.ChannelUpstreamHandler;
|
|
||||||
import org.jboss.netty.channel.Channels;
|
|
||||||
import org.jboss.netty.channel.ExceptionEvent;
|
|
||||||
import org.jboss.netty.channel.MessageEvent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
|
||||||
* @version $Rev$, $Date$
|
|
||||||
*/
|
|
||||||
class EmbeddedChannelHandlerContext implements ChannelHandlerContext, ChannelSink {
|
|
||||||
|
|
||||||
private static final String NAME = "__embedded__";
|
|
||||||
|
|
||||||
private final Channel channel;
|
|
||||||
private final ChannelHandler handler;
|
|
||||||
private final ChannelPipeline pipeline;
|
|
||||||
final Queue<Object> productQueue = new LinkedList<Object>();
|
|
||||||
|
|
||||||
EmbeddedChannelHandlerContext(ChannelHandler handler) {
|
|
||||||
this.handler = handler;
|
|
||||||
pipeline = Channels.pipeline();
|
|
||||||
pipeline.addLast(NAME, handler);
|
|
||||||
channel = new EmbeddedChannel(this, pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canHandleDownstream() {
|
|
||||||
return handler instanceof ChannelDownstreamHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canHandleUpstream() {
|
|
||||||
return handler instanceof ChannelUpstreamHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChannelHandler getHandler() {
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Channel getChannel() {
|
|
||||||
return channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChannelPipeline getPipeline() {
|
|
||||||
return pipeline;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendDownstream(ChannelEvent e) {
|
|
||||||
handleEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendUpstream(ChannelEvent e) {
|
|
||||||
handleEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) {
|
|
||||||
handleEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleEvent(ChannelEvent e) {
|
|
||||||
if (e instanceof MessageEvent) {
|
|
||||||
productQueue.offer(((MessageEvent) e).getMessage());
|
|
||||||
} else if (e instanceof ExceptionEvent) {
|
|
||||||
throw new CodecEmbedderException(((ExceptionEvent) e).getCause());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swallow otherwise.
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exceptionCaught(
|
|
||||||
ChannelPipeline pipeline, ChannelEvent e,
|
|
||||||
ChannelPipelineException cause) throws Exception {
|
|
||||||
Throwable actualCause = cause.getCause();
|
|
||||||
if (actualCause == null) {
|
|
||||||
actualCause = cause;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new CodecEmbedderException(actualCause);
|
|
||||||
}
|
|
||||||
}
|
|
@ -24,7 +24,6 @@ package org.jboss.netty.handler.codec.embedder;
|
|||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.*;
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
|
||||||
import org.jboss.netty.channel.ChannelDownstreamHandler;
|
import org.jboss.netty.channel.ChannelDownstreamHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,8 +38,7 @@ public class EncoderEmbedder<T> extends AbstractCodecEmbedder<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean offer(Object input) {
|
public boolean offer(Object input) {
|
||||||
Channel channel = context.getChannel();
|
write(getChannel(), input).setSuccess();
|
||||||
write(context, channel, succeededFuture(channel), input);
|
return !isEmpty();
|
||||||
return !context.productQueue.isEmpty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user