Automatic messageType detection for ChannelInboundMessageHandlerAdapter

This commit is contained in:
Trustin Lee 2013-02-08 13:48:47 +09:00
parent f9eff51683
commit e5616c85c4
8 changed files with 42 additions and 28 deletions

View File

@ -58,7 +58,6 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler
}
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
super(WebSocketFrame.class);
this.websocketPath = websocketPath;
this.subprotocols = subprotocols;
this.allowExtensions = allowExtensions;

View File

@ -44,7 +44,6 @@ public class WebSocketServerProtocolHandshakeHandler
public WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
boolean allowExtensions) {
super(FullHttpRequest.class);
this.websocketPath = websocketPath;
this.subprotocols = subprotocols;
this.allowExtensions = allowExtensions;

View File

@ -37,12 +37,7 @@ public final class SocksServerConnectHandler extends ChannelInboundMessageHandle
return name;
}
private final Bootstrap b;
public SocksServerConnectHandler() {
super(SocksCmdRequest.class);
b = new Bootstrap();
}
private final Bootstrap b = new Bootstrap();
@Override
public void messageReceived(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {

View File

@ -34,10 +34,6 @@ public final class SocksServerHandler extends ChannelInboundMessageHandlerAdapte
return name;
}
public SocksServerHandler() {
super(SocksRequest.class);
}
@Override
public void messageReceived(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
switch (socksRequest.requestType()) {

View File

@ -35,7 +35,6 @@ public class SctpInboundByteStreamHandler extends ChannelInboundMessageHandlerAd
* @param protocolIdentifier supported application protocol.
*/
public SctpInboundByteStreamHandler(int protocolIdentifier, int streamIdentifier) {
super(SctpMessage.class);
this.protocolIdentifier = protocolIdentifier;
this.streamIdentifier = streamIdentifier;
}

View File

@ -46,9 +46,6 @@ public class EchoMessageHandler extends
}
public EchoMessageHandler(final Meter meter, final int messageSize) {
super(UdtMessage.class);
this.meter = meter;
final ByteBuf byteBuf = Unpooled.buffer(messageSize);

View File

@ -18,6 +18,10 @@ package io.netty.channel;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* {@link ChannelHandler} which handles inbound messages of a specific type.
*
@ -43,14 +47,43 @@ import io.netty.buffer.Unpooled;
public abstract class ChannelInboundMessageHandlerAdapter<I>
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
private final Class<?>[] acceptedMsgTypes;
private static final ConcurrentMap<Class<?>, Class<?>> messageTypeMap =
new ConcurrentHashMap<Class<?>, Class<?>>();
/**
* The types which will be accepted by the message handler. If a received message is an other type it will be just
* forwarded to the next {@link ChannelInboundMessageHandler} in the {@link ChannelPipeline}.
*/
protected ChannelInboundMessageHandlerAdapter(Class<?>... acceptedMsgTypes) {
this.acceptedMsgTypes = ChannelHandlerUtil.acceptedMessageTypes(acceptedMsgTypes);
private final Class<?> acceptedMsgType = findMessageType();
private Class<?> findMessageType() {
Class<?> thisClass = getClass();
Class<?> messageType = messageTypeMap.get(thisClass);
if (messageType == null) {
for (Method m: getClass().getDeclaredMethods()) {
if (!"messageReceived".equals(m.getName())) {
continue;
}
if (m.isSynthetic() || m.isBridge()) {
continue;
}
Class<?>[] p = m.getParameterTypes();
if (p.length != 2) {
continue;
}
if (p[0] != ChannelHandlerContext.class) {
continue;
}
messageType = p[1];
break;
}
if (messageType == null) {
throw new IllegalStateException(
"cannot determine the inbound message type of " + thisClass.getSimpleName());
}
messageTypeMap.put(thisClass, messageType);
}
return messageType;
}
@Override
@ -121,7 +154,7 @@ public abstract class ChannelInboundMessageHandlerAdapter<I>
* @param msg the message
*/
public boolean isSupported(Object msg) throws Exception {
return ChannelHandlerUtil.acceptMessage(acceptedMsgTypes, msg);
return acceptedMsgType.isInstance(msg);
}
/**

View File

@ -155,10 +155,6 @@ public class DefaultChannelPipelineTest {
private static final class StringInboundHandler extends ChannelInboundMessageHandlerAdapter<String> {
boolean called;
public StringInboundHandler() {
super(String.class);
}
@Override
public boolean isSupported(Object msg) throws Exception {
called = true;