Use ChannelInboundMessageHandlerAdapter

This commit is contained in:
Trustin Lee 2012-05-23 09:49:03 -07:00
parent 7ecad7a8ac
commit 50b4894c36
2 changed files with 6 additions and 37 deletions

View File

@ -18,15 +18,13 @@ package io.netty.example.http.snoop;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelBufferHolder; import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders; import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundHandlerContext; import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.handler.codec.http.HttpChunk; import io.netty.handler.codec.http.HttpChunk;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import java.util.Queue; public class HttpSnoopClientHandler extends ChannelInboundMessageHandlerAdapter<Object> {
public class HttpSnoopClientHandler extends ChannelInboundHandlerAdapter<Object> {
private boolean readingChunks; private boolean readingChunks;
@ -39,19 +37,7 @@ public class HttpSnoopClientHandler extends ChannelInboundHandlerAdapter<Object>
@Override @Override
public void inboundBufferUpdated(ChannelInboundHandlerContext<Object> ctx) public void messageReceived(ChannelInboundHandlerContext<Object> ctx, Object msg) throws Exception {
throws Exception {
Queue<Object> in = ctx.in().messageBuffer();
while (handleMessage(in.poll())) {
continue;
}
}
private boolean handleMessage(Object msg) throws Exception {
if (msg == null) {
return false;
}
if (!readingChunks) { if (!readingChunks) {
HttpResponse response = (HttpResponse) msg; HttpResponse response = (HttpResponse) msg;
@ -89,8 +75,6 @@ public class HttpSnoopClientHandler extends ChannelInboundHandlerAdapter<Object>
System.out.flush(); System.out.flush();
} }
} }
return true;
} }
@Override @Override

View File

@ -25,8 +25,8 @@ import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelBufferHolders; import io.netty.channel.ChannelBufferHolders;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundHandlerContext; import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.handler.codec.http.Cookie; import io.netty.handler.codec.http.Cookie;
import io.netty.handler.codec.http.CookieDecoder; import io.netty.handler.codec.http.CookieDecoder;
import io.netty.handler.codec.http.CookieEncoder; import io.netty.handler.codec.http.CookieEncoder;
@ -42,10 +42,9 @@ import io.netty.util.CharsetUtil;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set; import java.util.Set;
public class HttpSnoopServerHandler extends ChannelInboundHandlerAdapter<Object> { public class HttpSnoopServerHandler extends ChannelInboundMessageHandlerAdapter<Object> {
private HttpRequest request; private HttpRequest request;
private boolean readingChunks; private boolean readingChunks;
@ -59,19 +58,7 @@ public class HttpSnoopServerHandler extends ChannelInboundHandlerAdapter<Object>
} }
@Override @Override
public void inboundBufferUpdated(ChannelInboundHandlerContext<Object> ctx) public void messageReceived(ChannelInboundHandlerContext<Object> ctx, Object msg) throws Exception {
throws Exception {
Queue<Object> in = ctx.in().messageBuffer();
while (handleMessage(ctx, in.poll())) {
continue;
}
}
private boolean handleMessage(ChannelInboundHandlerContext<Object> ctx, Object msg) throws Exception {
if (msg == null) {
return false;
}
if (!readingChunks) { if (!readingChunks) {
HttpRequest request = this.request = (HttpRequest) msg; HttpRequest request = this.request = (HttpRequest) msg;
@ -136,8 +123,6 @@ public class HttpSnoopServerHandler extends ChannelInboundHandlerAdapter<Object>
buf.append("CHUNK: " + chunk.getContent().toString(CharsetUtil.UTF_8) + "\r\n"); buf.append("CHUNK: " + chunk.getContent().toString(CharsetUtil.UTF_8) + "\r\n");
} }
} }
return true;
} }
private void writeResponse(ChannelInboundHandlerContext<Object> ctx) { private void writeResponse(ChannelInboundHandlerContext<Object> ctx) {