Permalink
Browse files

Not sure what I'm doing

  • Loading branch information...
1 parent 580f43c commit 4ca17f5a6b508b906e3503b141226dafdc69153c Dave committed Mar 14, 2012
View
@@ -1,2 +1,3 @@
*.iml
*.idea
+target
View
@@ -19,6 +19,13 @@
<artifactId>guava</artifactId>
<version>11.0</version>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
@@ -24,9 +24,13 @@ public ChannelOutputStream(Channel channel) {
this.channel = channel;
}
+ public Channel getChannel() {
+ return channel;
+ }
+
@Override
public void write(int b) throws IOException {
- channel.write(wrappedBuffer(new byte[]{(byte) (b & 0x000F)}));
+ channel.write(wrappedBuffer(new byte[]{(byte) b}));
}
@Override
@@ -14,4 +14,5 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Listen {
+ Class<?> type();
}
@@ -1,8 +0,0 @@
-package org.robotninjas.handler.codec.message;
-
-/**
- * A marker interface for channel listeners which dispatch based on the message type contained in the MessageEvent
- *
- */
-public interface TypeMessageEventListener {
-}
@@ -17,51 +17,34 @@
*/
public class TypedMessageEvent<T> implements MessageEvent {
- private final Channel channel;
- private final ChannelFuture future;
private final T message;
- private final SocketAddress remoteAddress;
-
- public TypedMessageEvent(Channel channel, ChannelFuture future, T message, SocketAddress remoteAddress) {
-
- checkNotNull(channel, "channel cannot be null");
- checkNotNull(future, "future cannot be null");
- checkNotNull(message, "message cannot be null");
- checkNotNull(remoteAddress, "remoteAddress cannot be null");
-
- this.channel = channel;
- this.future = future;
- this.message = message;
- this.remoteAddress = remoteAddress;
- }
-
+ private final MessageEvent wrappedEvent;
+
private TypedMessageEvent(MessageEvent e) {
checkNotNull(e, "message event cannot be null");
- this.channel = e.getChannel();
- this.future = e.getFuture();
this.message = (T) e.getMessage();
- this.remoteAddress = e.getRemoteAddress();
+ this.wrappedEvent = e;
}
- public static <U> TypedMessageEvent<U> typedEventFromGenericEvent(Class<U> type, MessageEvent e) {
+ public static <U> TypedMessageEvent<U> wrapMessageEvent(Class<U> type, MessageEvent e) {
checkNotNull(type, "type cannot be null");
checkNotNull(e, "event cannot be null");
return new TypedMessageEvent<U>(e);
}
public Channel getChannel() {
- return channel;
+ return wrappedEvent.getChannel();
}
public ChannelFuture getFuture() {
- return future;
+ return wrappedEvent.getFuture();
}
public T getMessage() {
return message;
}
public SocketAddress getRemoteAddress() {
- return remoteAddress;
+ return wrappedEvent.getRemoteAddress();
}
}
@@ -3,27 +3,28 @@
import org.jboss.netty.channel.*;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
-import static com.google.common.base.Preconditions.*;
+import static com.google.common.base.Preconditions.checkNotNull;
/**
- *
+ *
*/
public class TypedMessageEventHandler implements ChannelUpstreamHandler {
- private final TypeMessageEventListener messageHandler;
- private final Map<Class, Method> methodMap = new HashMap<Class, Method>();
+ private final SimpleChannelUpstreamHandler messageHandler;
+ private final Map<Type, Method> methodMap = new HashMap<Type, Method>();
- public TypedMessageEventHandler(TypeMessageEventListener handler) {
+ public TypedMessageEventHandler(SimpleChannelUpstreamHandler handler) {
+ super();
checkNotNull(handler, "handler cannot be null");
messageHandler = handler;
for (final Method method : messageHandler.getClass().getMethods()) {
-
if (method.isAnnotationPresent(Listen.class)) {
final Class<?>[] paramTypes = method.getParameterTypes();
if (2 != paramTypes.length) {
@@ -36,12 +37,8 @@ public TypedMessageEventHandler(TypeMessageEventListener handler) {
throw new IllegalArgumentException("Method " + method + " must take TypedMessageEvent as it's second argument");
}
- try {
- methodMap.put(paramTypes[1].getMethod("getMessage").getTypeParameters()[0].getClass(), method);
- } catch (NoSuchMethodException e) {
- // this should not happen since we have already confirmed that this argument is indeed a TypedMessageHandler
- throw new IllegalArgumentException("TypedMessageEvent doesn't have getMessage method!?!?");
- }
+ final Listen listenAnnotation = method.getAnnotation(Listen.class);
+ methodMap.put(listenAnnotation.type(), method);
}
}
@@ -50,8 +47,9 @@ public TypedMessageEventHandler(TypeMessageEventListener handler) {
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
if (e instanceof MessageEvent) {
final MessageEvent messageEvent = (MessageEvent) e;
- methodMap.get(messageEvent.getClass())
- .invoke(ctx, TypedMessageEvent.typedEventFromGenericEvent(messageEvent.getMessage().getClass(), messageEvent));
+ methodMap.get(messageEvent.getMessage().getClass())
+ .invoke(messageHandler,
+ ctx, TypedMessageEvent.wrapMessageEvent(messageEvent.getMessage().getClass(), messageEvent));
}
ctx.sendUpstream(e);
}
@@ -0,0 +1,41 @@
+package org.robotninjas.handler.codec.message;
+
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
+import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
+import org.junit.Test;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: drusek
+ * Date: 3/14/12
+ * Time: 10:11 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class TypedMessageEventHandlerTest {
+
+ @Test
+ public void testTypedMessages() {
+
+ final DecoderEmbedder<?> decoderEmbedder = new DecoderEmbedder<Object>(
+ new TypedMessageEventHandler(
+ new SimpleChannelUpstreamHandler() {
+
+ @Listen(type = Integer.class)
+ public void handleInteger(ChannelHandlerContext ctx, TypedMessageEvent<Integer> e) {
+ System.out.println("int");
+ }
+
+ @Listen(type = Float.class)
+ public void handleFloat(ChannelHandlerContext ctx, TypedMessageEvent<Float> e) {
+ System.out.println("float");
+ }
+
+ }));
+
+ decoderEmbedder.offer(new Integer(1));
+ decoderEmbedder.offer(new Float(1.0));
+
+ }
+
+}

0 comments on commit 4ca17f5

Please sign in to comment.